IsoSpec  1.95
misc.h
1 /*
2  * Copyright (C) 2015-2018 Mateusz Łącki and Michał Startek.
3  *
4  * This file is part of IsoSpec.
5  *
6  * IsoSpec is free software: you can redistribute it and/or modify
7  * it under the terms of the Simplified ("2-clause") BSD licence.
8  *
9  * IsoSpec is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the Simplified BSD Licence
14  * along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
15  */
16 
17 #pragma once
18 
19 #include <iostream>
20 #include <tuple>
21 #include <vector>
22 #include <fenv.h>
23 #include "isoMath.h"
24 
25 namespace IsoSpec
26 {
27 
28 inline double combinedSum(
29  const int* conf, const std::vector<double>** valuesContainer, int dimNumber
30 ){
31  double res = 0.0;
32  for(int i=0; i<dimNumber;i++)
33  res += (*(valuesContainer[i]))[conf[i]];
34  return res;
35 }
36 
37 inline int* getConf(void* conf)
38 {
39  return reinterpret_cast<int*>(
40  reinterpret_cast<char*>(conf) + sizeof(double)
41  );
42 }
43 
44 inline double getLProb(void* conf)
45 {
46  double ret = *reinterpret_cast<double*>(conf);
47  return ret;
48 }
49 
50 
51 inline double unnormalized_logProb(const int* conf, const double* logProbs, int dim)
52 {
53  double res = 0.0;
54 
55  int curr_method = fegetround();
56 
57  fesetround(FE_TOWARDZERO);
58 
59  for(int i=0; i < dim; i++)
60  res += minuslogFactorial(conf[i]);
61 
62  fesetround(FE_UPWARD);
63 
64  for(int i=0; i < dim; i++)
65  res += conf[i] * logProbs[i];
66 
67  fesetround(curr_method);
68 
69  return res;
70 }
71 
72 inline double mass(const int* conf, const double* masses, int dim)
73 {
74  double res = 0.0;
75 
76  for(int i=0; i < dim; i++)
77  {
78  res += conf[i] * masses[i];
79  }
80 
81  return res;
82 }
83 
84 
85 inline bool tupleCmp(
86  std::tuple<double,double,int*> t1,
87  std::tuple<double,double,int*> t2
88 ){
89  return std::get<1>(t1) > std::get<1>(t2);
90 }
91 
92 template<typename T> void printArray(const T* array, int size)
93 {
94  for (int i=0; i<size; i++)
95  std::cout << array[i] << " ";
96  std::cout << std::endl;
97 }
98 
99 template<typename T> void printVector(const std::vector<T>& vec)
100 {
101  printArray<T>(vec.data(), vec.size());
102 }
103 
104 
105 template<typename T> void printNestedArray(const T** array, const int* shape, int size)
106 {
107  for (int i=0; i<size; i++)
108  printArray(array[i], shape[i]);
109  std::cout << std::endl;
110 }
111 
112 #define mswap(x, y) swapspace = x; x = y; y=swapspace;
113 
114 
116 void* quickselect(void** array, int n, int start, int end);
117 
118 
119 template <typename T> inline static T* array_copy(const T* A, int size)
120 {
121  T* ret = new T[size];
122  memcpy(ret, A, size*sizeof(T));
123  return ret;
124 }
125 
126 template<typename T> void dealloc_table(T* tbl, int dim)
127 {
128  for(int i=0; i<dim; i++)
129  {
130  delete tbl[i];
131  }
132  delete[] tbl;
133 }
134 
135 } // namespace IsoSpec
136 
IsoSpec
Definition: allocator.cpp:22
IsoSpec::quickselect
void * quickselect(void **array, int n, int start, int end)
Quickly select the n'th positional statistic, including the weights.
Definition: misc.cpp:28