3146
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "Range.h" |
|
28 #include "dColVector.h" |
|
29 #include "dMatrix.h" |
|
30 #include "f77-fcn.h" |
|
31 #include "lo-error.h" |
|
32 #include "mx-inlines.cc" |
|
33 |
|
34 extern "C" |
|
35 { |
|
36 int F77_FCN (rjbesl, RJBESL) (const double&, const double&, |
|
37 const int&, double*, int&); |
|
38 |
|
39 int F77_FCN (rybesl, RYBESL) (const double&, const double&, |
|
40 const int&, double*, int&); |
|
41 |
|
42 int F77_FCN (ribesl, RIBESL) (const double&, const double&, |
|
43 const int&, const int&, double*, int&); |
|
44 |
|
45 int F77_FCN (rkbesl, RKBESL) (const double&, const double&, |
|
46 const int&, const int&, double*, int&); |
|
47 |
|
48 int F77_FCN (xdacosh, XDACOSH) (const double&, double&); |
|
49 |
|
50 int F77_FCN (xdasinh, XDASINH) (const double&, double&); |
|
51 |
|
52 int F77_FCN (xdatanh, XDATANH) (const double&, double&); |
|
53 |
|
54 int F77_FCN (xderf, XDERF) (const double&, double&); |
|
55 |
|
56 int F77_FCN (xderfc, XDERFC) (const double&, double&); |
|
57 |
|
58 int F77_FCN (xdbetai, XDBETAI) (const double&, const double&, |
|
59 const double&, double&); |
|
60 |
|
61 int F77_FCN (xdgamma, XDGAMMA) (const double&, double&); |
|
62 |
3164
|
63 int F77_FCN (xdgamit, XDGAMIT) (const double&, const double&, double&); |
3146
|
64 |
|
65 int F77_FCN (dlgams, DLGAMS) (const double&, double&, double&); |
|
66 } |
|
67 |
|
68 #if !defined (HAVE_ACOSH) |
|
69 double |
|
70 acosh (double x) |
|
71 { |
|
72 double retval; |
|
73 F77_XFCN (dacosh, DACOSH, (x, retval)); |
|
74 return retval; |
|
75 } |
|
76 #endif |
|
77 |
|
78 #if !defined (HAVE_ASINH) |
|
79 double |
|
80 asinh (double x) |
|
81 { |
|
82 double retval; |
|
83 F77_XFCN (dasinh, DASINH, (x, retval)); |
|
84 return retval; |
|
85 } |
|
86 #endif |
|
87 |
|
88 #if !defined (HAVE_ATANH) |
|
89 double |
|
90 atanh (double x) |
|
91 { |
|
92 double retval; |
|
93 F77_XFCN (datanh, DATANH, (x, retval)); |
|
94 return retval; |
|
95 } |
|
96 #endif |
|
97 |
|
98 #if !defined (HAVE_ERF) |
|
99 double |
|
100 erf (double x) |
|
101 { |
|
102 double retval; |
|
103 F77_XFCN (derf, DERF, (x, retval)); |
|
104 return retval; |
|
105 } |
|
106 #endif |
|
107 |
|
108 #if !defined (HAVE_ERFC) |
|
109 double |
|
110 erfc (double x) |
|
111 { |
|
112 double retval; |
|
113 F77_XFCN (derfc, DERFC, (x, retval)); |
|
114 return retval; |
|
115 } |
|
116 #endif |
|
117 |
|
118 double |
3156
|
119 xgamma (double x) |
3146
|
120 { |
3156
|
121 double result; |
|
122 F77_XFCN (xdgamma, XDGAMMA, (x, result)); |
|
123 return result; |
3146
|
124 } |
|
125 |
|
126 double |
3156
|
127 xlgamma (double x) |
3146
|
128 { |
3156
|
129 double result; |
3146
|
130 double sgngam; |
3156
|
131 F77_XFCN (dlgams, DLGAMS, (x, result, sgngam)); |
|
132 return result; |
3146
|
133 } |
|
134 |
|
135 int |
|
136 F77_FCN (ribesl, RIBESL) (const double& x, const double& alpha, |
|
137 const int& nb, double *result, int& ncalc) |
|
138 { |
|
139 int retval = 0; |
|
140 F77_FCN (ribesl, RIBESL) (x, alpha, nb, 1, result, ncalc); |
|
141 return retval; |
|
142 } |
|
143 |
|
144 int |
|
145 F77_FCN (rkbesl, RKBESL) (const double& x, const double& alpha, |
|
146 const int& nb, double *result, int& ncalc) |
|
147 { |
|
148 int retval = 0; |
|
149 F77_FCN (rkbesl, RKBESL) (x, alpha, nb, 1, result, ncalc); |
|
150 return retval; |
|
151 } |
|
152 |
|
153 typedef int (*fptr) (const double&, const double&, const int&, double*, int&); |
|
154 |
|
155 static Matrix |
|
156 do_bessel (fptr f, const char *fn, double alpha, const Matrix& x) |
|
157 { |
|
158 Matrix retval; |
|
159 |
|
160 if (alpha >= 0.0) |
|
161 { |
|
162 int nb = 1; |
|
163 |
|
164 if (alpha >= 1.0) |
|
165 { |
|
166 double integral; |
|
167 alpha = modf (alpha, &integral); |
|
168 nb = static_cast<int> (integral) + 1; |
|
169 } |
|
170 |
|
171 Array<double> tmp (nb); |
|
172 |
|
173 double *ptmp = tmp.fortran_vec (); |
|
174 |
|
175 int x_nr = x.rows (); |
|
176 int x_nc = x.cols (); |
|
177 |
|
178 retval.resize (x_nr, x_nc); |
|
179 |
|
180 int ncalc; |
|
181 |
|
182 for (int j = 0; j < x_nc; j++) |
|
183 { |
|
184 for (int i = 0; i < x_nr; i++) |
|
185 { |
|
186 f (x(i,j), alpha, nb, ptmp, ncalc); |
|
187 |
|
188 if (ncalc < 0) |
|
189 { |
|
190 (*current_liboctave_error_handler) |
|
191 ("%s: error computing function values", fn); |
|
192 |
|
193 return Matrix (); |
|
194 } |
|
195 |
|
196 retval(i,j) = tmp(nb-1); |
|
197 } |
|
198 } |
|
199 } |
|
200 else |
|
201 (*current_liboctave_error_handler) |
|
202 ("%s: alpha must be greater than zero", fn); |
|
203 |
|
204 return retval; |
|
205 } |
|
206 |
|
207 static Matrix |
|
208 do_bessel (fptr f, const char *fn, const Range& alpha_range, |
|
209 const ColumnVector& x) |
|
210 { |
|
211 Matrix retval; |
|
212 |
|
213 double alpha_base = alpha_range.base (); |
|
214 |
|
215 if (alpha_base >= 0.0) |
|
216 { |
|
217 int nb_orig = alpha_range.nelem (); |
|
218 int nb = nb_orig; |
|
219 int offset = 0; |
|
220 |
|
221 if (alpha_base >= 1.0) |
|
222 { |
|
223 double integral; |
|
224 alpha_base = modf (alpha_base, &integral); |
|
225 offset = static_cast<int> (integral); |
|
226 nb += offset; |
|
227 } |
|
228 |
|
229 Array<double> tmp (nb); |
|
230 |
|
231 double *ptmp = tmp.fortran_vec (); |
|
232 |
|
233 int x_len = x.length (); |
|
234 |
|
235 retval.resize (x_len, nb_orig); |
|
236 |
|
237 int ncalc; |
|
238 |
|
239 for (int i = 0; i < x_len; i++) |
|
240 { |
|
241 f (x(i), alpha_base, nb, ptmp, ncalc); |
|
242 |
|
243 if (ncalc < 0) |
|
244 { |
|
245 (*current_liboctave_error_handler) |
|
246 ("%s: error computing function values", fn); |
|
247 |
|
248 return Matrix (); |
|
249 } |
|
250 |
|
251 for (int j = 0; j < nb_orig; j++) |
|
252 retval(i,j) = tmp(offset+j); |
|
253 } |
|
254 } |
|
255 else |
|
256 (*current_liboctave_error_handler) |
|
257 ("%s: alpha must be greater than zero", fn); |
|
258 |
|
259 return retval; |
|
260 } |
|
261 |
|
262 Matrix |
|
263 besselj (double alpha, const Matrix& x) |
|
264 { |
|
265 return do_bessel (F77_FCN (rjbesl, RJBESL), "besselj", alpha, x); |
|
266 } |
|
267 |
|
268 Matrix |
|
269 bessely (double alpha, const Matrix& x) |
|
270 { |
|
271 return do_bessel (F77_FCN (rybesl, RYBESL), "bessely", alpha, x); |
|
272 } |
|
273 |
|
274 Matrix |
|
275 besseli (double alpha, const Matrix& x) |
|
276 { |
|
277 return do_bessel (F77_FCN (ribesl, RIBESL), "besseli", alpha, x); |
|
278 } |
|
279 |
|
280 Matrix |
|
281 besselk (double alpha, const Matrix& x) |
|
282 { |
|
283 return do_bessel (F77_FCN (rkbesl, RKBESL), "besselk", alpha, x); |
|
284 } |
|
285 |
|
286 Matrix |
|
287 besselj (const Range& alpha, const ColumnVector& x) |
|
288 { |
|
289 return do_bessel (F77_FCN (rjbesl, RJBESL), "besselj", alpha, x); |
|
290 } |
|
291 |
|
292 Matrix |
|
293 bessely (const Range& alpha, const ColumnVector& x) |
|
294 { |
|
295 return do_bessel (F77_FCN (rybesl, RYBESL), "bessely", alpha, x); |
|
296 } |
|
297 |
|
298 Matrix |
|
299 besseli (const Range& alpha, const ColumnVector& x) |
|
300 { |
|
301 return do_bessel (F77_FCN (ribesl, RIBESL), "besseli", alpha, x); |
|
302 } |
|
303 |
|
304 Matrix |
|
305 besselk (const Range& alpha, const ColumnVector& x) |
|
306 { |
|
307 return do_bessel (F77_FCN (rkbesl, RKBESL), "besselk", alpha, x); |
|
308 } |
|
309 |
|
310 static void |
|
311 gripe_betainc_nonconformant (int r1, int c1, int r2, int c2, int r3, |
|
312 int c3) |
|
313 { |
|
314 (*current_liboctave_error_handler) |
|
315 ("betainc: nonconformant arguments (x is %dx%d, a is %dx%d, b is %dx%d)", |
|
316 r1, c1, r2, c2, r3, c3); |
|
317 } |
|
318 |
|
319 double |
|
320 betainc (double x, double a, double b) |
|
321 { |
|
322 double retval; |
|
323 F77_XFCN (xdbetai, XDBETAI, (x, a, b, retval)); |
|
324 return retval; |
|
325 } |
|
326 |
|
327 Matrix |
|
328 betainc (double x, double a, const Matrix& b) |
|
329 { |
|
330 int nr = b.rows (); |
|
331 int nc = b.cols (); |
|
332 |
|
333 Matrix retval (nr, nc); |
|
334 |
|
335 for (int j = 0; j < nc; j++) |
|
336 for (int i = 0; i < nr; i++) |
|
337 retval(i,j) = betainc (x, a, b(i,j)); |
|
338 |
|
339 return retval; |
|
340 } |
|
341 |
|
342 Matrix |
|
343 betainc (double x, const Matrix& a, double b) |
|
344 { |
|
345 int nr = a.rows (); |
|
346 int nc = a.cols (); |
|
347 |
|
348 Matrix retval (nr, nc); |
|
349 |
|
350 for (int j = 0; j < nc; j++) |
|
351 for (int i = 0; i < nr; i++) |
|
352 retval(i,j) = betainc (x, a(i,j), b); |
|
353 |
|
354 return retval; |
|
355 } |
|
356 |
|
357 Matrix |
|
358 betainc (double x, const Matrix& a, const Matrix& b) |
|
359 { |
|
360 Matrix retval; |
|
361 |
|
362 int a_nr = a.rows (); |
|
363 int a_nc = a.cols (); |
|
364 |
|
365 int b_nr = b.rows (); |
|
366 int b_nc = b.cols (); |
|
367 |
|
368 if (a_nr == b_nr && a_nc == b_nc) |
|
369 { |
|
370 retval.resize (a_nr, a_nc); |
|
371 |
|
372 for (int j = 0; j < a_nc; j++) |
|
373 for (int i = 0; i < a_nr; i++) |
|
374 retval(i,j) = betainc (x, a(i,j), b(i,j)); |
|
375 } |
|
376 else |
|
377 gripe_betainc_nonconformant (1, 1, a_nr, a_nc, b_nr, b_nc); |
|
378 |
|
379 return retval; |
|
380 } |
|
381 |
|
382 Matrix |
|
383 betainc (const Matrix& x, double a, double b) |
|
384 { |
|
385 int nr = x.rows (); |
|
386 int nc = x.cols (); |
|
387 |
|
388 Matrix retval (nr, nc); |
|
389 |
|
390 for (int j = 0; j < nc; j++) |
|
391 for (int i = 0; i < nr; i++) |
|
392 retval(i,j) = betainc (x(i,j), a, b); |
|
393 |
|
394 return retval; |
|
395 } |
|
396 |
|
397 Matrix |
|
398 betainc (const Matrix& x, double a, const Matrix& b) |
|
399 { |
|
400 Matrix retval; |
|
401 |
|
402 int nr = x.rows (); |
|
403 int nc = x.cols (); |
|
404 |
|
405 int b_nr = b.rows (); |
|
406 int b_nc = b.cols (); |
|
407 |
|
408 if (nr == b_nr && nc == b_nc) |
|
409 { |
|
410 retval.resize (nr, nc); |
|
411 |
|
412 for (int j = 0; j < nc; j++) |
|
413 for (int i = 0; i < nr; i++) |
|
414 retval(i,j) = betainc (x(i,j), a, b(i,j)); |
|
415 } |
|
416 else |
|
417 gripe_betainc_nonconformant (nr, nc, 1, 1, b_nr, b_nc); |
|
418 |
|
419 return retval; |
|
420 } |
|
421 |
|
422 Matrix |
|
423 betainc (const Matrix& x, const Matrix& a, double b) |
|
424 { |
|
425 Matrix retval; |
|
426 |
|
427 int nr = x.rows (); |
|
428 int nc = x.cols (); |
|
429 |
|
430 int a_nr = a.rows (); |
|
431 int a_nc = a.cols (); |
|
432 |
|
433 if (nr == a_nr && nc == a_nc) |
|
434 { |
|
435 retval.resize (nr, nc); |
|
436 |
|
437 for (int j = 0; j < nc; j++) |
|
438 for (int i = 0; i < nr; i++) |
|
439 retval(i,j) = betainc (x(i,j), a(i,j), b); |
|
440 } |
|
441 else |
|
442 gripe_betainc_nonconformant (nr, nc, a_nr, a_nc, 1, 1); |
|
443 |
|
444 return retval; |
|
445 } |
|
446 |
|
447 Matrix |
|
448 betainc (const Matrix& x, const Matrix& a, const Matrix& b) |
|
449 { |
|
450 Matrix retval; |
|
451 |
|
452 int nr = x.rows (); |
|
453 int nc = x.cols (); |
|
454 |
|
455 int a_nr = a.rows (); |
|
456 int a_nc = a.cols (); |
|
457 |
|
458 int b_nr = b.rows (); |
|
459 int b_nc = b.cols (); |
|
460 |
|
461 if (nr == a_nr && nr == b_nr && nc == a_nc && nc == b_nc) |
|
462 { |
|
463 retval.resize (nr, nc); |
|
464 |
|
465 for (int j = 0; j < nc; j++) |
|
466 for (int i = 0; i < nr; i++) |
|
467 retval(i,j) = betainc (x(i,j), a(i,j), b(i,j)); |
|
468 } |
|
469 else |
|
470 gripe_betainc_nonconformant (nr, nc, a_nr, a_nc, b_nr, b_nc); |
|
471 |
|
472 return retval; |
|
473 } |
|
474 |
3164
|
475 // XXX FIXME XXX -- there is still room for improvement here... |
|
476 |
3146
|
477 double |
|
478 gammainc (double x, double a) |
|
479 { |
|
480 double retval; |
3164
|
481 |
|
482 F77_XFCN (xdgamit, XDGAMIT, (a, x, retval)); |
|
483 |
|
484 if (x == 0.0) |
|
485 retval = 0.0; |
|
486 else if (x > 0.0) |
|
487 retval = exp (a * log (x) + log (retval)); |
|
488 |
3146
|
489 return retval; |
|
490 } |
|
491 |
|
492 Matrix |
|
493 gammainc (double x, const Matrix& a) |
|
494 { |
|
495 int nr = a.rows (); |
|
496 int nc = a.cols (); |
|
497 |
|
498 Matrix retval (nr, nc); |
|
499 |
|
500 for (int j = 0; j < nc; j++) |
|
501 for (int i = 0; i < nr; i++) |
|
502 retval(i,j) = gammainc (x, a(i,j)); |
|
503 |
|
504 return retval; |
|
505 } |
|
506 |
|
507 Matrix |
|
508 gammainc (const Matrix& x, double a) |
|
509 { |
|
510 int nr = x.rows (); |
|
511 int nc = x.cols (); |
|
512 |
|
513 Matrix retval (nr, nc); |
|
514 |
|
515 for (int j = 0; j < nc; j++) |
|
516 for (int i = 0; i < nr; i++) |
|
517 retval(i,j) = gammainc (x(i,j), a); |
|
518 |
|
519 return retval; |
|
520 } |
|
521 |
|
522 Matrix |
|
523 gammainc (const Matrix& x, const Matrix& a) |
|
524 { |
|
525 Matrix retval; |
|
526 |
|
527 int nr = x.rows (); |
|
528 int nc = x.cols (); |
|
529 |
|
530 int a_nr = a.rows (); |
|
531 int a_nc = a.cols (); |
|
532 |
|
533 if (nr == a_nr && nc == a_nc) |
|
534 { |
|
535 retval.resize (nr, nc); |
|
536 |
|
537 for (int j = 0; j < nc; j++) |
|
538 for (int i = 0; i < nr; i++) |
|
539 retval(i,j) = gammainc (x(i,j), a(i,j)); |
|
540 } |
|
541 else |
|
542 (*current_liboctave_error_handler) |
|
543 ("gammainc: nonconformant arguments (arg 1 is %dx%d, arg 2 is %dx%d)", |
|
544 nr, nc, a_nr, a_nc); |
|
545 |
|
546 return retval; |
|
547 } |
|
548 |
|
549 /* |
|
550 ;;; Local Variables: *** |
|
551 ;;; mode: C++ *** |
|
552 ;;; End: *** |
|
553 */ |