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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/*
* Copyright (c) 2002-2006 Samit Basu
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "Exception.hpp"
#include "Array.hpp"
#include <math.h>
#include "Utils.hpp"
#include "Math.hpp"
#include "Operators.hpp"
#include "Complex.hpp"
#include "IEEEFP.hpp"
//@@Signature
//function log1p Log1PFunction
//inputs x
//output y
//DOCBLOCK mathfunctions_log1p
struct OpLog1P {
static inline float func(float x) {return log1pf(x);}
static inline double func(double x) {return log1p(x);}
static inline void func(float xr, float xi, float &yr, float &yi) {
yr = logf(complex_abs<float>(xr+1,xi));
yi = atan2f(xi,xr+1);
}
static inline void func(double xr, double xi, double &yr, double &yi) {
yr = log(complex_abs<double>(xr+1,xi));
yi = atan2(xi,xr+1);
}
};
ArrayVector Log1PFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("Log1p function takes exactly one argument");
Array input(arg[0]);
if (ArrayMin(input) < -1) {
if (input.dataClass() != Float)
input = input.toClass(Double);
input.forceComplex();
}
return ArrayVector(UnaryOp<OpLog1P>(input).toClass(input.dataClass()));
}
//@@Signature
//function log LogFunction
//inputs x
//outputs y
//DOCBLOCK mathfunctions_log
struct OpLog {
static inline float func(float x) {return logf(x);}
static inline double func(double x) {return log(x);}
static inline void func(float xr, float xi, float &yr, float &yi) {
yr = logf(complex_abs<float>(xr,xi));
yi = atan2f(xi,xr);
}
static inline void func(double xr, double xi, double &yr, double &yi) {
yr = log(complex_abs<double>(xr,xi));
yi = atan2(xi,xr);
}
};
ArrayVector LogFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("Log function takes exactly one argument");
Array input(arg[0]);
if (!IsNonNegativeOrNaN(input)) {
if (input.dataClass() != Float)
input = input.toClass(Double);
input.forceComplex();
}
return ArrayVector(UnaryOp<OpLog>(input));
}
//@@Signature
//function sqrt SqrtFunction
//inputs x
//outputs y
//DOCBLOCK mathfunctions_sqrt
struct OpSqrt {
static inline float func(float x) {return sqrtf(x);}
static inline double func(double x) {return sqrt(x);}
static inline void func(float xr, float xi, float &yr, float &yi) {
complex_sqrt<float>(xr,xi,yr,yi);
}
static inline void func(double xr, double xi, double &yr, double &yi) {
complex_sqrt<double>(xr,xi,yr,yi);
}
};
ArrayVector SqrtFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("Sqrt function takes exactly one argument");
Array input(arg[0]);
if (!IsNonNegativeOrNaN(input)) {
if (input.dataClass() != Float)
input = input.toClass(Double);
input.forceComplex();
}
return ArrayVector(UnaryOp<OpSqrt>(input));
}
//@@Signature
//function tanh TanhFunction jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_tanh
// e^r*c(i)+e^(-r)*c(-i)/2 + i
struct OpTanh {
static inline float func(float x) {return tanhf(x);}
static inline double func(double x) {return tanh(x);}
static void func(float xr, float xi, float &yr, float &yi) {
float cr = (expf(xr)*cosf(xi)+expf(-xr)*cosf(-xi))/2;
float ci = (expf(xr)*sinf(xi)+expf(-xr)*sinf(-xi))/2;
float sr = (expf(xr)*cosf(xi)-expf(-xr)*cosf(-xi))/2;
float si = (expf(xr)*sinf(xi)-expf(-xr)*sinf(-xi))/2;
complex_divide(sr,si,cr,ci,yr,yi);
}
static void func(double xr, double xi, double &yr, double &yi) {
double cr = (exp(xr)*cos(xi)+exp(-xr)*cos(-xi))/2;
double ci = (exp(xr)*sin(xi)+exp(-xr)*sin(-xi))/2;
double sr = (exp(xr)*cos(xi)-exp(-xr)*cos(-xi))/2;
double si = (exp(xr)*sin(xi)-exp(-xr)*sin(-xi))/2;
// double cr = (exp(xr)*cos(xi)+exp(-xr)*cos(-xi))/2;
// double ci = (exp(xr)*sin(xi)+exp(-xr)*sin(-xi))/2;
// double sr = (exp(xr)*cos(xi)-exp(-xr)*cos(-xi))/2;
// double si = (exp(xr)*sin(xi)-exp(-xr)*sin(-xi))/2;
complex_divide(sr,si,cr,ci,yr,yi);
}
};
ArrayVector TanhFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("Tanh function takes exactly one argument");
return ArrayVector(UnaryOp<OpTanh>(arg[0]));
}
JitScalarFunc1(tanh,OpTanh::func);
//@@Signature
//function acosh ArccoshFunction
//inputs x
//outputs y
//DOCBLOCK mathfunctions_acosh
// log(x+sqrt(x^2-1))
struct OpArccosh {
static inline float func(float x) {return acoshf(x);}
static inline double func(double x) {return acosh(x);}
template <typename T>
static inline void func(T xr, T xi, T &yr, T &yi) {
if (xr == -Inf() && xi == 0) {
yr = Inf();
yi = M_PI;
return;
}
T xrt_real1, xrt_imag1;
T xrt_real2, xrt_imag2;
complex_sqrt(xr+1,xi,xrt_real1,xrt_imag1);
complex_sqrt(xr-1,xi,xrt_real2,xrt_imag2);
T y_real, y_imag;
complex_multiply(xrt_real1,xrt_imag1,
xrt_real2,xrt_imag2,
y_real,y_imag);
y_real += xr;
y_imag += xi;
complex_log(y_real,y_imag,yr,yi);
}
};
ArrayVector ArccoshFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("acosh function takes exactly one argument");
Array input(arg[0]);
if (input.allReal() && (ArrayMin(input) <= 1)) {
if (input.dataClass() != Float)
input = input.toClass(Double);
input.forceComplex();
}
return ArrayVector(UnaryOp<OpArccosh>(input));
}
//@@Signature
//function asinh ArcsinhFunction
//inputs x
//outputs y
//DOCBLOCK mathfunctions_asinh
// log(x+sqrt(x^2-1))
struct OpArcsinh {
static inline float func(float x) {return asinhf(x);}
static inline double func(double x) {return asinh(x);}
template <typename T>
static inline void func(T xr, T xi, T &yr, T &yi) {
T xsq_real, xsq_imag;
complex_square(xr,xi,xsq_real,xsq_imag);
xsq_real += 1;
T xrt_real, xrt_imag;
complex_sqrt(xsq_real,xsq_imag,xrt_real,xrt_imag);
xrt_real += xr;
xrt_imag += xi;
complex_log(xrt_real,xrt_imag,yr,yi);
}
};
ArrayVector ArcsinhFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("acosh function takes exactly one argument");
return ArrayVector(UnaryOp<OpArcsinh>(arg[0]));
}
//@@Signature
//function asech ArcsechFunction
//inputs x
//outputs y
//DOCBLOCK mathfunctions_asech
struct OpArcsech {
template <typename T>
static inline T func(T x) {
if (x == 0)
return Inf();
return log(sqrt(1/x-1)*sqrt(1/x+1)+1/x);
}
template <typename T>
static inline void func(T xr, T xi, T &yr, T &yi) {
if ((xr == 0) && (xi == 0)) {
yr = Inf();
yi = 0;
return;
}
T xrp_real, xrp_imag;
complex_recip(xr,xi,xrp_real,xrp_imag);
OpArccosh::func(xrp_real,xrp_imag,yr,yi);
}
};
ArrayVector ArcsechFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("asech function takes exactly one argument");
Array input(arg[0]);
if (input.allReal() && ((ArrayMin(input) < 0) || (ArrayMax(input) > 1))) {
if (input.dataClass() != Float)
input = input.toClass(Double);
input.forceComplex();
}
return ArrayVector(UnaryOp<OpArcsech>(input));
}
//@@Signature
//function atanh ArctanhFunction
//inputs x
//outputs y
//DOCBLOCK mathfunctions_atanh
struct OpArctanh {
static inline float func(float x) {return atanhf(x);}
static inline double func(double x) {return atanh(x);}
template <typename T>
static inline void func(T xr, T xi, T &yr, T &yi) {
// if ((xr == 1) && (xi == 0)) {
// yr = Inf();
// yi = 0;
// return;
// }
if ((xr == Inf()) && (xi == 0)) {
yr = 0;
yi = M_PI/2.0;
return;
}
if ((xr == -Inf()) && (xi == 0)) {
yr = 0;
yi = -M_PI/2.0;
return;
}
T xa, xb;
T ya, yb;
complex_log(xr+1,xi,xa,xb);
complex_log(1-xr,-xi,ya,yb);
yr = (xa-ya)/2;
yi = (xb-yb)/2;
}
};
ArrayVector ArctanhFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("atanh function takes exactly one argument");
Array input(arg[0]);
if (input.allReal() && (ArrayRange(input) >= 1)) {
if (input.dataClass() != Float)
input = input.toClass(Double);
input.forceComplex();
}
return ArrayVector(UnaryOp<OpArctanh>(input));
}
//@@Signature
//function cosh CoshFunction jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_cosh
struct OpCosh {
static inline float func(float x) {return coshf(x);}
static inline double func(double x) {return cosh(x);}
static inline void func(float xr, float xi, float &yr, float &yi) {
yr = (expf(xr)*cosf(xi)+expf(-xr)*cosf(-xi))/2;
yi = (expf(xr)*sinf(xi)+expf(-xr)*sinf(-xi))/2;
}
static inline void func(double xr, double xi, double &yr, double &yi) {
yr = (exp(xr)*cos(xi)+exp(-xr)*cos(-xi))/2;
yi = (exp(xr)*sin(xi)+exp(-xr)*sin(-xi))/2;
}
};
ArrayVector CoshFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("Cosh function takes exactly one argument");
return ArrayVector(UnaryOp<OpCosh>(arg[0]));
}
JitScalarFunc1(cosh,OpCosh::func);
//@@Signature
//function sinh SinhFunction jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_sinh
struct OpSinh {
static inline float func(float x) {return sinhf(x);}
static inline double func(double x) {return sinh(x);}
static inline void func(float xr, float xi, float &yr, float &yi) {
yr = (expf(xr)*cosf(xi)-expf(-xr)*cosf(-xi))/2;
yi = (expf(xr)*sinf(xi)-expf(-xr)*sinf(-xi))/2;
}
static inline void func(double xr, double xi, double &yr, double &yi) {
yr = (exp(xr)*cos(xi)-exp(-xr)*cos(-xi))/2;
yi = (exp(xr)*sin(xi)-exp(-xr)*sin(-xi))/2;
}
};
ArrayVector SinhFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("Sinh function takes exactly one argument");
return ArrayVector(UnaryOp<OpSinh>(arg[0]));
}
JitScalarFunc1(sinh,OpSinh::func);
//@@Signature
//function exp ExpFunction jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_exp
struct OpExp {
static inline float func(float x) {return expf(x);}
static inline double func(double x) {return exp(x);}
static void func(float xr, float xi, float &yr, float &yi) {
yr = expf(xr)*cosf(xi);
yi = expf(xr)*sinf(xi);
}
static void func(double xr, double xi, double &yr, double &yi) {
yr = exp(xr)*cos(xi);
yi = exp(xr)*sin(xi);
}
};
ArrayVector ExpFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("Exp function takes exactly one argument");
return ArrayVector(UnaryOp<OpExp>(arg[0]));
}
JitScalarFunc1(exp,OpExp::func);
//@@Signature
//function expm1 ExpM1Function jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_expm1
// exp(x)-1 for x = xr+i*xi
// exp(xr+i*xi) - 1 = exp(xr)*cos(xi) + i*exp(xr)*sin(xi) - 1
struct OpExpM1 {
static inline float func(float x) {return expm1f(x);}
static inline double func(double x) {return expm1(x);}
static void func(float xr, float xi, float &yr, float &yi) {
yr = expf(xr)*cosf(xi) - 1;
yi = expf(xr)*sinf(xi);
}
static void func(double xr, double xi, double &yr, double &yi) {
yr = exp(xr)*cos(xi) - 1;
yi = exp(xr)*sin(xi);
}
};
ArrayVector ExpM1Function(int nargout, const ArrayVector& arg) {
if (arg.size() != 1)
throw Exception("ExpM1 function takes exactly one argument");
return ArrayVector(UnaryOp<OpExpM1>(arg[0]));
}
JitScalarFunc1(expm1,OpExpM1::func);
|