| 1 | use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | use PDL::Types qw(types ppdefs ppdefs_all ppdefs_complex); | ||||||||||||||||||||||||||||||||||||||||||
| 4 | require PDL::Core::Dev; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | my $A = [ppdefs_all]; | ||||||||||||||||||||||||||||||||||||||||||
| 7 | my $C = [ppdefs_complex]; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | my $F = [map $_->ppsym, grep $_->real && !$_->integer, types]; | ||||||||||||||||||||||||||||||||||||||||||
| 9 | $F = [(grep $_ ne 'D', @$F), 'D']; # so defaults to D if non-float given | ||||||||||||||||||||||||||||||||||||||||||
| 10 | my $AF = [map $_->ppsym, grep !$_->integer, types]; | ||||||||||||||||||||||||||||||||||||||||||
| 11 | $AF = [(grep $_ ne 'D', @$AF), 'D']; # so defaults to D if non-float given | ||||||||||||||||||||||||||||||||||||||||||
| 12 | my $T = [map $_->ppsym, grep $_->integer, types]; | ||||||||||||||||||||||||||||||||||||||||||
| 13 | my $U = [map $_->ppsym, grep $_->unsigned, types]; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | my $S = [map $_->ppsym, grep $_->real && !$_->unsigned, types]; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | my %is_real; @is_real{ppdefs()} = (); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | my @Rtypes = grep $_->real, types(); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | my @Ctypes = grep !$_->real, types(); | ||||||||||||||||||||||||||||||||||||||||||
| 18 | my @Ftypes = grep !$_->integer, types(); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | my %got_complex; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | pp_addpm({At=>'Top'},<<'EOD'); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 70 | ▶ | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 70 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | PDL::Ops - Fundamental mathematical operators | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | This module provides the functions used by PDL to | ||||||||||||||||||||||||||||||||||||||||||
| 33 | overload the basic mathematical operators (C<+ - / *> | ||||||||||||||||||||||||||||||||||||||||||
| 34 | etc.) and functions (C<sin sqrt> etc.) | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | It also includes the function C<log10>, which should | ||||||||||||||||||||||||||||||||||||||||||
| 37 | be a perl function so that we can overload it! | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | Matrix multiplication (the operator C<x>) is handled | ||||||||||||||||||||||||||||||||||||||||||
| 40 | by the module L<PDL::Primitive>. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | none | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | EOD | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | pp_addpm({At=>'Bot'},<<'EOPM'); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | Tuomas J. Lukka (lukka@fas.harvard.edu), | ||||||||||||||||||||||||||||||||||||||||||
| 55 | Karl Glazebrook (kgb@aaoepp.aao.gov.au), | ||||||||||||||||||||||||||||||||||||||||||
| 56 | Doug Hunt (dhunt@ucar.edu), | ||||||||||||||||||||||||||||||||||||||||||
| 57 | Christian Soeller (c.soeller@auckland.ac.nz), | ||||||||||||||||||||||||||||||||||||||||||
| 58 | Doug Burke (burke@ifa.hawaii.edu), | ||||||||||||||||||||||||||||||||||||||||||
| 59 | and Craig DeForest (deforest@boulder.swri.edu). | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | EOPM | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | pp_addhdr(' | ||||||||||||||||||||||||||||||||||||||||||
| 66 | #include <tgmath.h> | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | #define MOD(X,N) (((N) == 0) ? 0 : ( (X) - (PDL_ABS(N)) * ((long long)((X)/(PDL_ABS(N))) + ( ( ((N) * ((long long)((X)/(N)))) != (X) ) ? ( ( ((N)<0) ? 1 : 0 ) + ( (((X)<0) ? -1 : 0))) : 0 )))) | ||||||||||||||||||||||||||||||||||||||||||
| 69 | #define BU_MOD(X,N)(((N) == 0) ? 0 : ( (X)-(N)*((uint64_t)((X)/(N))) )) | ||||||||||||||||||||||||||||||||||||||||||
| 70 | #define SPACE(A,B) ( ((A)<(B)) ? -1 : ((A)!=(B)) ) | ||||||||||||||||||||||||||||||||||||||||||
| 71 | '); | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | my %char2escape = ('>'=>'E<gt>','<'=>'E<lt>'); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | my $chars = '(['.join('', map quotemeta, sort keys %char2escape).'])'; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | sub protect_chars { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | my ($txt) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | $txt =~ s/$chars/$char2escape{$1}/g; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | return $txt; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # simple binary operators | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | pp_addhdr(pp_line_numbers(__LINE__, <<'EOF')); | ||||||||||||||||||||||||||||||||||||||||||
| 84 | #define PDL_BADVAL_WARN_X(datatype, ctype, ppsym, ...) \ | ||||||||||||||||||||||||||||||||||||||||||
| 85 | bad_anyval.type = datatype; bad_anyval.value.ppsym = PDL->bvals.ppsym; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | #define PDL_BADVAL_WARN(var) \ | ||||||||||||||||||||||||||||||||||||||||||
| 87 | { \ | ||||||||||||||||||||||||||||||||||||||||||
| 88 | PDL_Anyval bad_anyval = { PDL_INVALID, {0} }; \ | ||||||||||||||||||||||||||||||||||||||||||
| 89 | if (!(var->has_badvalue && var->badvalue.type != var->datatype)) { \ | ||||||||||||||||||||||||||||||||||||||||||
| 90 | if (var->has_badvalue) \ | ||||||||||||||||||||||||||||||||||||||||||
| 91 | bad_anyval = var->badvalue; \ | ||||||||||||||||||||||||||||||||||||||||||
| 92 | else { \ | ||||||||||||||||||||||||||||||||||||||||||
| 93 | PDL_GENERICSWITCH(PDL_TYPELIST_ALL, var->datatype, PDL_BADVAL_WARN_X, ) \ | ||||||||||||||||||||||||||||||||||||||||||
| 94 | } \ | ||||||||||||||||||||||||||||||||||||||||||
| 95 | } \ | ||||||||||||||||||||||||||||||||||||||||||
| 96 | if (bad_anyval.type < 0) \ | ||||||||||||||||||||||||||||||||||||||||||
| 97 | barf("Error getting badvalue, type=%d", bad_anyval.type); \ | ||||||||||||||||||||||||||||||||||||||||||
| 98 | complex double bad_c; \ | ||||||||||||||||||||||||||||||||||||||||||
| 99 | ANYVAL_TO_CTYPE(bad_c, complex double, bad_anyval); \ | ||||||||||||||||||||||||||||||||||||||||||
| 100 | if( bad_c == 0 || bad_c == 1 ) \ | ||||||||||||||||||||||||||||||||||||||||||
| 101 | warn(#var " badvalue is set to 0 or 1. This will cause data loss when using badvalues for comparison operators."); \ | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 104 | sub biop { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | my ($name,$op,$mutator,$doc,%extra) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | my $optxt = protect_chars ref $op eq 'ARRAY' ? $op->[1] : $op; | ||||||||||||||||||||||||||||||||||||||||||
| 107 | $op = $op->[0] if ref $op eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | $extra{HdrCode} = << 'EOH'; | ||||||||||||||||||||||||||||||||||||||||||
| 109 | if (swap) { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | pdl *tmp = a; | ||||||||||||||||||||||||||||||||||||||||||
| 111 | a = b; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | b = tmp; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | EOH | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # handle exceptions | ||||||||||||||||||||||||||||||||||||||||||
| 116 | if ( exists $extra{Exception} ) { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # NOTE This option is unused. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # See also `ufunc()`. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | delete $extra{Exception}; | ||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | if ($extra{Comparison}) { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | my $first_complex = $Ctypes[0]->sym; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | $extra{HdrCode} .= <<EOF if $extra{Comparison} > 1; | ||||||||||||||||||||||||||||||||||||||||||
| 124 | if ((a->datatype >= $first_complex) || (b->datatype >= $first_complex)) | ||||||||||||||||||||||||||||||||||||||||||
| 125 | barf("Can't compare complex numbers"); | ||||||||||||||||||||||||||||||||||||||||||
| 126 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 127 | $extra{HdrCode} .= " PDL_BADVAL_WARN(a)\n PDL_BADVAL_WARN(b)\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 128 | delete $extra{Comparison}; | ||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | my $bitwise = delete $extra{Bitwise}; | ||||||||||||||||||||||||||||||||||||||||||
| 132 | pp_def($name, | ||||||||||||||||||||||||||||||||||||||||||
| 133 | Pars => 'a(); b(); [o]c();', | ||||||||||||||||||||||||||||||||||||||||||
| 134 | OtherPars => 'int $swap', | ||||||||||||||||||||||||||||||||||||||||||
| 135 | OtherParsDefaults => { swap => 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | HandleBad => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | NoBadifNaN => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 138 | Inplace => [ 'a' ], | ||||||||||||||||||||||||||||||||||||||||||
| 139 | Overload => [$op, $mutator, $bitwise], | ||||||||||||||||||||||||||||||||||||||||||
| 140 | NoExport => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | Code => pp_line_numbers(__LINE__, <<EOF), | ||||||||||||||||||||||||||||||||||||||||||
| 142 | PDL_IF_BAD(char anybad = 0;,) | ||||||||||||||||||||||||||||||||||||||||||
| 143 | broadcastloop %{ | ||||||||||||||||||||||||||||||||||||||||||
| 144 | PDL_IF_BAD(if ( ( \$PDLSTATEISBAD(a) && \$ISBAD(a()) ) | ||||||||||||||||||||||||||||||||||||||||||
| 145 | || ( \$PDLSTATEISBAD(b) && \$ISBAD(b()) )) { \$SETBAD(c()); anybad = 1; } else,) | ||||||||||||||||||||||||||||||||||||||||||
| 146 | \$c() = \$a() $op \$b(); | ||||||||||||||||||||||||||||||||||||||||||
| 147 | %} | ||||||||||||||||||||||||||||||||||||||||||
| 148 | PDL_IF_BAD(if (anybad) \$PDLSTATESETBAD(c);,) | ||||||||||||||||||||||||||||||||||||||||||
| 149 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 150 | %extra, | ||||||||||||||||||||||||||||||||||||||||||
| 151 | Doc => $doc, | ||||||||||||||||||||||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | sub g_c2 { # 2 so no warnings on redefine when run Makefile.PL, cf ::Math | ||||||||||||||||||||||||||||||||||||||||||
| 156 | my ($func, $arg) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 157 | return 0 if $func !~ /[a-z]/; | ||||||||||||||||||||||||||||||||||||||||||
| 158 | $got_complex{$func} = PDL::Core::Dev::got_complex_version($func, $arg); | ||||||||||||||||||||||||||||||||||||||||||
| 159 | print "got_complex_version($func, $arg): $got_complex{$func}\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 160 | $got_complex{$func}; | ||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | #simple binary functions | ||||||||||||||||||||||||||||||||||||||||||
| 164 | sub bifunc { | ||||||||||||||||||||||||||||||||||||||||||
| 165 | my ($name,$func,$mutator,$doc,%extra) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 166 | my $funcov = ref $func eq 'ARRAY' ? $func->[1] : $func; | ||||||||||||||||||||||||||||||||||||||||||
| 167 | my $isop = $funcov =~ s/^op//; | ||||||||||||||||||||||||||||||||||||||||||
| 168 | my $funcovp = protect_chars $funcov; | ||||||||||||||||||||||||||||||||||||||||||
| 169 | $func = $func->[0] if ref $func eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 170 | my $got_complex = g_c2($func, 2); | ||||||||||||||||||||||||||||||||||||||||||
| 171 | $extra{GenericTypes} = [ grep exists $is_real{$_}, @{$extra{GenericTypes}} ] | ||||||||||||||||||||||||||||||||||||||||||
| 172 | if !$got_complex and $extra{GenericTypes}; | ||||||||||||||||||||||||||||||||||||||||||
| 173 | $extra{HdrCode} .= << 'EOH'; | ||||||||||||||||||||||||||||||||||||||||||
| 174 | if (swap) { | ||||||||||||||||||||||||||||||||||||||||||
| 175 | pdl *tmp = a; | ||||||||||||||||||||||||||||||||||||||||||
| 176 | a = b; | ||||||||||||||||||||||||||||||||||||||||||
| 177 | b = tmp; | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | EOH | ||||||||||||||||||||||||||||||||||||||||||
| 180 | # is this one to be used as a function or operator ? | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | my $codestr; | ||||||||||||||||||||||||||||||||||||||||||
| 183 | if ($extra{unsigned}){ | ||||||||||||||||||||||||||||||||||||||||||
| 184 | #a little dance to avoid the MOD macro warnings for byte & ushort datatypes | ||||||||||||||||||||||||||||||||||||||||||
| 185 | my $t = join '', map $_->ppsym, grep $_->real, types(); | ||||||||||||||||||||||||||||||||||||||||||
| 186 | my $v = join ',', map | ||||||||||||||||||||||||||||||||||||||||||
| 187 | $_->unsigned ? 'BU_' : '', | ||||||||||||||||||||||||||||||||||||||||||
| 188 | grep $_->real, types(); | ||||||||||||||||||||||||||||||||||||||||||
| 189 | $codestr = << "ENDCODE"; | ||||||||||||||||||||||||||||||||||||||||||
| 190 | \$c() = (\$GENERIC(c))\$T$t($v)$func(\$a(),\$b()); | ||||||||||||||||||||||||||||||||||||||||||
| 191 | ENDCODE | ||||||||||||||||||||||||||||||||||||||||||
| 192 | #end dance | ||||||||||||||||||||||||||||||||||||||||||
| 193 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 194 | $codestr = '$c() = ($GENERIC(c))'.$func.'($a(),$b());'; | ||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | delete $extra{unsigned}; #remove the key so it doesn't get added in pp_def. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | pp_def($name, | ||||||||||||||||||||||||||||||||||||||||||
| 199 | HandleBad => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 200 | NoBadifNaN => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 201 | Pars => 'a(); b(); [o]c();', | ||||||||||||||||||||||||||||||||||||||||||
| 202 | OtherPars => 'int $swap', | ||||||||||||||||||||||||||||||||||||||||||
| 203 | OtherParsDefaults => { swap => 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 204 | Inplace => [ 'a' ], | ||||||||||||||||||||||||||||||||||||||||||
| 205 | Overload => [$funcov, $mutator], | ||||||||||||||||||||||||||||||||||||||||||
| 206 | NoExport => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 207 | Code => pp_line_numbers(__LINE__, <<EOF), | ||||||||||||||||||||||||||||||||||||||||||
| 208 | PDL_IF_BAD(char anybad = 0;,) | ||||||||||||||||||||||||||||||||||||||||||
| 209 | broadcastloop %{ | ||||||||||||||||||||||||||||||||||||||||||
| 210 | PDL_IF_BAD(if ( \$ISBAD(a()) || \$ISBAD(b()) ) { anybad = 1; \$SETBAD(c()); } else ,) { | ||||||||||||||||||||||||||||||||||||||||||
| 211 | $codestr | ||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | %} | ||||||||||||||||||||||||||||||||||||||||||
| 214 | PDL_IF_BAD(if (anybad) \$PDLSTATESETBAD(c);,) | ||||||||||||||||||||||||||||||||||||||||||
| 215 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 216 | %extra, | ||||||||||||||||||||||||||||||||||||||||||
| 217 | Doc => $doc, | ||||||||||||||||||||||||||||||||||||||||||
| 218 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | # simple unary functions and operators | ||||||||||||||||||||||||||||||||||||||||||
| 222 | sub ufunc { | ||||||||||||||||||||||||||||||||||||||||||
| 223 | my ($name,$func,$overload,$doc,%extra) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 224 | my $funcov = ref $func eq 'ARRAY' ? $func->[1] : $func; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | my $funcovp = protect_chars $funcov; | ||||||||||||||||||||||||||||||||||||||||||
| 226 | $func = $func->[0] if ref $func eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 227 | my $got_complex = g_c2($func, 1); | ||||||||||||||||||||||||||||||||||||||||||
| 228 | $extra{GenericTypes} = [ grep exists $is_real{$_}, @{$extra{GenericTypes}} ] | ||||||||||||||||||||||||||||||||||||||||||
| 229 | if !$got_complex and $extra{GenericTypes}; | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | # handle exceptions | ||||||||||||||||||||||||||||||||||||||||||
| 232 | if ( exists $extra{Exception} ) { | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # print "Warning: ignored exception for $name\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # NOTE This option is unused. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # See also `biop()`. | ||||||||||||||||||||||||||||||||||||||||||
| 236 | delete $extra{Exception}; | ||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | my $codestr = '$b() = ($GENERIC(b))'.$func.'($a());'; | ||||||||||||||||||||||||||||||||||||||||||
| 239 | if (delete $extra{NoTgmath} and $got_complex) { | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # don't bother if not got complex version | ||||||||||||||||||||||||||||||||||||||||||
| 241 | $codestr = join "\n", | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 'types('.join('', map $_->ppsym, @Rtypes).') %{'.$codestr.'%}', | ||||||||||||||||||||||||||||||||||||||||||
| 243 | (map 'types('.$_->ppsym.') %{$b() = c'.$func.$_->floatsuffix.'($a());%}', @Ctypes), | ||||||||||||||||||||||||||||||||||||||||||
| 244 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | # do not have to worry about propagation of the badflag when | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # inplace since only input ndarray is a, hence its badflag | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # won't change | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # UNLESS an exception occurs... | ||||||||||||||||||||||||||||||||||||||||||
| 250 | pp_def($name, | ||||||||||||||||||||||||||||||||||||||||||
| 251 | Pars => 'a(); [o]b()', | ||||||||||||||||||||||||||||||||||||||||||
| 252 | HandleBad => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 253 | NoBadifNaN => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 254 | Inplace => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 255 | !$overload ? () : (Overload => $funcov), | ||||||||||||||||||||||||||||||||||||||||||
| 256 | NoExport => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 257 | Code => pp_line_numbers(__LINE__, <<EOF), | ||||||||||||||||||||||||||||||||||||||||||
| 258 | PDL_IF_BAD(if ( \$ISBAD(a()) ) \$SETBAD(b()); else {,) | ||||||||||||||||||||||||||||||||||||||||||
| 259 | $codestr | ||||||||||||||||||||||||||||||||||||||||||
| 260 | PDL_IF_BAD(},) | ||||||||||||||||||||||||||||||||||||||||||
| 261 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 262 | %extra, | ||||||||||||||||||||||||||||||||||||||||||
| 263 | Doc => $doc, | ||||||||||||||||||||||||||||||||||||||||||
| 264 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | # we trap some illegal operations here -- see the Exception option | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # note, for the ufunc()'s, the checks do not work too well | ||||||||||||||||||||||||||||||||||||||||||
| 271 | # for unsigned integer types (ie < 0) | ||||||||||||||||||||||||||||||||||||||||||
| 272 | # | ||||||||||||||||||||||||||||||||||||||||||
| 273 | # XXX needs thinking about | ||||||||||||||||||||||||||||||||||||||||||
| 274 | # - have to integrate into Code section as well (so | ||||||||||||||||||||||||||||||||||||||||||
| 275 | # 12/pdl(2,4,0,3) is trapped and flagged bad) | ||||||||||||||||||||||||||||||||||||||||||
| 276 | # --> complicated | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # - perhaps could use type %{ %} ? | ||||||||||||||||||||||||||||||||||||||||||
| 278 | # | ||||||||||||||||||||||||||||||||||||||||||
| 279 | # ==> currently have commented out the exception code, since | ||||||||||||||||||||||||||||||||||||||||||
| 280 | # want to see if can use NaN/Inf for bad values | ||||||||||||||||||||||||||||||||||||||||||
| 281 | # (would solve many problems for F,D types) | ||||||||||||||||||||||||||||||||||||||||||
| 282 | # | ||||||||||||||||||||||||||||||||||||||||||
| 283 | # there is an issue over how we handle comparison operators | ||||||||||||||||||||||||||||||||||||||||||
| 284 | # - see Primitive/primitive.pd/zcover() for more discussion | ||||||||||||||||||||||||||||||||||||||||||
| 285 | # | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | ## arithmetic ops | ||||||||||||||||||||||||||||||||||||||||||
| 288 | biop('plus','+',1,'add two ndarrays',GenericTypes => $A); | ||||||||||||||||||||||||||||||||||||||||||
| 289 | biop('mult','*',1,'multiply two ndarrays',GenericTypes => $A); | ||||||||||||||||||||||||||||||||||||||||||
| 290 | biop('minus','-',1,'subtract two ndarrays',GenericTypes => $A); | ||||||||||||||||||||||||||||||||||||||||||
| 291 | biop('divide','/',1,'divide two ndarrays', Exception => '$b() == 0', GenericTypes => $A); | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | ## note: divide should perhaps trap division by zero as well | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | ## comparison ops | ||||||||||||||||||||||||||||||||||||||||||
| 296 | # not defined for complex numbers | ||||||||||||||||||||||||||||||||||||||||||
| 297 | biop('gt','>',0,'the binary E<gt> (greater than) operation', Comparison => 2); | ||||||||||||||||||||||||||||||||||||||||||
| 298 | biop('lt','<',0,'the binary E<lt> (less than) operation', Comparison => 2); | ||||||||||||||||||||||||||||||||||||||||||
| 299 | biop('le','<=',0,'the binary E<lt>= (less equal) operation', Comparison => 2); | ||||||||||||||||||||||||||||||||||||||||||
| 300 | biop('ge','>=',0,'the binary E<gt>= (greater equal) operation', Comparison => 2); | ||||||||||||||||||||||||||||||||||||||||||
| 301 | biop('eq','==',0,'binary I<equal to> operation (C<==>)', Comparison => 1, GenericTypes => $A); | ||||||||||||||||||||||||||||||||||||||||||
| 302 | biop('ne','!=',0,'binary I<not equal to> operation (C<!=>)', Comparison => 1, GenericTypes => $A); | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | ## bit ops | ||||||||||||||||||||||||||||||||||||||||||
| 305 | # those need to be limited to the right types | ||||||||||||||||||||||||||||||||||||||||||
| 306 | biop('shiftleft','<<',1,'bitwise leftshift C<$a> by C<$b>',GenericTypes => $T); | ||||||||||||||||||||||||||||||||||||||||||
| 307 | biop('shiftright','>>',1,'bitwise rightshift C<$a> by C<$b>',GenericTypes => $T); | ||||||||||||||||||||||||||||||||||||||||||
| 308 | biop('or2','|',1,'bitwise I<or> of two ndarrays',GenericTypes => $T, | ||||||||||||||||||||||||||||||||||||||||||
| 309 | Bitwise => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 310 | biop('and2','&',1,'bitwise I<and> of two ndarrays',GenericTypes => $T, | ||||||||||||||||||||||||||||||||||||||||||
| 311 | Bitwise => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 312 | biop('xor','^',1,'bitwise I<exclusive or> of two ndarrays',GenericTypes => $T, | ||||||||||||||||||||||||||||||||||||||||||
| 313 | Bitwise => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | pp_addpm( | ||||||||||||||||||||||||||||||||||||||||||
| 316 | "=head2 xor2\n\n=for ref\n\nSynonym for L</xor>.\n\n=cut\n | ||||||||||||||||||||||||||||||||||||||||||
| 317 | *PDL::xor2 = *xor2 = \\&PDL::xor;" | ||||||||||||||||||||||||||||||||||||||||||
| 318 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | # some standard binary functions | ||||||||||||||||||||||||||||||||||||||||||
| 321 | bifunc('power',['pow','op**'],1,'raise ndarray C<$a> to the power C<$b>',GenericTypes => [@$C, @$F]); | ||||||||||||||||||||||||||||||||||||||||||
| 322 | bifunc('atan2','atan2',0,'elementwise C<atan2> of two ndarrays',GenericTypes => $F); | ||||||||||||||||||||||||||||||||||||||||||
| 323 | bifunc('modulo',['MOD','op%'],1,'elementwise C<modulo> operation',unsigned=>1); | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | bifunc('spaceship',['SPACE','op<=>'],0,'elementwise "<=>" operation'); | |||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | # some standard unary functions | ||||||||||||||||||||||||||||||||||||||||||
| 327 | ufunc('bitnot','~',1,'unary bitwise negation',GenericTypes => $T); | ||||||||||||||||||||||||||||||||||||||||||
| 328 | ufunc('sqrt','sqrt',1,'elementwise square root', GenericTypes => $A); # Exception => '$a() < 0'); | ||||||||||||||||||||||||||||||||||||||||||
| 329 | ufunc('sin','sin',1,'the sin function', GenericTypes => $A); | ||||||||||||||||||||||||||||||||||||||||||
| 330 | ufunc('cos','cos',1,'the cos function', GenericTypes => $A); | ||||||||||||||||||||||||||||||||||||||||||
| 331 | ufunc('not','!',1,'the elementwise I<not> operation'); | ||||||||||||||||||||||||||||||||||||||||||
| 332 | ufunc('exp','exp',1,'the exponential function',GenericTypes => [@$C, @$F]); | ||||||||||||||||||||||||||||||||||||||||||
| 333 | ufunc('log','log',1,'the natural logarithm',GenericTypes => [@$C, @$F], Exception => '$a() <= 0'); | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # no export these because clash with Test::Deep (re) or internal (_*abs) | ||||||||||||||||||||||||||||||||||||||||||
| 336 | cfunc('re', 'creal', 1, 0, 'Returns the real part of a complex number.', | ||||||||||||||||||||||||||||||||||||||||||
| 337 | '$complexv() = $b() + I * cimag($complexv());' | ||||||||||||||||||||||||||||||||||||||||||
| 338 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 339 | cfunc('im', 'cimag', 1, 0, 'Returns the imaginary part of a complex number.', | ||||||||||||||||||||||||||||||||||||||||||
| 340 | '$complexv() = creal($complexv()) + I * $b();' | ||||||||||||||||||||||||||||||||||||||||||
| 341 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 342 | cfunc('_cabs', 'fabs', 1, 0, 'Returns the absolute (length) of a complex number.', undef, | ||||||||||||||||||||||||||||||||||||||||||
| 343 | PMFunc=>'', | ||||||||||||||||||||||||||||||||||||||||||
| 344 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 345 | my $rabs_code = ' | ||||||||||||||||||||||||||||||||||||||||||
| 346 | types('.join('', @$U).') %{ $b()=$a(); %} | ||||||||||||||||||||||||||||||||||||||||||
| 347 | types('.join('', @$S).') %{ $b()=PDL_ABS($a()); %} | ||||||||||||||||||||||||||||||||||||||||||
| 348 | '; | ||||||||||||||||||||||||||||||||||||||||||
| 349 | pp_def ( '_rabs', | ||||||||||||||||||||||||||||||||||||||||||
| 350 | Pars=>'a(); [o]b()', | ||||||||||||||||||||||||||||||||||||||||||
| 351 | HandleBad => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 352 | NoBadifNaN => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 353 | Inplace => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 354 | NoExport => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 355 | Code => pp_line_numbers(__LINE__-1, qq{ | ||||||||||||||||||||||||||||||||||||||||||
| 356 | PDL_IF_BAD(if ( \$ISBAD(a()) ) \$SETBAD(b()); else,) | ||||||||||||||||||||||||||||||||||||||||||
| 357 | $rabs_code | ||||||||||||||||||||||||||||||||||||||||||
| 358 | }), | ||||||||||||||||||||||||||||||||||||||||||
| 359 | Doc=>undef, | ||||||||||||||||||||||||||||||||||||||||||
| 360 | PMFunc=>'', | ||||||||||||||||||||||||||||||||||||||||||
| 361 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | # make log10() work on scalars (returning scalars) | ||||||||||||||||||||||||||||||||||||||||||
| 364 | # as well as ndarrays | ||||||||||||||||||||||||||||||||||||||||||
| 365 | ufunc('log10','log10',0,'the base 10 logarithm', GenericTypes => $A, | ||||||||||||||||||||||||||||||||||||||||||
| 366 | Exception => '$a() <= 0', | ||||||||||||||||||||||||||||||||||||||||||
| 367 | NoTgmath => 1, # glibc for at least GCC 8.3.0 won't tgmath log10 though 7.1.0 did | ||||||||||||||||||||||||||||||||||||||||||
| 368 | NoExport => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 369 | PMCode => <<'EOF', | ||||||||||||||||||||||||||||||||||||||||||
| 370 | sub PDL::log10 { | ||||||||||||||||||||||||||||||||||||||||||
| 371 | my ($x, $y) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 372 | return log($x) / log(10) if !UNIVERSAL::isa($x,"PDL"); | ||||||||||||||||||||||||||||||||||||||||||
| 373 | barf "inplace but output given" if $x->is_inplace and defined $y; | ||||||||||||||||||||||||||||||||||||||||||
| 374 | if ($x->is_inplace) { $x->set_inplace(0); $y = $x; } | ||||||||||||||||||||||||||||||||||||||||||
| 375 | elsif (!defined $y) { $y = $x->initialize; } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | &PDL::_log10_int( $x, $y ); | ||||||||||||||||||||||||||||||||||||||||||
| 377 | $y; | ||||||||||||||||||||||||||||||||||||||||||
| 378 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 379 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 380 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | pp_def( | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 'assgn', | ||||||||||||||||||||||||||||||||||||||||||
| 384 | HandleBad => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 385 | GenericTypes => $A, | ||||||||||||||||||||||||||||||||||||||||||
| 386 | Pars => 'a(); [o]b();', | ||||||||||||||||||||||||||||||||||||||||||
| 387 | Code => pp_line_numbers(__LINE__-1, q{ | ||||||||||||||||||||||||||||||||||||||||||
| 388 | PDL_IF_BAD(char anybad = 0;,) | ||||||||||||||||||||||||||||||||||||||||||
| 389 | broadcastloop %{ | ||||||||||||||||||||||||||||||||||||||||||
| 390 | PDL_IF_BAD(if ($ISBAD(a())) { anybad = 1; $SETBAD(b()); continue; },) | ||||||||||||||||||||||||||||||||||||||||||
| 391 | $b() = $a(); | ||||||||||||||||||||||||||||||||||||||||||
| 392 | %} | ||||||||||||||||||||||||||||||||||||||||||
| 393 | PDL_IF_BAD(if (anybad) $PDLSTATESETBAD(b);,) | ||||||||||||||||||||||||||||||||||||||||||
| 394 | }), | ||||||||||||||||||||||||||||||||||||||||||
| 395 | Doc => | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 'Plain numerical assignment. This is used to implement the ".=" operator', | ||||||||||||||||||||||||||||||||||||||||||
| 397 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | # special functions for complex data types that don't work well with | ||||||||||||||||||||||||||||||||||||||||||
| 400 | # the ufunc/bifunc logic | ||||||||||||||||||||||||||||||||||||||||||
| 401 | sub cfunc { | ||||||||||||||||||||||||||||||||||||||||||
| 402 | my ($name, $func, $make_real, $force_complex, $doc, $backcode, %extra) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 403 | my $codestr = pp_line_numbers(__LINE__-1,"\$b() = $func(\$complexv());"); | ||||||||||||||||||||||||||||||||||||||||||
| 404 | pp_def($name, | ||||||||||||||||||||||||||||||||||||||||||
| 405 | GenericTypes=>$C, | ||||||||||||||||||||||||||||||||||||||||||
| 406 | Pars => ($force_complex ? '!real ' : '').'complexv(); '.($make_real ? 'real' : '').' [o]b()', | ||||||||||||||||||||||||||||||||||||||||||
| 407 | HandleBad => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 408 | NoBadifNaN => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 409 | (($make_real || $force_complex) ? () : (Inplace => 1)), | ||||||||||||||||||||||||||||||||||||||||||
| 410 | NoExport => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 411 | Code => pp_line_numbers(__LINE__-1, qq{ | ||||||||||||||||||||||||||||||||||||||||||
| 412 | PDL_IF_BAD(if ( \$ISBAD(complexv()) ) \$SETBAD(b()); else,) | ||||||||||||||||||||||||||||||||||||||||||
| 413 | $codestr | ||||||||||||||||||||||||||||||||||||||||||
| 414 | }), | ||||||||||||||||||||||||||||||||||||||||||
| 415 | !$backcode ? () : ( | ||||||||||||||||||||||||||||||||||||||||||
| 416 | BackCode => pp_line_numbers(__LINE__-1, qq{ | ||||||||||||||||||||||||||||||||||||||||||
| 417 | PDL_IF_BAD(if ( \$ISBAD(b()) ) \$SETBAD(complexv()); else {,) | ||||||||||||||||||||||||||||||||||||||||||
| 418 | $backcode | ||||||||||||||||||||||||||||||||||||||||||
| 419 | PDL_IF_BAD(},) | ||||||||||||||||||||||||||||||||||||||||||
| 420 | }), | ||||||||||||||||||||||||||||||||||||||||||
| 421 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 422 | %extra, | ||||||||||||||||||||||||||||||||||||||||||
| 423 | Doc => $doc . (!$backcode ? '' : ' Flows data back & forth.'), | ||||||||||||||||||||||||||||||||||||||||||
| 424 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | cfunc('carg', 'carg', 1, 1, 'Returns the polar angle of a complex number.', undef, NoExport => 0); | ||||||||||||||||||||||||||||||||||||||||||
| 428 | cfunc('conj', 'conj', 0, 0, 'complex conjugate.', undef, NoExport => 0); | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | pp_def('czip', | ||||||||||||||||||||||||||||||||||||||||||
| 431 | Pars => '!complex r(); !complex i(); complex [o]c()', | ||||||||||||||||||||||||||||||||||||||||||
| 432 | Doc => <<'EOF', | ||||||||||||||||||||||||||||||||||||||||||
| 433 | =for ref | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | Convert real, imaginary to native complex, (sort of) like the LISP zip function. | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | Will add the C<r> ndarray to "i" times the C<i> ndarray. Only | ||||||||||||||||||||||||||||||||||||||||||
| 438 | takes real ndarrays as input. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 440 | Code => '$c() = $r() + $i() * I;' | ||||||||||||||||||||||||||||||||||||||||||
| 441 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | pp_def('ipow', | ||||||||||||||||||||||||||||||||||||||||||
| 444 | Inplace => [qw(a ans)], | ||||||||||||||||||||||||||||||||||||||||||
| 445 | Doc => qq{ | ||||||||||||||||||||||||||||||||||||||||||
| 446 | =for ref | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | raise ndarray C<\$a> to integer power C<\$b> | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | Algorithm from L<Wikipedia|http://en.wikipedia.org/wiki/Exponentiation_by_squaring> | ||||||||||||||||||||||||||||||||||||||||||
| 451 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 452 | Pars => 'a(); longlong b(); [o] ans()', | ||||||||||||||||||||||||||||||||||||||||||
| 453 | GenericTypes => [qw(P Q), @$AF], | ||||||||||||||||||||||||||||||||||||||||||
| 454 | Code => pp_line_numbers(__LINE__-1, q{ | ||||||||||||||||||||||||||||||||||||||||||
| 455 | $GENERIC(b) n = $b(); | ||||||||||||||||||||||||||||||||||||||||||
| 456 | if (n == 0) { | ||||||||||||||||||||||||||||||||||||||||||
| 457 | $ans() = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 458 | continue; | ||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | $GENERIC() y = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 461 | $GENERIC() x = $a(); | ||||||||||||||||||||||||||||||||||||||||||
| 462 | if (n < 0) { | ||||||||||||||||||||||||||||||||||||||||||
| 463 | x = 1 / x; | ||||||||||||||||||||||||||||||||||||||||||
| 464 | n = -n; | ||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||
| 466 | while (n > 1) { | ||||||||||||||||||||||||||||||||||||||||||
| 467 | if (n % 2) { | ||||||||||||||||||||||||||||||||||||||||||
| 468 | y *= x; | ||||||||||||||||||||||||||||||||||||||||||
| 469 | n -= 1; | ||||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 471 | x *= x; | ||||||||||||||||||||||||||||||||||||||||||
| 472 | n /= 2; | ||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | $ans() = x * y; | ||||||||||||||||||||||||||||||||||||||||||
| 475 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 476 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | =head2 abs | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | =for ref | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | Returns the absolute value of a number. | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 487 | 348 | ▶ | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
abs: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | sub PDL::abs { $_[0]->type->real ? goto &PDL::_rabs : goto &PDL::_cabs } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | EOPM | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | pp_def('abs2', | ||||||||||||||||||||||||||||||||||||||||||
| 492 | GenericTypes=>$A, | ||||||||||||||||||||||||||||||||||||||||||
| 493 | HandleBad => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 494 | Pars => 'a(); real [o]b()', | ||||||||||||||||||||||||||||||||||||||||||
| 495 | Doc => 'Returns the square of the absolute value of a number.', | ||||||||||||||||||||||||||||||||||||||||||
| 496 | Code => <<'EOF', | ||||||||||||||||||||||||||||||||||||||||||
| 497 | PDL_IF_BAD(if ($ISBAD(a())) { $SETBAD(b()); continue; },) | ||||||||||||||||||||||||||||||||||||||||||
| 498 | $b() = PDL_IF_GENTYPE_REAL( | ||||||||||||||||||||||||||||||||||||||||||
| 499 | $a()*$a(), | ||||||||||||||||||||||||||||||||||||||||||
| 500 | creall($a())*creall($a()) + cimagl($a())*cimagl($a()) | ||||||||||||||||||||||||||||||||||||||||||
| 501 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 502 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 503 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | pp_def('r2C', | ||||||||||||||||||||||||||||||||||||||||||
| 506 | GenericTypes=>$AF, | ||||||||||||||||||||||||||||||||||||||||||
| 507 | Pars => 'r(); complex [o]c()', | ||||||||||||||||||||||||||||||||||||||||||
| 508 | Doc => 'convert real to native complex, with an imaginary part of zero', | ||||||||||||||||||||||||||||||||||||||||||
| 509 | PMCode => << 'EOF', | ||||||||||||||||||||||||||||||||||||||||||
| 510 | sub PDL::r2C ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 511 | return $_[0] if UNIVERSAL::isa($_[0], 'PDL') and !$_[0]->type->real; | ||||||||||||||||||||||||||||||||||||||||||
| 512 | my $r = $_[1] // PDL->nullcreate($_[0]); | ||||||||||||||||||||||||||||||||||||||||||
| 513 | PDL::_r2C_int($_[0], $r); | ||||||||||||||||||||||||||||||||||||||||||
| 514 | $r; | ||||||||||||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||||||||||||
| 516 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 517 | Code => '$c() = $r();' | ||||||||||||||||||||||||||||||||||||||||||
| 518 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | pp_def('i2C', | ||||||||||||||||||||||||||||||||||||||||||
| 521 | GenericTypes=>$AF, | ||||||||||||||||||||||||||||||||||||||||||
| 522 | Pars => 'i(); complex [o]c()', | ||||||||||||||||||||||||||||||||||||||||||
| 523 | Doc => 'convert imaginary to native complex, with a real part of zero', | ||||||||||||||||||||||||||||||||||||||||||
| 524 | PMCode => << 'EOF', | ||||||||||||||||||||||||||||||||||||||||||
| 525 | sub PDL::i2C ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 526 | return $_[0] if UNIVERSAL::isa($_[0], 'PDL') and !$_[0]->type->real; | ||||||||||||||||||||||||||||||||||||||||||
| 527 | my $r = $_[1] // PDL->nullcreate($_[0]); | ||||||||||||||||||||||||||||||||||||||||||
| 528 | PDL::_i2C_int($_[0], $r); | ||||||||||||||||||||||||||||||||||||||||||
| 529 | $r; | ||||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 532 | Code => '$c() = $i() * I;' | ||||||||||||||||||||||||||||||||||||||||||
| 533 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | pp_addpm(<<'EOF'); | ||||||||||||||||||||||||||||||||||||||||||
| 536 | # This is to used warn if an operand is non-numeric or non-PDL. | ||||||||||||||||||||||||||||||||||||||||||
| 537 | sub warn_non_numeric_op_wrapper { | ||||||||||||||||||||||||||||||||||||||||||
| 538 | 70 | ▶ | require Scalar::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
warn_non_numeric_op_wrapper: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 539 | 70 | my ($cb, $op_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 540 | return sub { | ||||||||||||||||||||||||||||||||||||||||||
| 541 | 72 | ▶ | my ($op1, $op2) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | 72 | ▶ | warn "'$op2' is not numeric nor a PDL in operator $op_name" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
&Scalar::Util::looks_like_number($op2) or builtin::blessed($op2) and $op2->isa("PDL")
builtin::blessed($op2) and $op2->isa("PDL")
MC/DC0%0/3
&Scalar::Util::looks_like_number($op2) or builtin::blessed($op2) and $op2->isa("PDL")
&Scalar::Util::looks_like_number($op2)
builtin::blessed($op2)
$op2->isa("PDL")
Truth table50%2/4
A or B and C
A= &Scalar::Util::looks_like_number($op2)B= builtin::blessed($op2)C= $op2->isa("PDL")
| |||||||||||||||||||||||||||||||||||||||||||
| 543 | unless Scalar::Util::looks_like_number($op2) | ||||||||||||||||||||||||||||||||||||||||||
| 544 | || ( Scalar::Util::blessed($op2) && $op2->isa('PDL') ); | ||||||||||||||||||||||||||||||||||||||||||
| 545 | 72 | $cb->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||||||||||||
| 547 | 70 | } | |||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | { package # hide from MetaCPAN | ||||||||||||||||||||||||||||||||||||||||||
| 550 | PDL; | ||||||||||||||||||||||||||||||||||||||||||
| 551 | sub overload_dotequals { | ||||||||||||||||||||||||||||||||||||||||||
| 552 | 6000 | ▶ | my @args = !$_[2] ? @_[1,0] : @_[0,1]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
overload_dotequals: called CC=2 SCAR=7.1
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 553 | 6000 | PDL::Ops::assgn(@args); | |||||||||||||||||||||||||||||||||||||||||
| 554 | 5995 | return $args[1]; | |||||||||||||||||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||||||||||||||||
| 556 | 157 | ▶ | sub overload_abs { PDL::abs($_[0]) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
overload_abs: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | sub overload_plusplus { | ||||||||||||||||||||||||||||||||||||||||||
| 558 | 137 | ▶ | $_[0] += ($PDL::Core::pdl_ones[$_[0]->get_datatype] | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
overload_plusplus: called CC=2 SCAR=9.2
Pod
undocumented
Condition33%1/3
$PDL::Core::pdl_ones[$_[0]->get_datatype] // barf("Couldn't find 'one' for type ", $_[0]->get_datatype)
MC/DC0%0/2
$PDL::Core::pdl_ones[$_[0]->get_datatype] // barf("Couldn't find 'one' for type ", $_[0]->get_datatype)
$PDL::Core::pdl_ones[$_[0]->get_datatype]
barf("Couldn't find 'one' for type ", $_[0]->get_datatype)
Truth table33%1/3
A // B
A= $PDL::Core::pdl_ones[$_[0]->get_datatype]B= barf("Couldn't find 'one' for type ", $_[0]->get_datatype)
| |||||||||||||||||||||||||||||||||||||||||||
| 559 | // barf "Couldn't find 'one' for type ", $_[0]->get_datatype | ||||||||||||||||||||||||||||||||||||||||||
| 560 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||||||||||||
| 562 | sub overload_minusminus { | ||||||||||||||||||||||||||||||||||||||||||
| 563 | 0 | ▶ | $_[0] -= ($PDL::Core::pdl_ones[$_[0]->get_datatype] | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
overload_minusminus: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/3
$PDL::Core::pdl_ones[$_[0]->get_datatype] // barf("Couldn't find 'one' for type ", $_[0]->get_datatype)
MC/DC0%0/2
$PDL::Core::pdl_ones[$_[0]->get_datatype] // barf("Couldn't find 'one' for type ", $_[0]->get_datatype)
$PDL::Core::pdl_ones[$_[0]->get_datatype]
barf("Couldn't find 'one' for type ", $_[0]->get_datatype)
Truth table0%0/3
A // B
A= $PDL::Core::pdl_ones[$_[0]->get_datatype]B= barf("Couldn't find 'one' for type ", $_[0]->get_datatype)
| |||||||||||||||||||||||||||||||||||||||||||
| 564 | // barf "Couldn't find 'one' for type ", $_[0]->get_datatype | ||||||||||||||||||||||||||||||||||||||||||
| 565 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||||||||||
| 567 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 568 | 70 | "eq" => PDL::Ops::warn_non_numeric_op_wrapper(\&PDL::eq, 'eq'), | |||||||||||||||||||||||||||||||||||||||||
| 569 | ".=" => \&overload_dotequals, | ||||||||||||||||||||||||||||||||||||||||||
| 570 | 'abs' => \&overload_abs, | ||||||||||||||||||||||||||||||||||||||||||
| 571 | '++' => \&overload_plusplus, | ||||||||||||||||||||||||||||||||||||||||||
| 572 | '--' => \&overload_minusminus, | ||||||||||||||||||||||||||||||||||||||||||
| 573 | 70 | ▶ | ; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||||||||||||||||
| 575 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | my $gc_text = "our %got_complex = (\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 578 | $gc_text .= " $_ => $got_complex{$_},\n" for sort grep /[a-z]/, keys %got_complex; | ||||||||||||||||||||||||||||||||||||||||||
| 579 | $gc_text .= ");\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 580 | 0 | pp_addpm({At=>'Bot'}, $gc_text); | |||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | pp_done(); | ||||||||||||||||||||||||||||||||||||||||||