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