Mercurial > octave
comparison libinterp/corefcn/besselj.cc @ 30937:85a67c1a5712
besselj.cc: Improve input validation for function airy (bug #62321)
besselj.cc: Also expand function docstring and reduce duplicated code
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Sat, 16 Apr 2022 11:08:32 -0400 |
parents | c9788d7f6e65 |
children | 84944164799e |
comparison
equal
deleted
inserted
replaced
30936:30f7f409861a | 30937:85a67c1a5712 |
---|---|
614 %!# Function besselh is tested along with other bessels at the end of this file | 614 %!# Function besselh is tested along with other bessels at the end of this file |
615 */ | 615 */ |
616 | 616 |
617 DEFUN (airy, args, nargout, | 617 DEFUN (airy, args, nargout, |
618 doc: /* -*- texinfo -*- | 618 doc: /* -*- texinfo -*- |
619 @deftypefn {} {[@var{a}, @var{ierr}] =} airy (@var{k}, @var{z}, @var{opt}) | 619 @deftypefn {} {@var{a} =} airy (@var{z}) |
620 @deftypefnx {} {@var{a} =} airy (@var{k}, @var{z}) | |
621 @deftypefnx {} {@var{a} =} airy (@var{k}, @var{z}, @var{scale}) | |
622 @deftypefnx {} {[@var{a}, @var{ierr}] =} airy (@dots{}) | |
623 | |
620 Compute Airy functions of the first and second kind, and their derivatives. | 624 Compute Airy functions of the first and second kind, and their derivatives. |
621 | 625 |
622 @example | 626 @example |
623 @group | 627 @group |
624 K Function Scale factor (if "opt" is supplied) | 628 K Function Scale factor (if @var{scale} is true) |
625 --- -------- --------------------------------------- | 629 --- -------- --------------------------------------- |
626 0 Ai (Z) exp ((2/3) * Z * sqrt (Z)) | 630 0 Ai (Z) exp ((2/3) * Z * sqrt (Z)) |
627 1 dAi(Z)/dZ exp ((2/3) * Z * sqrt (Z)) | 631 1 dAi(Z)/dZ exp ((2/3) * Z * sqrt (Z)) |
628 2 Bi (Z) exp (-abs (real ((2/3) * Z * sqrt (Z)))) | 632 2 Bi (Z) exp (-abs (real ((2/3) * Z * sqrt (Z)))) |
629 3 dBi(Z)/dZ exp (-abs (real ((2/3) * Z * sqrt (Z)))) | 633 3 dBi(Z)/dZ exp (-abs (real ((2/3) * Z * sqrt (Z)))) |
631 @end example | 635 @end example |
632 | 636 |
633 The function call @code{airy (@var{z})} is equivalent to | 637 The function call @code{airy (@var{z})} is equivalent to |
634 @code{airy (0, @var{z})}. | 638 @code{airy (0, @var{z})}. |
635 | 639 |
636 The result is the same size as @var{z}. | 640 The optional third input @var{scale} determines whether to |
637 | 641 apply scaling as described above. It is false by default. |
638 If requested, @var{ierr} contains the following status information and | 642 |
643 The result @var{a} is the same size as @var{z}. | |
644 | |
645 The optional output @var{ierr} contains the following status information and | |
639 is the same size as the result. | 646 is the same size as the result. |
640 | 647 |
641 @enumerate 0 | 648 @enumerate 0 |
642 @item | 649 @item |
643 Normal return. | 650 Normal return. |
675 | 682 |
676 if (kind < 0 || kind > 3) | 683 if (kind < 0 || kind > 3) |
677 error ("airy: K must be 0, 1, 2, or 3"); | 684 error ("airy: K must be 0, 1, 2, or 3"); |
678 } | 685 } |
679 | 686 |
680 bool scale = (nargin == 3); | 687 bool scale = (nargin == 3) && args(2).xbool_value ("airy: scale option must be a logical value"); |
681 | 688 |
682 int idx = (nargin == 1 ? 0 : 1); | 689 int idx = (nargin == 1 ? 0 : 1); |
690 | |
691 Array<octave_idx_type> ierr; | |
692 octave_value result; | |
683 | 693 |
684 if (args(idx).is_single_type ()) | 694 if (args(idx).is_single_type ()) |
685 { | 695 { |
686 FloatComplexNDArray z = args(idx).xfloat_complex_array_value ("airy: Z must be a complex matrix"); | 696 FloatComplexNDArray z = args(idx).xfloat_complex_array_value ("airy: Z must be a complex matrix"); |
687 | |
688 Array<octave_idx_type> ierr; | |
689 octave_value result; | |
690 | 697 |
691 if (kind > 1) | 698 if (kind > 1) |
692 result = math::biry (z, kind == 3, scale, ierr); | 699 result = math::biry (z, kind == 3, scale, ierr); |
693 else | 700 else |
694 result = math::airy (z, kind == 1, scale, ierr); | 701 result = math::airy (z, kind == 1, scale, ierr); |
695 | |
696 retval(0) = result; | |
697 if (nargout > 1) | |
698 retval(1) = NDArray (ierr); | |
699 } | 702 } |
700 else | 703 else |
701 { | 704 { |
702 ComplexNDArray z = args(idx).xcomplex_array_value ("airy: Z must be a complex matrix"); | 705 ComplexNDArray z = args(idx).xcomplex_array_value ("airy: Z must be a complex matrix"); |
703 | 706 |
704 Array<octave_idx_type> ierr; | |
705 octave_value result; | |
706 | |
707 if (kind > 1) | 707 if (kind > 1) |
708 result = math::biry (z, kind == 3, scale, ierr); | 708 result = math::biry (z, kind == 3, scale, ierr); |
709 else | 709 else |
710 result = math::airy (z, kind == 1, scale, ierr); | 710 result = math::airy (z, kind == 1, scale, ierr); |
711 | |
712 retval(0) = result; | |
713 if (nargout > 1) | |
714 retval(1) = NDArray (ierr); | |
715 } | 711 } |
712 | |
713 retval(0) = result; | |
714 if (nargout > 1) | |
715 retval(1) = NDArray (ierr); | |
716 | 716 |
717 return retval; | 717 return retval; |
718 } | 718 } |
719 | 719 |
720 /* | 720 /* |