File: FNFun.cpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (316 lines) | stat: -rw-r--r-- 9,598 bytes parent folder | download | duplicates (2)
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
/*
 * 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"
#include "mathfunc5.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 

//@@Signature
//function erfc ErfcFunction jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_erfc

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]));
}

JitScalarFunc1(erfc,OpErfc::func);

//@@Signature
//function erf ErfFunction jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_erf

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]));
}

JitScalarFunc1(erf,OpErf::func);
  
//@@Signature
//function erfinv ErfInvFunction jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_erfinv

struct OpErfInv{
  static inline float func(float x) {return erfinv(x);}
  static inline double func(double x) {return erfinv(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 ErfInvFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 1)
    throw Exception("erf requires at least one argument");
  return ArrayVector(UnaryOp<OpErfInv>(arg[0]));
}

JitScalarFunc1(erfinv,OpErfInv::func);

//@@Signature
//function gamma GammaFunction jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_gamma

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");
  
  ArrayVector ret;
  try{
	ret = UnaryOp<OpGamma>(arg[0]);
  }
  catch(...){
	throw Exception("Error evaluating gamma function");
  }
  return ret;
}

JitScalarFunc1(gamma,OpGamma::func);

//@@Signature
//function gammaln GammaLnFunction jitsafe
//inputs x
//outputs y
//DOCBLOCK mathfunctions_gammaln

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]));
}

JitScalarFunc1(gammaln,OpGammaLn::func);

//@@Signature
//function betainc BetaIncFunction
//inputs varargin
//outputs y
//DOCBLOCK mathfunctions_betainc
#ifdef HAVE_BOOST
#include <boost/math/special_functions/beta.hpp>

ArrayVector BetaIncFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 3)
    throw Exception("betainc requires at least three arguments");
  ArrayVector retVec;
  Array X( arg[0] );
  Array Y( arg[1] );
  Array Z( arg[2] );
  int maxLen = std::max( X.length(), std::max( Y.length(), Z.length() ) );
  NTuple retDims = max( X.dimensions(), max( Y.dimensions(), Z.dimensions() ) );
  
  if( !(X.isScalar()) && retDims != X.dimensions() )
    throw Exception("wrong size of the first argument");
  if( !(Y.isScalar()) && retDims != Y.dimensions() )
    throw Exception("wrong size of the second argument");
  if( !(Z.isScalar()) && retDims != Z.dimensions() )
    throw Exception("wrong size of the third argument");
  
  if( X.dataClass() == Double && Y.dataClass() == Double && Z.dataClass() == Double ){
    BasicArray< double > result( retDims );
    for( int i = 1; i <= maxLen; ++i ){
      double x,y,z,r;
      x = (X.isScalar()) ? X.constRealScalar<double>() : X.real<double>()[i];
      y = (Y.isScalar()) ? Y.constRealScalar<double>() : Y.real<double>()[i];
      z = (Z.isScalar()) ? Z.constRealScalar<double>() : Z.real<double>()[i];
	  try{
		result[i] = boost::math::ibeta( y, z, x );
	  }
	  catch(...){
		throw Exception("Error evaluating ibeta");
      }
	}
    retVec.push_back( result );
  }
  else if( X.dataClass() == Float && Y.dataClass() == Float && Z.dataClass() == Float ){
    BasicArray< float > result( retDims );
    for( int i = 1; i <= maxLen; ++i ){
      float x,y,z,r;
      x = (X.isScalar()) ? X.realScalar<float>() : X.real<float>()[i];
      y = (Y.isScalar()) ? Y.realScalar<float>() : Y.real<float>()[i];
      z = (Z.isScalar()) ? Z.realScalar<float>() : Z.real<float>()[i];
	  try{
		result[i] = boost::math::ibeta( y, z, x );
	  }
	  catch(...){
		throw Exception("Error evaluating ibeta");
	  }
    }
    retVec.push_back( result );
  }
  else{
    throw Exception("Inputs must be either double or single");
  }
  return retVec;
}
#else
ArrayVector BetaIncFunction(int nargout, const ArrayVector& arg) {
    throw Exception("FreeMat must be compiled with boost to enable betainc");
    return ArrayVector();
}
#endif

//@@Signature
//function legendre LegendreFunction
//inputs varargin
//outputs y
//DOCBLOCK mathfunctions_legendre

#ifdef HAVE_BOOST
#include <boost/math/special_functions/legendre.hpp>
ArrayVector LegendreFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 2)
    throw Exception("Legendre function requires at least two argument");
  Array x( arg[1] );
  Array n( arg[0] );
  NTuple retDims;
  ArrayVector retVec;
  
  if( x.isComplex() )
    throw Exception("Second argument must be real");

  if( n.isComplex() )
    throw Exception("First argument must be real integer");
  
  if( x.dimensions().count() > 1 && !n.isScalar() )
    throw Exception("If second argument is a not a vector or scalar, then first argument must be scalar");
  
  if( (n.dimensions().count() == 2 && n.dimensions()[1] != x.length()) || (n.dimensions().count() > 2) )
    throw Exception("Incompatible dimensions between first and second argument");
  
  if( n.isScalar() )
    retDims = x.dimensions();
  else
    retDims = n.dimensions();
  
  if( x.dataClass() == Double ){
    BasicArray< double > result( retDims );
    if( n.isScalar() ){
      for( int i=1; i<=x.length(); ++i ){
	double xt = (x.isScalar()) ? x.constRealScalar<double>() : x.real<double>()[i];
	try{
		result[i]=boost::math::legendre_p<double>(n.constRealScalar<double>(), xt);
	}
	catch(...){
		throw Exception("Error evaluating legendre");
	}
      }
    }
    retVec.push_back(result); 
  }
  else if( x.dataClass() == Float ){
    BasicArray< float > result( retDims );
    if( n.isScalar() ){
      for( int i=1; i<=x.length(); ++i ){
	float xt = (x.isScalar()) ? x.constRealScalar<float>() : x.real<float>()[i];
	try{
		result[i]=boost::math::legendre_p<float>(n.constRealScalar<float>(), xt);
	}
	catch(...){
		throw Exception("Error evaluating legendre");
	}
      }
    }
    retVec.push_back(result); 
  }
  else
    throw Exception("Second argument must be double or single");
  return retVec;
}
#else
ArrayVector LegendreFunction(int nargout, const ArrayVector& arg) {
    throw Exception("FreeMat must be compiled with boost to enable legendre");
    return ArrayVector();
}
#endif