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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/*!
* \file
* \brief Elementary mathematical functions - source file
* \author Tony Ottosson and Adam Piatyszek
*
* -------------------------------------------------------------------------
*
* IT++ - C++ library of mathematical, signal processing, speech processing,
* and communications classes and functions
*
* Copyright (C) 1995-2008 (see AUTHORS file for a list of contributors)
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* -------------------------------------------------------------------------
*/
#include <itpp/base/math/elem_math.h>
#ifndef HAVE_TGAMMA
// "True" gamma function
double tgamma(double x)
{
double s = (2.50662827510730 + 190.9551718930764 / (x + 1)
- 216.8366818437280 / (x + 2) + 60.19441764023333
/ (x + 3) - 3.08751323928546 / (x + 4) + 0.00302963870525
/ (x + 5) - 0.00001352385959072596 / (x + 6)) / x;
if (s < 0)
return -exp((x + 0.5) * log(x + 5.5) - x - 5.5 + log(-s));
else
return exp((x + 0.5) * log(x + 5.5) - x - 5.5 + log(s));
}
#endif
#if !defined(HAVE_LGAMMA) || (HAVE_DECL_SIGNGAM != 1)
// The sign of the Gamma function is returned in the external integer
// signgam declared in <math.h>. It is 1 when the Gamma function is positive
// or zero, -1 when it is negative. However, MinGW definition of lgamma()
// function does not use the global signgam variable.
int signgam;
// Logarithm of an absolute value of gamma function
double lgamma(double x)
{
double gam = tgamma(x);
signgam = (gam < 0) ? -1 : 1;
return log(fabs(gam));
}
#endif
#ifndef HAVE_CBRT
// Cubic root
double cbrt(double x) { return std::pow(x, 1.0/3.0); }
#endif
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;
}
// 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
|