IT++
4.3.1
Toggle main menu visibility
itpp
base
math
elem_math.h
Go to the documentation of this file.
1
28
29
#ifndef ELEM_MATH_H
30
#define ELEM_MATH_H
31
32
#include <
itpp/base/help_functions.h
>
33
#include <
itpp/base/converters.h
>
34
#include <cstdlib>
// required by std::abs()
35
#include <itpp/itexports.h>
36
37
namespace
itpp
38
{
39
42
43
// -------------------- sqr function --------------------
44
46
inline
double
sqr
(
double
x) {
return
(x * x); }
48
inline
double
sqr
(
const
std::complex<double>& x)
49
{
50
return
(x.real() * x.real() + x.imag() * x.imag());
51
}
52
53
inline
vec
sqr
(
const
vec &x) {
return
apply_function<double>
(
sqr
, x); }
55
inline
mat
sqr
(
const
mat &x) {
return
apply_function<double>
(
sqr
, x); }
57
ITPP_EXPORT vec
sqr
(
const
cvec &x);
59
ITPP_EXPORT mat
sqr
(
const
cmat &x);
60
61
62
// -------------------- abs function --------------------
63
65
inline
vec
abs
(
const
vec &x) {
return
apply_function<double>
(std::fabs, x); }
67
inline
mat
abs
(
const
mat &x) {
return
apply_function<double>
(std::fabs, x); }
69
inline
ivec
abs
(
const
ivec &x) {
return
apply_function<int>
(
std::abs
, x); }
71
inline
imat
abs
(
const
imat &x) {
return
apply_function<int>
(
std::abs
, x); }
73
ITPP_EXPORT vec
abs
(
const
cvec &x);
75
ITPP_EXPORT mat
abs
(
const
cmat &x);
76
77
78
// -------------------- sign/sgn functions --------------------
79
81
inline
double
sign
(
double
x)
82
{
83
return
(x == 0.0 ? 0.0 : (x < 0.0 ? -1.0 : 1.0));
84
}
85
86
inline
vec
sign
(
const
vec &x) {
return
apply_function<double>
(
sign
, x); }
88
inline
mat
sign
(
const
mat &x) {
return
apply_function<double>
(
sign
, x); }
89
91
inline
double
sgn
(
double
x) {
return
sign
(x); }
93
inline
vec
sgn
(
const
vec &x) {
return
apply_function<double>
(
sign
, x); }
95
inline
mat
sgn
(
const
mat &x) {
return
apply_function<double>
(
sign
, x); }
96
98
inline
int
sign_i
(
int
x)
99
{
100
return
(x == 0 ? 0 : (x < 0 ? -1 : 1));
101
}
102
103
inline
ivec
sign_i
(
const
ivec &x) {
return
apply_function<int>
(
sign_i
, x); }
105
inline
imat
sign_i
(
const
imat &x) {
return
apply_function<int>
(
sign_i
, x); }
106
108
inline
int
sgn_i
(
int
x) {
return
sign_i
(x); }
110
inline
ivec
sgn_i
(
const
ivec &x) {
return
apply_function<int>
(
sign_i
, x); }
112
inline
imat
sgn_i
(
const
imat &x) {
return
apply_function<int>
(
sign_i
, x); }
113
115
inline
int
sign_i
(
double
x)
116
{
117
return
(x == 0.0 ? 0 : (x < 0.0 ? -1 : 1));
118
}
119
120
// -------------------- sqrt function --------------------
121
123
inline
vec
sqrt
(
const
vec &x) {
return
apply_function<double>
(std::sqrt, x); }
125
inline
mat
sqrt
(
const
mat &x) {
return
apply_function<double>
(std::sqrt, x); }
126
127
128
// -------------------- gamma function --------------------
129
131
ITPP_EXPORT
double
gamma
(
double
x);
133
ITPP_EXPORT vec
gamma
(
const
vec &x);
135
ITPP_EXPORT mat
gamma
(
const
mat &x);
136
137
138
// -------------------- rem function --------------------
139
141
inline
double
rem
(
double
x,
double
y) {
return
fmod(x, y); }
143
inline
vec
rem
(
const
vec &x,
double
y)
144
{
145
return
apply_function<double>
(
rem
, x, y);
146
}
147
148
inline
vec
rem
(
double
x,
const
vec &y)
149
{
150
return
apply_function<double>
(
rem
, x, y);
151
}
152
153
inline
mat
rem
(
const
mat &x,
double
y)
154
{
155
return
apply_function<double>
(
rem
, x, y);
156
}
157
158
inline
mat
rem
(
double
x,
const
mat &y)
159
{
160
return
apply_function<double>
(
rem
, x, y);
161
}
162
163
// -------------------- mod function --------------------
164
166
inline
int
mod
(
int
k,
int
n)
167
{
168
return
(n == 0) ? k : (k - n *
floor_i
(
static_cast<
double
>
(k) / n));
169
}
170
171
172
// -------------------- factorial coefficient function --------------------
173
175
ITPP_EXPORT
double
fact
(
int
index);
176
177
178
// -------------------- binomial coefficient function --------------------
179
181
ITPP_EXPORT
double
binom
(
int
n,
int
k);
182
184
ITPP_EXPORT
int
binom_i
(
int
n,
int
k);
185
187
ITPP_EXPORT
double
log_binom
(
int
n,
int
k);
188
189
190
// -------------------- greatest common divisor function --------------------
191
199
ITPP_EXPORT
int
gcd
(
int
a,
int
b);
200
201
202
// -------------------- complex related functions --------------------
203
205
ITPP_EXPORT vec
real
(
const
cvec &x);
207
ITPP_EXPORT mat
real
(
const
cmat &x);
209
ITPP_EXPORT vec
imag
(
const
cvec &x);
211
ITPP_EXPORT mat
imag
(
const
cmat &x);
212
214
ITPP_EXPORT vec
arg
(
const
cvec &x);
216
ITPP_EXPORT mat
arg
(
const
cmat &x);
218
inline
vec
angle
(
const
cvec &x) {
return
arg
(x); }
220
inline
mat
angle
(
const
cmat &x) {
return
arg
(x); }
221
222
// Added due to a failure in MSVC++ .NET 2005, which crashes on this
223
// code.
224
#ifndef _MSC_VER
226
inline
cvec
conj
(
const
cvec &x)
227
{
228
return
apply_function<std::complex<double>
>(std::conj, x);
229
}
230
231
inline
cmat
conj
(
const
cmat &x)
232
{
233
return
apply_function<std::complex<double>
>(std::conj, x);
234
}
235
#else
237
ITPP_EXPORT cvec
conj
(
const
cvec &x);
238
240
ITPP_EXPORT cmat
conj
(
const
cmat &x);
241
#endif
242
244
245
}
// namespace itpp
246
247
#endif
// #ifndef ELEM_MATH_H
248
249
250
251
converters.h
Definitions of converters between different vector and matrix types.
itpp::gamma
double gamma(double x)
Deprecated gamma function - please use tgamma() instead.
Definition
elem_math.cpp:79
itpp::sgn_i
int sgn_i(int x)
Signum function.
Definition
elem_math.h:108
itpp::sign
double sign(double x)
Signum function.
Definition
elem_math.h:81
itpp::binom
double binom(int n, int k)
Compute the binomial coefficient "n over k".
Definition
elem_math.cpp:95
itpp::sgn
double sgn(double x)
Signum function.
Definition
elem_math.h:91
itpp::mod
int mod(int k, int n)
Calculates the modulus, i.e. the signed reminder after division.
Definition
elem_math.h:166
itpp::rem
double rem(double x, double y)
The reminder of the division x/y.
Definition
elem_math.h:141
itpp::fact
double fact(int index)
Calculates factorial coefficient for index <= 170.
Definition
elem_math.cpp:84
itpp::imag
vec imag(const cvec &data)
Imaginary part of complex values.
Definition
elem_math.cpp:180
itpp::angle
vec angle(const cvec &x)
Angle.
Definition
elem_math.h:218
itpp::arg
vec arg(const cvec &data)
Argument (angle).
Definition
elem_math.cpp:202
itpp::apply_function
Vec< T > apply_function(T(*f)(T), const Vec< T > &v)
Help function to call for a function: Vec<T> function(Vec<T>).
Definition
help_functions.h:124
itpp::binom_i
int binom_i(int n, int k)
Compute the binomial coefficient "n over k".
Definition
elem_math.cpp:110
itpp::real
vec real(const cvec &data)
Real part of complex values.
Definition
elem_math.cpp:157
itpp::sqr
vec sqr(const cvec &data)
Absolute square of elements.
Definition
elem_math.cpp:36
itpp::gcd
int gcd(int a, int b)
Compute the greatest common divisor (GCD) g of the elements a and b.
Definition
elem_math.cpp:140
itpp::sign_i
int sign_i(int x)
Signum function.
Definition
elem_math.h:98
itpp::conj
cvec conj(const cvec &x)
Conjugate of complex value.
Definition
elem_math.h:226
itpp::sqrt
vec sqrt(const vec &x)
Square root of the elements.
Definition
elem_math.h:123
itpp::log_binom
double log_binom(int n, int k)
Compute the base 10 logarithm of the binomial coefficient "n over k".
Definition
elem_math.cpp:125
help_functions.h
Help functions to make functions with vec and mat as arguments.
itpp
itpp namespace
Definition
itmex.h:37
itpp::floor_i
int floor_i(double x)
The nearest smaller integer.
Definition
converters.h:350
itpp::abs
bin abs(const bin &inbin)
absolute value of bin
Definition
binary.h:174
std::abs
int abs(const itpp::bin &inbin)
absolute value of bin
Definition
binary.h:186
Generated by
1.17.0