| 1 | use strict; |
| 2 | use warnings; |
| 3 | use PDL::LiteF; |
| 4 | use PDL::MatrixOps qw(identity stretcher gurney); |
| 5 | use PDL::LinearAlgebra; |
| 6 | use PDL::LinearAlgebra::Trans qw //; |
| 7 | use PDL::LinearAlgebra::Real; |
| 8 | use Test::More; |
| 9 | use Test::PDL; |
| 10 | |
| 11 | sub runtest { |
| 12 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 13 | my ($in, $method, $expected, $extra) = @_; |
| 14 | $_ = $_->copy for $in; |
| 15 | ($expected, my $expected_cplx) = ref($expected) eq 'ARRAY' ? @$expected : ($expected, $expected); |
| 16 | $_ = PDL->topdl($_) for grep defined && !ref, $expected, $expected_cplx; |
| 17 | my @extra = map UNIVERSAL::isa($_, 'PDL') ? $_->copy : $_, @{$extra||[]}; |
| 18 | if (defined $expected) { |
| 19 | my ($got) = $in->$method(@extra); |
| 20 | is_pdl +PDL->topdl($got), $expected, {test_name=>$method, require_equal_types=>0, atol=>1e-3}; |
| 21 | } |
| 22 | $_ = $_->r2C for $in; |
| 23 | my ($got) = $in->$method(map ref() && ref() ne 'CODE' ? $_->r2C : $_, @extra); |
| 24 | $got = PDL->topdl($got); |
| 25 | my @cplx = ref($expected_cplx) eq 'ARRAY' ? @$expected_cplx : $expected_cplx; |
| 26 | @cplx = map PDL->topdl($_)->r2C, @cplx; |
| 27 | my @res = map [Test::PDL::eq_pdl($got, $_, {require_equal_types=>0, atol=>1e-3})], @cplx; |
| 28 | my $ok = grep $_->[0], @res; |
| 29 | ok $ok, "native complex $method" or diag explain map $_->[1], @res; |
| 30 | } |
| 31 | |
| 32 | my $aa = random(2,2,2); |
| 33 | $aa = czip($aa->slice('(0)'), $aa->slice('(1)')); |
| 34 | runtest($aa, 't', [undef,$aa->xchg(0,1)->conj], [1]); |
| 35 | |
| 36 | do './t/common.pl'; die if $@; |
| 37 | |
| 38 | is_pdl pdl([1,1,-1],[-1,-1,2])->positivise, pdl([1,1,-1],[1,1,-2]), 'positivise'; # real only |
| 39 | |
| 40 | my $a = pdl([[1.7,3.2],[9.2,7.3]]); |
| 41 | my $id = identity(2); |
| 42 | is_pdl $a->minv x $a,$id; |
| 43 | |
| 44 | is_pdl $a->mcrossprod->mposinv->tritosym x $a->mcrossprod,$id; |
| 45 | |
| 46 | ok($a->mcrossprod->mposdet !=0); |
| 47 | |
| 48 | my $A = identity(4) + ones(4, 4); |
| 49 | $A->slice('2,0') .= 0; # if don't break symmetry, don't show need transpose |
| 50 | my $B = sequence(2, 4); |
| 51 | getrf(my $lu=$A->copy, my $ipiv=null, my $info=null); |
| 52 | # if don't transpose the $B input, get memory crashes |
| 53 | getrs($lu, 1, my $x=$B->xchg(0,1)->copy, $ipiv, $info=null); |
| 54 | is_pdl $A x $x->t, $B; |
| 55 | |
| 56 | $A=pdl cdouble, <<'EOF'; |
| 57 | [ |
| 58 | [ 1 0 0 0 0 0] |
| 59 | [0.5 1 0 0.5 0 0] |
| 60 | [0.5 0 1 0 0 0.5] |
| 61 | [ 0 0 0 1 0 0] |
| 62 | [ 0 0 0 0.5 1 0.5] |
| 63 | [ 0 0 0 0 0 1] |
| 64 | ] |
| 65 | EOF |
| 66 | PDL::LinearAlgebra::Complex::cgetrf($lu=$A->copy, $ipiv=null, $info=null); |
| 67 | is $info, 0, 'cgetrf native worked'; |
| 68 | is $ipiv->nelem, 6, 'cgetrf gave right-sized ipiv'; |
| 69 | $B=pdl q[0.233178433563939+0.298197173371207i 1.09431208340166+1.30493506686269i 1.09216041861621+0.794394153882734i 0.55609433247125+0.515431151337765i 0.439100406078467+1.39139453403467i 0.252359761958406+0.570614019329113i]; |
| 70 | PDL::LinearAlgebra::Complex::cgetrs($lu, 1, $x=$B->copy, $ipiv, $info=null); |
| 71 | is $info, 0; |
| 72 | $x = $x->dummy(0); # transpose; xchg rightly fails if 1-D |
| 73 | is_pdl $A x $x, $B->dummy(0); |
| 74 | is_pdl +((1+sequence(2,2))*i())->mdet, cdouble(2), "Complex mdet"; |
| 75 | |
| 76 | $A = pdl '[[1+i 2+i][3+i 4+i]]'; |
| 77 | $B = identity(2); |
| 78 | is_pdl $A x $B, $A, 'complex first'; |
| 79 | is_pdl $B x $A, $A, 'complex second'; |
| 80 | |
| 81 | my $d = (identity(2) * 2)->sqrt; |
| 82 | is_pdl scalar $d->minv, my $exp = pdl('0.70710678 0; 0 0.70710678'), 'simple minv of double'; |
| 83 | my $ld = (identity(2)->ldouble * 2)->sqrt; |
| 84 | is_pdl scalar $ld->minv, $exp->ldouble, 'simple minv of ldouble'; |
| 85 | |
| 86 | $A=pdl <<'EOF'; |
| 87 | [ |
| 88 | [ 3 1] |
| 89 | [-1 3] |
| 90 | [-2 3] |
| 91 | ] |
| 92 | EOF |
| 93 | my ($u, $s, $vt) = msvd($A); |
| 94 | is_pdl $u x gurney($s, $A->dims) x $vt, $A, "msvd 2,3"; |
| 95 | ($u, $s, $vt) = mdsvd($A); |
| 96 | is_pdl $u x gurney($s, $A->dims) x $vt, $A, "mdsvd 2,3"; |
| 97 | $A = sequence(2,3) + i; |
| 98 | ($u, $s, $vt) = mdsvd($A); |
| 99 | is_pdl $u x gurney($s, $A->dims) x $vt, $A, "mdsvd complex 2,3"; |
| 100 | ($u, $s, $vt) = msvd($A); |
| 101 | is_pdl $u x gurney($s, $A->dims) x $vt, $A, "msvd complex 2,3"; |
| 102 | |
| 103 | $A=pdl <<'EOF'; |
| 104 | [ |
| 105 | [ 3 1 2] |
| 106 | [-1 3 0] |
| 107 | ] |
| 108 | EOF |
| 109 | ($u, $s, $vt) = msvd($A); |
| 110 | is_pdl $u x gurney($s, $A->dims) x $vt, $A, "msvd 3,2"; |
| 111 | ($u, $s, $vt) = mdsvd($A); |
| 112 | is_pdl $u x gurney($s, $A->dims) x $vt, $A, "mdsvd 3,2"; |
| 113 | $A = sequence(3,2) + i; |
| 114 | ($u, $s, $vt) = mdsvd($A); |
| 115 | is_pdl $u x gurney($s, $A->dims) x $vt, $A, "mdsvd complex 3,2"; |
| 116 | ($u, $s, $vt) = msvd($A); |
| 117 | is_pdl $u x gurney($s, $A->dims) x $vt, $A, "msvd complex 3,2"; |
| 118 | |
| 119 | $A = pdl([0.43,0.03],[0.75,0.72]); |
| 120 | $B = sequence(2,2); |
| 121 | (my $c, $s, my %ret) = mgsvd($A, $B, U => 1, V => 1, X => 1); |
| 122 | ($u, my $v, $x) = @ret{qw(U V X)}; |
| 123 | is_pdl $u x stretcher($c) x $x->t, $A, 'mgsvd A'; |
| 124 | is_pdl $v x stretcher($s) x $x->t, $B, 'mgsvd B'; |
| 125 | |
| 126 | $A = pdl '1 2 3 4 21; 5 6 7 8 22; 9 10 11 12 20'; |
| 127 | ($u, $s, $vt) = mdsvd($A); |
| 128 | is_pdl $u x gurney($s, $A->dims) x $vt, $A, "mdsvd 5,3"; |
| 129 | is_pdl $u->t x $u, identity(3), 'U is orthonormal'; |
| 130 | is_pdl $vt->t x $vt, identity(5), 'Vt is orthonormal'; |
| 131 | my $v_null = $vt->slice(",-1"); |
| 132 | is_pdl $A x $v_null->t, zeroes(1,3), 'correct right null-space'; |
| 133 | |
| 134 | my $rank2 = pdl([1,0,1],[-2,-3,1],[3,3,0]); |
| 135 | for my $in ($rank2, $rank2->cdouble) { |
| 136 | my $res = $in->mnull; |
| 137 | is_pdl $in x $res, zeroes($in->type, $res->dim(0), $in->dim(1)), 'mnull '.$in->type; |
| 138 | } |
| 139 | |
| 140 | $A = sequence(2,3); |
| 141 | is_pdl $A x $A->mpinv x $A, $A, 'mpinv tall works'; |
| 142 | $A = sequence(3,2); |
| 143 | is_pdl $A x $A->mpinv x $A, $A, 'mpinv wide works'; |
| 144 | $A = sequence(2,3) + i; |
| 145 | is_pdl $A x $A->mpinv x $A, $A, 'mpinv complex tall works'; |
| 146 | $A = sequence(3,2) + i; |
| 147 | is_pdl $A x $A->mpinv x $A, $A, 'mpinv complex wide works'; |
| 148 | $A = sequence(2,3,2); |
| 149 | is_pdl $A x $A->mpinv x $A, $A, 'mpinv tall broadcast works'; |
| 150 | $A = sequence(2,3,2) + i; |
| 151 | is_pdl $A x $A->mpinv x $A, $A, 'mpinv tall complex broadcast works'; |
| 152 | |
| 153 | $A = pdl('[0 0 0; 1 2 3; 4 5 6] [1 2 3; 4 8 6; 7 8 9]'); |
| 154 | is_pdl $A->mrank, pdl('2 3'), 'mrank broadcasts'; |
| 155 | |
| 156 | done_testing; |