| 1 |
|
| use strict; |
| 2 |
|
| use warnings; |
| 3 |
|
| use Config; |
| 4 |
|
| use PDL::Types qw(ppdefs ppdefs_complex types); |
| 5 |
|
| require PDL::Core::Dev; |
| 6 |
|
| |
| 7 |
|
| { # pass info back to Makefile.PL |
| 8 |
|
| # Files for each routine (.c assumed) |
| 9 |
|
| my %source = qw( |
| 10 |
|
| j0 j0 |
| 11 |
|
| j1 j1 |
| 12 |
|
| jn jn |
| 13 |
|
| y0 j0 |
| 14 |
|
| y1 j1 |
| 15 |
|
| yn yn |
| 16 |
|
| ); |
| 17 |
|
| my @keys = sort keys %source; |
| 18 |
|
| my $libs = PDL::Core::Dev::get_maths_libs(); |
| 19 |
|
| # Test for presence of besfuncs |
| 20 |
|
| require File::Spec::Functions; |
| 21 |
|
| my $include = qq{#include "}.File::Spec::Functions::rel2abs("$::PDLBASE/mconf.h").qq{"}; |
| 22 |
|
| $source{$_} = 'system' for grep PDL::Core::Dev::trylink('', $include, "$_(1.);", $libs), qw(j0 j1 y0 y1); |
| 23 |
|
| $source{$_} = 'system' for grep PDL::Core::Dev::trylink('', $include, "$_(1,1.);", $libs), qw(jn yn); |
| 24 |
|
| my %seen; # Build object file list |
| 25 |
|
| foreach my $func (@keys) { |
| 26 |
|
| my $file = $source{$func}; |
| 27 |
|
| next if $file eq 'system'; |
| 28 |
|
| die "File for function $func not found\n" if $file eq ''; |
| 29 |
|
| $PDL::Core::Dev::EXTRAS{$::PDLMOD}{OBJECT} .= " $::PDLBASE/$file\$(OBJ_EXT)" unless $seen{$file}++; |
| 30 |
|
| } |
| 31 |
|
| # Add support routines |
| 32 |
|
| $PDL::Core::Dev::EXTRAS{$::PDLMOD}{OBJECT} .= join '', map " $::PDLBASE/$_\$(OBJ_EXT)", qw(const mtherr polevl cpoly ndtri); |
| 33 |
|
| $PDL::Core::Dev::EXTRAS{$::PDLMOD}{INC} .= qq{ "-I$::PDLBASE"}; |
| 34 |
|
| } |
| 35 |
|
| |
| 36 |
|
| my $R = [ppdefs()]; |
| 37 |
|
| my $F = [map $_->ppsym, grep $_->real && !$_->integer, types()]; |
| 38 |
|
| my $C = [ppdefs_complex()]; |
| 39 |
|
| my @Rtypes = grep $_->real, types(); |
| 40 |
|
| my @Ctypes = grep !$_->real, types(); |
| 41 |
|
| my $AF = [map $_->ppsym, grep !$_->integer, types]; |
| 42 |
|
| $AF = [(grep $_ ne 'D', @$AF), 'D']; # so defaults to D if non-float given |
| 43 |
|
| my %got_complex; |
| 44 |
|
| |
| 45 |
|
| pp_addpm({At=>'Top'},<<'EOD'); |
| 46 |
|
| use strict; |
| 47 |
|
| use warnings; |
| 48 |
|
| |
| 49 |
|
| =head1 NAME |
| 50 |
|
| |
| 51 |
|
| PDL::Math - extended mathematical operations and special functions |
| 52 |
|
| |
| 53 |
|
| =head1 SYNOPSIS |
| 54 |
|
| |
| 55 |
|
| use PDL::Math; |
| 56 |
|
| |
| 57 |
|
| use PDL::Graphics::TriD; |
| 58 |
|
| imag3d [SURF2D,bessj0(rvals(zeroes(50,50))/2)]; |
| 59 |
|
| |
| 60 |
|
| =head1 DESCRIPTION |
| 61 |
|
| |
| 62 |
|
| This module extends PDL with more advanced mathematical functions than |
| 63 |
|
| provided by standard Perl. |
| 64 |
|
| |
| 65 |
|
| All the functions have one input pdl, and one output, unless otherwise |
| 66 |
|
| stated. |
| 67 |
|
| |
| 68 |
|
| Many of the functions are linked from the system maths library or the |
| 69 |
|
| Cephes maths library (determined when PDL is compiled); a few are implemented |
| 70 |
|
| entirely in PDL. |
| 71 |
|
| |
| 72 |
|
| =cut |
| 73 |
|
| |
| 74 |
|
| ### Kludge for backwards compatibility with older scripts |
| 75 |
|
| ### This should be deleted at some point later than 21-Nov-2003. |
| 76 |
|
| BEGIN {use PDL::MatrixOps;} |
| 77 |
|
| |
| 78 |
|
| EOD |
| 79 |
|
| |
| 80 |
|
| # Internal doc util |
| 81 |
|
| |
| 82 |
|
| my %doco; |
| 83 |
|
| sub doco { |
| 84 |
|
| my @funcs = @_; |
| 85 |
|
| my $doc = pop @funcs; |
| 86 |
|
| for (@funcs) { $doco{$_} = $doc } |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| doco (qw/acos asin atan tan/, <<'EOF'); |
| 90 |
|
| The usual trigonometric function. |
| 91 |
|
| EOF |
| 92 |
|
| |
| 93 |
|
| doco (qw/cosh sinh tanh acosh asinh atanh/, <<'EOF'); |
| 94 |
|
| The standard hyperbolic function. |
| 95 |
|
| EOF |
| 96 |
|
| |
| 97 |
|
| doco (qw/ceil floor/, |
| 98 |
|
| 'Round to integer values in floating-point format.'); |
| 99 |
|
| |
| 100 |
|
| doco ('rint', |
| 101 |
|
| q/=for ref |
| 102 |
|
| |
| 103 |
|
| Round to integer values in floating-point format. |
| 104 |
|
| |
| 105 |
|
| This is the C99 function; previous to 2.096, the doc referred to a |
| 106 |
|
| bespoke function that did banker's rounding, but this was not used |
| 107 |
|
| as a system version will have been detected and used. |
| 108 |
|
| |
| 109 |
|
| If you are looking to round half-integers up (regardless of sign), try |
| 110 |
|
| C<floor($x+0.5)>. If you want to round half-integers away from zero, |
| 111 |
|
| try C<< ceil(abs($x)+0.5)*($x<=>0) >>./); |
| 112 |
|
| |
| 113 |
|
| doco( 'pow',"Synonym for `**'."); |
| 114 |
|
| |
| 115 |
|
| doco ('erf',"The error function."); |
| 116 |
|
| doco ('erfc',"The complement of the error function."); |
| 117 |
|
| doco ('erfi',"The inverse of the error function."); |
| 118 |
|
| doco ('ndtri', |
| 119 |
|
| "=for ref |
| 120 |
|
| |
| 121 |
|
| The value for which the area under the |
| 122 |
|
| Gaussian probability density function (integrated from |
| 123 |
|
| minus infinity) is equal to the argument (cf L</erfi>)."); |
| 124 |
|
| |
| 125 |
|
| doco(qw/bessj0 bessj1/, |
| 126 |
|
| "The regular Bessel function of the first kind, J_n" ); |
| 127 |
|
| |
| 128 |
|
| doco(qw/bessy0 bessy1/, |
| 129 |
|
| "The regular Bessel function of the second kind, Y_n." ); |
| 130 |
|
| |
| 131 |
|
| doco( qw/bessjn/, |
| 132 |
|
| '=for ref |
| 133 |
|
| |
| 134 |
|
| The regular Bessel function of the first kind, J_n |
| 135 |
|
| . |
| 136 |
|
| This takes a second int argument which gives the order |
| 137 |
|
| of the function required. |
| 138 |
|
| '); |
| 139 |
|
| |
| 140 |
|
| doco( qw/bessyn/, |
| 141 |
|
| '=for ref |
| 142 |
|
| |
| 143 |
|
| The regular Bessel function of the first kind, Y_n |
| 144 |
|
| . |
| 145 |
|
| This takes a second int argument which gives the order |
| 146 |
|
| of the function required. |
| 147 |
|
| '); |
| 148 |
|
| |
| 149 |
|
| if ($^O !~ /win32/i || $Config{cc} =~ /\bgcc/i) { # doesn't seem to be in the MS VC lib |
| 150 |
|
| doco( 'lgamma' ,<<'EOD'); |
| 151 |
|
| =for ref |
| 152 |
|
| |
| 153 |
|
| log gamma function |
| 154 |
|
| |
| 155 |
|
| This returns 2 ndarrays -- the first set gives the log(gamma) values, |
| 156 |
|
| while the second set, of integer values, gives the sign of the gamma |
| 157 |
|
| function. This is useful for determining factorials, amongst other |
| 158 |
|
| things. |
| 159 |
|
| |
| 160 |
|
| EOD |
| 161 |
|
| |
| 162 |
|
| } # if: $^O !~ win32 |
| 163 |
|
| |
| 164 |
|
| pp_addhdr(' |
| 165 |
|
| #include <tgmath.h> |
| 166 |
|
| #include "protos.h" |
| 167 |
|
| #include "cpoly.h" |
| 168 |
|
| '); |
| 169 |
|
| |
| 170 |
|
| # Standard `-lm' |
| 171 |
|
| my (@ufuncs1) = qw(acos asin atan cosh sinh tan tanh); |
| 172 |
|
| my (@ufuncs1g) = qw(ceil floor rint); # Any real type |
| 173 |
|
| |
| 174 |
|
| # Note: |
| 175 |
|
| # ops.pd has a power() function that does the same thing |
| 176 |
|
| # (although it has OtherPars => 'int swap;' as well) |
| 177 |
|
| # - left this in for now. |
| 178 |
|
| # |
| 179 |
|
| my (@bifuncs1) = qw(pow); # Any type |
| 180 |
|
| |
| 181 |
|
| # Extended `-lm' |
| 182 |
|
| my (@ufuncs2) = qw(acosh asinh atanh erf erfc); # F,D only |
| 183 |
|
| my (@besufuncs) = qw(j0 j1 y0 y1); # " |
| 184 |
|
| my (@besbifuncs) = qw(jn yn); # " |
| 185 |
|
| # Need igamma, ibeta, and a fall-back implementation of the above |
| 186 |
|
| |
| 187 |
|
| sub code_ufunc { |
| 188 |
|
| <<EOF |
| 189 |
|
| PDL_IF_BAD(if ( \$ISBAD(a()) ) { \$SETBAD(b()); } else,) |
| 190 |
|
| \$b() = $_[0](\$a()); |
| 191 |
|
| EOF |
| 192 |
|
| } |
| 193 |
|
| |
| 194 |
|
| sub code_bifunc { |
| 195 |
|
| my $name = $_[0]; |
| 196 |
|
| my $x = $_[1] || 'a'; my $y = $_[2] || 'b'; my $c = $_[3] || 'c'; |
| 197 |
|
| <<EOF |
| 198 |
|
| PDL_IF_BAD(if ( \$ISBAD($x()) || \$ISBAD($y()) ) { \$SETBAD($c()); } else,) |
| 199 |
|
| \$$c() = $name(\$$x(),\$$y()); |
| 200 |
|
| EOF |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
|
| sub g_c { |
| 204 |
|
| my ($func, $arg) = @_; |
| 205 |
|
| return $got_complex{$func} if defined $got_complex{$func}; |
| 206 |
|
| return $got_complex{$func} = 0 if $func !~ /[a-z]/; |
| 207 |
|
| $got_complex{$func} = PDL::Core::Dev::got_complex_version($func, $arg); |
| 208 |
|
| print "got_complex_version($func, $arg): $got_complex{$func}\n"; |
| 209 |
|
| $got_complex{$func}; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| foreach my $func (@ufuncs1) { |
| 213 |
|
| my $got_complex = g_c($func, 1); |
| 214 |
|
| pp_def($func, |
| 215 |
|
| HandleBad => 1, |
| 216 |
|
| NoBadifNaN => 1, |
| 217 |
|
| GenericTypes => [($got_complex ? @$C : ()), @$F], |
| 218 |
|
| Pars => 'a(); [o]b();', |
| 219 |
|
| Inplace => 1, |
| 220 |
|
| Doc => $doco{$func}, |
| 221 |
|
| Code => code_ufunc($func), |
| 222 |
|
| ); |
| 223 |
|
| } |
| 224 |
|
| # real types |
| 225 |
|
| foreach my $func (@ufuncs1g) { |
| 226 |
|
| pp_def($func, |
| 227 |
|
| HandleBad => 1, |
| 228 |
|
| NoBadifNaN => 1, |
| 229 |
|
| Pars => 'a(); [o]b();', |
| 230 |
|
| Inplace => 1, |
| 231 |
|
| Doc => $doco{$func}, |
| 232 |
|
| Code => code_ufunc($func), |
| 233 |
|
| ); |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
|
| foreach my $func (@bifuncs1) { |
| 237 |
|
| my $got_complex = g_c($func, 2); |
| 238 |
|
| pp_def($func, |
| 239 |
|
| HandleBad => 1, |
| 240 |
|
| NoBadifNaN => 1, |
| 241 |
|
| Pars => 'a(); b(); [o]c();', |
| 242 |
|
| Inplace => [ 'a' ], |
| 243 |
|
| GenericTypes => [($got_complex ? @$C : ()), @$R], |
| 244 |
|
| Doc => $doco{$func}, |
| 245 |
|
| Code => code_bifunc($func), |
| 246 |
|
| ); |
| 247 |
|
| } |
| 248 |
|
| |
| 249 |
|
| # Functions provided by extended -lm |
| 250 |
|
| foreach my $func (@ufuncs2) { |
| 251 |
|
| pp_def($func, |
| 252 |
|
| HandleBad => 1, |
| 253 |
|
| NoBadifNaN => 1, |
| 254 |
|
| GenericTypes => $F, |
| 255 |
|
| Pars => 'a(); [o]b();', |
| 256 |
|
| Inplace => 1, |
| 257 |
|
| Doc => $doco{$func}, |
| 258 |
|
| Code => code_ufunc($func), |
| 259 |
|
| ); |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
|
| foreach my $func (@besufuncs) { |
| 263 |
|
| my $fname = "bess$func"; |
| 264 |
|
| pp_def($fname, |
| 265 |
|
| HandleBad => 1, |
| 266 |
|
| NoBadifNaN => 1, |
| 267 |
|
| GenericTypes => $F, |
| 268 |
|
| Pars => 'a(); [o]b();', |
| 269 |
|
| Inplace => 1, |
| 270 |
|
| Doc => $doco{$fname}, |
| 271 |
|
| Code => code_ufunc($func), |
| 272 |
|
| ); |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
|
| foreach my $func (@besbifuncs) { |
| 276 |
|
| my $fname = "bess$func"; |
| 277 |
|
| pp_def($fname, |
| 278 |
|
| HandleBad => 1, |
| 279 |
|
| NoBadifNaN => 1, |
| 280 |
|
| GenericTypes => $F, |
| 281 |
|
| Pars => 'a(); int n(); [o]b();', |
| 282 |
|
| Inplace => [ 'a' ], |
| 283 |
|
| Doc => $doco{$fname}, |
| 284 |
|
| Code => code_bifunc($func,'n','a','b'), |
| 285 |
|
| ); |
| 286 |
|
| } |
| 287 |
|
| |
| 288 |
|
| if ($^O !~ /win32/i) { |
| 289 |
|
| pp_def("lgamma", |
| 290 |
|
| HandleBad => 1, |
| 291 |
|
| Pars => 'a(); [o]b(); int[o]s()', |
| 292 |
|
| Doc => $doco{"lgamma"}, |
| 293 |
|
| Code => ' |
| 294 |
|
| extern int signgam; |
| 295 |
|
| PDL_IF_BAD(if ( $ISBAD(a()) ) { $SETBAD(b()); $SETBAD(s()); } else,) { |
| 296 |
|
| $b() = lgamma($a()); |
| 297 |
|
| $s() = signgam; |
| 298 |
|
| } |
| 299 |
|
| ', # what happens to signgam if $a() is bad? |
| 300 |
|
| ); |
| 301 |
|
| } # if: os !~ win32 |
| 302 |
|
| |
| 303 |
|
| elsif ($Config{cc} =~ /\bgcc/i) { |
| 304 |
|
| pp_def("lgamma", |
| 305 |
|
| HandleBad => 1, |
| 306 |
|
| Pars => 'a(); [o]b(); int[o]s()', |
| 307 |
|
| Doc => $doco{"lgamma"}, |
| 308 |
|
| Code => ' |
| 309 |
|
| PDL_IF_BAD(if ( $ISBAD(a()) ) { $SETBAD(b()); $SETBAD(s()); } else,) { |
| 310 |
|
| $b() = lgamma($a()); |
| 311 |
|
| $s() = tgamma($a()) < 0 ? -1 : 1; |
| 312 |
|
| } |
| 313 |
|
| ', # what happens to signgam if $a() is bad? |
| 314 |
|
| ); |
| 315 |
|
| } # elsif: cc =~ /\bgcc/i |
| 316 |
|
| |
| 317 |
|
| pp_def('isfinite', |
| 318 |
|
| Pars => 'a(); int [o]mask();', |
| 319 |
|
| HandleBad => 1, |
| 320 |
|
| Code => <<'EOF', |
| 321 |
|
| broadcastloop %{ |
| 322 |
|
| $mask() = isfinite((double) $a()) != 0 PDL_IF_BAD(&& $ISGOOD($a()),); |
| 323 |
|
| %} |
| 324 |
|
| $PDLSTATESETGOOD(mask); |
| 325 |
|
| EOF |
| 326 |
|
| Doc => |
| 327 |
|
| 'Sets C<$mask> true if C<$a> is not a C<NaN> or C<inf> (either positive or negative).', |
| 328 |
|
| BadDoc => |
| 329 |
|
| 'Bad values are treated as C<NaN> or C<inf>.', |
| 330 |
|
| ); |
| 331 |
|
| |
| 332 |
|
| # Extra functions from cephes |
| 333 |
|
| pp_def("erfi", |
| 334 |
|
| HandleBad => 1, |
| 335 |
|
| NoBadifNaN => 1, |
| 336 |
|
| GenericTypes => $F, |
| 337 |
|
| Pars => 'a(); [o]b()', |
| 338 |
|
| Inplace => 1, |
| 339 |
|
| Doc => "erfi", |
| 340 |
|
| Code => |
| 341 |
|
| 'extern double SQRTH; |
| 342 |
|
| PDL_IF_BAD(if ( $ISBAD(a()) ) { $SETBAD(b()); } |
| 343 |
|
| else,) { $b() = SQRTH*ndtri((1+(double)$a())/2); }', |
| 344 |
|
| ); |
| 345 |
|
| |
| 346 |
|
| pp_def("ndtri", |
| 347 |
|
| HandleBad => 1, |
| 348 |
|
| NoBadifNaN => 1, |
| 349 |
|
| GenericTypes => $F, |
| 350 |
|
| Pars => 'a(); [o]b()', |
| 351 |
|
| Inplace => 1, |
| 352 |
|
| Doc => "ndtri", |
| 353 |
|
| Code => |
| 354 |
|
| 'PDL_IF_BAD(if ( $ISBAD(a()) ) { $SETBAD(b()); } |
| 355 |
|
| else,) { $b() = ndtri((double)$a()); }', |
| 356 |
|
| ); |
| 357 |
|
| |
| 358 |
|
| pp_def("polyroots", |
| 359 |
|
| Pars => 'cr(n); ci(n); [o]rr(m=CALC($SIZE(n)-1)); [o]ri(m);', |
| 360 |
|
| GenericTypes => ['D'], |
| 361 |
|
| Code => <<'EOF', |
| 362 |
|
| char *fail = cpoly($P(cr), $P(ci), $SIZE(m), $P(rr), $P(ri)); |
| 363 |
|
| if (fail) |
| 364 |
|
| $CROAK("cpoly: %s", fail); |
| 365 |
|
| EOF |
| 366 |
|
| PMCode => pp_line_numbers(__LINE__, <<'EOF'), |
| 367 |
|
| sub PDL::polyroots { |
| 368 |
|
| my @args = map PDL->topdl($_), @_; |
| 369 |
|
| my $natcplx = !$args[0]->type->real; |
| 370 |
|
| barf "need array context if give real data and no outputs" |
| 371 |
|
| if !$natcplx and @_ < 3 and !(wantarray//1); |
| 372 |
|
| splice @args, 0, 1, map $args[0]->$_, qw(re im) if $natcplx; |
| 373 |
|
| my @ins = splice @args, 0, 2; |
| 374 |
|
| my $explicit_out = my @outs = @args; |
| 375 |
|
| if ($natcplx) { |
| 376 |
|
| $_ //= PDL->null for $outs[0]; |
| 377 |
|
| } else { |
| 378 |
|
| $_ //= PDL->null for @outs[0,1]; |
| 379 |
|
| } |
| 380 |
|
| my @args_out = $natcplx ? (map PDL->null, 1..2) : @outs; # opposite from polyfromroots |
| 381 |
|
| PDL::_polyroots_int(@ins, @args_out); |
| 382 |
|
| return @args_out if !$natcplx; |
| 383 |
|
| $outs[0] .= PDL::czip(@args_out[0,1]); |
| 384 |
|
| } |
| 385 |
|
| EOF |
| 386 |
|
| Doc => ' |
| 387 |
|
| =for ref |
| 388 |
|
| |
| 389 |
|
| Complex roots of a complex polynomial, given coefficients in order |
| 390 |
|
| of decreasing powers. Only works for degree >= 1. |
| 391 |
|
| Uses the Jenkins-Traub algorithm (see |
| 392 |
|
| L<https://en.wikipedia.org/wiki/Jenkins%E2%80%93Traub_algorithm>). |
| 393 |
|
| As of 2.086, works with native-complex data. |
| 394 |
|
| |
| 395 |
|
| =for usage |
| 396 |
|
| |
| 397 |
|
| $roots = polyroots($coeffs); # native complex |
| 398 |
|
| polyroots($coeffs, $roots=null); # native complex |
| 399 |
|
| ($rr, $ri) = polyroots($cr, $ci); |
| 400 |
|
| polyroots($cr, $ci, $rr, $ri); |
| 401 |
|
| ',); |
| 402 |
|
| |
| 403 |
|
| pp_def("polyfromroots", |
| 404 |
|
| Pars => 'r(m); [o]c(n=CALC($SIZE(m)+1));', |
| 405 |
|
| GenericTypes => ['CD'], |
| 406 |
|
| Code => <<'EOF', |
| 407 |
|
| $c(n=>0) = 1.0; |
| 408 |
|
| loop(m) %{ $c(n=>m+1) = 0.0; %} |
| 409 |
|
| PDL_Indx k; |
| 410 |
|
| loop(m) %{ |
| 411 |
|
| for (k = m; k >= 0; k--) /* count down to use data before we mutate */ |
| 412 |
|
| $c(n=>k+1) -= $r() * $c(n=>k); |
| 413 |
|
| %} |
| 414 |
|
| EOF |
| 415 |
|
| PMCode => pp_line_numbers(__LINE__, <<'EOF'), |
| 416 |
|
| sub PDL::polyfromroots { |
| 417 |
|
| my @args = map PDL->topdl($_), @_; |
| 418 |
|
| my $natcplx = !$args[0]->type->real; |
| 419 |
|
| barf "need array context" if !$natcplx and !(wantarray//1); |
| 420 |
|
| if (!$natcplx) { |
| 421 |
|
| splice @args, 0, 2, $args[0]->czip($args[1]); # r |
| 422 |
|
| } |
| 423 |
|
| my @ins = splice @args, 0, 1; |
| 424 |
|
| my $explicit_out = my @outs = @args; |
| 425 |
|
| if ($natcplx) { |
| 426 |
|
| $_ //= PDL->null for $outs[0]; |
| 427 |
|
| } else { |
| 428 |
|
| $_ //= PDL->null for @outs[0,1]; |
| 429 |
|
| } |
| 430 |
|
| my @args_out = $natcplx ? @outs : PDL->null; |
| 431 |
|
| PDL::_polyfromroots_int(@ins, @args_out); |
| 432 |
|
| if (!$natcplx) { |
| 433 |
|
| $outs[0] .= $args_out[0]->re; |
| 434 |
|
| $outs[1] .= $args_out[0]->im; |
| 435 |
|
| } |
| 436 |
|
| $natcplx ? $outs[0] : @outs; |
| 437 |
|
| } |
| 438 |
|
| EOF |
| 439 |
|
| Doc => ' |
| 440 |
|
| =for ref |
| 441 |
|
| |
| 442 |
|
| Calculates the complex coefficients of a polynomial from its complex |
| 443 |
|
| roots, in order of decreasing powers. Added in 2.086, works with |
| 444 |
|
| native-complex data. |
| 445 |
|
| |
| 446 |
|
| Algorithm is from Octave poly.m, O(n^2), per |
| 447 |
|
| L<https://cs.stackexchange.com/questions/116643/what-is-the-most-efficient-algorithm-to-compute-polynomial-coefficients-from-its>; |
| 448 |
|
| using an FFT would allow O(n*log(n)^2). |
| 449 |
|
| |
| 450 |
|
| =for usage |
| 451 |
|
| |
| 452 |
|
| $coeffs = polyfromroots($roots); # native complex |
| 453 |
|
| ($cr, $ci) = polyfromroots($rr, $ri); |
| 454 |
|
| ',); |
| 455 |
|
| |
| 456 |
|
| pp_def("polyval", |
| 457 |
|
| Pars => 'c(n); x(); [o]y();', |
| 458 |
|
| GenericTypes => ['CD'], |
| 459 |
|
| Code => <<'EOF', |
| 460 |
|
| $GENERIC(y) vc = $c(n=>0), sc = $x(); |
| 461 |
|
| loop(n=1) %{ vc = vc*sc + $c(); %} |
| 462 |
|
| $y() = vc; |
| 463 |
|
| EOF |
| 464 |
|
| PMCode => pp_line_numbers(__LINE__, <<'EOF'), |
| 465 |
|
| sub PDL::polyval { |
| 466 |
|
| my @args = map PDL->topdl($_), @_; |
| 467 |
|
| my $natcplx = !$args[0]->type->real; |
| 468 |
|
| barf "need array context" if !$natcplx and !(wantarray//1); |
| 469 |
|
| if (!$natcplx) { |
| 470 |
|
| splice @args, 0, 2, $args[0]->czip($args[1]); # c |
| 471 |
|
| splice @args, 1, 2, $args[1]->czip($args[2]); # x |
| 472 |
|
| } |
| 473 |
|
| my @ins = splice @args, 0, 2; |
| 474 |
|
| my $explicit_out = my @outs = @args; |
| 475 |
|
| if ($natcplx) { |
| 476 |
|
| $_ //= PDL->null for $outs[0]; |
| 477 |
|
| } else { |
| 478 |
|
| $_ //= PDL->null for @outs[0,1]; |
| 479 |
|
| } |
| 480 |
|
| my @args_out = $natcplx ? @outs : PDL->null; |
| 481 |
|
| PDL::_polyval_int(@ins, @args_out); |
| 482 |
|
| if (!$natcplx) { |
| 483 |
|
| $outs[0] .= $args_out[0]->re; |
| 484 |
|
| $outs[1] .= $args_out[0]->im; |
| 485 |
|
| } |
| 486 |
|
| $natcplx ? $outs[0] : @outs; |
| 487 |
|
| } |
| 488 |
|
| EOF |
| 489 |
|
| Doc => ' |
| 490 |
|
| =for ref |
| 491 |
|
| |
| 492 |
|
| Complex value of a complex polynomial at given point, given coefficients |
| 493 |
|
| in order of decreasing powers. Uses Horner recurrence. Added in 2.086, |
| 494 |
|
| works with native-complex data. |
| 495 |
|
| |
| 496 |
|
| =for usage |
| 497 |
|
| |
| 498 |
|
| $y = polyval($coeffs, $x); # native complex |
| 499 |
|
| ($yr, $yi) = polyval($cr, $ci, $xr, $xi); |
| 500 |
|
| ',); |
| 501 |
|
| |
| 502 |
|
| sub cequiv { |
| 503 |
|
| my ($func, $ref) = @_; |
| 504 |
|
| return if !g_c($func, 1); |
| 505 |
|
| pp_def("c$func", |
| 506 |
|
| GenericTypes => $AF, |
| 507 |
|
| Pars => 'i(); complex [o] o()', |
| 508 |
|
| Doc => <<EOF, |
| 509 |
|
| =for ref\n |
| 510 |
|
| Takes real or complex data, returns the complex C<$func>.\n |
| 511 |
|
| Added in 2.099. |
| 512 |
|
| EOF |
| 513 |
|
| Code => pp_line_numbers(__LINE__, <<EOF), |
| 514 |
|
| \$TFDEGCH(PDL_CFloat,PDL_CDouble,PDL_CLDouble,PDL_CFloat,PDL_CDouble,PDL_CLDouble) tmp = \$i(); |
| 515 |
|
| tmp = c$func(tmp); |
| 516 |
|
| \$o() = tmp; |
| 517 |
|
| EOF |
| 518 |
|
| ); |
| 519 |
|
| } |
| 520 |
|
| |
| 521 |
|
| cequiv($_) for qw(sqrt log acos asin asinh acosh atanh); |
| 522 |
|
| |
| 523 |
|
| pp_def('csqrt_up', |
| 524 |
|
| GenericTypes => $AF, |
| 525 |
|
| Pars => 'i(); complex [o] o()', |
| 526 |
|
| Doc => <<'EOF', |
| 527 |
|
| Take the complex square root of a number choosing that whose imaginary |
| 528 |
|
| part is not negative, i.e., it is a square root with a branch cut |
| 529 |
|
| 'infinitesimally' below the positive real axis. |
| 530 |
|
| EOF |
| 531 |
|
| Code => pp_line_numbers(__LINE__, <<'EOF'), |
| 532 |
128 |
| $TFDEGCH(PDL_CFloat,PDL_CDouble,PDL_CLDouble,PDL_CFloat,PDL_CDouble,PDL_CLDouble) tmp = $i(); |
| 533 |
128 |
| tmp = csqrt(tmp); |
| 534 |
128 |
▶ | if (cimag(tmp)<0) |
Branch: 6 branches
| Branch | True | False | Total |
| if (cimag(tmp)<0) |
0 |
23 |
23 |
| if (cimag(tmp)<0) |
3 |
35 |
38 |
| if (cimag(tmp)<0) |
3 |
20 |
23 |
| if (cimag(tmp)<0) |
3 |
20 |
23 |
| if (cimag(tmp)<0) |
7 |
10 |
17 |
| if (cimag(tmp)<0) |
0 |
11 |
11 |
|
| 535 |
71 |
| tmp = -tmp; |
| 536 |
128 |
| $o() = tmp; |
| 537 |
|
| EOF |
| 538 |
|
| ); |
| 539 |
|
| |
| 540 |
|
| pp_addpm({At=>'Bot'},<<'EOD'); |
| 541 |
|
| =head1 AUTHOR |
| 542 |
|
| |
| 543 |
|
| Copyright (C) R.J.R. Williams 1997 (rjrw@ast.leeds.ac.uk), Karl Glazebrook |
| 544 |
|
| (kgb@aaoepp.aao.gov.au) and Tuomas J. Lukka (Tuomas.Lukka@helsinki.fi). |
| 545 |
|
| Portions (C) Craig DeForest 2002 (deforest@boulder.swri.edu). |
| 546 |
|
| |
| 547 |
|
| All rights reserved. There is no warranty. You are allowed |
| 548 |
|
| to redistribute this software / documentation under certain |
| 549 |
|
| conditions. For details, see the file COPYING in the PDL |
| 550 |
|
| distribution. If this file is separated from the PDL distribution, |
| 551 |
|
| the PDL copyright notice should be included in the file. |
| 552 |
|
| |
| 553 |
|
| =cut |
| 554 |
|
| |
| 555 |
|
| EOD |
| 556 |
|
| |
| 557 |
|
| my $gc_text = "our %got_complex = (\n"; |
| 558 |
|
| $gc_text .= " $_ => $got_complex{$_},\n" for sort keys %got_complex; |
| 559 |
|
| $gc_text .= ");\n"; |
| 560 |
|
| pp_addpm({At=>'Bot'}, $gc_text); |
| 561 |
|
| |
| 562 |
|
| pp_done(); |