3155
|
1 /* |
|
2 |
|
3 Copyright (C) 1997 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 "lo-specfun.h" |
|
28 |
|
29 #include "defun-dld.h" |
|
30 #include "error.h" |
|
31 #include "gripes.h" |
|
32 #include "oct-obj.h" |
|
33 #include "utils.h" |
|
34 |
3220
|
35 enum bessel_type |
|
36 { |
|
37 BESSEL_J, |
|
38 BESSEL_Y, |
|
39 BESSEL_I, |
|
40 BESSEL_K, |
|
41 BESSEL_H1, |
|
42 BESSEL_H2 |
|
43 }; |
|
44 |
|
45 #define DO_BESSEL(type, alpha, x, scaled, ierr, result) \ |
3155
|
46 do \ |
|
47 { \ |
|
48 switch (type) \ |
|
49 { \ |
3220
|
50 case BESSEL_J: \ |
|
51 result = besselj (alpha, x, scaled, ierr); \ |
|
52 break; \ |
|
53 \ |
|
54 case BESSEL_Y: \ |
|
55 result = bessely (alpha, x, scaled, ierr); \ |
3155
|
56 break; \ |
|
57 \ |
3220
|
58 case BESSEL_I: \ |
|
59 result = besseli (alpha, x, scaled, ierr); \ |
3155
|
60 break; \ |
|
61 \ |
3220
|
62 case BESSEL_K: \ |
|
63 result = besselk (alpha, x, scaled, ierr); \ |
3155
|
64 break; \ |
|
65 \ |
3220
|
66 case BESSEL_H1: \ |
|
67 result = besselh1 (alpha, x, scaled, ierr); \ |
|
68 break; \ |
|
69 \ |
|
70 case BESSEL_H2: \ |
|
71 result = besselh2 (alpha, x, scaled, ierr); \ |
3155
|
72 break; \ |
|
73 \ |
|
74 default: \ |
|
75 break; \ |
|
76 } \ |
|
77 } \ |
|
78 while (0) |
|
79 |
3220
|
80 static inline Matrix |
|
81 int_array2_to_matrix (const Array2<int>& a) |
|
82 { |
|
83 int nr = a.rows (); |
|
84 int nc = a.cols (); |
|
85 |
|
86 Matrix retval (nr, nc); |
|
87 |
|
88 for (int j = 0; j < nc; j++) |
|
89 for (int i = 0; i < nr; i++) |
|
90 retval(i,j) = (double) (a(i,j)); |
|
91 |
|
92 return retval; |
|
93 } |
|
94 |
3155
|
95 static void |
3220
|
96 gripe_bessel_arg (const char *fn, const char *arg) |
3155
|
97 { |
3220
|
98 error ("%s: expecting scalar or matrix as %s argument", fn, arg); |
3155
|
99 } |
|
100 |
|
101 octave_value_list |
3220
|
102 do_bessel (enum bessel_type type, const char *fn, |
|
103 const octave_value_list& args, int nargout) |
3155
|
104 { |
3220
|
105 octave_value_list retval; |
3155
|
106 |
|
107 int nargin = args.length (); |
|
108 |
3220
|
109 if (nargin == 2 || nargin == 3) |
3155
|
110 { |
3220
|
111 bool scaled = (nargin == 3); |
|
112 |
3155
|
113 octave_value alpha_arg = args(0); |
3220
|
114 octave_value x_arg = args(1); |
3155
|
115 |
|
116 if (alpha_arg.is_scalar_type ()) |
|
117 { |
3220
|
118 double alpha = args(0).double_value (); |
3155
|
119 |
|
120 if (! error_state) |
|
121 { |
3220
|
122 if (x_arg.is_scalar_type ()) |
|
123 { |
|
124 Complex x = x_arg.complex_value (); |
|
125 |
|
126 if (! error_state) |
|
127 { |
|
128 int ierr; |
|
129 octave_value result; |
|
130 |
|
131 DO_BESSEL (type, alpha, x, scaled, ierr, result); |
|
132 |
|
133 if (nargout > 1) |
|
134 retval(1) = (double) ierr; |
3155
|
135 |
3220
|
136 retval(0) = result; |
|
137 } |
|
138 else |
|
139 gripe_bessel_arg (fn, "second"); |
|
140 } |
3155
|
141 else |
3220
|
142 { |
|
143 ComplexMatrix x = x_arg.complex_matrix_value (); |
|
144 |
|
145 if (! error_state) |
|
146 { |
|
147 Array2<int> ierr; |
|
148 octave_value result; |
|
149 |
|
150 DO_BESSEL (type, alpha, x, scaled, ierr, result); |
|
151 |
|
152 if (nargout > 1) |
|
153 retval(1) = int_array2_to_matrix (ierr); |
|
154 |
|
155 retval(0) = result; |
|
156 } |
|
157 else |
|
158 gripe_bessel_arg (fn, "second"); |
|
159 } |
3155
|
160 } |
|
161 else |
3220
|
162 gripe_bessel_arg (fn, "first"); |
3155
|
163 } |
|
164 else |
|
165 { |
3220
|
166 Matrix alpha = args(0).matrix_value (); |
3155
|
167 |
3220
|
168 if (! error_state) |
3155
|
169 { |
3220
|
170 if (x_arg.is_scalar_type ()) |
|
171 { |
|
172 Complex x = x_arg.complex_value (); |
|
173 |
|
174 if (! error_state) |
|
175 { |
|
176 Array2<int> ierr; |
|
177 octave_value result; |
3155
|
178 |
3220
|
179 DO_BESSEL (type, alpha, x, scaled, ierr, result); |
|
180 |
|
181 if (nargout > 1) |
|
182 retval(1) = int_array2_to_matrix (ierr); |
|
183 |
|
184 retval(0) = result; |
|
185 } |
|
186 else |
|
187 gripe_bessel_arg (fn, "second"); |
|
188 } |
|
189 else |
3155
|
190 { |
3220
|
191 ComplexMatrix x = x_arg.complex_matrix_value (); |
3155
|
192 |
3220
|
193 if (! error_state) |
|
194 { |
|
195 if (alpha.rows () == 1 && x.columns () == 1) |
|
196 { |
|
197 RowVector ralpha = alpha.row (0); |
|
198 ComplexColumnVector cx = x.column (0); |
|
199 |
|
200 Array2<int> ierr; |
|
201 octave_value result; |
|
202 |
|
203 DO_BESSEL (type, ralpha, cx, scaled, ierr, result); |
3155
|
204 |
3220
|
205 if (nargout > 1) |
|
206 retval(1) = int_array2_to_matrix (ierr); |
|
207 |
|
208 retval(0) = result; |
|
209 } |
|
210 else |
3155
|
211 { |
3220
|
212 Array2<int> ierr; |
|
213 octave_value result; |
|
214 |
|
215 DO_BESSEL (type, alpha, x, scaled, ierr, result); |
|
216 |
|
217 if (nargout > 1) |
|
218 retval(1) = int_array2_to_matrix (ierr); |
|
219 |
|
220 retval(0) = result; |
3155
|
221 } |
|
222 } |
3220
|
223 else |
|
224 gripe_bessel_arg (fn, "second"); |
3155
|
225 } |
|
226 } |
|
227 else |
3220
|
228 gripe_bessel_arg (fn, "first"); |
3155
|
229 } |
|
230 } |
|
231 else |
|
232 print_usage (fn); |
|
233 |
|
234 return retval; |
|
235 } |
|
236 |
3220
|
237 DEFUN_DLD (besselj, args, nargout, |
|
238 "[J, IERR] = BESSELJ (ALPHA, X [, 1])\n\ |
3155
|
239 \n\ |
|
240 Compute Bessel functions of the first kind.\n\ |
|
241 \n\ |
3220
|
242 If a third argument is supplied, scale the result by exp(-I*Z) for\n\ |
|
243 K = 1 or exp(I*Z) for K = 2.\n\ |
|
244 \n\ |
|
245 If ALPHA is a scalar, the result is the same size as X. If X is a\n\ |
|
246 scalar, the result is the same size as ALPHA. If ALPHA is a row\n\ |
|
247 vector and X is a column vector, the result is a matrix with\n\ |
|
248 length(X) rows and length(ALPHA) columns. Otherwise, ALPHA and X must\n\ |
|
249 conform and the result will be the same size.\n\ |
|
250 \n\ |
|
251 ALPHA must be real. X may be complex.\n\ |
3155
|
252 \n\ |
3220
|
253 If requested, IERR contains the following status information and is\n\ |
|
254 the same size as the result.\n\ |
|
255 \n |
|
256 0 normal return\n\ |
|
257 1 input error, return NaN\n\ |
|
258 2 overflow, return Inf\n\ |
|
259 3 loss of significance by argument reduction results in less than\n\ |
|
260 half of machine accuracy\n\ |
|
261 4 complete loss of significance by argument reduction, return NaN\n\ |
|
262 5 error -- no computation, algorithm termination condition not met,\n\ |
|
263 return NaN") |
3155
|
264 { |
3220
|
265 return do_bessel (BESSEL_J, "besselj", args, nargout); |
3155
|
266 } |
|
267 |
3220
|
268 DEFUN_DLD (bessely, args, nargout, |
|
269 "[Y, IERR] = BESSELY (ALPHA, X [, 1])\n\ |
3155
|
270 \n\ |
|
271 Compute Bessel functions of the second kind.\n\ |
|
272 \n\ |
3220
|
273 If a third argument is supplied, scale the result by exp(-I*Z) for\n\ |
|
274 K = 1 or exp(I*Z) for K = 2.\n\ |
|
275 \n\ |
|
276 If ALPHA is a scalar, the result is the same size as X. If X is a\n\ |
|
277 scalar, the result is the same size as ALPHA. If ALPHA is a row\n\ |
|
278 vector and X is a column vector, the result is a matrix with\n\ |
|
279 length(X) rows and length(ALPHA) columns. Otherwise, ALPHA and X must\n\ |
|
280 conform and the result will be the same size.\n\ |
|
281 \n\ |
|
282 ALPHA must be real. X may be complex.\n\ |
3155
|
283 \n\ |
3220
|
284 If requested, IERR contains the following status information and is\n\ |
|
285 the same size as the result.\n\ |
|
286 \n |
|
287 0 normal return\n\ |
|
288 1 input error, return NaN\n\ |
|
289 2 overflow, return Inf\n\ |
|
290 3 loss of significance by argument reduction results in less than\n\ |
|
291 half of machine accuracy\n\ |
|
292 4 complete loss of significance by argument reduction, return NaN\n\ |
|
293 5 error -- no computation, algorithm termination condition not met,\n\ |
|
294 return NaN") |
3155
|
295 { |
3220
|
296 return do_bessel (BESSEL_Y, "bessely", args, nargout); |
3155
|
297 } |
|
298 |
3220
|
299 DEFUN_DLD (besseli, args, nargout, |
|
300 "[I, IERR] = BESSELI (ALPHA, X [, 1])\n\ |
3155
|
301 \n\ |
|
302 Compute modified Bessel functions of the first kind.\n\ |
|
303 \n\ |
3220
|
304 If a third argument is supplied, scale the result by exp(-I*Z) for\n\ |
|
305 K = 1 or exp(I*Z) for K = 2.\n\ |
|
306 \n\ |
|
307 If ALPHA is a scalar, the result is the same size as X. If X is a\n\ |
|
308 scalar, the result is the same size as ALPHA. If ALPHA is a row\n\ |
|
309 vector and X is a column vector, the result is a matrix with\n\ |
|
310 length(X) rows and length(ALPHA) columns. Otherwise, ALPHA and X must\n\ |
|
311 conform and the result will be the same size.\n\ |
|
312 \n\ |
|
313 ALPHA must be real. X may be complex.\n\ |
3155
|
314 \n\ |
3220
|
315 If requested, IERR contains the following status information and is\n\ |
|
316 the same size as the result.\n\ |
|
317 \n |
|
318 0 normal return\n\ |
|
319 1 input error, return NaN\n\ |
|
320 2 overflow, return Inf\n\ |
|
321 3 loss of significance by argument reduction results in less than\n\ |
|
322 half of machine accuracy\n\ |
|
323 4 complete loss of significance by argument reduction, return NaN\n\ |
|
324 5 error -- no computation, algorithm termination condition not met,\n\ |
|
325 return NaN") |
3155
|
326 { |
3220
|
327 return do_bessel (BESSEL_I, "besseli", args, nargout); |
3155
|
328 } |
|
329 |
3220
|
330 DEFUN_DLD (besselk, args, nargout, |
|
331 "[K, IERR] = BESSELK (ALPHA, X [, 1])\n\ |
3155
|
332 \n\ |
|
333 Compute modified Bessel functions of the second kind.\n\ |
|
334 \n\ |
3220
|
335 If a third argument is supplied, scale the result by exp(-I*Z) for\n\ |
|
336 K = 1 or exp(I*Z) for K = 2.\n\ |
|
337 \n\ |
|
338 If ALPHA is a scalar, the result is the same size as X. If X is a\n\ |
|
339 scalar, the result is the same size as ALPHA. If ALPHA is a row\n\ |
|
340 vector and X is a column vector, the result is a matrix with\n\ |
|
341 length(X) rows and length(ALPHA) columns. Otherwise, ALPHA and X must\n\ |
|
342 conform and the result will be the same size.\n\ |
|
343 \n\ |
|
344 ALPHA must be real. X may be complex.\n\ |
|
345 \n\ |
|
346 If requested, IERR contains the following status information and is\n\ |
|
347 the same size as the result.\n\ |
|
348 \n |
|
349 0 normal return\n\ |
|
350 1 input error, return NaN\n\ |
|
351 2 overflow, return Inf\n\ |
|
352 3 loss of significance by argument reduction results in less than\n\ |
|
353 half of machine accuracy\n\ |
|
354 4 complete loss of significance by argument reduction, return NaN\n\ |
|
355 5 error -- no computation, algorithm termination condition not met,\n\ |
|
356 return NaN") |
|
357 { |
|
358 return do_bessel (BESSEL_K, "besselk", args, nargout); |
|
359 } |
|
360 |
|
361 DEFUN_DLD (besselh, args, nargout, |
|
362 "[H, IERR] = besselh (ALPHA, K, X [, 1])\n\ |
|
363 \n\ |
|
364 Compute Hankel functions of the first (K = 1) or second (K = 2) kind.\n\ |
|
365 \n\ |
|
366 If a fourth argument is supplied, scale the result by exp(-I*Z) for\n\ |
|
367 K = 1 or exp(I*Z) for K = 2.\n\ |
|
368 \n\ |
|
369 If ALPHA is a scalar, the result is the same size as X. If X is a\n\ |
|
370 scalar, the result is the same size as ALPHA. If ALPHA is a row\n\ |
|
371 vector and X is a column vector, the result is a matrix with\n\ |
|
372 length(X) rows and length(ALPHA) columns. Otherwise, ALPHA and X must\n\ |
|
373 conform and the result will be the same size.\n\ |
|
374 \n\ |
|
375 ALPHA must be real. X may be complex.\n\ |
3155
|
376 \n\ |
3220
|
377 If requested, IERR contains the following status information and is\n\ |
|
378 the same size as the result.\n\ |
|
379 \n |
|
380 0 normal return\n\ |
|
381 1 input error, return NaN\n\ |
|
382 2 overflow, return Inf\n\ |
|
383 3 loss of significance by argument reduction results in less than\n\ |
|
384 half of machine accuracy\n\ |
|
385 4 complete loss of significance by argument reduction, return NaN\n\ |
|
386 5 error -- no computation, algorithm termination condition not met,\n\ |
|
387 return NaN") |
|
388 { |
|
389 octave_value_list retval; |
|
390 |
|
391 int nargin = args.length (); |
|
392 |
|
393 int kind = 1; |
|
394 |
|
395 if (nargin == 2) |
|
396 { |
|
397 retval = do_bessel (BESSEL_H1, "besselh", args, nargout); |
|
398 } |
|
399 else if (nargin == 3 || nargin == 4) |
|
400 { |
|
401 double d_kind = args(1).double_value (); |
|
402 |
|
403 if (! error_state && D_NINT (d_kind) == d_kind) |
|
404 { |
|
405 octave_value_list tmp_args; |
|
406 |
|
407 if (nargin == 4) |
|
408 tmp_args(2) = args(3); |
|
409 |
|
410 tmp_args(1) = args(2); |
|
411 tmp_args(0) = args(0); |
|
412 |
|
413 if (kind == 1) |
|
414 retval = do_bessel (BESSEL_H1, "besselh", tmp_args, nargout); |
|
415 else if (kind == 2) |
|
416 retval = do_bessel (BESSEL_H2, "besselh", tmp_args, nargout); |
|
417 else |
|
418 error ("besselh: expecting K = 1 or 2"); |
|
419 } |
|
420 else |
|
421 error ("besselh: invalid value of K"); |
|
422 } |
|
423 else |
|
424 print_usage ("besselh"); |
|
425 |
|
426 return retval; |
|
427 } |
|
428 |
|
429 DEFUN_DLD (airy, args, nargout, |
|
430 "[A, IERR] = airy (K, Z, [, 1])\n\ |
|
431 \n\ |
|
432 Compute Airy functions of the first and second kind, and their\n\ |
|
433 derivatives.\n\ |
|
434 \n\ |
|
435 K Function Scale factor (if a third argument is supplied)\n\ |
|
436 --- -------- ----------------------------------------------\n\ |
|
437 0 Ai (Z) exp ((2/3) * Z * sqrt (Z))\n\ |
|
438 1 dAi(Z)/dZ exp ((2/3) * Z * sqrt (Z))\n\ |
|
439 2 Bi (Z) exp (-abs (real ((2/3) * Z *sqrt (Z))))\n\ |
|
440 3 dBi(Z)/dZ exp (-abs (real ((2/3) * Z *sqrt (Z))))\n\ |
|
441 \n\ |
|
442 The function call airy (Z) is equivalent to airy (0, Z).\n\ |
3155
|
443 \n\ |
3220
|
444 The result is the same size as Z. |
|
445 \n\ |
|
446 If requested, IERR contains the following status information and is\n\ |
|
447 the same size as the result.\n\ |
|
448 \n |
|
449 0 normal return\n\ |
|
450 1 input error, return NaN\n\ |
|
451 2 overflow, return Inf\n\ |
|
452 3 loss of significance by argument reduction results in less than\n\ |
|
453 half of machine accuracy\n\ |
|
454 4 complete loss of significance by argument reduction, return NaN\n\ |
|
455 5 error -- no computation, algorithm termination condition not met,\n\ |
|
456 return NaN") |
3155
|
457 { |
3220
|
458 octave_value_list retval; |
|
459 |
|
460 int nargin = args.length (); |
|
461 |
|
462 if (nargin > 0 && nargin < 4) |
|
463 { |
|
464 bool scale = (nargin == 3); |
|
465 |
|
466 int kind = 0; |
|
467 |
|
468 ComplexMatrix z; |
|
469 |
|
470 if (nargin > 1) |
|
471 { |
|
472 double d_kind = args(0).double_value (); |
|
473 |
|
474 if (! error_state) |
|
475 { |
|
476 kind = (int) d_kind; |
|
477 |
|
478 if (kind < 0 || kind > 3) |
|
479 error ("airy: expecting K = 0, 1, 2, or 3"); |
|
480 } |
|
481 else |
|
482 error ("airy: expecting integer value for K"); |
|
483 } |
|
484 |
|
485 if (! error_state) |
|
486 { |
|
487 z = args(nargin == 1 ? 0 : 1).complex_matrix_value (); |
|
488 |
|
489 if (! error_state) |
|
490 { |
|
491 Array2<int> ierr; |
|
492 octave_value result; |
|
493 |
|
494 if (kind > 1) |
|
495 result = biry (z, kind == 3, scale, ierr); |
|
496 else |
|
497 result = airy (z, kind == 1, scale, ierr); |
|
498 |
|
499 if (nargout > 1) |
|
500 retval(1) = int_array2_to_matrix (ierr); |
|
501 |
|
502 retval(0) = result; |
|
503 } |
|
504 else |
|
505 error ("airy: expecting complex matrix for Z"); |
|
506 } |
|
507 } |
|
508 else |
|
509 print_usage ("airy"); |
|
510 |
|
511 return retval; |
3155
|
512 } |
|
513 |
|
514 /* |
|
515 ;;; Local Variables: *** |
|
516 ;;; mode: C++ *** |
|
517 ;;; End: *** |
|
518 */ |