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
|
/*
* 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 "FN.hpp"
#include "Exception.hpp"
#include "Array.hpp"
#include <math.h>
#include "Operators.hpp"
#if defined(_MSC_VER )
float erff(float x);
float erfcf(float x);
double erf(double x);
double erfc(double x);
double tgamma(double x);
float tgammaf(float x);
double lgamma(double x);
float lgammaf(float x);
double trunc( double x );
float truncf( float x );
#endif
//!
//@Module ERFC Complimentary Error Function
//@@Section MATHFUNCTIONS
//@@Usage
//Computes the complimentary error function for real arguments. The @|erfc|
//function takes only a single argument
//@[
// y = erfc(x)
//@]
//where @|x| is either a @|float| or @|double| array. The output
//vector @|y| is the same size (and type) as @|x|.
//@@Function Internals
//The erfc function is defined by the integral:
//\[
// \mathrm{erfc}(x) = \frac{2}{\sqrt{\pi}}\int_{x}^{\infty} e^{-t^2} \, dt,
//\]
//and is the integral of the normal distribution.
//@@Example
//Here is a plot of the @|erfc| function over the range @|[-5,5]|.
//@<
//x = linspace(-5,5);
//y = erfc(x);
//plot(x,y); xlabel('x'); ylabel('erfc(x)');
//mprint erfc1
//@>
//which results in the following plot.
//@figure erfc1
//@@Tests
//@$near#y1=erfc(x1)
//@@Signature
//function erfc ErfcFunction
//inputs x
//outputs y
//!
struct OpErfc {
static inline float func(float x) {return erfcf(x);}
static inline double func(double x) {return erfc(x);}
static inline void func(float, float, float&, float&)
{ throw Exception("erfc not defined for complex types");}
static inline void func(float, float, double&, double&)
{ throw Exception("erfc not defined for complex types");}
};
ArrayVector ErfcFunction(int nargout, const ArrayVector& arg) {
if (arg.size() < 1)
throw Exception("erfc requires at least one argument");
return ArrayVector(UnaryOp<OpErfc>(arg[0]));
}
//!
//@Module ERF Error Function
//@@Section MATHFUNCTIONS
//@@Usage
//Computes the error function for real arguments. The @|erf|
//function takes only a single argument
//@[
// y = erf(x)
//@]
//where @|x| is either a @|float| or @|double| array. The output
//vector @|y| is the same size (and type) as @|x|.
//@@Function Internals
//The erf function is defined by the integral:
//\[
// \mathrm{erf}(x) = \frac{2}{\sqrt{\pi}}\int_{0}^{x} e^{-t^2} \, dt,
//\]
//and is the integral of the normal distribution.
//@@Example
//Here is a plot of the erf function over the range @|[-5,5]|.
//@<
//x = linspace(-5,5);
//y = erf(x);
//plot(x,y); xlabel('x'); ylabel('erf(x)');
//mprint erf1
//@>
//which results in the following plot.
//@figure erf1
//@@Tests
//@$near#y1=erf(x1)
//@@Signature
//function erf ErfFunction
//inputs x
//outputs y
//!
struct OpErf {
static inline float func(float x) {return erff(x);}
static inline double func(double x) {return erf(x);}
static inline void func(float, float, float&, float&)
{ throw Exception("erf not defined for complex types");}
static inline void func(float, float, double&, double&)
{ throw Exception("erf not defined for complex types");}
};
ArrayVector ErfFunction(int nargout, const ArrayVector& arg) {
if (arg.size() < 1)
throw Exception("erf requires at least one argument");
return ArrayVector(UnaryOp<OpErf>(arg[0]));
}
//!
//@Module GAMMA Gamma Function
//@@Section MATHFUNCTIONS
//@@Usage
//Computes the gamma function for real arguments. The @|gamma|
//function takes only a single argument
//@[
// y = gamma(x)
//@]
//where @|x| is either a @|float| or @|double| array. The output
//vector @|y| is the same size (and type) as @|x|.
//@@Function Internals
//The gamma function is defined by the integral:
//\[
// \Gamma(x) = \int_{0}^{\infty} e^{-t} t^{x-1} \, dt
//\]
//The gamma function obeys the interesting relationship
//\[
// \Gamma(x) = (x-1)\Gamma(x-1),
//\]
//and for integer arguments, is equivalent to the factorial function.
//@@Example
//Here is a plot of the gamma function over the range @|[-5,5]|.
//@<
//x = linspace(-5,5);
//y = gamma(x);
//plot(x,y); xlabel('x'); ylabel('gamma(x)');
//axis([-5,5,-5,5]);
//mprint gamma1
//@>
//which results in the following plot.
//@figure gamma1
//@@Tests
//@$near#y1=gamma(x1)
//@@Signature
//function gamma GammaFunction
//inputs x
//outputs y
//!
struct OpGamma {
static inline float func(float x) {
if ((x < 0) && (x == truncf(x))) return Inf();
return tgammaf(x);
}
static inline double func(double x) {
if ((x < 0) && (x == trunc(x))) return Inf();
return tgamma(x);
}
static inline void func(float, float, float&, float&)
{ throw Exception("gamma not defined for complex types");}
static inline void func(float, float, double&, double&)
{ throw Exception("gamma not defined for complex types");}
};
ArrayVector GammaFunction(int nargout, const ArrayVector& arg) {
if (arg.size() < 1)
throw Exception("gamma requires at least one argument");
return ArrayVector(UnaryOp<OpGamma>(arg[0]));
}
//!
//@Module GAMMALN Log Gamma Function
//@@Section MATHFUNCTIONS
//@@Usage
//Computes the natural log of the gamma function for real arguments. The @|gammaln|
//function takes only a single argument
//@[
// y = gammaln(x)
//@]
//where @|x| is either a @|float| or @|double| array. The output
//vector @|y| is the same size (and type) as @|x|.
//@@Example
//Here is a plot of the @|gammaln| function over the range @|[-5,5]|.
//@<
//x = linspace(0,10);
//y = gammaln(x);
//plot(x,y); xlabel('x'); ylabel('gammaln(x)');
//mprint gammaln1
//@>
//which results in the following plot.
//@figure gammaln1
//@@Tests
//@$near#y1=gammaln(x1)
//@@Signature
//function gammaln GammaLnFunction
//inputs x
//outputs y
//!
struct OpGammaLn {
static inline float func(float x) {
if (x < 0) return Inf();
return lgammaf(x);
}
static inline double func(double x) {
if (x < 0) return Inf();
return lgamma(x);
}
static inline void func(float, float, float&, float&)
{ throw Exception("gammaln not defined for complex types");}
static inline void func(float, float, double&, double&)
{ throw Exception("gammaln not defined for complex types");}
};
ArrayVector GammaLnFunction(int nargout, const ArrayVector& arg) {
if (arg.size() < 1)
throw Exception("gammaln requires at least one argument");
return ArrayVector(UnaryOp<OpGammaLn>(arg[0]));
}
|