| 1 |
{ # required because PAUSE will reject undefined $VERSION |
| 2 |
require ExtUtils::MM; |
| 3 |
my $MM = bless { NAME => 'Fake' }, 'MM'; |
| 4 |
my $global_version = $MM->parse_version('lib/PDL/LinearAlgebra.pm'); |
| 5 |
pp_setversion($global_version); |
| 6 |
} |
| 7 |
|
| 8 |
{ no warnings 'once'; # pass info back to Makefile.PL |
| 9 |
$PDL::Core::Dev::EXTRAS{$::PDLMOD}{OBJECT} .= join '', map " $::PDLBASE-$_\$(OBJ_EXT)", qw(selectfunc); |
| 10 |
} |
| 11 |
|
| 12 |
if ($^O =~ /MSWin/) { |
| 13 |
pp_addhdr(' |
| 14 |
#include <float.h> |
| 15 |
'); |
| 16 |
} |
| 17 |
|
| 18 |
pp_addhdr(' |
| 19 |
#include <math.h> |
| 20 |
|
| 21 |
#define CONCAT_(A, B) A ## B |
| 22 |
#define CONCAT(A, B) CONCAT_(A, B) |
| 23 |
#define FORTRAN(name) CONCAT(name, F77_USCORE) |
| 24 |
|
| 25 |
typedef PDL_Long logical; |
| 26 |
typedef PDL_Long integer; |
| 27 |
typedef PDL_Long ftnlen; |
| 28 |
|
| 29 |
#ifndef abs |
| 30 |
#define abs(x) ((x) >= 0 ? (x) : -(x)) |
| 31 |
#endif |
| 32 |
#ifndef max |
| 33 |
#define max(a,b) ((a) >= (b) ? (a) : (b)) |
| 34 |
#endif |
| 35 |
'); |
| 36 |
|
| 37 |
pp_addpm({At=>'Top'},<<'EOD'); |
| 38 |
use PDL::Core; |
| 39 |
use PDL::Slices; |
| 40 |
use PDL::Ops qw//; |
| 41 |
use PDL::Math qw/floor/; |
| 42 |
use PDL::MatrixOps qw/identity/; |
| 43 |
use PDL::LinearAlgebra; |
| 44 |
use PDL::LinearAlgebra::Real qw //; |
| 45 |
use PDL::LinearAlgebra::Complex qw //; |
| 46 |
use strict; |
| 47 |
|
| 48 |
=encoding utf8 |
| 49 |
|
| 50 |
=head1 NAME |
| 51 |
|
| 52 |
PDL::LinearAlgebra::Trans - Linear Algebra based transcendental functions for PDL |
| 53 |
|
| 54 |
=head1 SYNOPSIS |
| 55 |
|
| 56 |
use PDL::LinearAlgebra::Trans; |
| 57 |
|
| 58 |
$a = random (100,100); |
| 59 |
$sqrt = msqrt($a); |
| 60 |
|
| 61 |
=head1 DESCRIPTION |
| 62 |
|
| 63 |
This module provides some transcendental functions for matrices. |
| 64 |
Moreover it provides sec, asec, sech, asech, cot, acot, acoth, coth, csc, |
| 65 |
acsc, csch, acsch. Beware, importing this module will overwrite the hidden |
| 66 |
PDL routine sec. If you need to call it specify its origin module : PDL::Basic::sec(args) |
| 67 |
|
| 68 |
=cut |
| 69 |
EOD |
| 70 |
|
| 71 |
pp_add_exported('', ' mexp mexpts mlog msqrt mpow |
| 72 |
mcos msin mtan msec mcsc mcot |
| 73 |
mcosh msinh mtanh msech mcsch mcoth |
| 74 |
macos masin matan masec macsc macot |
| 75 |
macosh masinh matanh masech macsch macoth |
| 76 |
sec asec sech asech |
| 77 |
cot acot acoth coth mfun |
| 78 |
csc acsc csch acsch toreal pi'); |
| 79 |
|
| 80 |
pp_def('geexp', |
| 81 |
Pars => '[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)));', |
| 82 |
GenericTypes => [D], |
| 83 |
Code => ' |
| 84 |
/* ----------------------------------------------------------------------| |
| 85 |
int dgpadm_(integer *ideg, integer *m, double *t, |
| 86 |
double *h__, integer *ldh, double *wsp, integer *lwsp, |
| 87 |
integer *ipiv, integer *iexph, integer *ns, integer *iflag) |
| 88 |
|
| 89 |
-----Purpose----------------------------------------------------------| |
| 90 |
|
| 91 |
Computes exp(t*H), the matrix exponential of a general matrix in |
| 92 |
full, using the irreducible rational Pade approximation to the |
| 93 |
exponential function exp(x) = r(x) = (+/-)( I + 2*(q(x)/p(x)) ), |
| 94 |
combined with scaling-and-squaring. |
| 95 |
|
| 96 |
-----Arguments--------------------------------------------------------| |
| 97 |
|
| 98 |
ideg|deg : (input) the degre of the diagonal Pade to be used. |
| 99 |
a value of 6 is generally satisfactory. |
| 100 |
|
| 101 |
m : (input) order of H. |
| 102 |
|
| 103 |
H|A(ldh,m): (input) argument matrix. |
| 104 |
|
| 105 |
t|scale : (input) time-scale (can be < 0). |
| 106 |
|
| 107 |
wsp|exp(lwsp) : (workspace/output) lwsp .ge. 4*m*m+ideg+1. |
| 108 |
ipiv(m) : (workspace) |
| 109 |
|
| 110 |
>>>> iexph|index : (output) number such that wsp(iexph) points to exp(tH) |
| 111 |
i.e., exp(tH) is located at wsp(iexph ... iexph+m*m-1) |
| 112 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 113 |
NOTE: if the routine was called with wsp(iptr), |
| 114 |
then exp(tH) will start at wsp(iptr+iexph-1). |
| 115 |
|
| 116 |
ns : (output) number of scaling-squaring used. |
| 117 |
|
| 118 |
iflag|info : (output) exit flag. |
| 119 |
0 - no problem |
| 120 |
> 0 - Singularity in LU factorization when solving |
| 121 |
Pade approximation |
| 122 |
----------------------------------------------------------------------| |
| 123 |
Roger B. Sidje ([email protected]) |
| 124 |
EXPOKIT: Software Package for Computing Matrix Exponentials. |
| 125 |
ACM - Transactions On Mathematical Software, 24(1):130-156, 1998 |
| 126 |
Gregory Vanuxem ([email protected]) |
| 127 |
Minor modifications (Fortran to C with help of f2c, memory allocation, |
| 128 |
PDL adaptation, null entry matrix, error return, |
| 129 |
exp(tH) in entry matrix, trace normalization). |
| 130 |
----------------------------------------------------------------------| |
| 131 |
*/ |
| 132 |
|
| 133 |
extern int FORTRAN(dscal)(integer *, double *, double *, |
| 134 |
integer *); |
| 135 |
extern int FORTRAN(dgemm)(char *, char *, integer *, integer *, integer *, |
| 136 |
double *, double *, integer *, double *, |
| 137 |
integer *, double *, double *, integer *, |
| 138 |
ftnlen, ftnlen); |
| 139 |
extern int FORTRAN(dgesv)(integer *, integer *, double *, |
| 140 |
integer *, integer *, double *, integer *, integer *); |
| 141 |
extern int FORTRAN(daxpy)(integer *, double *, double *, |
| 142 |
integer *, double *, integer *); |
| 143 |
extern double FORTRAN(dlange)(char *norm, integer *m, integer *n, double *a, integer |
| 144 |
*lda, double *work); |
| 145 |
|
| 146 |
integer i__1, i__2, i__, j, k; |
| 147 |
integer ip, mm, iq, ih2, ifree, iused, iodd, iget, iput; |
| 148 |
|
| 149 |
double cp, cq, scale, scale2, hnorm, tracen; |
| 150 |
double *coef, *wsp = $P(wsp); |
| 151 |
|
| 152 |
integer c__1 = 1; |
| 153 |
integer c__2 = 2; |
| 154 |
double c_b7 = 0.; |
| 155 |
double c_b11 = 1.; |
| 156 |
double c_b19 = -1.; |
| 157 |
double c_b23 = 2.; |
| 158 |
double *h__ = $P(A); |
| 159 |
|
| 160 |
$info() = 0; |
| 161 |
|
| 162 |
///////////// |
| 163 |
mm = $SIZE(n) * $SIZE(n); |
| 164 |
coef = (double *) malloc(($deg() + 1 + mm) * sizeof(double)); |
| 165 |
////////// |
| 166 |
|
| 167 |
// --- initialise pointers ... |
| 168 |
ih2 = $deg() + 1; |
| 169 |
ip = 0; |
| 170 |
iq = mm; |
| 171 |
ifree = iq + mm; |
| 172 |
tracen = 0; |
| 173 |
|
| 174 |
if ($trace()) |
| 175 |
{ |
| 176 |
loop(n0) %{ tracen += h__[n0 + n0 * $SIZE(n)]; %} |
| 177 |
tracen /= $SIZE(n); |
| 178 |
if (tracen > 0) |
| 179 |
{ |
| 180 |
loop(n0) %{ h__[n0 + n0 * $SIZE(n)] -= tracen; %} |
| 181 |
|
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
// --- scaling: seek ns such that ||t*H/2^ns|| < 1/2; |
| 187 |
// and set scale = t/2^ns ... |
| 188 |
|
| 189 |
// Compute Infinity norm |
| 190 |
hnorm = FORTRAN(dlange)("I", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, $P(A), |
| 191 |
&(integer){$SIZE(n)}, wsp); |
| 192 |
|
| 193 |
hnorm = abs($scale() * hnorm); |
| 194 |
if (hnorm == 0.) |
| 195 |
{ |
| 196 |
free(coef); |
| 197 |
coef = $P(A); |
| 198 |
$ns() = 0; |
| 199 |
loop(n0,n1) %{ |
| 200 |
coef[n1 + n0 * $SIZE(n)] = (n0 == n1) ? 1 : 0; |
| 201 |
%} |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
i__2 = (integer ) (log(hnorm) / log(2.0) + 2); |
| 206 |
$ns() = max(0, i__2); |
| 207 |
scale = $scale() / pow(2.0, (double )$ns()); |
| 208 |
scale2 = scale * scale; |
| 209 |
|
| 210 |
// --- compute Pade coefficients ... |
| 211 |
i__ = $deg() + 1; |
| 212 |
j = ($deg() << 1) + 1; |
| 213 |
coef[0] = 1.; |
| 214 |
i__1 = $deg(); |
| 215 |
for (k = 1; k <= i__1; ++k) { |
| 216 |
coef[k] = coef[k - 1] * (double) (i__ - k) / (double) (k * (j - k)); |
| 217 |
} |
| 218 |
|
| 219 |
// --- H2 = scale2*H*H ... |
| 220 |
|
| 221 |
FORTRAN(dgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &scale2, h__, &(integer){$SIZE(n)}, h__, |
| 222 |
&(integer){$SIZE(n)}, &c_b7, &coef[ih2], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 223 |
|
| 224 |
// --- initialize p (numerator) and q (denominator) ... |
| 225 |
cp = coef[$deg() - 1]; |
| 226 |
cq = coef[$deg()]; |
| 227 |
loop(n0) %{ |
| 228 |
loop(n1) %{ |
| 229 |
wsp[ip + n0 * $SIZE(n) + n1] = 0.; |
| 230 |
wsp[iq + n0 * $SIZE(n) + n1] = 0.; |
| 231 |
%} |
| 232 |
wsp[ip + n0 * ($SIZE(n) + 1)] = cp; |
| 233 |
wsp[iq + n0 * ($SIZE(n) + 1)] = cq; |
| 234 |
%} |
| 235 |
|
| 236 |
// --- Apply Horner rule ... |
| 237 |
iodd = 1; |
| 238 |
k = $deg() - 2; |
| 239 |
do{ |
| 240 |
iused = iodd * iq + (1 - iodd) * ip; |
| 241 |
FORTRAN(dgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &c_b11, &wsp[iused], &(integer){$SIZE(n)}, &coef[ih2], &(integer){$SIZE(n)}, &c_b7, & |
| 242 |
wsp[ifree], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 243 |
loop(n0) %{ |
| 244 |
wsp[ifree + n0 * ($SIZE(n) + 1)] += coef[k]; |
| 245 |
%} |
| 246 |
ip = (1 - iodd) * ifree + iodd * ip; |
| 247 |
iq = iodd * ifree + (1 - iodd) * iq; |
| 248 |
ifree = iused; |
| 249 |
iodd = 1 - iodd; |
| 250 |
--k; |
| 251 |
} |
| 252 |
while (k >= 0); |
| 253 |
// --- Obtain (+/-)(I + 2*(p\q)) ... |
| 254 |
if (iodd == 1) |
| 255 |
{ |
| 256 |
FORTRAN(dgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &scale, &wsp[iq], &(integer){$SIZE(n)}, h__, &(integer){$SIZE(n)}, & |
| 257 |
c_b7, &wsp[ifree], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 258 |
iq = ifree; |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
FORTRAN(dgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &scale, &wsp[ip], &(integer){$SIZE(n)}, h__, &(integer){$SIZE(n)}, & |
| 263 |
c_b7, &wsp[ifree], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 264 |
ip = ifree; |
| 265 |
} |
| 266 |
FORTRAN(daxpy)(&mm, &c_b19, &wsp[ip], &c__1, &wsp[iq], &c__1); |
| 267 |
FORTRAN(dgesv)(&(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &wsp[iq], &(integer){$SIZE(n)}, $P(ipiv), &wsp[ip], &(integer){$SIZE(n)}, $P(info)); |
| 268 |
|
| 269 |
if ($info() != 0) |
| 270 |
{ |
| 271 |
free(coef); |
| 272 |
} |
| 273 |
else |
| 274 |
{ |
| 275 |
FORTRAN(dscal)(&mm, &c_b23, &wsp[ip], &c__1); |
| 276 |
loop(n0) %{ |
| 277 |
wsp[ip + n0 * ($SIZE(n) + 1)] += 1.; |
| 278 |
%} |
| 279 |
iput = ip; |
| 280 |
if ($ns() == 0 && iodd == 1) |
| 281 |
FORTRAN(dscal)(&mm, &c_b19, &wsp[ip], &c__1); |
| 282 |
else{ |
| 283 |
// -- squaring : exp(t*H) = (exp(t*H))^(2^ns) ... |
| 284 |
iodd = 1; |
| 285 |
i__1 = $ns(); |
| 286 |
for (k = 0; k < i__1; k++) { |
| 287 |
iget = iodd * ip + (1 - iodd) * iq; |
| 288 |
iput = (1 - iodd) * ip + iodd * iq; |
| 289 |
FORTRAN(dgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &c_b11, &wsp[iget], &(integer){$SIZE(n)}, &wsp[iget], &(integer){$SIZE(n)}, &c_b7, |
| 290 |
&wsp[iput], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 291 |
iodd = 1 - iodd; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
free(coef); |
| 296 |
coef = $P(A); |
| 297 |
i__2 = iput + mm; |
| 298 |
i__ = 0; |
| 299 |
$trace() = tracen; |
| 300 |
if (tracen > 0) |
| 301 |
{ |
| 302 |
scale = exp(tracen); |
| 303 |
for (i__1 = iput; i__1 < i__2 ; i__1++) |
| 304 |
coef[i__++] = scale * wsp[i__1]; |
| 305 |
} |
| 306 |
else |
| 307 |
{ |
| 308 |
for (i__1 = iput; i__1 < i__2 ; i__1++) |
| 309 |
coef[i__++] = wsp[i__1]; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
', |
| 314 |
Doc => <<'EOT', |
| 315 |
=for ref |
| 316 |
|
| 317 |
Computes exp(t*A), the matrix exponential of a general matrix, |
| 318 |
using the irreducible rational Pade approximation to the |
| 319 |
exponential function exp(x) = r(x) = (+/-)( I + 2*(q(x)/p(x)) ), |
| 320 |
combined with scaling-and-squaring and optionally normalization of the trace. |
| 321 |
The algorithm is described in Roger B. Sidje ([email protected]) |
| 322 |
"EXPOKIT: Software Package for Computing Matrix Exponentials". |
| 323 |
ACM - Transactions On Mathematical Software, 24(1):130-156, 1998 |
| 324 |
|
| 325 |
A: On input argument matrix. On output exp(t*A). |
| 326 |
Use Fortran storage type. |
| 327 |
|
| 328 |
deg: the degre of the diagonal Pade to be used. |
| 329 |
a value of 6 is generally satisfactory. |
| 330 |
|
| 331 |
scale: time-scale (can be < 0). |
| 332 |
|
| 333 |
trace: on input, boolean value indicating whether or not perform |
| 334 |
a trace normalization. On output value used. |
| 335 |
|
| 336 |
ns: on output number of scaling-squaring used. |
| 337 |
|
| 338 |
info: exit flag. |
| 339 |
0 - no problem |
| 340 |
> 0 - Singularity in LU factorization when solving |
| 341 |
Pade approximation |
| 342 |
|
| 343 |
=for example |
| 344 |
|
| 345 |
$a = random(5,5); |
| 346 |
$trace = pdl(1); |
| 347 |
$a->t->geexp(6,1,$trace, ($ns = null), ($info = null)); |
| 348 |
|
| 349 |
=cut |
| 350 |
|
| 351 |
EOT |
| 352 |
); |
| 353 |
|
| 354 |
do './pp_defc.pl'; die if $@; |
| 355 |
pp_defc('geexp', |
| 356 |
Pars => '[io]A(2,n,n);int deg();scale();int trace();int [o]ns();int [o]info(); int [t] ipiv(n)', |
| 357 |
Doc => ' |
| 358 |
=for ref |
| 359 |
|
| 360 |
Complex version of geexp. The value used for trace normalization is not returned. |
| 361 |
The algorithm is described in Roger B. Sidje ([email protected]) |
| 362 |
"EXPOKIT: Software Package for Computing Matrix Exponentials". |
| 363 |
ACM - Transactions On Mathematical Software, 24(1):130-156, 1998 |
| 364 |
|
| 365 |
', |
| 366 |
GenericTypes => [D], |
| 367 |
Code => ' |
| 368 |
/* ----------------------------------------------------------------------| |
| 369 |
int dgpadm_(integer *ideg, integer *m, double *t, |
| 370 |
PDL_CDouble *h__, integer *ldh, PDL_CDouble *wsp, integer *lwsp, |
| 371 |
integer *ipiv, integer *iexph, integer *ns, integer *iflag) |
| 372 |
|
| 373 |
-----Purpose----------------------------------------------------------| |
| 374 |
|
| 375 |
Computes exp(t*H), the matrix exponential of a general matrix in |
| 376 |
full, using the irreducible rational Pade approximation to the |
| 377 |
exponential function exp(x) = r(x) = (+/-)( I + 2*(q(x)/p(x)) ), |
| 378 |
combined with scaling-and-squaring. |
| 379 |
|
| 380 |
-----Arguments--------------------------------------------------------| |
| 381 |
|
| 382 |
ideg|deg : (input) the degre of the diagonal Pade to be used. |
| 383 |
a value of 6 is generally satisfactory. |
| 384 |
|
| 385 |
m : (input) order of H. |
| 386 |
|
| 387 |
H|A(ldh,m): (input) argument matrix. |
| 388 |
|
| 389 |
t|scale : (input) time-scale (can be < 0). |
| 390 |
|
| 391 |
wsp|exp(lwsp) : (workspace/output) lwsp .ge. 4*m*m+ideg+1. |
| 392 |
ipiv(m) : (workspace) |
| 393 |
|
| 394 |
>>>> iexph : (output) number such that wsp(iexph) points to exp(tH) |
| 395 |
i.e., exp(tH) is located at wsp(iexph ... iexph+m*m-1) |
| 396 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 397 |
NOTE: if the routine was called with wsp(iptr), |
| 398 |
then exp(tH) will start at wsp(iptr+iexph-1). |
| 399 |
|
| 400 |
ns : (output) number of scaling-squaring used. |
| 401 |
|
| 402 |
iflag|info : (output) exit flag. |
| 403 |
0 - no problem |
| 404 |
> 0 - Singularity in LU factorization when solving |
| 405 |
Pade approximation |
| 406 |
----------------------------------------------------------------------| |
| 407 |
Roger B. Sidje ([email protected]) |
| 408 |
EXPOKIT: Software Package for Computing Matrix Exponentials. |
| 409 |
ACM - Transactions On Mathematical Software, 24(1):130-156, 1998 |
| 410 |
Gregory Vanuxem ([email protected]) |
| 411 |
Minor modifications (Fortran to C with help of f2c, memory allocation, |
| 412 |
PDL adaptation, null entry matrix, error return, |
| 413 |
exp(tH) in entry matrix), trace normalization. |
| 414 |
----------------------------------------------------------------------| |
| 415 |
*/ |
| 416 |
static PDL_CDouble c_b1 = 0; |
| 417 |
static PDL_CDouble c_b2 = 1; |
| 418 |
static integer c__2 = 2; |
| 419 |
static integer c__1 = 1; |
| 420 |
static double c_b19 = 2.; |
| 421 |
static double c_b21 = -1.; |
| 422 |
|
| 423 |
integer i__1, i__2, i__3, i__, j, k; |
| 424 |
integer ip, mm, iq, ih2, iodd, iget, iput, ifree, iused; |
| 425 |
|
| 426 |
double hnorm,d__1, d__2; |
| 427 |
PDL_CDouble scale, tracen, cp, cq, z__1; |
| 428 |
PDL_CDouble scale2; |
| 429 |
PDL_CDouble *wsp, *h__; |
| 430 |
|
| 431 |
extern int FORTRAN(zgemm)(char *, char *, integer *, integer *, |
| 432 |
integer *, PDL_CDouble *, PDL_CDouble *, integer *, |
| 433 |
PDL_CDouble *, integer *, PDL_CDouble *, PDL_CDouble *, |
| 434 |
integer *, ftnlen, ftnlen); |
| 435 |
extern int FORTRAN(zgesv)(integer *, integer *, PDL_CDouble *, |
| 436 |
integer *, integer *, PDL_CDouble *, integer *, integer *); |
| 437 |
extern int FORTRAN(zaxpy)(integer *, PDL_CDouble *, |
| 438 |
PDL_CDouble *, integer *, PDL_CDouble *, integer *), FORTRAN(zdscal)( |
| 439 |
integer *, double *, PDL_CDouble *, integer *); |
| 440 |
extern double FORTRAN(zlange)(char *norm, integer *m, integer *n, PDL_CDouble *a, |
| 441 |
integer *lda, PDL_CDouble *work); |
| 442 |
|
| 443 |
h__ = (PDL_CDouble *) $P(A); |
| 444 |
|
| 445 |
wsp = malloc (( $deg() + 1 + 4 * $SIZE(n) * $SIZE(n)) * sizeof (PDL_CDouble)); |
| 446 |
|
| 447 |
mm = $SIZE(n) * $SIZE(n); |
| 448 |
$info() = 0; |
| 449 |
|
| 450 |
ih2 = $deg() + 1; |
| 451 |
ip = ih2 + mm; |
| 452 |
iq = ip + mm; |
| 453 |
ifree = iq + mm; |
| 454 |
|
| 455 |
tracen = 0; |
| 456 |
if ($trace()) |
| 457 |
{ |
| 458 |
loop(n0) %{ |
| 459 |
tracen += h__[n0 + n0 * $SIZE(n)]; |
| 460 |
%} |
| 461 |
tracen /= $SIZE(n); |
| 462 |
if (creal(tracen) < 0){ |
| 463 |
tracen = cimag(tracen); |
| 464 |
} |
| 465 |
loop(n0) %{ |
| 466 |
h__[n0 + n0 * $SIZE(n)] -= tracen; |
| 467 |
%} |
| 468 |
} |
| 469 |
|
| 470 |
// --- scaling: seek ns such that ||t*H/2^ns|| < 1/2; |
| 471 |
// and set scale = t/2^ns ... |
| 472 |
// Compute Infinity norm |
| 473 |
hnorm = FORTRAN(zlange)("I", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, h__, |
| 474 |
&(integer){$SIZE(n)}, wsp); |
| 475 |
|
| 476 |
hnorm = abs($scale() * hnorm); |
| 477 |
if (hnorm == 0.) |
| 478 |
{ |
| 479 |
$ns() = 0; |
| 480 |
loop(n0,n1) %{ |
| 481 |
h__[n1 + n0 * $SIZE(n) ] = (n0 == n1) ? 1 : 0; |
| 482 |
%} |
| 483 |
} |
| 484 |
else |
| 485 |
{ |
| 486 |
i__2 = (integer) ((log(hnorm) / log(2.)) + 2); |
| 487 |
$ns() = max(0,i__2); |
| 488 |
scale = $scale() / pow(2, (double )$ns()); |
| 489 |
scale2 = scale * scale; |
| 490 |
|
| 491 |
// --- compute Pade coefficients ... |
| 492 |
|
| 493 |
i__ = $deg() + 1; |
| 494 |
j = ($deg() << 1) + 1; |
| 495 |
wsp[0] = 1; |
| 496 |
for (k = 1; k <= $deg(); ++k) { |
| 497 |
d__1 = (double) (i__ - k); |
| 498 |
d__2 = (double) (k * (j - k)); |
| 499 |
wsp[k] = (d__1 * wsp[k - 1]) / d__2; |
| 500 |
} |
| 501 |
|
| 502 |
// --- H2 = scale2*H*H ... |
| 503 |
FORTRAN(zgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &scale2, &h__[0], &(integer){$SIZE(n)}, &h__[0], |
| 504 |
&(integer){$SIZE(n)}, &c_b1, &wsp[ih2], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 505 |
|
| 506 |
// --- initialise p (numerator) and q (denominator) ... |
| 507 |
|
| 508 |
cp = wsp[$deg() - 1]; |
| 509 |
cq = wsp[$deg()]; |
| 510 |
|
| 511 |
loop(n0) %{ |
| 512 |
loop(n1) %{ |
| 513 |
i__3 = ip + n0 * $SIZE(n) + n1; |
| 514 |
wsp[i__3] = 0.; |
| 515 |
i__3 = iq + n0 * $SIZE(n) + n1; |
| 516 |
wsp[i__3] = 0.; |
| 517 |
%} |
| 518 |
i__2 = ip + n0 * ($SIZE(n) + 1); |
| 519 |
wsp[i__2] = cp; |
| 520 |
i__2 = iq + n0 * ($SIZE(n) + 1); |
| 521 |
wsp[i__2] = cq; |
| 522 |
%} |
| 523 |
|
| 524 |
// --- Apply Horner rule ... |
| 525 |
iodd = 1; |
| 526 |
k = $deg() - 2; |
| 527 |
do |
| 528 |
{ |
| 529 |
iused = iodd * iq + (1 - iodd) * ip; |
| 530 |
FORTRAN(zgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &c_b2, &wsp[iused], &(integer){$SIZE(n)}, &wsp[ih2], &(integer){$SIZE(n)}, &c_b1, & |
| 531 |
wsp[ifree], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 532 |
loop(n0) %{ |
| 533 |
i__1 = ifree + n0 * ($SIZE(n) + 1); |
| 534 |
i__2 = ifree + n0 * ($SIZE(n) + 1); |
| 535 |
i__3 = k; |
| 536 |
wsp[i__1] = wsp[i__2] + wsp[i__3]; |
| 537 |
%} |
| 538 |
ip = (1 - iodd) * ifree + iodd * ip; |
| 539 |
iq = iodd * ifree + (1 - iodd) * iq; |
| 540 |
ifree = iused; |
| 541 |
iodd = 1 - iodd; |
| 542 |
--k; |
| 543 |
} |
| 544 |
while(k >= 0); |
| 545 |
|
| 546 |
// --- Obtain (+/-)(I + 2*(p\q)) ... |
| 547 |
if (iodd != 0) |
| 548 |
{ |
| 549 |
FORTRAN(zgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &scale, &wsp[iq], &(integer){$SIZE(n)}, &h__[0], &(integer){$SIZE(n)}, & |
| 550 |
c_b1, &wsp[ifree], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 551 |
iq = ifree; |
| 552 |
}else |
| 553 |
{ |
| 554 |
FORTRAN(zgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &scale, &wsp[ip], &(integer){$SIZE(n)}, &h__[0], &(integer){$SIZE(n)}, & |
| 555 |
c_b1, &wsp[ifree], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 556 |
ip = ifree; |
| 557 |
} |
| 558 |
z__1 = -1.; |
| 559 |
FORTRAN(zaxpy)(&mm, &z__1, &wsp[ip], &c__1, &wsp[iq], &c__1); |
| 560 |
FORTRAN(zgesv)(&(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &wsp[iq], &(integer){$SIZE(n)}, $P(ipiv), &wsp[ip], &(integer){$SIZE(n)}, $P(info)); |
| 561 |
|
| 562 |
if ($info() == 0) |
| 563 |
{ |
| 564 |
FORTRAN(zdscal)(&mm, &c_b19, &wsp[ip], &c__1); |
| 565 |
loop(n0) %{ |
| 566 |
i__2 = ip + n0 * ($SIZE(n) + 1); |
| 567 |
i__3 = ip + n0 * ($SIZE(n) + 1); |
| 568 |
wsp[i__2] = wsp[i__3] + 1.; |
| 569 |
%} |
| 570 |
iput = ip; |
| 571 |
if ($ns() == 0 && iodd != 0) |
| 572 |
FORTRAN(zdscal)(&mm, &c_b21, &wsp[ip], &c__1); |
| 573 |
else |
| 574 |
{ |
| 575 |
// -- squaring : exp(t*H) = (exp(t*H))^(2^ns) ... |
| 576 |
iodd = 1; |
| 577 |
for (k = 0; k < $ns(); k++) |
| 578 |
{ |
| 579 |
iget = iodd * ip + (1 - iodd) * iq; |
| 580 |
iput = (1 - iodd) * ip + iodd * iq; |
| 581 |
FORTRAN(zgemm)("n", "n", &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &(integer){$SIZE(n)}, &c_b2, &wsp[iget], &(integer){$SIZE(n)}, &wsp[iget], &(integer){$SIZE(n)}, &c_b1, |
| 582 |
&wsp[iput], &(integer){$SIZE(n)}, (ftnlen)1, (ftnlen)1); |
| 583 |
iodd = 1 - iodd; |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
i__2 = iput + mm; |
| 589 |
i__ = 0; |
| 590 |
if ($trace()) |
| 591 |
{ |
| 592 |
tracen = cexp(tracen); |
| 593 |
for (i__1 = iput; i__1 < i__2 ; i__1++) |
| 594 |
{ |
| 595 |
h__[i__++] = tracen * wsp[i__1]; |
| 596 |
} |
| 597 |
} |
| 598 |
else |
| 599 |
{ |
| 600 |
for (i__1 = iput; i__1 < i__2 ; i__1++) |
| 601 |
{ |
| 602 |
h__[i__++] = wsp[i__1]; |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
free(wsp); |
| 607 |
', |
| 608 |
); |
| 609 |
|
| 610 |
pp_defc('trsqrt', |
| 611 |
Pars => '[io]A(2,n,n);int uplo();[o] B(2,n,n);int [o]info()', |
| 612 |
Doc => <<'EOF', |
| 613 |
=for ref |
| 614 |
|
| 615 |
Root square of complex triangular matrix. Uses a recurrence of Björck and Hammarling. |
| 616 |
If uplo is true, A is lower triangular. |
| 617 |
|
| 618 |
See Nicholas J. Higham, A new sqrtm for MATLAB. Numerical Analysis |
| 619 |
Report No. 336, Manchester Centre for Computational Mathematics, |
| 620 |
Manchester, England, January 1999, available at L<https://web.archive.org/web/20210420221902/https://www.maths.manchester.ac.uk/~higham/papers/narep336.pdf> |
| 621 |
EOF |
| 622 |
GenericTypes => [D], |
| 623 |
Code => <<'EOF', |
| 624 |
$info() = 0; |
| 625 |
loop (n1,n0) %{ $B() = 0; %} |
| 626 |
loop(n0) %{ $B(n1=>n0) = csqrt($A(n1=>n0)); %} |
| 627 |
|
| 628 |
loop (n1=:-1,n0=:-n1-1) %{ |
| 629 |
integer j = n0 + n1 + 1; |
| 630 |
PDL_CDouble s = 0; |
| 631 |
|
| 632 |
if ($uplo()) |
| 633 |
loop (n1=n0+1:j) %{ s += $B() * $B(n0=>n1,n1=>j); %} |
| 634 |
else |
| 635 |
loop (n1=n0+1:j) %{ s += $B(n0=>n1,n1=>n0) * $B(n0=>j); %} |
| 636 |
|
| 637 |
PDL_CDouble sdenum = $B(n1=>n0) + $B(n0=>j,n1=>j); |
| 638 |
PDL_CDouble snum = ($uplo() ? $A(n1=>j) : $A(n0=>j,n1=>n0)) - s; |
| 639 |
|
| 640 |
char realbigger = fabs(creal(sdenum)) > fabs(cimag(sdenum)); |
| 641 |
double bigger = realbigger ? creal(sdenum) : cimag(sdenum); |
| 642 |
double smaller = realbigger ? cimag(sdenum) : creal(sdenum); |
| 643 |
if (bigger == 0){ |
| 644 |
$info() = -1; |
| 645 |
goto ENDCTRSQRT; |
| 646 |
} |
| 647 |
double tt = smaller / bigger, dn = bigger + tt * smaller; |
| 648 |
PDL_CDouble newval = snum * (realbigger ? (1 - tt * I) : (tt - I)) / dn; |
| 649 |
if ($uplo()) |
| 650 |
$B(n1=>j) = newval; |
| 651 |
else |
| 652 |
$B(n0=>j,n1=>n0) = newval; |
| 653 |
%} |
| 654 |
ENDCTRSQRT: |
| 655 |
; |
| 656 |
EOF |
| 657 |
); |
| 658 |
|
| 659 |
pp_addhdr(' |
| 660 |
void dfunc_wrapper(PDL_CDouble *p, integer n, SV* dfunc); |
| 661 |
'); |
| 662 |
|
| 663 |
pp_defc('trfun', |
| 664 |
Pars => '[io]A(2,n,n);int uplo();[o] B(2,n,n);int [o]info(); complex [t]diag(n);', |
| 665 |
OtherPars => "SV* func" , |
| 666 |
Doc => ' |
| 667 |
=for ref |
| 668 |
|
| 669 |
Apply an arbitrary function to a complex triangular matrix. Uses a recurrence of Parlett. |
| 670 |
If uplo is true, A is lower triangular. |
| 671 |
', |
| 672 |
GenericTypes => [D], |
| 673 |
Code => ' |
| 674 |
PDL_CDouble *cb, *ca; |
| 675 |
PDL_CDouble s, snum, sdenum; |
| 676 |
double *b; |
| 677 |
double tt, dn; |
| 678 |
integer i, j, k, l, p, ind, ind1, ind2, tmp, c__1; |
| 679 |
extern double FORTRAN(zdotu)(PDL_CDouble *ret, integer *n, PDL_CDouble *dx, integer *incx, PDL_CDouble *dy, |
| 680 |
integer *incy); |
| 681 |
|
| 682 |
ca = (PDL_CDouble *) $P(A); |
| 683 |
b = (double *)$P(B); |
| 684 |
cb = (PDL_CDouble *) $P(B); |
| 685 |
c__1 = 1; |
| 686 |
|
| 687 |
#define subscr(a_1,a_2) \ |
| 688 |
( $uplo() ) ? (a_2)+ $SIZE(n) * (a_1) : (a_1)+ $SIZE(n) * (a_2) |
| 689 |
|
| 690 |
$info() = 0; |
| 691 |
ind = $SIZE(n) * $SIZE(n) * 2; |
| 692 |
for (i = 0; i < ind;i++) |
| 693 |
b[i] = 0; |
| 694 |
|
| 695 |
loop (n) %{ |
| 696 |
$diag() = ca[subscr(n,n)]; |
| 697 |
%} |
| 698 |
|
| 699 |
dfunc_wrapper($P(diag), $SIZE(n), $COMP(func)); |
| 700 |
loop (n) %{ |
| 701 |
cb[subscr(n,n)] = $diag(); |
| 702 |
%} |
| 703 |
|
| 704 |
for (p = 1; p < $SIZE(n); p++) |
| 705 |
{ |
| 706 |
tmp = $SIZE(n) - p; |
| 707 |
for (i = 0; i < tmp; i++) |
| 708 |
{ |
| 709 |
j = i + p; |
| 710 |
//$s = $T(,($j),($i))->Cmul($F(,($j),($j))->Csub($F(,($i),($i)))); |
| 711 |
ind1 = subscr(i,i); |
| 712 |
ind2 = subscr(j,j); |
| 713 |
ind = subscr(j,i); |
| 714 |
s = snum = ca[ind] * (cb[ind2] - cb[ind1]); |
| 715 |
if (i < (j-1)) |
| 716 |
{ |
| 717 |
//$s = $s + $T(,$i+1:$j-1,($i))->cdot(1, $F(,($j), $i+1:$j-1),1)->Csub($F(,$i+1:$j-1,($i))->cdot(1,$T(,($j), $i+1:$j-1),1)); |
| 718 |
// $T(,$i+1:$j-1,($i))->cdot(1, $F(,($j), $i+1:$j-1),1) |
| 719 |
l = 0; |
| 720 |
for(k = i+1; k < j; k++) |
| 721 |
$diag(n=>l++) = cb[subscr(j,k)]; |
| 722 |
ind = subscr(i+1,i); |
| 723 |
// TODO : Humm |
| 724 |
FORTRAN(zdotu)(&s, &l, &ca[ind], &c__1, $P(diag), &c__1); |
| 725 |
snum += s; |
| 726 |
|
| 727 |
// $F(,$i+1:$j-1,($i))->cdot(1,$T(,($j), $i+1:$j-1),1) |
| 728 |
l = 0; |
| 729 |
for(k = i+1; k < j; k++) |
| 730 |
$diag(n=>l++) = ca[subscr(j,k)]; |
| 731 |
ind = subscr(i+1,i); |
| 732 |
// TODO : Humm |
| 733 |
FORTRAN(zdotu)(&s, &l, &cb[ind], &c__1, $P(diag), &c__1); |
| 734 |
snum -= s; |
| 735 |
ind = subscr(j,i); |
| 736 |
} |
| 737 |
|
| 738 |
//$sdenum = $T(,($j),($j))->Csub($T(,($i),($i))); |
| 739 |
sdenum = ca[ind2] - ca[ind1]; |
| 740 |
|
| 741 |
//$s = $s / $sdenum; |
| 742 |
if (fabs (creal(sdenum)) > fabs (cimag(sdenum))) |
| 743 |
{ |
| 744 |
if (creal(sdenum) == 0) |
| 745 |
{ |
| 746 |
$info() = -1; |
| 747 |
goto ENDCTRFUN; |
| 748 |
} |
| 749 |
tt = cimag(sdenum) / creal(sdenum); |
| 750 |
dn = creal(sdenum) + tt * cimag(sdenum); |
| 751 |
cb[ind] = (snum + tt * (-I * snum)) / dn; |
| 752 |
} |
| 753 |
else |
| 754 |
{ |
| 755 |
if (cimag(sdenum) == 0) |
| 756 |
{ |
| 757 |
$info() = -1; |
| 758 |
goto ENDCTRFUN; |
| 759 |
} |
| 760 |
tt = creal(sdenum) / cimag(sdenum); |
| 761 |
dn = creal(sdenum) * tt + cimag(sdenum); |
| 762 |
cb[ind] = (snum * tt + (-I * snum)) / dn; |
| 763 |
} |
| 764 |
} |
| 765 |
} |
| 766 |
ENDCTRFUN: |
| 767 |
; |
| 768 |
#undef subscr |
| 769 |
', |
| 770 |
); |
| 771 |
|
| 772 |
pp_addpm(<<'EOD'); |
| 773 |
my $pi; |
| 774 |
BEGIN { $pi = pdl(3.1415926535897932384626433832795029) } |
| 775 |
sub pi () { $pi->copy }; |
| 776 |
|
| 777 |
*sec = \&PDL::sec; |
| 778 |
sub PDL::sec{1/cos($_[0])} |
| 779 |
|
| 780 |
*csc = \&PDL::csc; |
| 781 |
sub PDL::csc($) {1/sin($_[0])} |
| 782 |
|
| 783 |
*cot = \&PDL::cot; |
| 784 |
sub PDL::cot($) {1/(sin($_[0])/cos($_[0]))} |
| 785 |
|
| 786 |
*sech = \&PDL::sech; |
| 787 |
sub PDL::sech($){1/pdl($_[0])->cosh} |
| 788 |
|
| 789 |
*csch = \&PDL::csch; |
| 790 |
sub PDL::csch($) {1/pdl($_[0])->sinh} |
| 791 |
|
| 792 |
*coth = \&PDL::coth; |
| 793 |
sub PDL::coth($) {1/pdl($_[0])->tanh} |
| 794 |
|
| 795 |
*asec = \&PDL::asec; |
| 796 |
sub PDL::asec($) {my $tmp = 1/pdl($_[0]) ; $tmp->acos} |
| 797 |
|
| 798 |
*acsc = \&PDL::acsc; |
| 799 |
sub PDL::acsc($) {my $tmp = 1/pdl($_[0]) ; $tmp->asin} |
| 800 |
|
| 801 |
*acot = \&PDL::acot; |
| 802 |
sub PDL::acot($) {my $tmp = 1/pdl($_[0]) ; $tmp->atan} |
| 803 |
|
| 804 |
*asech = \&PDL::asech; |
| 805 |
sub PDL::asech($) {my $tmp = 1/pdl($_[0]) ; $tmp->acosh} |
| 806 |
|
| 807 |
*acsch = \&PDL::acsch; |
| 808 |
sub PDL::acsch($) {my $tmp = 1/pdl($_[0]) ; $tmp->asinh} |
| 809 |
|
| 810 |
*acoth = \&PDL::acoth; |
| 811 |
sub PDL::acoth($) {my $tmp = 1/pdl($_[0]) ; $tmp->atanh} |
| 812 |
|
| 813 |
my $_tol = 9.99999999999999e-15; |
| 814 |
|
| 815 |
sub toreal { |
| 816 |
$_tol = $_[1] if defined $_[1]; |
| 817 |
return $_[0] if $_[0]->isempty or $_[0]->type->real; |
| 818 |
my ($min, $max, $tmp); |
| 819 |
($min, $max) = $_[0]->im->minmax; |
| 820 |
return $_[0]->re->sever unless (abs($min) > $_tol || abs($max) > $_tol); |
| 821 |
$_[0]; |
| 822 |
} |
| 823 |
|
| 824 |
=head2 mlog |
| 825 |
|
| 826 |
=for ref |
| 827 |
|
| 828 |
Return matrix logarithm of a square matrix. |
| 829 |
|
| 830 |
=for usage |
| 831 |
|
| 832 |
PDL = mlog(PDL(A)) |
| 833 |
|
| 834 |
=for example |
| 835 |
|
| 836 |
my $a = random(10,10); |
| 837 |
my $log = mlog($a); |
| 838 |
|
| 839 |
=cut |
| 840 |
|
| 841 |
*mlog = \&PDL::mlog; |
| 842 |
sub PDL::mlog { |
| 843 |
&PDL::LinearAlgebra::_square; |
| 844 |
my ($m, $tol) = @_; |
| 845 |
mfun($m, sub{$_[0].=log $_[0]} , 0, $tol); |
| 846 |
} |
| 847 |
|
| 848 |
=head2 msqrt |
| 849 |
|
| 850 |
=for ref |
| 851 |
|
| 852 |
Return matrix square root (principal) of a square matrix. |
| 853 |
|
| 854 |
=for usage |
| 855 |
|
| 856 |
PDL = msqrt(PDL(A)) |
| 857 |
|
| 858 |
=for example |
| 859 |
|
| 860 |
my $a = random(10,10); |
| 861 |
my $sqrt = msqrt($a); |
| 862 |
|
| 863 |
=cut |
| 864 |
|
| 865 |
*msqrt = \&PDL::msqrt; |
| 866 |
|
| 867 |
sub PDL::msqrt { |
| 868 |
&PDL::LinearAlgebra::_square; |
| 869 |
my ($m, $tol) = @_; |
| 870 |
$m = $m->r2C unless $m->_is_complex; |
| 871 |
my ($t, undef, $z, undef, $info) = $m->mschur(1); |
| 872 |
if ($info){ |
| 873 |
warn "msqrt: Can't compute Schur form\n"; |
| 874 |
return; |
| 875 |
} |
| 876 |
($t, $info) = $t->ctrsqrt(0); |
| 877 |
if($info){ |
| 878 |
warn "msqrt: can't compute square root\n"; |
| 879 |
return; |
| 880 |
} |
| 881 |
$m = $z x $t x $z->t(1); |
| 882 |
return $m->_is_complex ? $m : toreal($m, $tol); |
| 883 |
} |
| 884 |
|
| 885 |
=head2 mexp |
| 886 |
|
| 887 |
=for ref |
| 888 |
|
| 889 |
Return matrix exponential of a square matrix. |
| 890 |
|
| 891 |
=for usage |
| 892 |
|
| 893 |
PDL = mexp(PDL(A)) |
| 894 |
|
| 895 |
=for example |
| 896 |
|
| 897 |
my $a = random(10,10); |
| 898 |
my $exp = mexp($a); |
| 899 |
|
| 900 |
=cut |
| 901 |
|
| 902 |
*mexp = \&PDL::mexp; |
| 903 |
sub PDL::mexp { |
| 904 |
&PDL::LinearAlgebra::_square; |
| 905 |
my ($m, $order, $trace) = @_; |
| 906 |
$trace = 1 unless defined $trace; |
| 907 |
$order = 6 unless defined $order; |
| 908 |
$m = $m->copy; |
| 909 |
$m->t->_call_method('geexp', $order, 1, $trace, my $ns = PDL->null, my $info = PDL->null); |
| 910 |
if ($info){ |
| 911 |
warn "mexp: Error $info"; |
| 912 |
} |
| 913 |
else{ |
| 914 |
return $m; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
*mexpts = \&PDL::mexpts; |
| 919 |
sub PDL::mexpts { |
| 920 |
&PDL::LinearAlgebra::_square; |
| 921 |
my ($m, $order, $tol) = @_; |
| 922 |
my @dims = $m->dims; |
| 923 |
my ($em, $trm); |
| 924 |
$order = 20 unless defined $order; |
| 925 |
$em = $m->_is_complex ? diag(r2C(ones($dims[1]))) : diag(ones($dims[1])); |
| 926 |
$trm = $em->copy; |
| 927 |
for (1..($order - 1)){ |
| 928 |
$trm = $trm x ($m / $_); |
| 929 |
$em += $trm; |
| 930 |
} |
| 931 |
return $m->_is_complex ? $em : toreal($em, $tol); |
| 932 |
} |
| 933 |
|
| 934 |
=head2 mpow |
| 935 |
|
| 936 |
=for ref |
| 937 |
|
| 938 |
Return matrix power of a square matrix. |
| 939 |
|
| 940 |
=for usage |
| 941 |
|
| 942 |
PDL = mpow(PDL(A), SCALAR(exponent)) |
| 943 |
|
| 944 |
=for example |
| 945 |
|
| 946 |
my $a = random(10,10); |
| 947 |
my $powered = mpow($a,2.5); |
| 948 |
|
| 949 |
=cut |
| 950 |
|
| 951 |
*mpow = \&PDL::mpow; |
| 952 |
sub PDL::mpow { |
| 953 |
&PDL::LinearAlgebra::_square; |
| 954 |
my ($m, $power, $tol, $eigen) = @_; |
| 955 |
my @dims = $m->dims; |
| 956 |
my $ret; |
| 957 |
if (UNIVERSAL::isa($power,'PDL') and $power->dims > 1){ |
| 958 |
my ($e, $v) = $m->meigen(0,1); |
| 959 |
$ret = $v * ($e**$power) x $v->minv; |
| 960 |
} |
| 961 |
elsif( 1/$dims[0] * 1000 > abs($power) and !$eigen){ |
| 962 |
$ret = identity($dims[0]); |
| 963 |
$ret = $ret->r2C if $m->_is_complex; |
| 964 |
my $pow = floor($power); |
| 965 |
$pow++ if ($power < 0 and $power != $pow); |
| 966 |
# TODO: what a beautiful thing (is it a game ?) |
| 967 |
for(my $i = 0; $i < abs($pow); $i++){$ret x= $m;} |
| 968 |
$ret = $ret->minv if $power < 0; |
| 969 |
if ($power = $power - $pow){ |
| 970 |
if($power == 0.5){ |
| 971 |
my $v = $m->msqrt; |
| 972 |
$ret = ($pow == 0) ? $v : $ret x $v; |
| 973 |
} |
| 974 |
else{ |
| 975 |
my ($e, $v) = $m->meigen(0,1); |
| 976 |
$ret = ($pow == 0) ? ($v * $e**$power x $v->minv) : $ret->r2C x ($v * $e**$power x $v->minv); |
| 977 |
} |
| 978 |
} |
| 979 |
} |
| 980 |
else{ |
| 981 |
my ($e, $v) = $m->meigen(0,1); |
| 982 |
$ret = $v * $e**$power x $v->minv; |
| 983 |
} |
| 984 |
return $m->_is_complex ? $ret : toreal($ret, $tol); |
| 985 |
} |
| 986 |
|
| 987 |
=head2 mcos |
| 988 |
|
| 989 |
=for ref |
| 990 |
|
| 991 |
Return matrix cosine of a square matrix. |
| 992 |
|
| 993 |
=for usage |
| 994 |
|
| 995 |
PDL = mcos(PDL(A)) |
| 996 |
|
| 997 |
=for example |
| 998 |
|
| 999 |
my $a = random(10,10); |
| 1000 |
my $cos = mcos($a); |
| 1001 |
|
| 1002 |
=cut |
| 1003 |
|
| 1004 |
sub _i { |
| 1005 |
i(); |
| 1006 |
} |
| 1007 |
|
| 1008 |
*mcos = \&PDL::mcos; |
| 1009 |
sub PDL::mcos { |
| 1010 |
&PDL::LinearAlgebra::_square; |
| 1011 |
my $m = shift; |
| 1012 |
my $i = _i(); |
| 1013 |
return $m->_is_complex ? (mexp($i*$m) + mexp(- $i*$m)) / 2 : mexp($i*$m)->re->sever; |
| 1014 |
} |
| 1015 |
|
| 1016 |
=head2 macos |
| 1017 |
|
| 1018 |
=for ref |
| 1019 |
|
| 1020 |
Return matrix inverse cosine of a square matrix. |
| 1021 |
|
| 1022 |
=for usage |
| 1023 |
|
| 1024 |
PDL = macos(PDL(A)) |
| 1025 |
|
| 1026 |
=for example |
| 1027 |
|
| 1028 |
my $a = random(10,10); |
| 1029 |
my $acos = macos($a); |
| 1030 |
|
| 1031 |
=cut |
| 1032 |
|
| 1033 |
*macos = \&PDL::macos; |
| 1034 |
sub PDL::macos { |
| 1035 |
&PDL::LinearAlgebra::_square; |
| 1036 |
my ($m, $tol) = @_; |
| 1037 |
my @dims = $m->dims; |
| 1038 |
my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 1039 |
my $i = _i(); |
| 1040 |
my $ret = $i * mlog( ($m->r2C - $i * msqrt( ($id - $m x $m), $tol))); |
| 1041 |
return $m->_is_complex ? $ret : toreal($ret, $tol); |
| 1042 |
} |
| 1043 |
|
| 1044 |
=head2 msin |
| 1045 |
|
| 1046 |
=for ref |
| 1047 |
|
| 1048 |
Return matrix sine of a square matrix. |
| 1049 |
|
| 1050 |
=for usage |
| 1051 |
|
| 1052 |
PDL = msin(PDL(A)) |
| 1053 |
|
| 1054 |
=for example |
| 1055 |
|
| 1056 |
my $a = random(10,10); |
| 1057 |
my $sin = msin($a); |
| 1058 |
|
| 1059 |
=cut |
| 1060 |
|
| 1061 |
*msin = \&PDL::msin; |
| 1062 |
sub PDL::msin { |
| 1063 |
&PDL::LinearAlgebra::_square; |
| 1064 |
my $m = shift; |
| 1065 |
my $i = _i(); |
| 1066 |
$m->_is_complex ? (mexp($i*$m) - mexp(- $i*$m))/(2*$i) : mexp($i*$m)->im->sever; |
| 1067 |
} |
| 1068 |
|
| 1069 |
=head2 masin |
| 1070 |
|
| 1071 |
=for ref |
| 1072 |
|
| 1073 |
Return matrix inverse sine of a square matrix. |
| 1074 |
|
| 1075 |
=for usage |
| 1076 |
|
| 1077 |
PDL = masin(PDL(A)) |
| 1078 |
|
| 1079 |
=for example |
| 1080 |
|
| 1081 |
my $a = random(10,10); |
| 1082 |
my $asin = masin($a); |
| 1083 |
|
| 1084 |
=cut |
| 1085 |
|
| 1086 |
*masin = \&PDL::masin; |
| 1087 |
sub PDL::masin { |
| 1088 |
&PDL::LinearAlgebra::_square; |
| 1089 |
my ($m, $tol) = @_; |
| 1090 |
my @dims = $m->dims; |
| 1091 |
my $i = _i(); |
| 1092 |
my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 1093 |
my $ret = (-$i) * mlog((($i*$m) + msqrt($id - $m x $m, $tol))); |
| 1094 |
return $m->_is_complex ? $ret : toreal($ret, $tol); |
| 1095 |
} |
| 1096 |
|
| 1097 |
=head2 mtan |
| 1098 |
|
| 1099 |
=for ref |
| 1100 |
|
| 1101 |
Return matrix tangent of a square matrix. |
| 1102 |
|
| 1103 |
=for usage |
| 1104 |
|
| 1105 |
PDL = mtan(PDL(A)) |
| 1106 |
|
| 1107 |
=for example |
| 1108 |
|
| 1109 |
my $a = random(10,10); |
| 1110 |
my $tan = mtan($a); |
| 1111 |
|
| 1112 |
=cut |
| 1113 |
|
| 1114 |
*mtan = \&PDL::mtan; |
| 1115 |
sub PDL::mtan { |
| 1116 |
&PDL::LinearAlgebra::_square; |
| 1117 |
my ($m, $id) = @_; |
| 1118 |
my @dims = $m->dims; |
| 1119 |
return scalar msolvex(mcos($m), msin($m),equilibrate=>1) unless $id; |
| 1120 |
my $i = _i(); |
| 1121 |
if ($m->_is_complex){ |
| 1122 |
$id = identity($dims[0])->r2C; |
| 1123 |
$m = mexp(-2*$i*$m); |
| 1124 |
return scalar msolvex( ($id + $m ),( (- $i) * ($id - $m)),equilibrate=>1); |
| 1125 |
} |
| 1126 |
else{ |
| 1127 |
$m = mexp($i * $m); |
| 1128 |
return scalar $m->re->msolvex($m->im,equilibrate=>1); |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
=head2 matan |
| 1133 |
|
| 1134 |
=for ref |
| 1135 |
|
| 1136 |
Return matrix inverse tangent of a square matrix. |
| 1137 |
|
| 1138 |
=for usage |
| 1139 |
|
| 1140 |
PDL = matan(PDL(A)) |
| 1141 |
|
| 1142 |
=for example |
| 1143 |
|
| 1144 |
my $a = random(10,10); |
| 1145 |
my $atan = matan($a); |
| 1146 |
|
| 1147 |
=cut |
| 1148 |
|
| 1149 |
*matan = \&PDL::matan; |
| 1150 |
sub PDL::matan { |
| 1151 |
&PDL::LinearAlgebra::_square; |
| 1152 |
my ($m, $tol) = @_; |
| 1153 |
my @dims = $m->dims; |
| 1154 |
my $i = _i(); |
| 1155 |
my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 1156 |
my $ret = -$i/2 * mlog( scalar PDL::msolvex( ($id - $i*$m) ,($id + $i*$m),equilibrate=>1 )); |
| 1157 |
return $m->_is_complex ? $ret : toreal($ret, $tol); |
| 1158 |
} |
| 1159 |
|
| 1160 |
=head2 mcot |
| 1161 |
|
| 1162 |
=for ref |
| 1163 |
|
| 1164 |
Return matrix cotangent of a square matrix. |
| 1165 |
|
| 1166 |
=for usage |
| 1167 |
|
| 1168 |
PDL = mcot(PDL(A)) |
| 1169 |
|
| 1170 |
=for example |
| 1171 |
|
| 1172 |
my $a = random(10,10); |
| 1173 |
my $cot = mcot($a); |
| 1174 |
|
| 1175 |
=cut |
| 1176 |
|
| 1177 |
*mcot = \&PDL::mcot; |
| 1178 |
sub PDL::mcot { |
| 1179 |
&PDL::LinearAlgebra::_square; |
| 1180 |
my ($m, $id) = @_; |
| 1181 |
my @dims = $m->dims; |
| 1182 |
return scalar msolvex(msin($m),mcos($m),equilibrate=>1) unless $id; |
| 1183 |
my $i = _i(); |
| 1184 |
if ($m->_is_complex){ |
| 1185 |
$id = identity($dims[0])->r2C; |
| 1186 |
$m = mexp(-2*$i*$m); |
| 1187 |
return scalar msolvex( ($id - $m), ($i * ($id + $m)), equilibrate=>1); |
| 1188 |
} |
| 1189 |
else{ |
| 1190 |
$m = mexp($i * $m); |
| 1191 |
return scalar $m->im->msolvex($m->re,equilibrate=>1); |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
=head2 macot |
| 1196 |
|
| 1197 |
=for ref |
| 1198 |
|
| 1199 |
Return matrix inverse cotangent of a square matrix. |
| 1200 |
|
| 1201 |
=for usage |
| 1202 |
|
| 1203 |
PDL = macot(PDL(A)) |
| 1204 |
|
| 1205 |
=for example |
| 1206 |
|
| 1207 |
my $a = random(10,10); |
| 1208 |
my $acot = macot($a); |
| 1209 |
|
| 1210 |
=cut |
| 1211 |
|
| 1212 |
*macot = \&PDL::macot; |
| 1213 |
sub PDL::macot { |
| 1214 |
&PDL::LinearAlgebra::_square; |
| 1215 |
my ($m, $tol, $id) = @_; |
| 1216 |
my ($inv, $info) = $m->minv; |
| 1217 |
if ($info){ |
| 1218 |
warn "macot: singular matrix"; |
| 1219 |
return; |
| 1220 |
} |
| 1221 |
return matan($inv,$tol); |
| 1222 |
} |
| 1223 |
|
| 1224 |
=head2 msec |
| 1225 |
|
| 1226 |
=for ref |
| 1227 |
|
| 1228 |
Return matrix secant of a square matrix. |
| 1229 |
|
| 1230 |
=for usage |
| 1231 |
|
| 1232 |
PDL = msec(PDL(A)) |
| 1233 |
|
| 1234 |
=for example |
| 1235 |
|
| 1236 |
my $a = random(10,10); |
| 1237 |
my $sec = msec($a); |
| 1238 |
|
| 1239 |
=cut |
| 1240 |
|
| 1241 |
*msec = \&PDL::msec; |
| 1242 |
sub PDL::msec { |
| 1243 |
&PDL::LinearAlgebra::_square; |
| 1244 |
my $m = shift; |
| 1245 |
my $i = _i(); |
| 1246 |
return $m->_is_complex ? PDL::minv(mexp($i*$m) + mexp(- $i*$m)) * 2 : scalar PDL::minv(re(mexp($i*$m))); |
| 1247 |
} |
| 1248 |
|
| 1249 |
=head2 masec |
| 1250 |
|
| 1251 |
=for ref |
| 1252 |
|
| 1253 |
Return matrix inverse secant of a square matrix. |
| 1254 |
|
| 1255 |
=for usage |
| 1256 |
|
| 1257 |
PDL = masec(PDL(A)) |
| 1258 |
|
| 1259 |
=for example |
| 1260 |
|
| 1261 |
my $a = random(10,10); |
| 1262 |
my $asec = masec($a); |
| 1263 |
|
| 1264 |
=cut |
| 1265 |
|
| 1266 |
*masec = \&PDL::masec; |
| 1267 |
sub PDL::masec { |
| 1268 |
&PDL::LinearAlgebra::_square; |
| 1269 |
my ($m, $tol) = @_; |
| 1270 |
my ($inv, $info) = $m->minv; |
| 1271 |
if ($info){ |
| 1272 |
warn "masec: singular matrix"; |
| 1273 |
return; |
| 1274 |
} |
| 1275 |
return macos($inv,$tol); |
| 1276 |
} |
| 1277 |
|
| 1278 |
=head2 mcsc |
| 1279 |
|
| 1280 |
=for ref |
| 1281 |
|
| 1282 |
Return matrix cosecant of a square matrix. |
| 1283 |
|
| 1284 |
=for usage |
| 1285 |
|
| 1286 |
PDL = mcsc(PDL(A)) |
| 1287 |
|
| 1288 |
=for example |
| 1289 |
|
| 1290 |
my $a = random(10,10); |
| 1291 |
my $csc = mcsc($a); |
| 1292 |
|
| 1293 |
=cut |
| 1294 |
|
| 1295 |
*mcsc = \&PDL::mcsc; |
| 1296 |
sub PDL::mcsc { |
| 1297 |
&PDL::LinearAlgebra::_square; |
| 1298 |
my $m = shift; |
| 1299 |
my $i = _i(); |
| 1300 |
$m->_is_complex ? PDL::minv(mexp($i*$m) - mexp(- $i*$m)) * 2*$i : scalar PDL::minv(im(mexp($i*$m))); |
| 1301 |
} |
| 1302 |
|
| 1303 |
=head2 macsc |
| 1304 |
|
| 1305 |
=for ref |
| 1306 |
|
| 1307 |
Return matrix inverse cosecant of a square matrix. |
| 1308 |
|
| 1309 |
=for usage |
| 1310 |
|
| 1311 |
PDL = macsc(PDL(A)) |
| 1312 |
|
| 1313 |
=for example |
| 1314 |
|
| 1315 |
my $a = random(10,10); |
| 1316 |
my $acsc = macsc($a); |
| 1317 |
|
| 1318 |
=cut |
| 1319 |
|
| 1320 |
*macsc = \&PDL::macsc; |
| 1321 |
sub PDL::macsc { |
| 1322 |
&PDL::LinearAlgebra::_square; |
| 1323 |
my ($m, $tol) = @_; |
| 1324 |
my ($inv, $info) = $m->minv; |
| 1325 |
if ($info){ |
| 1326 |
warn "macsc: singular matrix"; |
| 1327 |
return; |
| 1328 |
} |
| 1329 |
return masin($inv,$tol); |
| 1330 |
} |
| 1331 |
|
| 1332 |
=head2 mcosh |
| 1333 |
|
| 1334 |
=for ref |
| 1335 |
|
| 1336 |
Return matrix hyperbolic cosine of a square matrix. |
| 1337 |
|
| 1338 |
=for usage |
| 1339 |
|
| 1340 |
PDL = mcosh(PDL(A)) |
| 1341 |
|
| 1342 |
=for example |
| 1343 |
|
| 1344 |
my $a = random(10,10); |
| 1345 |
my $cos = mcosh($a); |
| 1346 |
|
| 1347 |
=cut |
| 1348 |
|
| 1349 |
*mcosh = \&PDL::mcosh; |
| 1350 |
|
| 1351 |
sub PDL::mcosh { |
| 1352 |
&PDL::LinearAlgebra::_square; |
| 1353 |
my $m = shift; |
| 1354 |
( $m->mexp + mexp(-$m) )/2; |
| 1355 |
} |
| 1356 |
|
| 1357 |
=head2 macosh |
| 1358 |
|
| 1359 |
=for ref |
| 1360 |
|
| 1361 |
Return matrix hyperbolic inverse cosine of a square matrix. |
| 1362 |
|
| 1363 |
=for usage |
| 1364 |
|
| 1365 |
PDL = macosh(PDL(A)) |
| 1366 |
|
| 1367 |
=for example |
| 1368 |
|
| 1369 |
my $a = random(10,10); |
| 1370 |
my $acos = macosh($a); |
| 1371 |
|
| 1372 |
=cut |
| 1373 |
|
| 1374 |
*macosh = \&PDL::macosh; |
| 1375 |
|
| 1376 |
sub PDL::macosh { |
| 1377 |
&PDL::LinearAlgebra::_square; |
| 1378 |
my ($m, $tol) = @_; |
| 1379 |
my @dims = $m->dims; |
| 1380 |
my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 1381 |
my $ret = msqrt($m x $m - $id); |
| 1382 |
$m = $m->r2C if $ret->getndims > @dims; |
| 1383 |
mlog($m + $ret, $tol); |
| 1384 |
} |
| 1385 |
|
| 1386 |
=head2 msinh |
| 1387 |
|
| 1388 |
=for ref |
| 1389 |
|
| 1390 |
Return matrix hyperbolic sine of a square matrix. |
| 1391 |
|
| 1392 |
=for usage |
| 1393 |
|
| 1394 |
PDL = msinh(PDL(A)) |
| 1395 |
|
| 1396 |
=for example |
| 1397 |
|
| 1398 |
my $a = random(10,10); |
| 1399 |
my $sinh = msinh($a); |
| 1400 |
|
| 1401 |
=cut |
| 1402 |
|
| 1403 |
*msinh = \&PDL::msinh; |
| 1404 |
|
| 1405 |
sub PDL::msinh { |
| 1406 |
&PDL::LinearAlgebra::_square; |
| 1407 |
my $m = shift; |
| 1408 |
( $m->mexp - mexp(-$m) )/2; |
| 1409 |
} |
| 1410 |
|
| 1411 |
=head2 masinh |
| 1412 |
|
| 1413 |
=for ref |
| 1414 |
|
| 1415 |
Return matrix hyperbolic inverse sine of a square matrix. |
| 1416 |
|
| 1417 |
=for usage |
| 1418 |
|
| 1419 |
PDL = masinh(PDL(A)) |
| 1420 |
|
| 1421 |
=for example |
| 1422 |
|
| 1423 |
my $a = random(10,10); |
| 1424 |
my $asinh = masinh($a); |
| 1425 |
|
| 1426 |
=cut |
| 1427 |
|
| 1428 |
*masinh = \&PDL::masinh; |
| 1429 |
|
| 1430 |
sub PDL::masinh { |
| 1431 |
&PDL::LinearAlgebra::_square; |
| 1432 |
my ($m, $tol) = @_; |
| 1433 |
my @dims = $m->dims; |
| 1434 |
my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 1435 |
my $ret = msqrt($m x $m + $id); |
| 1436 |
$m = $m->r2C if $ret->getndims > @dims; |
| 1437 |
mlog(($m + $ret), $tol); |
| 1438 |
} |
| 1439 |
|
| 1440 |
=head2 mtanh |
| 1441 |
|
| 1442 |
=for ref |
| 1443 |
|
| 1444 |
Return matrix hyperbolic tangent of a square matrix. |
| 1445 |
|
| 1446 |
=for usage |
| 1447 |
|
| 1448 |
PDL = mtanh(PDL(A)) |
| 1449 |
|
| 1450 |
=for example |
| 1451 |
|
| 1452 |
my $a = random(10,10); |
| 1453 |
my $tanh = mtanh($a); |
| 1454 |
|
| 1455 |
=cut |
| 1456 |
|
| 1457 |
*mtanh = \&PDL::mtanh; |
| 1458 |
|
| 1459 |
sub PDL::mtanh { |
| 1460 |
&PDL::LinearAlgebra::_square; |
| 1461 |
my ($m, $id) = @_; |
| 1462 |
my @dims = $m->dims; |
| 1463 |
return scalar msolvex(mcosh($m), msinh($m),equilibrate=>1) unless $id; |
| 1464 |
$id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 1465 |
$m = mexp(-2*$m); |
| 1466 |
return scalar msolvex( ($id + $m ),($id - $m), equilibrate=>1); |
| 1467 |
} |
| 1468 |
|
| 1469 |
=head2 matanh |
| 1470 |
|
| 1471 |
=for ref |
| 1472 |
|
| 1473 |
Return matrix hyperbolic inverse tangent of a square matrix. |
| 1474 |
|
| 1475 |
=for usage |
| 1476 |
|
| 1477 |
PDL = matanh(PDL(A)) |
| 1478 |
|
| 1479 |
=for example |
| 1480 |
|
| 1481 |
my $a = random(10,10); |
| 1482 |
my $atanh = matanh($a); |
| 1483 |
|
| 1484 |
=cut |
| 1485 |
|
| 1486 |
*matanh = \&PDL::matanh; |
| 1487 |
|
| 1488 |
sub PDL::matanh { |
| 1489 |
&PDL::LinearAlgebra::_square; |
| 1490 |
my ($m, $tol) = @_; |
| 1491 |
my @dims = $m->dims; |
| 1492 |
my $id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 1493 |
mlog( scalar msolvex( ($id - $m ),($id + $m),equilibrate=>1), $tol ) / 2; |
| 1494 |
} |
| 1495 |
|
| 1496 |
=head2 mcoth |
| 1497 |
|
| 1498 |
=for ref |
| 1499 |
|
| 1500 |
Return matrix hyperbolic cotangent of a square matrix. |
| 1501 |
|
| 1502 |
=for usage |
| 1503 |
|
| 1504 |
PDL = mcoth(PDL(A)) |
| 1505 |
|
| 1506 |
=for example |
| 1507 |
|
| 1508 |
my $a = random(10,10); |
| 1509 |
my $coth = mcoth($a); |
| 1510 |
|
| 1511 |
=cut |
| 1512 |
|
| 1513 |
*mcoth = \&PDL::mcoth; |
| 1514 |
|
| 1515 |
sub PDL::mcoth { |
| 1516 |
&PDL::LinearAlgebra::_square; |
| 1517 |
my ($m, $id) = @_; |
| 1518 |
my @dims = $m->dims; |
| 1519 |
scalar msolvex(msinh($m), mcosh($m),equilibrate=>1) unless $id; |
| 1520 |
$id = identity($dims[0]); $id = $id->r2C if $m->_is_complex; |
| 1521 |
$m = mexp(-2*$m); |
| 1522 |
return scalar msolvex( ($id - $m ),($id + $m),equilibrate=>1); |
| 1523 |
} |
| 1524 |
|
| 1525 |
=head2 macoth |
| 1526 |
|
| 1527 |
=for ref |
| 1528 |
|
| 1529 |
Return matrix hyperbolic inverse cotangent of a square matrix. |
| 1530 |
|
| 1531 |
=for usage |
| 1532 |
|
| 1533 |
PDL = macoth(PDL(A)) |
| 1534 |
|
| 1535 |
=for example |
| 1536 |
|
| 1537 |
my $a = random(10,10); |
| 1538 |
my $acoth = macoth($a); |
| 1539 |
|
| 1540 |
=cut |
| 1541 |
|
| 1542 |
*macoth = \&PDL::macoth; |
| 1543 |
|
| 1544 |
sub PDL::macoth { |
| 1545 |
&PDL::LinearAlgebra::_square; |
| 1546 |
my ($m, $tol) = @_; |
| 1547 |
my ($inv, $info) = $m->minv; |
| 1548 |
if ($info){ |
| 1549 |
warn "macoth: singular matrix"; |
| 1550 |
return; |
| 1551 |
} |
| 1552 |
return matanh($inv,$tol); |
| 1553 |
} |
| 1554 |
|
| 1555 |
=head2 msech |
| 1556 |
|
| 1557 |
=for ref |
| 1558 |
|
| 1559 |
Return matrix hyperbolic secant of a square matrix. |
| 1560 |
|
| 1561 |
=for usage |
| 1562 |
|
| 1563 |
PDL = msech(PDL(A)) |
| 1564 |
|
| 1565 |
=for example |
| 1566 |
|
| 1567 |
my $a = random(10,10); |
| 1568 |
my $sech = msech($a); |
| 1569 |
|
| 1570 |
=cut |
| 1571 |
|
| 1572 |
*msech = \&PDL::msech; |
| 1573 |
|
| 1574 |
sub PDL::msech { |
| 1575 |
&PDL::LinearAlgebra::_square; |
| 1576 |
my $m = shift; |
| 1577 |
PDL::minv( $m->mexp + mexp(-$m) ) * 2; |
| 1578 |
} |
| 1579 |
|
| 1580 |
=head2 masech |
| 1581 |
|
| 1582 |
=for ref |
| 1583 |
|
| 1584 |
Return matrix hyperbolic inverse secant of a square matrix. |
| 1585 |
|
| 1586 |
=for usage |
| 1587 |
|
| 1588 |
PDL = masech(PDL(A)) |
| 1589 |
|
| 1590 |
=for example |
| 1591 |
|
| 1592 |
my $a = random(10,10); |
| 1593 |
my $asech = masech($a); |
| 1594 |
|
| 1595 |
=cut |
| 1596 |
|
| 1597 |
*masech = \&PDL::masech; |
| 1598 |
|
| 1599 |
sub PDL::masech { |
| 1600 |
&PDL::LinearAlgebra::_square; |
| 1601 |
my ($m, $tol) = @_; |
| 1602 |
my ($inv, $info) = $m->minv; |
| 1603 |
if ($info){ |
| 1604 |
warn "masech: singular matrix"; |
| 1605 |
return; |
| 1606 |
} |
| 1607 |
return macosh($inv,$tol); |
| 1608 |
} |
| 1609 |
|
| 1610 |
=head2 mcsch |
| 1611 |
|
| 1612 |
=for ref |
| 1613 |
|
| 1614 |
Return matrix hyperbolic cosecant of a square matrix. |
| 1615 |
|
| 1616 |
=for usage |
| 1617 |
|
| 1618 |
PDL = mcsch(PDL(A)) |
| 1619 |
|
| 1620 |
=for example |
| 1621 |
|
| 1622 |
my $a = random(10,10); |
| 1623 |
my $csch = mcsch($a); |
| 1624 |
|
| 1625 |
=cut |
| 1626 |
|
| 1627 |
*mcsch = \&PDL::mcsch; |
| 1628 |
|
| 1629 |
sub PDL::mcsch { |
| 1630 |
&PDL::LinearAlgebra::_square; |
| 1631 |
my $m = shift; |
| 1632 |
PDL::minv( $m->mexp - mexp(-$m) ) * 2; |
| 1633 |
} |
| 1634 |
|
| 1635 |
=head2 macsch |
| 1636 |
|
| 1637 |
=for ref |
| 1638 |
|
| 1639 |
Return matrix hyperbolic inverse cosecant of a square matrix. |
| 1640 |
|
| 1641 |
=for usage |
| 1642 |
|
| 1643 |
PDL = macsch(PDL(A)) |
| 1644 |
|
| 1645 |
=for example |
| 1646 |
|
| 1647 |
my $a = random(10,10); |
| 1648 |
my $acsch = macsch($a); |
| 1649 |
|
| 1650 |
=cut |
| 1651 |
|
| 1652 |
*macsch = \&PDL::macsch; |
| 1653 |
|
| 1654 |
sub PDL::macsch { |
| 1655 |
&PDL::LinearAlgebra::_square; |
| 1656 |
my ($m, $tol) = @_; |
| 1657 |
my ($inv, $info) = $m->minv; |
| 1658 |
if ($info){ |
| 1659 |
warn "macsch: singular matrix"; |
| 1660 |
return; |
| 1661 |
} |
| 1662 |
return masinh($inv,$tol); |
| 1663 |
} |
| 1664 |
|
| 1665 |
=head2 mfun |
| 1666 |
|
| 1667 |
=for ref |
| 1668 |
|
| 1669 |
Return matrix function of second argument of a square matrix. |
| 1670 |
Function will be applied on a complex ndarray. |
| 1671 |
|
| 1672 |
=for usage |
| 1673 |
|
| 1674 |
PDL = mfun(PDL(A),'cos') |
| 1675 |
|
| 1676 |
=for example |
| 1677 |
|
| 1678 |
my $a = random(10,10); |
| 1679 |
my $fun = mfun($a,'cos'); |
| 1680 |
sub sinbycos2{ |
| 1681 |
$_[0]->set_inplace(0); |
| 1682 |
$_[0] .= $_[0]->Csin/$_[0]->Ccos**2; |
| 1683 |
} |
| 1684 |
# Try diagonalization |
| 1685 |
$fun = mfun($a, \&sinbycos2,1); |
| 1686 |
# Now try Schur/Parlett |
| 1687 |
$fun = mfun($a, \&sinbycos2); |
| 1688 |
# Now with function. |
| 1689 |
scalar msolve($a->mcos->mpow(2), $a->msin); |
| 1690 |
|
| 1691 |
=cut |
| 1692 |
|
| 1693 |
*mfun = \&PDL::mfun; |
| 1694 |
|
| 1695 |
sub PDL::mfun { |
| 1696 |
&PDL::LinearAlgebra::_square; |
| 1697 |
my ($m, $method, $diag, $tol) = @_; |
| 1698 |
my @dims = $m->dims; |
| 1699 |
if ($diag){ |
| 1700 |
my ($e, $v) = $m->meigen(0,1); |
| 1701 |
my ($inv, $info) = $v->minv; |
| 1702 |
unless ($info){ |
| 1703 |
eval {$v = ($v * $e->$method) x $v->minv;}; |
| 1704 |
if ($@){ |
| 1705 |
warn "mfun: Error $@\n"; |
| 1706 |
return; |
| 1707 |
} |
| 1708 |
} |
| 1709 |
else{ |
| 1710 |
warn "mfun: Non invertible matrix in computation of $method\n"; |
| 1711 |
return; |
| 1712 |
} |
| 1713 |
return $m->_is_complex ? $v : toreal($v, $tol); |
| 1714 |
} |
| 1715 |
else{ |
| 1716 |
$m = $m->r2C unless $m->_is_complex; |
| 1717 |
my ($t, undef, $z, undef, $info) = $m->mschur(1); |
| 1718 |
if ($info){ |
| 1719 |
warn "mfun: Can't compute Schur form\n"; |
| 1720 |
return; |
| 1721 |
} |
| 1722 |
($t, $info) = $t->ctrfun(0,$method); |
| 1723 |
if($info){ |
| 1724 |
warn "mfun: Can't compute $method\n"; |
| 1725 |
return; |
| 1726 |
} |
| 1727 |
$m = $z x $t x $z->t(1); |
| 1728 |
return $m->_is_complex ? $m : toreal($m, $tol); |
| 1729 |
} |
| 1730 |
} |
| 1731 |
|
| 1732 |
=head1 TODO |
| 1733 |
|
| 1734 |
Improve error return and check singularity. |
| 1735 |
Improve (msqrt,mlog) / r2C |
| 1736 |
|
| 1737 |
=head1 AUTHOR |
| 1738 |
|
| 1739 |
Copyright (C) Grégory Vanuxem 2005-2018. |
| 1740 |
|
| 1741 |
This library is free software; you can redistribute it and/or modify |
| 1742 |
it under the terms of the Perl Artistic License as in the file Artistic_2 |
| 1743 |
in this distribution. |
| 1744 |
|
| 1745 |
=cut |
| 1746 |
|
| 1747 |
EOD |
| 1748 |
|
| 1749 |
pp_done(); |
| 1750 |
|
| 1751 |
1; |