IT++ Logo
elem_math.cpp
Go to the documentation of this file.
1
30#include <itpp/base/itcompat.h>
31
32
33namespace itpp
34{
35
36vec sqr(const cvec &data)
37{
38 vec temp(data.length());
39 for (int i = 0; i < data.length(); i++)
40 temp(i) = sqr(data(i));
41 return temp;
42}
43
44mat sqr(const cmat &data)
45{
46 mat temp(data.rows(), data.cols());
47 for (int i = 0; i < temp.rows(); i++) {
48 for (int j = 0; j < temp.cols(); j++) {
49 temp(i, j) = sqr(data(i, j));
50 }
51 }
52 return temp;
53}
54
55vec abs(const cvec &data)
56{
57 vec temp(data.length());
58
59 for (int i = 0;i < data.length();i++)
60 temp[i] = std::abs(data[i]);
61
62 return temp;
63}
64
65mat abs(const cmat &data)
66{
67 mat temp(data.rows(), data.cols());
68
69 for (int i = 0;i < temp.rows();i++) {
70 for (int j = 0;j < temp.cols();j++) {
71 temp(i, j) = std::abs(data(i, j));
72 }
73 }
74
75 return temp;
76}
77
78// Deprecated gamma function. Will be changed to tgamma().
79double gamma(double x) { return tgamma(x); }
80vec gamma(const vec &x) { return apply_function<double>(tgamma, x); }
81mat gamma(const mat &x) { return apply_function<double>(tgamma, x); }
82
83// Calculates factorial coefficient for index <= 170.
84double fact(int index)
85{
86 it_error_if(index > 170, "fact(int index): Function overflows if index > 170.");
87 it_error_if(index < 0, "fact(int index): index must be non-negative integer");
88 double prod = 1;
89 for (int i = 1; i <= index; i++)
90 prod *= static_cast<double>(i);
91 return prod;
92}
93
94// Calculates binomial coefficient "n over k".
95double binom(int n, int k)
96{
97 it_assert(k <= n, "binom(n, k): k can not be larger than n");
98 it_assert((n >= 0) && (k >= 0), "binom(n, k): n and k must be non-negative integers");
99 k = ((n - k) < k) ? n - k : k;
100
101 double out = 1.0;
102 for (int i = 1; i <= k; ++i) {
103 out *= (i + n - k);
104 out /= i;
105 }
106 return out;
107}
108
109// Calculates binomial coefficient "n over k".
110int binom_i(int n, int k)
111{
112 it_assert(k <= n, "binom_i(n, k): k can not be larger than n");
113 it_assert((n >= 0) && (k >= 0), "binom_i(n, k): n and k must be non-negative integers");
114 k = ((n - k) < k) ? n - k : k;
115
116 int out = 1;
117 for (int i = 1; i <= k; ++i) {
118 out *= (i + n - k);
119 out /= i;
120 }
121 return out;
122}
123
124// Calculates the base 10-logarithm of the binomial coefficient "n over k".
125double log_binom(int n, int k)
126{
127 it_assert(k <= n, "log_binom(n, k): k can not be larger than n");
128 it_assert((n >= 0) && (k >= 0), "log_binom(n, k): n and k must be non-negative integers");
129 k = ((n - k) < k) ? n - k : k;
130
131 double out = 0.0;
132 for (int i = 1; i <= k; i++)
133 out += log10(static_cast<double>(i + n - k))
134 - log10(static_cast<double>(i));
135
136 return out;
137}
138
139// Calculates the greatest common divisor
140int gcd(int a, int b)
141{
142 it_assert((a >= 0) && (b >= 0), "gcd(a, b): a and b must be non-negative integers");
143 int v, u, t, q;
144
145 u = a;
146 v = b;
147 while (v > 0) {
148 q = u / v;
149 t = u - v * q;
150 u = v;
151 v = t;
152 }
153 return u;
154}
155
156
157vec real(const cvec &data)
158{
159 vec temp(data.length());
160
161 for (int i = 0;i < data.length();i++)
162 temp[i] = data[i].real();
163
164 return temp;
165}
166
167mat real(const cmat &data)
168{
169 mat temp(data.rows(), data.cols());
170
171 for (int i = 0;i < temp.rows();i++) {
172 for (int j = 0;j < temp.cols();j++) {
173 temp(i, j) = data(i, j).real();
174 }
175 }
176
177 return temp;
178}
179
180vec imag(const cvec &data)
181{
182 vec temp(data.length());
183
184 for (int i = 0;i < data.length();i++)
185 temp[i] = data[i].imag();
186 return temp;
187}
188
189mat imag(const cmat &data)
190{
191 mat temp(data.rows(), data.cols());
192
193 for (int i = 0;i < temp.rows();i++) {
194 for (int j = 0;j < temp.cols();j++) {
195 temp(i, j) = data(i, j).imag();
196 }
197 }
198
199 return temp;
200}
201
202vec arg(const cvec &data)
203{
204 vec temp(data.length());
205
206 for (int i = 0;i < data.length();i++)
207 temp[i] = std::arg(data[i]);
208
209 return temp;
210}
211
212mat arg(const cmat &data)
213{
214 mat temp(data.rows(), data.cols());
215
216 for (int i = 0;i < temp.rows();i++) {
217 for (int j = 0;j < temp.cols();j++) {
218 temp(i, j) = std::arg(data(i, j));
219 }
220 }
221
222 return temp;
223}
224
225#ifdef _MSC_VER
226cvec conj(const cvec &x)
227{
228 cvec temp(x.size());
229
230 for (int i = 0; i < x.size(); i++) {
231 temp(i) = std::conj(x(i));
232 }
233
234 return temp;
235}
236
237cmat conj(const cmat &x)
238{
239 cmat temp(x.rows(), x.cols());
240
241 for (int i = 0; i < x.rows(); i++) {
242 for (int j = 0; j < x.cols(); j++) {
243 temp(i, j) = std::conj(x(i, j));
244 }
245 }
246
247 return temp;
248}
249#endif
250
251} // namespace itpp
Elementary mathematical functions - header file.
#define it_error_if(t, s)
Abort if t is true.
Definition itassert.h:117
#define it_assert(t, s)
Abort if t is not true.
Definition itassert.h:94
vec log10(const vec &x)
log-10 of the elements
Definition log_exp.h:271
T prod(const Vec< T > &v)
The product of all elements in the vector.
Definition matfunc.h:195
double gamma(double x)
Deprecated gamma function - please use tgamma() instead.
Definition elem_math.cpp:79
double binom(int n, int k)
Compute the binomial coefficient "n over k".
Definition elem_math.cpp:95
double fact(int index)
Calculates factorial coefficient for index <= 170.
Definition elem_math.cpp:84
vec imag(const cvec &data)
Imaginary part of complex values.
vec arg(const cvec &data)
Argument (angle)
Vec< T > apply_function(T(*f)(T), const Vec< T > &v)
Help function to call for a function: Vec<T> function(Vec<T>)
int binom_i(int n, int k)
Compute the binomial coefficient "n over k".
vec real(const cvec &data)
Real part of complex values.
vec sqr(const cvec &data)
Absolute square of elements.
Definition elem_math.cpp:36
int gcd(int a, int b)
Compute the greatest common divisor (GCD) g of the elements a and b.
cvec conj(const cvec &x)
Conjugate of complex value.
Definition elem_math.h:226
double log_binom(int n, int k)
Compute the base 10 logarithm of the binomial coefficient "n over k".
IT++ compatibility types and functions.
itpp namespace
Definition itmex.h:37
bin abs(const bin &inbin)
absolute value of bin
Definition binary.h:174
int abs(const itpp::bin &inbin)
absolute value of bin
Definition binary.h:186
SourceForge Logo

Generated on Tue Dec 10 2024 04:49:37 for IT++ by Doxygen 1.12.0