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 |
3155
|
100 static void |
3220
|
101 gripe_bessel_arg (const char *fn, const char *arg) |
3155
|
102 { |
3220
|
103 error ("%s: expecting scalar or matrix as %s argument", fn, arg); |
3155
|
104 } |
|
105 |
|
106 octave_value_list |
3220
|
107 do_bessel (enum bessel_type type, const char *fn, |
|
108 const octave_value_list& args, int nargout) |
3155
|
109 { |
3220
|
110 octave_value_list retval; |
3155
|
111 |
|
112 int nargin = args.length (); |
|
113 |
3220
|
114 if (nargin == 2 || nargin == 3) |
3155
|
115 { |
3220
|
116 bool scaled = (nargin == 3); |
|
117 |
3155
|
118 octave_value alpha_arg = args(0); |
3220
|
119 octave_value x_arg = args(1); |
3155
|
120 |
|
121 if (alpha_arg.is_scalar_type ()) |
|
122 { |
3220
|
123 double alpha = args(0).double_value (); |
3155
|
124 |
|
125 if (! error_state) |
|
126 { |
3220
|
127 if (x_arg.is_scalar_type ()) |
|
128 { |
|
129 Complex x = x_arg.complex_value (); |
|
130 |
|
131 if (! error_state) |
|
132 { |
|
133 int ierr; |
|
134 octave_value result; |
|
135 |
|
136 DO_BESSEL (type, alpha, x, scaled, ierr, result); |
|
137 |
|
138 if (nargout > 1) |
|
139 retval(1) = (double) ierr; |
3155
|
140 |
3220
|
141 retval(0) = result; |
|
142 } |
|
143 else |
|
144 gripe_bessel_arg (fn, "second"); |
|
145 } |
3155
|
146 else |
3220
|
147 { |
|
148 ComplexMatrix x = x_arg.complex_matrix_value (); |
|
149 |
|
150 if (! error_state) |
|
151 { |
|
152 Array2<int> ierr; |
|
153 octave_value result; |
|
154 |
|
155 DO_BESSEL (type, alpha, x, scaled, ierr, result); |
|
156 |
|
157 if (nargout > 1) |
|
158 retval(1) = int_array2_to_matrix (ierr); |
|
159 |
|
160 retval(0) = result; |
|
161 } |
|
162 else |
|
163 gripe_bessel_arg (fn, "second"); |
|
164 } |
3155
|
165 } |
|
166 else |
3220
|
167 gripe_bessel_arg (fn, "first"); |
3155
|
168 } |
|
169 else |
|
170 { |
3220
|
171 Matrix alpha = args(0).matrix_value (); |
3155
|
172 |
3220
|
173 if (! error_state) |
3155
|
174 { |
3220
|
175 if (x_arg.is_scalar_type ()) |
|
176 { |
|
177 Complex x = x_arg.complex_value (); |
|
178 |
|
179 if (! error_state) |
|
180 { |
|
181 Array2<int> ierr; |
|
182 octave_value result; |
3155
|
183 |
3220
|
184 DO_BESSEL (type, alpha, x, scaled, ierr, result); |
|
185 |
|
186 if (nargout > 1) |
|
187 retval(1) = int_array2_to_matrix (ierr); |
|
188 |
|
189 retval(0) = result; |
|
190 } |
|
191 else |
|
192 gripe_bessel_arg (fn, "second"); |
|
193 } |
|
194 else |
3155
|
195 { |
3220
|
196 ComplexMatrix x = x_arg.complex_matrix_value (); |
3155
|
197 |
3220
|
198 if (! error_state) |
|
199 { |
|
200 if (alpha.rows () == 1 && x.columns () == 1) |
|
201 { |
|
202 RowVector ralpha = alpha.row (0); |
|
203 ComplexColumnVector cx = x.column (0); |
|
204 |
|
205 Array2<int> ierr; |
|
206 octave_value result; |
|
207 |
|
208 DO_BESSEL (type, ralpha, cx, scaled, ierr, result); |
3155
|
209 |
3220
|
210 if (nargout > 1) |
|
211 retval(1) = int_array2_to_matrix (ierr); |
|
212 |
|
213 retval(0) = result; |
|
214 } |
|
215 else |
3155
|
216 { |
3220
|
217 Array2<int> ierr; |
|
218 octave_value result; |
|
219 |
|
220 DO_BESSEL (type, alpha, x, scaled, ierr, result); |
|
221 |
|
222 if (nargout > 1) |
|
223 retval(1) = int_array2_to_matrix (ierr); |
|
224 |
|
225 retval(0) = result; |
3155
|
226 } |
|
227 } |
3220
|
228 else |
|
229 gripe_bessel_arg (fn, "second"); |
3155
|
230 } |
|
231 } |
|
232 else |
3220
|
233 gripe_bessel_arg (fn, "first"); |
3155
|
234 } |
|
235 } |
|
236 else |
|
237 print_usage (fn); |
|
238 |
|
239 return retval; |
|
240 } |
|
241 |
3220
|
242 DEFUN_DLD (besselj, args, nargout, |
3459
|
243 "-*- texinfo -*-\n\ |
|
244 @deftypefn {Loadable Function} {[@var{j}, @var{ierr}] =} besselj (@var{alpha}, @var{x}, @var{opt})\n\ |
|
245 @deftypefnx {Loadable Function} {[@var{y}, @var{ierr}] =} bessely (@var{alpha}, @var{x}, @var{opt})\n\ |
|
246 @deftypefnx {Loadable Function} {[@var{i}, @var{ierr}] =} besseli (@var{alpha}, @var{x}, @var{opt})\n\ |
|
247 @deftypefnx {Loadable Function} {[@var{k}, @var{ierr}] =} besselk (@var{alpha}, @var{x}, @var{opt})\n\ |
|
248 @deftypefnx {Loadable Function} {[@var{h}, @var{ierr}] =} besselh (@var{alpha}, @var{k}, @var{x}, @var{opt})\n\ |
|
249 Compute Bessel or Hankel functions of various kinds:\n\ |
3155
|
250 \n\ |
3459
|
251 @table @code\n\ |
|
252 @item besselj\n\ |
|
253 Bessel functions of the first kind.\n\ |
|
254 @item bessely\n\ |
|
255 Bessel functions of the second kind.\n\ |
|
256 @item besseli\n\ |
|
257 Modified Bessel functions of the first kind.\n\ |
|
258 @item besselk\n\ |
|
259 Modified Bessel functions of the second kind.\n\ |
|
260 @item besselh\n\ |
|
261 Compute Hankel functions of the first (@var{k} = 1) or second (@var{k}\n\ |
|
262 = 2) kind.\n\ |
|
263 @end table\n\ |
3220
|
264 \n\ |
3568
|
265 If the argument @var{opt} is supplied, the result is scaled by the\n\ |
3459
|
266 @code{exp (-I*@var{x})} for @var{k} = 1 or @code{exp (I*@var{x})} for\n\ |
3499
|
267 @var{k} = 2.\n\ |
3220
|
268 \n\ |
3459
|
269 If @var{alpha} is a scalar, the result is the same size as @var{x}.\n\ |
|
270 If @var{x} is a scalar, the result is the same size as @var{alpha}.\n\ |
|
271 If @var{alpha} is a row vector and @var{x} is a column vector, the\n\ |
|
272 result is a matrix with @code{length (@var{x})} rows and\n\ |
|
273 @code{length (@var{alpha})} columns. Otherwise, @var{alpha} and\n\ |
|
274 @var{x} must conform and the result will be the same size.\n\ |
3155
|
275 \n\ |
3459
|
276 The value of @var{alpha} must be real. The value of @var{x} may be\n\ |
|
277 complex.\n\ |
|
278 \n\ |
|
279 If requested, @var{ierr} contains the following status information\n\ |
|
280 and is the same size as the result.\n\ |
3548
|
281 \n\ |
3459
|
282 @enumerate 0\n\ |
|
283 @item\n\ |
|
284 Normal return.\n\ |
|
285 @item\n\ |
|
286 Input error, return @code{NaN}.\n\ |
|
287 @item\n\ |
|
288 Overflow, return @code{Inf}.\n\ |
|
289 @item\n\ |
|
290 Loss of significance by argument reduction results in less than\n\ |
|
291 half of machine accuracy.\n\ |
|
292 @item\n\ |
|
293 Complete loss of significance by argument reduction, return @code{NaN}.\n\ |
|
294 @item\n\ |
|
295 Error---no computation, algorithm termination condition not met,\n\ |
|
296 return @code{NaN}.\n\ |
|
297 @end enumerate\n\ |
|
298 @end deftypefn") |
3155
|
299 { |
3220
|
300 return do_bessel (BESSEL_J, "besselj", args, nargout); |
3155
|
301 } |
|
302 |
3220
|
303 DEFUN_DLD (bessely, args, nargout, |
3459
|
304 "-*- texinfo -*-\n\ |
|
305 @deftypefn {Loadable Function} {[@var{y}, @var{ierr}] =} bessely (@var{alpha}, @var{x}, @var{opt})\n\ |
|
306 See besselj.\n\ |
|
307 @end deftypefn") |
3155
|
308 { |
3220
|
309 return do_bessel (BESSEL_Y, "bessely", args, nargout); |
3155
|
310 } |
|
311 |
3220
|
312 DEFUN_DLD (besseli, args, nargout, |
3459
|
313 "-*- texinfo -*-\n\ |
|
314 @deftypefn {Loadable Function} {[@var{i}, @var{ierr}] =} besseli (@var{alpha}, @var{x}, @var{opt})\n\ |
|
315 See besselj.\n\ |
|
316 @end deftypefn") |
3155
|
317 { |
3220
|
318 return do_bessel (BESSEL_I, "besseli", args, nargout); |
3155
|
319 } |
|
320 |
3220
|
321 DEFUN_DLD (besselk, args, nargout, |
3459
|
322 "-*- texinfo -*-\n\ |
|
323 @deftypefn {Loadable Function} {[@var{k}, @var{ierr}] =} besselk (@var{alpha}, @var{x}, @var{opt})\n\ |
|
324 See besselj.\n\ |
|
325 @end deftypefn") |
3220
|
326 { |
|
327 return do_bessel (BESSEL_K, "besselk", args, nargout); |
|
328 } |
|
329 |
|
330 DEFUN_DLD (besselh, args, nargout, |
3459
|
331 "-*- texinfo -*-\n\ |
|
332 @deftypefn {Loadable Function} {[@var{h}, @var{ierr}] =} besselh (@var{alpha}, @var{k}, @var{x}, @var{opt})\n\ |
|
333 See besselj.\n\ |
|
334 @end deftypefn") |
3220
|
335 { |
|
336 octave_value_list retval; |
|
337 |
|
338 int nargin = args.length (); |
|
339 |
|
340 if (nargin == 2) |
|
341 { |
|
342 retval = do_bessel (BESSEL_H1, "besselh", args, nargout); |
|
343 } |
|
344 else if (nargin == 3 || nargin == 4) |
|
345 { |
3810
|
346 int kind = args(1).int_value (); |
3220
|
347 |
3810
|
348 if (! error_state) |
3220
|
349 { |
|
350 octave_value_list tmp_args; |
|
351 |
|
352 if (nargin == 4) |
|
353 tmp_args(2) = args(3); |
|
354 |
|
355 tmp_args(1) = args(2); |
|
356 tmp_args(0) = args(0); |
|
357 |
|
358 if (kind == 1) |
|
359 retval = do_bessel (BESSEL_H1, "besselh", tmp_args, nargout); |
|
360 else if (kind == 2) |
|
361 retval = do_bessel (BESSEL_H2, "besselh", tmp_args, nargout); |
|
362 else |
|
363 error ("besselh: expecting K = 1 or 2"); |
|
364 } |
|
365 else |
|
366 error ("besselh: invalid value of K"); |
|
367 } |
|
368 else |
|
369 print_usage ("besselh"); |
|
370 |
|
371 return retval; |
|
372 } |
|
373 |
|
374 DEFUN_DLD (airy, args, nargout, |
3459
|
375 "-*- texinfo -*-\n\ |
|
376 @deftypefn {Loadable Function} {[@var{a}, @var{ierr}] =} airy (@var{k}, @var{z}, @var{opt})\n\ |
3220
|
377 Compute Airy functions of the first and second kind, and their\n\ |
|
378 derivatives.\n\ |
|
379 \n\ |
3459
|
380 @example\n\ |
3220
|
381 K Function Scale factor (if a third argument is supplied)\n\ |
|
382 --- -------- ----------------------------------------------\n\ |
|
383 0 Ai (Z) exp ((2/3) * Z * sqrt (Z))\n\ |
|
384 1 dAi(Z)/dZ exp ((2/3) * Z * sqrt (Z))\n\ |
|
385 2 Bi (Z) exp (-abs (real ((2/3) * Z *sqrt (Z))))\n\ |
|
386 3 dBi(Z)/dZ exp (-abs (real ((2/3) * Z *sqrt (Z))))\n\ |
3459
|
387 @end example\n\ |
3220
|
388 \n\ |
3549
|
389 The function call @code{airy (@var{z})} is equivalent to\n\ |
3459
|
390 @code{airy (0, @var{z})}.\n\ |
3155
|
391 \n\ |
3549
|
392 The result is the same size as @var{z}.\n\ |
3220
|
393 \n\ |
3459
|
394 If requested, @var{ierr} contains the following status information and\n\ |
|
395 is the same size as the result.\n\ |
3548
|
396 \n\ |
3459
|
397 @enumerate 0\n\ |
|
398 @item\n\ |
|
399 Normal return.\n\ |
|
400 @item\n\ |
|
401 Input error, return @code{NaN}.\n\ |
|
402 @item\n\ |
|
403 Overflow, return @code{Inf}.\n\ |
|
404 @item\n\ |
|
405 Loss of significance by argument reduction results in less than half\n\ |
|
406 of machine accuracy.\n\ |
|
407 @item\n\ |
|
408 Complete loss of significance by argument reduction, return @code{NaN}.\n\ |
|
409 @item\n\ |
|
410 Error---no computation, algorithm termination condition not met,\n\ |
|
411 return @code{NaN}\n\ |
|
412 @end enumerate\n\ |
|
413 @end deftypefn") |
3155
|
414 { |
3220
|
415 octave_value_list retval; |
|
416 |
|
417 int nargin = args.length (); |
|
418 |
|
419 if (nargin > 0 && nargin < 4) |
|
420 { |
|
421 bool scale = (nargin == 3); |
|
422 |
|
423 int kind = 0; |
|
424 |
|
425 ComplexMatrix z; |
|
426 |
|
427 if (nargin > 1) |
|
428 { |
|
429 double d_kind = args(0).double_value (); |
|
430 |
|
431 if (! error_state) |
|
432 { |
|
433 kind = (int) d_kind; |
|
434 |
|
435 if (kind < 0 || kind > 3) |
|
436 error ("airy: expecting K = 0, 1, 2, or 3"); |
|
437 } |
|
438 else |
|
439 error ("airy: expecting integer value for K"); |
|
440 } |
|
441 |
|
442 if (! error_state) |
|
443 { |
|
444 z = args(nargin == 1 ? 0 : 1).complex_matrix_value (); |
|
445 |
|
446 if (! error_state) |
|
447 { |
|
448 Array2<int> ierr; |
|
449 octave_value result; |
|
450 |
|
451 if (kind > 1) |
|
452 result = biry (z, kind == 3, scale, ierr); |
|
453 else |
|
454 result = airy (z, kind == 1, scale, ierr); |
|
455 |
|
456 if (nargout > 1) |
|
457 retval(1) = int_array2_to_matrix (ierr); |
|
458 |
|
459 retval(0) = result; |
|
460 } |
|
461 else |
|
462 error ("airy: expecting complex matrix for Z"); |
|
463 } |
|
464 } |
|
465 else |
|
466 print_usage ("airy"); |
|
467 |
|
468 return retval; |
3155
|
469 } |
|
470 |
|
471 /* |
|
472 ;;; Local Variables: *** |
|
473 ;;; mode: C++ *** |
|
474 ;;; End: *** |
|
475 */ |