Skip to content

Commit a0add20

Browse files
committed
tidy svd pp_def
1 parent f4c42bb commit a0add20

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

lib/PDL/MatrixOps.pd

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -610,36 +610,27 @@ To compare with L<PDL::LinearAlgebra>:
610610
EOF
611611
);
612612

613-
######################################################################
614-
### svd
615613
pp_def(
616614
"svd",
617615
HandleBad => 0,
618-
Pars => 'a(n,m); [t]w(wsize=CALC($SIZE(n) * ($SIZE(m) + $SIZE(n)))); [o]u(n,m); [o,phys]z(n); [o]v(n,n);',
616+
Pars => 'a(n,m); [o]u(n,m); [o,phys]s(n); [o]v(n,n);
617+
[t]w(wsize=CALC($SIZE(n) * ($SIZE(m) + $SIZE(n))));',
619618
GenericTypes => ['D'],
620619
RedoDimsCode => '
621620
if ($SIZE(m)<$SIZE(n))
622621
$CROAK("svd requires input ndarrays to have m >= n; you have m=%td and n=%td. Try inputting the transpose. See the docs for svd.",$SIZE(m),$SIZE(n));
623622
',
624-
Code => '
625-
extern void SVD( double *W, double *Z, int nRow, int nCol );
626-
double *t = $P(w);
627-
loop (m,n) %{
628-
*t++ = $a();
629-
%}
630-
SVD($P(w), $P(z), $SIZE(m), $SIZE(n));
631-
loop (n) %{
632-
$z() = sqrt($z());
633-
%}
634-
t = $P(w);
635-
loop (m,n) %{
636-
$u() = *t++/$z();
637-
%}
638-
loop (n1,n0) %{
639-
$v() = *t++;
640-
%}
641-
',
642-
, Doc => q{
623+
CHeader => 'void SVD( double *W, double *Z, int nRow, int nCol );',
624+
Code => <<'EOF',
625+
double *t = $P(w);
626+
loop (m,n) %{ *t++ = $a(); %}
627+
SVD($P(w), $P(s), $SIZE(m), $SIZE(n));
628+
loop (n) %{ $s() = sqrt($s()); %}
629+
t = $P(w);
630+
loop (m,n) %{ $u() = *t++/$s(); %}
631+
loop (n1,n0) %{ $v() = *t++; %}
632+
EOF
633+
Doc => q{
643634
=for ref
644635

645636
Singular value decomposition of a matrix.
@@ -654,9 +645,7 @@ x n, C<$v> is n x n, and there are <=n singular values. As long as m
654645
>= n, the original matrix can be reconstructed as follows:
655646

656647
($u,$s,$v) = svd($x);
657-
$ess = zeroes($x->dim(0),$x->dim(0));
658-
$ess->slice("$_","$_").=$s->slice("$_") foreach (0..$x->dim(0)-1); #generic diagonal
659-
$a_copy = $u x $ess x $v->transpose;
648+
$a_copy = $u x stretcher($s) x $v->transpose;
660649

661650
If m==n, C<$u> and C<$v> can be thought of as rotation matrices that
662651
convert from the original matrix's singular coordinates to final

0 commit comments

Comments
 (0)