| 1 | # |
| 2 | # GENERATED WITH PDL::PP from lib/PDL/LinearAlgebra/Trans.pd! Don't modify! |
| 3 | # |
| 4 | package PDL::LinearAlgebra::Trans; |
| 5 | |
| 6 | our @EXPORT_OK = qw( mexp mexpts mlog msqrt mpow |
| 7 | mcos msin mtan msec mcsc mcot |
| 8 | mcosh msinh mtanh msech mcsch mcoth |
| 9 | macos masin matan masec macsc macot |
| 10 | macosh masinh matanh masech macsch macoth |
| 11 | sec asec sech asech |
| 12 | cot acot acoth coth mfun |
| 13 | csc acsc csch acsch toreal pi geexp cgeexp ctrsqrt ctrfun ); |
| 14 | our %EXPORT_TAGS = (Func=>\@EXPORT_OK); |
| 15 | |
| 16 | use PDL::Core; |
| 17 | use PDL::Exporter; |
| 18 | use DynaLoader; |
| 19 | |
| 20 | |
| 21 | our $VERSION = '0.437'; |
| 22 | our @ISA = ( 'PDL::Exporter','DynaLoader' ); |
| 23 | push @PDL::Core::PP, __PACKAGE__; |
| 24 | bootstrap PDL::LinearAlgebra::Trans $VERSION; |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | #line 37 "lib/PDL/LinearAlgebra/Trans.pd" |
| 34 | |
| 35 | use PDL::Core; |
| 36 | use PDL::Slices; |
| 37 | use PDL::Ops qw//; |
| 38 | use PDL::Math qw/floor/; |
| 39 | use PDL::MatrixOps qw/identity/; |
| 40 | use PDL::LinearAlgebra; |
| 41 | use PDL::LinearAlgebra::Real qw //; |
| 42 | use PDL::LinearAlgebra::Complex qw //; |
| 43 | use strict; |
| 44 | |
| 45 | =encoding utf8 |
| 46 | |
| 47 | =head1 NAME |
| 48 | |
| 49 | PDL::LinearAlgebra::Trans - Linear Algebra based transcendental functions for PDL |
| 50 | |
| 51 | =head1 SYNOPSIS |
| 52 | |
| 53 | use PDL::LinearAlgebra::Trans; |
| 54 | |
| 55 | $a = random (100,100); |
| 56 | $sqrt = msqrt($a); |
| 57 | |
| 58 | =head1 DESCRIPTION |
| 59 | |
| 60 | This module provides some transcendental functions for matrices. |
| 61 | Moreover it provides sec, asec, sech, asech, cot, acot, acoth, coth, csc, |
| 62 | acsc, csch, acsch. Beware, importing this module will overwrite the hidden |
| 63 | PDL routine sec. If you need to call it specify its origin module : PDL::Basic::sec(args) |
| 64 | |
| 65 | =cut |
| 66 | #line 67 "lib/PDL/LinearAlgebra/Trans.pm" |
| 67 | |
| 68 | |
| 69 | =head1 FUNCTIONS |
| 70 | |
| 71 | =cut |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | =head2 geexp |
| 79 | |
| 80 | =for sig |
| 81 | |
| 82 | Signature: ([io]A(n,n);int deg();scale();[io]trace();int [o]ns();int [o]info(); int [t]ipiv(n); [t]wsp(wspn=CALC(3*$SIZE(n)*$SIZE(n)))) |
| 83 | Types: (double) |
| 84 | |
| 85 | =for usage |
| 86 | |
| 87 | ($ns, $info) = geexp($A, $deg, $scale, $trace); |
| 88 | geexp($A, $deg, $scale, $trace, $ns, $info); # all arguments given |
| 89 | ($ns, $info) = $A->geexp($deg, $scale, $trace); # method call |
| 90 | $A->geexp($deg, $scale, $trace, $ns, $info); |
| 91 | |
| 92 | =for ref |
| 93 | |
| 94 | Computes exp(t*A), the matrix exponential of a general matrix, |
| 95 | using the irreducible rational Pade approximation to the |
| 96 | exponential function exp(x) = r(x) = (+/-)( I + 2*(q(x)/p(x)) ), |
| 97 | combined with scaling-and-squaring and optionally normalization of the trace. |
| 98 | The algorithm is described in Roger B. Sidje ([email protected]) |
| 99 | "EXPOKIT: Software Package for Computing Matrix Exponentials". |
| 100 | ACM - Transactions On Mathematical Software, 24(1):130-156, 1998 |
| 101 | |
| 102 | A: On input argument matrix. On output exp(t*A). |
| 103 | Use Fortran storage type. |
| 104 | |
| 105 | deg: the degre of the diagonal Pade to be used. |
| 106 | a value of 6 is generally satisfactory. |
| 107 | |
| 108 | scale: time-scale (can be < 0). |
| 109 | |
| 110 | trace: on input, boolean value indicating whether or not perform |
| 111 | a trace normalization. On output value used. |
| 112 | |
| 113 | ns: on output number of scaling-squaring used. |
| 114 | |
| 115 | info: exit flag. |
| 116 | 0 - no problem |
| 117 | > 0 - Singularity in LU factorization when solving |
| 118 | Pade approximation |
| 119 | |
| 120 | =for example |
| 121 | |
| 122 | $a = random(5,5); |
| 123 | $trace = pdl(1); |
| 124 | $a->t->geexp(6,1,$trace, ($ns = null), ($info = null)); |
| 125 | |
| 126 | =pod |
| 127 | |
| 128 | Broadcasts over its inputs. |
| 129 | |
| 130 | =for bad |
| 131 | |
| 132 | C<geexp> does not process bad values. |
| 133 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. |
| 134 | |
| 135 | =cut |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | *geexp = \&PDL::geexp; |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | =head2 cgeexp |
| 148 | |
| 149 | =for sig |
| 150 | |
| 151 | Signature: (complex [io]A(n,n);int deg();scale();int trace();int [o]ns();int [o]info(); int [t] ipiv(n)) |
| 152 | Types: (double) |
| 153 | |
| 154 | =for usage |
| 155 | |
| 156 | ($ns, $info) = cgeexp($A, $deg, $scale, $trace); |
| 157 | cgeexp($A, $deg, $scale, $trace, $ns, $info); # all arguments given |
| 158 | ($ns, $info) = $A->cgeexp($deg, $scale, $trace); # method call |
| 159 | $A->cgeexp($deg, $scale, $trace, $ns, $info); |
| 160 | |
| 161 | =for ref |
| 162 | |
| 163 | Complex version of geexp. The value used for trace normalization is not returned. |
| 164 | The algorithm is described in Roger B. Sidje ([email protected]) |
| 165 | "EXPOKIT: Software Package for Computing Matrix Exponentials". |
| 166 | ACM - Transactions On Mathematical Software, 24(1):130-156, 1998 |
| 167 | |
| 168 | =pod |
| 169 | |
| 170 | Broadcasts over its inputs. |
| 171 | |
| 172 | =for bad |
| 173 | |
| 174 | C<cgeexp> does not process bad values. |
| 175 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. |
| 176 | |
| 177 | =cut |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | *cgeexp = \&PDL::cgeexp; |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | =head2 ctrsqrt |
| 190 | |
| 191 | =for sig |
| 192 | |
| 193 | Signature: (complex [io]A(n,n);int uplo();complex [o] B(n,n);int [o]info()) |
| 194 | Types: (double) |
| 195 | |
| 196 | =for usage |
| 197 | |
| 198 | ($B, $info) = ctrsqrt($A, $uplo); |
| 199 | ctrsqrt($A, $uplo, $B, $info); # all arguments given |
| 200 | ($B, $info) = $A->ctrsqrt($uplo); # method call |
| 201 | $A->ctrsqrt($uplo, $B, $info); |
| 202 | |
| 203 | =for ref |
| 204 | |
| 205 | Root square of complex triangular matrix. Uses a recurrence of Björck and Hammarling. |
| 206 | If uplo is true, A is lower triangular. |
| 207 | |
| 208 | See Nicholas J. Higham, A new sqrtm for MATLAB. Numerical Analysis |
| 209 | Report No. 336, Manchester Centre for Computational Mathematics, |
| 210 | Manchester, England, January 1999, available at L<https://web.archive.org/web/20210420221902/https://www.maths.manchester.ac.uk/~higham/papers/narep336.pdf> |
| 211 | |
| 212 | =pod |
| 213 | |
| 214 | Broadcasts over its inputs. |
| 215 | |
| 216 | =for bad |
| 217 | |
| 218 | C<ctrsqrt> does not process bad values. |
| 219 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. |
| 220 | |
| 221 | =cut |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | *ctrsqrt = \&PDL::ctrsqrt; |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | =head2 ctrfun |
| 234 | |
| 235 | =for sig |
| 236 | |
| 237 | Signature: (complex [io]A(n,n);int uplo();complex [o] B(n,n);int [o]info(); complex [t]diag(n); SV* func) |
| 238 | Types: (double) |
| 239 | |
| 240 | =for usage |
| 241 | |
| 242 | ($B, $info) = ctrfun($A, $uplo, $func); |
| 243 | ctrfun($A, $uplo, $B, $info, $func); # all arguments given |
| 244 | ($B, $info) = $A->ctrfun($uplo, $func); # method call |
| 245 | $A->ctrfun($uplo, $B, $info, $func); |
| 246 | |
| 247 | =for ref |
| 248 | |
| 249 | Apply an arbitrary function to a complex triangular matrix. Uses a recurrence of Parlett. |
| 250 | If uplo is true, A is lower triangular. |
| 251 | |
| 252 | =pod |
| 253 | |
| 254 | Broadcasts over its inputs. |
| 255 | |
| 256 | =for bad |
| 257 | |
| 258 | C<ctrfun> does not process bad values. |
| 259 | It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. |
| 260 | |
| 261 | =cut |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | *ctrfun = \&PDL::ctrfun; |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | #line 772 "lib/PDL/LinearAlgebra/Trans.pd" |
| 273 | |
| 274 | my $pi; |
| 275 | BEGIN { $pi = pdl(3.1415926535897932384626433832795029) } |
| 276 | sub pi () { $pi->copy }; |
| 277 | |
| 278 | *sec = \&PDL::sec; |
| 279 | sub PDL::sec{1/cos($_[0])} |
| 280 | |
| 281 | *csc = \&PDL::csc; |
| 282 | sub PDL::csc($) {1/sin($_[0])} |
| 283 | |
| 284 | *cot = \&PDL::cot; |
| 285 | sub PDL::cot($) {1/(sin($_[0])/cos($_[0]))} |
| 286 | |
| 287 | *sech = \&PDL::sech; |
| 288 | sub PDL::sech($){1/pdl($_[0])->cosh} |
| 289 | |
| 290 | *csch = \&PDL::csch; |
| 291 | sub PDL::csch($) {1/pdl($_[0])->sinh} |
| 292 | |
| 293 | *coth = \&PDL::coth; |
| 294 | sub PDL::coth($) {1/pdl($_[0])->tanh} |
| 295 | |
| 296 | *asec = \&PDL::asec; |
| 297 | sub PDL::asec($) {my $tmp = 1/pdl($_[0]) ; $tmp->acos} |
| 298 | |
| 299 | *acsc = \&PDL::acsc; |
| 300 | sub PDL::acsc($) {my $tmp = 1/pdl($_[0]) ; $tmp->asin} |
| 301 | |
| 302 | *acot = \&PDL::acot; |
| 303 | sub PDL::acot($) {my $tmp = 1/pdl($_[0]) ; $tmp->atan} |
| 304 | |
| 305 | *asech = \&PDL::asech; |
| 306 | sub PDL::asech($) {my $tmp = 1/pdl($_[0]) ; $tmp->acosh} |
| 307 | |
| 308 | *acsch = \&PDL::acsch; |
| 309 | sub PDL::acsch($) {my $tmp = 1/pdl($_[0]) ; $tmp->asinh} |
| 310 | |
| 311 | *acoth = \&PDL::acoth; |
| 312 | sub PDL::acoth($) {my $tmp = 1/pdl($_[0]) ; $tmp->atanh} |
| 313 | |
| 314 | my $_tol = 9.99999999999999e-15; |
| 315 | |
| 316 | sub toreal { |
| 317 | $_tol = $_[1] if defined $_[1]; |
| 318 | return $_[0] if $_[0]->isempty or $_[0]->type->real; |
| 319 | my ($min, $max, $tmp); |
| 320 | ($min, $max) = $_[0]->im->minmax; |
| 321 | return $_[0]->re->sever unless (abs($min) > $_tol || abs($max) > $_tol); |
| 322 | $_[0]; |
| 323 | } |
| 324 | |
| 325 | =head2 mlog |
| 326 | |
| 327 | =for ref |
| 328 | |
| 329 | Return matrix logarithm of a square matrix. |
| 330 | |
| 331 | =for usage |
| 332 | |
| 333 | PDL = mlog(PDL(A)) |
| 334 | |
| 335 | =for example |
| 336 | |
| 337 | my $a = random(10,10); |
| 338 | my $log = mlog($a); |
| 339 | |
| 340 | =cut |
| 341 | |
| 342 | *mlog = \&PDL::mlog; |
| 343 | sub PDL::mlog { |
| 344 | &PDL::LinearAlgebra::_square; |
| 345 | my ($m, $tol) = @_; |
| 346 | mfun($m, sub{$_[0].=log $_[0]} , 0, $tol); |
| 347 | } |
| 348 | |
| 349 | =head2 msqrt |
| 350 | |
| 351 | =for ref |
| 352 | |
| 353 | Return matrix square root (principal) of a square matrix. |
| 354 | |
| 355 | =for usage |
| 356 | |
| 357 | PDL = msqrt(PDL(A)) |
| 358 | |
| 359 | =for example |
| 360 | |
| 361 | my $a = random(10,10); |
| 362 | my $sqrt = msqrt($a); |
| 363 | |
| 364 | =cut |
| 365 | |
| 366 | *msqrt = \&PDL::msqrt; |
| 367 | |
| 368 | sub PDL::msqrt { |
| 369 | &PDL::LinearAlgebra::_square; |
| 370 | my ($m, $tol) = @_; |
| 371 | $m = $m->r2C unless $m->_is_complex; |
| 372 | my ($t, undef, $z, undef, $info) = $m->mschur(1); |
| 373 | if ($info){ |
| 374 | warn "msqrt: Can't compute Schur form\n"; |
| 375 | return; |
| 376 | } |
| 377 | ($t, $info) = $t->ctrsqrt(0); |
| 378 | if($info){ |
| 379 | warn "msqrt: can't compute square root\n"; |
| 380 | return; |
| 381 | } |
| 382 | $m = $z x $t x $z->t(1); |
| 383 | return $m->_is_complex ? $m : toreal($m, $tol); |
| 384 | } |
| 385 | |
| 386 | =head2 mexp |
| 387 | |
| 388 | =for ref |
| 389 | |
| 390 | Return matrix exponential of a square matrix. |
| 391 | |
| 392 | =for usage |
| 393 | |
| 394 | PDL = mexp(PDL(A)) |
| 395 | |
| 396 | =for example |
| 397 | |
| 398 | my $a = random(10,10); |
| 399 | my $exp = mexp($a); |
| 400 | |
| 401 | =cut |
| 402 | |
| 403 | *mexp = \&PDL::mexp; |
| 404 | sub PDL::mexp { |
| 405 | &PDL::LinearAlgebra::_square; |
| 406 | my ($m, $order, $trace) = @_; |
| 407 | $trace = 1 unless defined $trace; |
| 408 | $order = 6 unless defined $order; |
| 409 | $m = $m->copy; |
| 410 | $m->t->_call_method('geexp', $order, 1, $trace, my $ns = PDL->null, my $info = PDL->null); |
| 411 | if ($info){ |
| 412 | warn "mexp: Error $info"; |
| 413 | } |
| 414 | else{ |
| 415 | return $m; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | *mexpts = \&PDL::mexpts; |
| 420 | sub PDL::mexpts { |
| 421 | &PDL::LinearAlgebra::_square; |
| 422 | my ($m, $order, $tol) = @_; |
| 423 | my @dims = $m->dims; |
| 424 | my ($em, $trm); |
| 425 | $order = 20 unless defined $order; |
| 426 | $em = $m->_is_complex ? diag(r2C(ones($dims[1]))) : diag(ones($dims[1])); |
| 427 | $trm = $em->copy; |
| 428 | for (1..($order - 1)){ |
| 429 | $trm = $trm x ($m / $_); |
| 430 | $em += $trm; |
| 431 | } |
| 432 | return $m->_is_complex ? $em : toreal($em, $tol); |
| 433 | } |
| 434 | |
| 435 | =head2 mpow |
| 436 | |
| 437 | =for ref |
| 438 | |
| 439 | Return matrix power of a square matrix. |
| 440 | |
| 441 | =for usage |
| 442 | |
| 443 | PDL = mpow(PDL(A), SCALAR(exponent)) |
| 444 | |
| 445 | =for example |
| 446 | |
| 447 | my $a = random(10,10); |
| 448 | my $powered = mpow($a,2.5); |
| 449 | |
| 450 | =cut |
| 451 | |
| 452 | *mpow = \&PDL::mpow; |
| 453 | sub PDL::mpow { |
| 454 | &PDL::LinearAlgebra::_square; |
| 455 | my ($m, $power, $tol, $eigen) = @_; |
| 456 | my @dims = $m->dims; |
| 457 | my $ret; |
| 458 | if (UNIVERSAL::isa($power,'PDL') and $power->dims > 1){ |
| 459 | my ($e, $v) = $m->meigen(0,1); |
| 460 | $ret = $v * ($e**$power) x $v->minv; |
| 461 | } |
| 462 | elsif( 1/$dims[0] * 1000 > abs($power) and !$eigen){ |
| 463 | $ret = identity($dims[0]); |
| 464 | $ret = $ret->r2C if $m->_is_complex; |
| 465 | my $pow = floor($power); |
| 466 | $pow++ if ($power < 0 and $power != $pow); |
| 467 | # TODO: what a beautiful thing (is it a game ?) |
| 468 | for(my $i = 0; $i < abs($pow); $i++){$ret x= $m;} |
| 469 | $ret = $ret->minv if $power < 0; |
| 470 | if ($power = $power - $pow){ |
| 471 | if($power == 0.5){ |
| 472 | my $v = $m->msqrt; |
| 473 | $ret = ($pow == 0) ? $v : $ret x $v; |
| 474 | } |
| 475 | else{ |
| 476 | my ($e, $v) = $m->meigen(0,1); |
| 477 | $ret = ($pow == 0) ? ($v * $e**$power x $v->minv) : $ret->r2C x ($v * $e**$power x $v->minv); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | else{ |
| 482 | my ($e, $v) = $m->meigen(0,1); |
| 483 | $ret = $v * $e**$power x $v->minv; |
| 484 | } |
| 485 | return $m->_is_complex ? $ret : toreal($ret, $tol); |
| 486 | } |
| 487 | |
| 488 | =head2 mcos |
| 489 | |
| 490 | =for ref |
| 491 | |
| 492 | Return matrix cosine of a square matrix. |
| 493 | |
| 494 | =for usage |
| 495 | |
| 496 | PDL = mcos(PDL(A)) |
| 497 | |
| 498 | =for example |
| 499 | |
| 500 | my $a = random(10,10); |
| 501 | my $cos = mcos($a); |
| 502 | |
| 503 | =cut |
| 504 | |
| 505 | sub _i { |
| 506 | i(); |
| 507 | } |
| 508 | |
| 509 | *mcos = \&PDL::mcos; |
| 510 | sub PDL::mcos { |
| 511 | &PDL::LinearAlgebra::_square; |
| 512 | my $m = shift; |
| 513 | my $i = _i(); |
| 514 | return $m->_is_complex ? (mexp($i*$m) + mexp(- $i*$m)) / 2 : mexp($i*$m)->re->sever; |
| 515 | } |
| 516 | |
| 517 | =head2 macos |
| 518 | |
| 519 | =for ref |
| 520 | |
| 521 | Return matrix inverse cosine of a square matrix. |
| 522 | |
| 523 | =for usage |
| 524 | |
| 525 | PDL = macos(PDL(A)) |
| 526 | |
| 527 | =for example |
| 528 | |
| 529 | my $a = random(10,10); |
| 530 | my $acos = macos($a); |
| 531 | |
| 532 | =cut |
| 533 | |
| 534 | *macos = \&PDL::macos; |
| 535 | sub PDL::macos { |
| 536 | &PDL::LinearAlgebra::_square; |
| 537 | my ($m, $tol) = @_; |
| 538 | my @dims = $m->dims; |
| 539 | my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 540 | my $i = _i(); |
| 541 | my $ret = $i * mlog( ($m->r2C - $i * msqrt( ($id - $m x $m), $tol))); |
| 542 | return $m->_is_complex ? $ret : toreal($ret, $tol); |
| 543 | } |
| 544 | |
| 545 | =head2 msin |
| 546 | |
| 547 | =for ref |
| 548 | |
| 549 | Return matrix sine of a square matrix. |
| 550 | |
| 551 | =for usage |
| 552 | |
| 553 | PDL = msin(PDL(A)) |
| 554 | |
| 555 | =for example |
| 556 | |
| 557 | my $a = random(10,10); |
| 558 | my $sin = msin($a); |
| 559 | |
| 560 | =cut |
| 561 | |
| 562 | *msin = \&PDL::msin; |
| 563 | sub PDL::msin { |
| 564 | &PDL::LinearAlgebra::_square; |
| 565 | my $m = shift; |
| 566 | my $i = _i(); |
| 567 | $m->_is_complex ? (mexp($i*$m) - mexp(- $i*$m))/(2*$i) : mexp($i*$m)->im->sever; |
| 568 | } |
| 569 | |
| 570 | =head2 masin |
| 571 | |
| 572 | =for ref |
| 573 | |
| 574 | Return matrix inverse sine of a square matrix. |
| 575 | |
| 576 | =for usage |
| 577 | |
| 578 | PDL = masin(PDL(A)) |
| 579 | |
| 580 | =for example |
| 581 | |
| 582 | my $a = random(10,10); |
| 583 | my $asin = masin($a); |
| 584 | |
| 585 | =cut |
| 586 | |
| 587 | *masin = \&PDL::masin; |
| 588 | sub PDL::masin { |
| 589 | &PDL::LinearAlgebra::_square; |
| 590 | my ($m, $tol) = @_; |
| 591 | my @dims = $m->dims; |
| 592 | my $i = _i(); |
| 593 | my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 594 | my $ret = (-$i) * mlog((($i*$m) + msqrt($id - $m x $m, $tol))); |
| 595 | return $m->_is_complex ? $ret : toreal($ret, $tol); |
| 596 | } |
| 597 | |
| 598 | =head2 mtan |
| 599 | |
| 600 | =for ref |
| 601 | |
| 602 | Return matrix tangent of a square matrix. |
| 603 | |
| 604 | =for usage |
| 605 | |
| 606 | PDL = mtan(PDL(A)) |
| 607 | |
| 608 | =for example |
| 609 | |
| 610 | my $a = random(10,10); |
| 611 | my $tan = mtan($a); |
| 612 | |
| 613 | =cut |
| 614 | |
| 615 | *mtan = \&PDL::mtan; |
| 616 | sub PDL::mtan { |
| 617 | &PDL::LinearAlgebra::_square; |
| 618 | my ($m, $id) = @_; |
| 619 | my @dims = $m->dims; |
| 620 | return scalar msolvex(mcos($m), msin($m),equilibrate=>1) unless $id; |
| 621 | my $i = _i(); |
| 622 | if ($m->_is_complex){ |
| 623 | $id = identity($dims[0])->r2C; |
| 624 | $m = mexp(-2*$i*$m); |
| 625 | return scalar msolvex( ($id + $m ),( (- $i) * ($id - $m)),equilibrate=>1); |
| 626 | } |
| 627 | else{ |
| 628 | $m = mexp($i * $m); |
| 629 | return scalar $m->re->msolvex($m->im,equilibrate=>1); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | =head2 matan |
| 634 | |
| 635 | =for ref |
| 636 | |
| 637 | Return matrix inverse tangent of a square matrix. |
| 638 | |
| 639 | =for usage |
| 640 | |
| 641 | PDL = matan(PDL(A)) |
| 642 | |
| 643 | =for example |
| 644 | |
| 645 | my $a = random(10,10); |
| 646 | my $atan = matan($a); |
| 647 | |
| 648 | =cut |
| 649 | |
| 650 | *matan = \&PDL::matan; |
| 651 | sub PDL::matan { |
| 652 | &PDL::LinearAlgebra::_square; |
| 653 | my ($m, $tol) = @_; |
| 654 | my @dims = $m->dims; |
| 655 | my $i = _i(); |
| 656 | my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 657 | my $ret = -$i/2 * mlog( scalar PDL::msolvex( ($id - $i*$m) ,($id + $i*$m),equilibrate=>1 )); |
| 658 | return $m->_is_complex ? $ret : toreal($ret, $tol); |
| 659 | } |
| 660 | |
| 661 | =head2 mcot |
| 662 | |
| 663 | =for ref |
| 664 | |
| 665 | Return matrix cotangent of a square matrix. |
| 666 | |
| 667 | =for usage |
| 668 | |
| 669 | PDL = mcot(PDL(A)) |
| 670 | |
| 671 | =for example |
| 672 | |
| 673 | my $a = random(10,10); |
| 674 | my $cot = mcot($a); |
| 675 | |
| 676 | =cut |
| 677 | |
| 678 | *mcot = \&PDL::mcot; |
| 679 | sub PDL::mcot { |
| 680 | &PDL::LinearAlgebra::_square; |
| 681 | my ($m, $id) = @_; |
| 682 | my @dims = $m->dims; |
| 683 | return scalar msolvex(msin($m),mcos($m),equilibrate=>1) unless $id; |
| 684 | my $i = _i(); |
| 685 | if ($m->_is_complex){ |
| 686 | $id = identity($dims[0])->r2C; |
| 687 | $m = mexp(-2*$i*$m); |
| 688 | return scalar msolvex( ($id - $m), ($i * ($id + $m)), equilibrate=>1); |
| 689 | } |
| 690 | else{ |
| 691 | $m = mexp($i * $m); |
| 692 | return scalar $m->im->msolvex($m->re,equilibrate=>1); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | =head2 macot |
| 697 | |
| 698 | =for ref |
| 699 | |
| 700 | Return matrix inverse cotangent of a square matrix. |
| 701 | |
| 702 | =for usage |
| 703 | |
| 704 | PDL = macot(PDL(A)) |
| 705 | |
| 706 | =for example |
| 707 | |
| 708 | my $a = random(10,10); |
| 709 | my $acot = macot($a); |
| 710 | |
| 711 | =cut |
| 712 | |
| 713 | *macot = \&PDL::macot; |
| 714 | sub PDL::macot { |
| 715 | &PDL::LinearAlgebra::_square; |
| 716 | my ($m, $tol, $id) = @_; |
| 717 | my ($inv, $info) = $m->minv; |
| 718 | if ($info){ |
| 719 | warn "macot: singular matrix"; |
| 720 | return; |
| 721 | } |
| 722 | return matan($inv,$tol); |
| 723 | } |
| 724 | |
| 725 | =head2 msec |
| 726 | |
| 727 | =for ref |
| 728 | |
| 729 | Return matrix secant of a square matrix. |
| 730 | |
| 731 | =for usage |
| 732 | |
| 733 | PDL = msec(PDL(A)) |
| 734 | |
| 735 | =for example |
| 736 | |
| 737 | my $a = random(10,10); |
| 738 | my $sec = msec($a); |
| 739 | |
| 740 | =cut |
| 741 | |
| 742 | *msec = \&PDL::msec; |
| 743 | sub PDL::msec { |
| 744 | &PDL::LinearAlgebra::_square; |
| 745 | my $m = shift; |
| 746 | my $i = _i(); |
| 747 | return $m->_is_complex ? PDL::minv(mexp($i*$m) + mexp(- $i*$m)) * 2 : scalar PDL::minv(re(mexp($i*$m))); |
| 748 | } |
| 749 | |
| 750 | =head2 masec |
| 751 | |
| 752 | =for ref |
| 753 | |
| 754 | Return matrix inverse secant of a square matrix. |
| 755 | |
| 756 | =for usage |
| 757 | |
| 758 | PDL = masec(PDL(A)) |
| 759 | |
| 760 | =for example |
| 761 | |
| 762 | my $a = random(10,10); |
| 763 | my $asec = masec($a); |
| 764 | |
| 765 | =cut |
| 766 | |
| 767 | *masec = \&PDL::masec; |
| 768 | sub PDL::masec { |
| 769 | &PDL::LinearAlgebra::_square; |
| 770 | my ($m, $tol) = @_; |
| 771 | my ($inv, $info) = $m->minv; |
| 772 | if ($info){ |
| 773 | warn "masec: singular matrix"; |
| 774 | return; |
| 775 | } |
| 776 | return macos($inv,$tol); |
| 777 | } |
| 778 | |
| 779 | =head2 mcsc |
| 780 | |
| 781 | =for ref |
| 782 | |
| 783 | Return matrix cosecant of a square matrix. |
| 784 | |
| 785 | =for usage |
| 786 | |
| 787 | PDL = mcsc(PDL(A)) |
| 788 | |
| 789 | =for example |
| 790 | |
| 791 | my $a = random(10,10); |
| 792 | my $csc = mcsc($a); |
| 793 | |
| 794 | =cut |
| 795 | |
| 796 | *mcsc = \&PDL::mcsc; |
| 797 | sub PDL::mcsc { |
| 798 | &PDL::LinearAlgebra::_square; |
| 799 | my $m = shift; |
| 800 | my $i = _i(); |
| 801 | $m->_is_complex ? PDL::minv(mexp($i*$m) - mexp(- $i*$m)) * 2*$i : scalar PDL::minv(im(mexp($i*$m))); |
| 802 | } |
| 803 | |
| 804 | =head2 macsc |
| 805 | |
| 806 | =for ref |
| 807 | |
| 808 | Return matrix inverse cosecant of a square matrix. |
| 809 | |
| 810 | =for usage |
| 811 | |
| 812 | PDL = macsc(PDL(A)) |
| 813 | |
| 814 | =for example |
| 815 | |
| 816 | my $a = random(10,10); |
| 817 | my $acsc = macsc($a); |
| 818 | |
| 819 | =cut |
| 820 | |
| 821 | *macsc = \&PDL::macsc; |
| 822 | sub PDL::macsc { |
| 823 | &PDL::LinearAlgebra::_square; |
| 824 | my ($m, $tol) = @_; |
| 825 | my ($inv, $info) = $m->minv; |
| 826 | if ($info){ |
| 827 | warn "macsc: singular matrix"; |
| 828 | return; |
| 829 | } |
| 830 | return masin($inv,$tol); |
| 831 | } |
| 832 | |
| 833 | =head2 mcosh |
| 834 | |
| 835 | =for ref |
| 836 | |
| 837 | Return matrix hyperbolic cosine of a square matrix. |
| 838 | |
| 839 | =for usage |
| 840 | |
| 841 | PDL = mcosh(PDL(A)) |
| 842 | |
| 843 | =for example |
| 844 | |
| 845 | my $a = random(10,10); |
| 846 | my $cos = mcosh($a); |
| 847 | |
| 848 | =cut |
| 849 | |
| 850 | *mcosh = \&PDL::mcosh; |
| 851 | |
| 852 | sub PDL::mcosh { |
| 853 | &PDL::LinearAlgebra::_square; |
| 854 | my $m = shift; |
| 855 | ( $m->mexp + mexp(-$m) )/2; |
| 856 | } |
| 857 | |
| 858 | =head2 macosh |
| 859 | |
| 860 | =for ref |
| 861 | |
| 862 | Return matrix hyperbolic inverse cosine of a square matrix. |
| 863 | |
| 864 | =for usage |
| 865 | |
| 866 | PDL = macosh(PDL(A)) |
| 867 | |
| 868 | =for example |
| 869 | |
| 870 | my $a = random(10,10); |
| 871 | my $acos = macosh($a); |
| 872 | |
| 873 | =cut |
| 874 | |
| 875 | *macosh = \&PDL::macosh; |
| 876 | |
| 877 | sub PDL::macosh { |
| 878 | &PDL::LinearAlgebra::_square; |
| 879 | my ($m, $tol) = @_; |
| 880 | my @dims = $m->dims; |
| 881 | my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 882 | my $ret = msqrt($m x $m - $id); |
| 883 | $m = $m->r2C if $ret->getndims > @dims; |
| 884 | mlog($m + $ret, $tol); |
| 885 | } |
| 886 | |
| 887 | =head2 msinh |
| 888 | |
| 889 | =for ref |
| 890 | |
| 891 | Return matrix hyperbolic sine of a square matrix. |
| 892 | |
| 893 | =for usage |
| 894 | |
| 895 | PDL = msinh(PDL(A)) |
| 896 | |
| 897 | =for example |
| 898 | |
| 899 | my $a = random(10,10); |
| 900 | my $sinh = msinh($a); |
| 901 | |
| 902 | =cut |
| 903 | |
| 904 | *msinh = \&PDL::msinh; |
| 905 | |
| 906 | sub PDL::msinh { |
| 907 | &PDL::LinearAlgebra::_square; |
| 908 | my $m = shift; |
| 909 | ( $m->mexp - mexp(-$m) )/2; |
| 910 | } |
| 911 | |
| 912 | =head2 masinh |
| 913 | |
| 914 | =for ref |
| 915 | |
| 916 | Return matrix hyperbolic inverse sine of a square matrix. |
| 917 | |
| 918 | =for usage |
| 919 | |
| 920 | PDL = masinh(PDL(A)) |
| 921 | |
| 922 | =for example |
| 923 | |
| 924 | my $a = random(10,10); |
| 925 | my $asinh = masinh($a); |
| 926 | |
| 927 | =cut |
| 928 | |
| 929 | *masinh = \&PDL::masinh; |
| 930 | |
| 931 | sub PDL::masinh { |
| 932 | &PDL::LinearAlgebra::_square; |
| 933 | my ($m, $tol) = @_; |
| 934 | my @dims = $m->dims; |
| 935 | my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 936 | my $ret = msqrt($m x $m + $id); |
| 937 | $m = $m->r2C if $ret->getndims > @dims; |
| 938 | mlog(($m + $ret), $tol); |
| 939 | } |
| 940 | |
| 941 | =head2 mtanh |
| 942 | |
| 943 | =for ref |
| 944 | |
| 945 | Return matrix hyperbolic tangent of a square matrix. |
| 946 | |
| 947 | =for usage |
| 948 | |
| 949 | PDL = mtanh(PDL(A)) |
| 950 | |
| 951 | =for example |
| 952 | |
| 953 | my $a = random(10,10); |
| 954 | my $tanh = mtanh($a); |
| 955 | |
| 956 | =cut |
| 957 | |
| 958 | *mtanh = \&PDL::mtanh; |
| 959 | |
| 960 | sub PDL::mtanh { |
| 961 | &PDL::LinearAlgebra::_square; |
| 962 | my ($m, $id) = @_; |
| 963 | my @dims = $m->dims; |
| 964 | return scalar msolvex(mcosh($m), msinh($m),equilibrate=>1) unless $id; |
| 965 | $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 966 | $m = mexp(-2*$m); |
| 967 | return scalar msolvex( ($id + $m ),($id - $m), equilibrate=>1); |
| 968 | } |
| 969 | |
| 970 | =head2 matanh |
| 971 | |
| 972 | =for ref |
| 973 | |
| 974 | Return matrix hyperbolic inverse tangent of a square matrix. |
| 975 | |
| 976 | =for usage |
| 977 | |
| 978 | PDL = matanh(PDL(A)) |
| 979 | |
| 980 | =for example |
| 981 | |
| 982 | my $a = random(10,10); |
| 983 | my $atanh = matanh($a); |
| 984 | |
| 985 | =cut |
| 986 | |
| 987 | *matanh = \&PDL::matanh; |
| 988 | |
| 989 | sub PDL::matanh { |
| 990 | &PDL::LinearAlgebra::_square; |
| 991 | my ($m, $tol) = @_; |
| 992 | my @dims = $m->dims; |
| 993 | my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 994 | mlog( scalar msolvex( ($id - $m ),($id + $m),equilibrate=>1), $tol ) / 2; |
| 995 | } |
| 996 | |
| 997 | =head2 mcoth |
| 998 | |
| 999 | =for ref |
| 1000 | |
| 1001 | Return matrix hyperbolic cotangent of a square matrix. |
| 1002 | |
| 1003 | =for usage |
| 1004 | |
| 1005 | PDL = mcoth(PDL(A)) |
| 1006 | |
| 1007 | =for example |
| 1008 | |
| 1009 | my $a = random(10,10); |
| 1010 | my $coth = mcoth($a); |
| 1011 | |
| 1012 | =cut |
| 1013 | |
| 1014 | *mcoth = \&PDL::mcoth; |
| 1015 | |
| 1016 | sub PDL::mcoth { |
| 1017 | &PDL::LinearAlgebra::_square; |
| 1018 | my ($m, $id) = @_; |
| 1019 | my @dims = $m->dims; |
| 1020 | scalar msolvex(msinh($m), mcosh($m),equilibrate=>1) unless $id; |
| 1021 | $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 1022 | $m = mexp(-2*$m); |
| 1023 | return scalar msolvex( ($id - $m ),($id + $m),equilibrate=>1); |
| 1024 | } |
| 1025 | |
| 1026 | =head2 macoth |
| 1027 | |
| 1028 | =for ref |
| 1029 | |
| 1030 | Return matrix hyperbolic inverse cotangent of a square matrix. |
| 1031 | |
| 1032 | =for usage |
| 1033 | |
| 1034 | PDL = macoth(PDL(A)) |
| 1035 | |
| 1036 | =for example |
| 1037 | |
| 1038 | my $a = random(10,10); |
| 1039 | my $acoth = macoth($a); |
| 1040 | |
| 1041 | =cut |
| 1042 | |
| 1043 | *macoth = \&PDL::macoth; |
| 1044 | |
| 1045 | sub PDL::macoth { |
| 1046 | &PDL::LinearAlgebra::_square; |
| 1047 | my ($m, $tol) = @_; |
| 1048 | my ($inv, $info) = $m->minv; |
| 1049 | if ($info){ |
| 1050 | warn "macoth: singular matrix"; |
| 1051 | return; |
| 1052 | } |
| 1053 | return matanh($inv,$tol); |
| 1054 | } |
| 1055 | |
| 1056 | =head2 msech |
| 1057 | |
| 1058 | =for ref |
| 1059 | |
| 1060 | Return matrix hyperbolic secant of a square matrix. |
| 1061 | |
| 1062 | =for usage |
| 1063 | |
| 1064 | PDL = msech(PDL(A)) |
| 1065 | |
| 1066 | =for example |
| 1067 | |
| 1068 | my $a = random(10,10); |
| 1069 | my $sech = msech($a); |
| 1070 | |
| 1071 | =cut |
| 1072 | |
| 1073 | *msech = \&PDL::msech; |
| 1074 | |
| 1075 | sub PDL::msech { |
| 1076 | &PDL::LinearAlgebra::_square; |
| 1077 | my $m = shift; |
| 1078 | PDL::minv( $m->mexp + mexp(-$m) ) * 2; |
| 1079 | } |
| 1080 | |
| 1081 | =head2 masech |
| 1082 | |
| 1083 | =for ref |
| 1084 | |
| 1085 | Return matrix hyperbolic inverse secant of a square matrix. |
| 1086 | |
| 1087 | =for usage |
| 1088 | |
| 1089 | PDL = masech(PDL(A)) |
| 1090 | |
| 1091 | =for example |
| 1092 | |
| 1093 | my $a = random(10,10); |
| 1094 | my $asech = masech($a); |
| 1095 | |
| 1096 | =cut |
| 1097 | |
| 1098 | *masech = \&PDL::masech; |
| 1099 | |
| 1100 | sub PDL::masech { |
| 1101 | &PDL::LinearAlgebra::_square; |
| 1102 | my ($m, $tol) = @_; |
| 1103 | my ($inv, $info) = $m->minv; |
| 1104 | if ($info){ |
| 1105 | warn "masech: singular matrix"; |
| 1106 | return; |
| 1107 | } |
| 1108 | return macosh($inv,$tol); |
| 1109 | } |
| 1110 | |
| 1111 | =head2 mcsch |
| 1112 | |
| 1113 | =for ref |
| 1114 | |
| 1115 | Return matrix hyperbolic cosecant of a square matrix. |
| 1116 | |
| 1117 | =for usage |
| 1118 | |
| 1119 | PDL = mcsch(PDL(A)) |
| 1120 | |
| 1121 | =for example |
| 1122 | |
| 1123 | my $a = random(10,10); |
| 1124 | my $csch = mcsch($a); |
| 1125 | |
| 1126 | =cut |
| 1127 | |
| 1128 | *mcsch = \&PDL::mcsch; |
| 1129 | |
| 1130 | sub PDL::mcsch { |
| 1131 | &PDL::LinearAlgebra::_square; |
| 1132 | my $m = shift; |
| 1133 | PDL::minv( $m->mexp - mexp(-$m) ) * 2; |
| 1134 | } |
| 1135 | |
| 1136 | =head2 macsch |
| 1137 | |
| 1138 | =for ref |
| 1139 | |
| 1140 | Return matrix hyperbolic inverse cosecant of a square matrix. |
| 1141 | |
| 1142 | =for usage |
| 1143 | |
| 1144 | PDL = macsch(PDL(A)) |
| 1145 | |
| 1146 | =for example |
| 1147 | |
| 1148 | my $a = random(10,10); |
| 1149 | my $acsch = macsch($a); |
| 1150 | |
| 1151 | =cut |
| 1152 | |
| 1153 | *macsch = \&PDL::macsch; |
| 1154 | |
| 1155 | sub PDL::macsch { |
| 1156 | &PDL::LinearAlgebra::_square; |
| 1157 | my ($m, $tol) = @_; |
| 1158 | my ($inv, $info) = $m->minv; |
| 1159 | if ($info){ |
| 1160 | warn "macsch: singular matrix"; |
| 1161 | return; |
| 1162 | } |
| 1163 | return masinh($inv,$tol); |
| 1164 | } |
| 1165 | |
| 1166 | =head2 mfun |
| 1167 | |
| 1168 | =for ref |
| 1169 | |
| 1170 | Return matrix function of second argument of a square matrix. |
| 1171 | Function will be applied on a complex ndarray. |
| 1172 | |
| 1173 | =for usage |
| 1174 | |
| 1175 | PDL = mfun(PDL(A),'cos') |
| 1176 | |
| 1177 | =for example |
| 1178 | |
| 1179 | my $a = random(10,10); |
| 1180 | my $fun = mfun($a,'cos'); |
| 1181 | sub sinbycos2{ |
| 1182 | $_[0]->set_inplace(0); |
| 1183 | $_[0] .= $_[0]->Csin/$_[0]->Ccos**2; |
| 1184 | } |
| 1185 | # Try diagonalization |
| 1186 | $fun = mfun($a, \&sinbycos2,1); |
| 1187 | # Now try Schur/Parlett |
| 1188 | $fun = mfun($a, \&sinbycos2); |
| 1189 | # Now with function. |
| 1190 | scalar msolve($a->mcos->mpow(2), $a->msin); |
| 1191 | |
| 1192 | =cut |
| 1193 | |
| 1194 | *mfun = \&PDL::mfun; |
| 1195 | |
| 1196 | sub PDL::mfun { |
| 1197 | &PDL::LinearAlgebra::_square; |
| 1198 | my ($m, $method, $diag, $tol) = @_; |
| 1199 | my @dims = $m->dims; |
| 1200 | if ($diag){ |
| 1201 | my ($e, $v) = $m->meigen(0,1); |
| 1202 | my ($inv, $info) = $v->minv; |
| 1203 | unless ($info){ |
| 1204 | eval {$v = ($v * $e->$method) x $v->minv;}; |
| 1205 | if ($@){ |
| 1206 | warn "mfun: Error $@\n"; |
| 1207 | return; |
| 1208 | } |
| 1209 | } |
| 1210 | else{ |
| 1211 | warn "mfun: Non invertible matrix in computation of $method\n"; |
| 1212 | return; |
| 1213 | } |
| 1214 | return $m->_is_complex ? $v : toreal($v, $tol); |
| 1215 | } |
| 1216 | else{ |
| 1217 | $m = $m->r2C unless $m->_is_complex; |
| 1218 | my ($t, undef, $z, undef, $info) = $m->mschur(1); |
| 1219 | if ($info){ |
| 1220 | warn "mfun: Can't compute Schur form\n"; |
| 1221 | return; |
| 1222 | } |
| 1223 | ($t, $info) = $t->ctrfun(0,$method); |
| 1224 | if($info){ |
| 1225 | warn "mfun: Can't compute $method\n"; |
| 1226 | return; |
| 1227 | } |
| 1228 | $m = $z x $t x $z->t(1); |
| 1229 | return $m->_is_complex ? $m : toreal($m, $tol); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | =head1 TODO |
| 1234 | |
| 1235 | Improve error return and check singularity. |
| 1236 | Improve (msqrt,mlog) / r2C |
| 1237 | |
| 1238 | =head1 AUTHOR |
| 1239 | |
| 1240 | Copyright (C) Grégory Vanuxem 2005-2018. |
| 1241 | |
| 1242 | This library is free software; you can redistribute it and/or modify |
| 1243 | it under the terms of the Perl Artistic License as in the file Artistic_2 |
| 1244 | in this distribution. |
| 1245 | |
| 1246 | =cut |
| 1247 | #line 1248 "lib/PDL/LinearAlgebra/Trans.pm" |
| 1248 | |
| 1249 | # Exit with OK status |
| 1250 | |
| 1251 | 1; |