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, |
3459
|
238 "-*- texinfo -*-\n\ |
|
239 @deftypefn {Loadable Function} {[@var{j}, @var{ierr}] =} besselj (@var{alpha}, @var{x}, @var{opt})\n\ |
|
240 @deftypefnx {Loadable Function} {[@var{y}, @var{ierr}] =} bessely (@var{alpha}, @var{x}, @var{opt})\n\ |
|
241 @deftypefnx {Loadable Function} {[@var{i}, @var{ierr}] =} besseli (@var{alpha}, @var{x}, @var{opt})\n\ |
|
242 @deftypefnx {Loadable Function} {[@var{k}, @var{ierr}] =} besselk (@var{alpha}, @var{x}, @var{opt})\n\ |
|
243 @deftypefnx {Loadable Function} {[@var{h}, @var{ierr}] =} besselh (@var{alpha}, @var{k}, @var{x}, @var{opt})\n\ |
|
244 Compute Bessel or Hankel functions of various kinds:\n\ |
3155
|
245 \n\ |
3459
|
246 @table @code\n\ |
|
247 @item besselj\n\ |
|
248 Bessel functions of the first kind.\n\ |
|
249 @item bessely\n\ |
|
250 Bessel functions of the second kind.\n\ |
|
251 @item besseli\n\ |
|
252 Modified Bessel functions of the first kind.\n\ |
|
253 @item besselk\n\ |
|
254 Modified Bessel functions of the second kind.\n\ |
|
255 @item besselh\n\ |
|
256 Compute Hankel functions of the first (@var{k} = 1) or second (@var{k}\n\ |
|
257 = 2) kind.\n\ |
|
258 @end table\n\ |
3220
|
259 \n\ |
3568
|
260 If the argument @var{opt} is supplied, the result is scaled by the\n\ |
3459
|
261 @code{exp (-I*@var{x})} for @var{k} = 1 or @code{exp (I*@var{x})} for\n\ |
3499
|
262 @var{k} = 2.\n\ |
3220
|
263 \n\ |
3459
|
264 If @var{alpha} is a scalar, the result is the same size as @var{x}.\n\ |
|
265 If @var{x} is a scalar, the result is the same size as @var{alpha}.\n\ |
|
266 If @var{alpha} is a row vector and @var{x} is a column vector, the\n\ |
|
267 result is a matrix with @code{length (@var{x})} rows and\n\ |
|
268 @code{length (@var{alpha})} columns. Otherwise, @var{alpha} and\n\ |
|
269 @var{x} must conform and the result will be the same size.\n\ |
3155
|
270 \n\ |
3459
|
271 The value of @var{alpha} must be real. The value of @var{x} may be\n\ |
|
272 complex.\n\ |
|
273 \n\ |
|
274 If requested, @var{ierr} contains the following status information\n\ |
|
275 and is the same size as the result.\n\ |
3548
|
276 \n\ |
3459
|
277 @enumerate 0\n\ |
|
278 @item\n\ |
|
279 Normal return.\n\ |
|
280 @item\n\ |
|
281 Input error, return @code{NaN}.\n\ |
|
282 @item\n\ |
|
283 Overflow, return @code{Inf}.\n\ |
|
284 @item\n\ |
|
285 Loss of significance by argument reduction results in less than\n\ |
|
286 half of machine accuracy.\n\ |
|
287 @item\n\ |
|
288 Complete loss of significance by argument reduction, return @code{NaN}.\n\ |
|
289 @item\n\ |
|
290 Error---no computation, algorithm termination condition not met,\n\ |
|
291 return @code{NaN}.\n\ |
|
292 @end enumerate\n\ |
|
293 @end deftypefn") |
3155
|
294 { |
3220
|
295 return do_bessel (BESSEL_J, "besselj", args, nargout); |
3155
|
296 } |
|
297 |
3220
|
298 DEFUN_DLD (bessely, args, nargout, |
3459
|
299 "-*- texinfo -*-\n\ |
|
300 @deftypefn {Loadable Function} {[@var{y}, @var{ierr}] =} bessely (@var{alpha}, @var{x}, @var{opt})\n\ |
|
301 See besselj.\n\ |
|
302 @end deftypefn") |
3155
|
303 { |
3220
|
304 return do_bessel (BESSEL_Y, "bessely", args, nargout); |
3155
|
305 } |
|
306 |
3220
|
307 DEFUN_DLD (besseli, args, nargout, |
3459
|
308 "-*- texinfo -*-\n\ |
|
309 @deftypefn {Loadable Function} {[@var{i}, @var{ierr}] =} besseli (@var{alpha}, @var{x}, @var{opt})\n\ |
|
310 See besselj.\n\ |
|
311 @end deftypefn") |
3155
|
312 { |
3220
|
313 return do_bessel (BESSEL_I, "besseli", args, nargout); |
3155
|
314 } |
|
315 |
3220
|
316 DEFUN_DLD (besselk, args, nargout, |
3459
|
317 "-*- texinfo -*-\n\ |
|
318 @deftypefn {Loadable Function} {[@var{k}, @var{ierr}] =} besselk (@var{alpha}, @var{x}, @var{opt})\n\ |
|
319 See besselj.\n\ |
|
320 @end deftypefn") |
3220
|
321 { |
|
322 return do_bessel (BESSEL_K, "besselk", args, nargout); |
|
323 } |
|
324 |
|
325 DEFUN_DLD (besselh, args, nargout, |
3459
|
326 "-*- texinfo -*-\n\ |
|
327 @deftypefn {Loadable Function} {[@var{h}, @var{ierr}] =} besselh (@var{alpha}, @var{k}, @var{x}, @var{opt})\n\ |
|
328 See besselj.\n\ |
|
329 @end deftypefn") |
3220
|
330 { |
|
331 octave_value_list retval; |
|
332 |
|
333 int nargin = args.length (); |
|
334 |
|
335 int kind = 1; |
|
336 |
|
337 if (nargin == 2) |
|
338 { |
|
339 retval = do_bessel (BESSEL_H1, "besselh", args, nargout); |
|
340 } |
|
341 else if (nargin == 3 || nargin == 4) |
|
342 { |
|
343 double d_kind = args(1).double_value (); |
|
344 |
|
345 if (! error_state && D_NINT (d_kind) == d_kind) |
|
346 { |
|
347 octave_value_list tmp_args; |
|
348 |
|
349 if (nargin == 4) |
|
350 tmp_args(2) = args(3); |
|
351 |
|
352 tmp_args(1) = args(2); |
|
353 tmp_args(0) = args(0); |
|
354 |
|
355 if (kind == 1) |
|
356 retval = do_bessel (BESSEL_H1, "besselh", tmp_args, nargout); |
|
357 else if (kind == 2) |
|
358 retval = do_bessel (BESSEL_H2, "besselh", tmp_args, nargout); |
|
359 else |
|
360 error ("besselh: expecting K = 1 or 2"); |
|
361 } |
|
362 else |
|
363 error ("besselh: invalid value of K"); |
|
364 } |
|
365 else |
|
366 print_usage ("besselh"); |
|
367 |
|
368 return retval; |
|
369 } |
|
370 |
|
371 DEFUN_DLD (airy, args, nargout, |
3459
|
372 "-*- texinfo -*-\n\ |
|
373 @deftypefn {Loadable Function} {[@var{a}, @var{ierr}] =} airy (@var{k}, @var{z}, @var{opt})\n\ |
3220
|
374 Compute Airy functions of the first and second kind, and their\n\ |
|
375 derivatives.\n\ |
|
376 \n\ |
3459
|
377 @example\n\ |
3220
|
378 K Function Scale factor (if a third argument is supplied)\n\ |
|
379 --- -------- ----------------------------------------------\n\ |
|
380 0 Ai (Z) exp ((2/3) * Z * sqrt (Z))\n\ |
|
381 1 dAi(Z)/dZ exp ((2/3) * Z * sqrt (Z))\n\ |
|
382 2 Bi (Z) exp (-abs (real ((2/3) * Z *sqrt (Z))))\n\ |
|
383 3 dBi(Z)/dZ exp (-abs (real ((2/3) * Z *sqrt (Z))))\n\ |
3459
|
384 @end example\n\ |
3220
|
385 \n\ |
3549
|
386 The function call @code{airy (@var{z})} is equivalent to\n\ |
3459
|
387 @code{airy (0, @var{z})}.\n\ |
3155
|
388 \n\ |
3549
|
389 The result is the same size as @var{z}.\n\ |
3220
|
390 \n\ |
3459
|
391 If requested, @var{ierr} contains the following status information and\n\ |
|
392 is the same size as the result.\n\ |
3548
|
393 \n\ |
3459
|
394 @enumerate 0\n\ |
|
395 @item\n\ |
|
396 Normal return.\n\ |
|
397 @item\n\ |
|
398 Input error, return @code{NaN}.\n\ |
|
399 @item\n\ |
|
400 Overflow, return @code{Inf}.\n\ |
|
401 @item\n\ |
|
402 Loss of significance by argument reduction results in less than half\n\ |
|
403 of machine accuracy.\n\ |
|
404 @item\n\ |
|
405 Complete loss of significance by argument reduction, return @code{NaN}.\n\ |
|
406 @item\n\ |
|
407 Error---no computation, algorithm termination condition not met,\n\ |
|
408 return @code{NaN}\n\ |
|
409 @end enumerate\n\ |
|
410 @end deftypefn") |
3155
|
411 { |
3220
|
412 octave_value_list retval; |
|
413 |
|
414 int nargin = args.length (); |
|
415 |
|
416 if (nargin > 0 && nargin < 4) |
|
417 { |
|
418 bool scale = (nargin == 3); |
|
419 |
|
420 int kind = 0; |
|
421 |
|
422 ComplexMatrix z; |
|
423 |
|
424 if (nargin > 1) |
|
425 { |
|
426 double d_kind = args(0).double_value (); |
|
427 |
|
428 if (! error_state) |
|
429 { |
|
430 kind = (int) d_kind; |
|
431 |
|
432 if (kind < 0 || kind > 3) |
|
433 error ("airy: expecting K = 0, 1, 2, or 3"); |
|
434 } |
|
435 else |
|
436 error ("airy: expecting integer value for K"); |
|
437 } |
|
438 |
|
439 if (! error_state) |
|
440 { |
|
441 z = args(nargin == 1 ? 0 : 1).complex_matrix_value (); |
|
442 |
|
443 if (! error_state) |
|
444 { |
|
445 Array2<int> ierr; |
|
446 octave_value result; |
|
447 |
|
448 if (kind > 1) |
|
449 result = biry (z, kind == 3, scale, ierr); |
|
450 else |
|
451 result = airy (z, kind == 1, scale, ierr); |
|
452 |
|
453 if (nargout > 1) |
|
454 retval(1) = int_array2_to_matrix (ierr); |
|
455 |
|
456 retval(0) = result; |
|
457 } |
|
458 else |
|
459 error ("airy: expecting complex matrix for Z"); |
|
460 } |
|
461 } |
|
462 else |
|
463 print_usage ("airy"); |
|
464 |
|
465 return retval; |
3155
|
466 } |
|
467 |
|
468 /* |
|
469 ;;; Local Variables: *** |
|
470 ;;; mode: C++ *** |
|
471 ;;; End: *** |
|
472 */ |