File: elem_math.cpp

package info (click to toggle)
libitpp 4.3.1-13
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 9,952 kB
  • sloc: cpp: 73,628; makefile: 661; python: 548; sh: 261
file content (251 lines) | stat: -rw-r--r-- 5,528 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*!
 * \file
 * \brief Elementary mathematical functions - source file
 * \author Tony Ottosson and Adam Piatyszek
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2010  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/base/math/elem_math.h>
#include <itpp/base/itcompat.h>


namespace itpp
{

vec sqr(const cvec &data)
{
  vec temp(data.length());
  for (int i = 0; i < data.length(); i++)
    temp(i) = sqr(data(i));
  return temp;
}

mat sqr(const cmat &data)
{
  mat temp(data.rows(), data.cols());
  for (int i = 0; i < temp.rows(); i++) {
    for (int j = 0; j < temp.cols(); j++) {
      temp(i, j) = sqr(data(i, j));
    }
  }
  return temp;
}

vec abs(const cvec &data)
{
  vec temp(data.length());

  for (int i = 0;i < data.length();i++)
    temp[i] = std::abs(data[i]);

  return temp;
}

mat abs(const cmat &data)
{
  mat temp(data.rows(), data.cols());

  for (int i = 0;i < temp.rows();i++) {
    for (int j = 0;j < temp.cols();j++) {
      temp(i, j) = std::abs(data(i, j));
    }
  }

  return temp;
}

// Deprecated gamma function. Will be changed to tgamma().
double gamma(double x) { return tgamma(x); }
vec gamma(const vec &x) { return apply_function<double>(tgamma, x); }
mat gamma(const mat &x) { return apply_function<double>(tgamma, x); }

// Calculates factorial coefficient for index <= 170.
double fact(int index)
{
  it_error_if(index > 170, "fact(int index): Function overflows if index > 170.");
  it_error_if(index < 0, "fact(int index): index must be non-negative integer");
  double prod = 1;
  for (int i = 1; i <= index; i++)
    prod *= static_cast<double>(i);
  return prod;
}

// Calculates binomial coefficient "n over k".
double binom(int n, int k)
{
  it_assert(k <= n, "binom(n, k): k can not be larger than n");
  it_assert((n >= 0) && (k >= 0), "binom(n, k): n and k must be non-negative integers");
  k = ((n - k) < k) ? n - k : k;

  double out = 1.0;
  for (int i = 1; i <= k; ++i) {
    out *= (i + n - k);
    out /= i;
  }
  return out;
}

// Calculates binomial coefficient "n over k".
int binom_i(int n, int k)
{
  it_assert(k <= n, "binom_i(n, k): k can not be larger than n");
  it_assert((n >= 0) && (k >= 0), "binom_i(n, k): n and k must be non-negative integers");
  k = ((n - k) < k) ? n - k : k;

  int out = 1;
  for (int i = 1; i <= k; ++i) {
    out *= (i + n - k);
    out /= i;
  }
  return out;
}

// Calculates the base 10-logarithm of the binomial coefficient "n over k".
double log_binom(int n, int k)
{
  it_assert(k <= n, "log_binom(n, k): k can not be larger than n");
  it_assert((n >= 0) && (k >= 0), "log_binom(n, k): n and k must be non-negative integers");
  k = ((n - k) < k) ? n - k : k;

  double out = 0.0;
  for (int i = 1; i <= k; i++)
    out += log10(static_cast<double>(i + n - k))
           - log10(static_cast<double>(i));

  return out;
}

// Calculates the greatest common divisor
int gcd(int a, int b)
{
  it_assert((a >= 0) && (b >= 0), "gcd(a, b): a and b must be non-negative integers");
  int v, u, t, q;

  u = a;
  v = b;
  while (v > 0) {
    q = u / v;
    t = u - v * q;
    u = v;
    v = t;
  }
  return u;
}


vec real(const cvec &data)
{
  vec temp(data.length());

  for (int i = 0;i < data.length();i++)
    temp[i] = data[i].real();

  return temp;
}

mat real(const cmat &data)
{
  mat temp(data.rows(), data.cols());

  for (int i = 0;i < temp.rows();i++) {
    for (int j = 0;j < temp.cols();j++) {
      temp(i, j) = data(i, j).real();
    }
  }

  return temp;
}

vec imag(const cvec &data)
{
  vec temp(data.length());

  for (int i = 0;i < data.length();i++)
    temp[i] = data[i].imag();
  return temp;
}

mat imag(const cmat &data)
{
  mat temp(data.rows(), data.cols());

  for (int i = 0;i < temp.rows();i++) {
    for (int j = 0;j < temp.cols();j++) {
      temp(i, j) = data(i, j).imag();
    }
  }

  return temp;
}

vec arg(const cvec &data)
{
  vec temp(data.length());

  for (int i = 0;i < data.length();i++)
    temp[i] = std::arg(data[i]);

  return temp;
}

mat arg(const cmat &data)
{
  mat temp(data.rows(), data.cols());

  for (int i = 0;i < temp.rows();i++) {
    for (int j = 0;j < temp.cols();j++) {
      temp(i, j) = std::arg(data(i, j));
    }
  }

  return temp;
}

#ifdef _MSC_VER
cvec conj(const cvec &x)
{
  cvec temp(x.size());

  for (int i = 0; i < x.size(); i++) {
    temp(i) = std::conj(x(i));
  }

  return temp;
}

cmat conj(const cmat &x)
{
  cmat temp(x.rows(), x.cols());

  for (int i = 0; i < x.rows(); i++) {
    for (int j = 0; j < x.cols(); j++) {
      temp(i, j) = std::conj(x(i, j));
    }
  }

  return temp;
}
#endif

} // namespace itpp