Hi Ed, Thanks for looking at this. It was not too important, but it was surprising, as I was chaining pdl methods (...->det->approx(0) to detetct singular matrices) and I was getting somewhat strange error messages as det was sometimes an ndarray and other times a scalar. Regards, Luis
On Sat, Feb 11, 2023 at 04:49:39AM +0000, Ed . wrote: > Hi Luis, > > The key (if you’ll forgive the pun) is the behaviour of the “det” function, > which is (at this writing) defined in Basic/MatrixOps/matrixops.pd as: > > sub det { > my($x) = shift; > my($opt) = shift; > $opt = {} unless defined($opt); > my($lu,$perm,$par); > if(exists ($opt->{lu}) and (ref $opt->{lu} eq 'ARRAY')) { > ($lu,$perm,$par) = @{$opt->{lu}}; > } else { > ($lu,$perm,$par) = lu_decomp($x); > $opt->{lu} = [$lu,$perm,$par] > if(exists($opt->{lu})); > } > ( (defined $lu) ? $lu->diagonal(0,1)->prodover * $par : 0 ); > } > > You will see the last line returns a Perl scalar 0 if the $lu came back > undefined, otherwise it will do a prodover on the diagonal, which returns a > PDL ndarray. Here is my slightly-modified version of your script: > > use strict; use warnings; use feature 'say'; > use PDL; > my $m1=pdl[[-2,-2,-2], [-1,-2,-2], [0,-2,-2]]; > my $m2=pdl[[-2,-2,-2],[-1,-1,-2],[0,0,-2]]; > for($m1, $m2){ > my $det = $_->det(my $opt={lu=>undef}); > say "Got det=$det (Ref=@{[ref $det]}) lu=(@{$opt->{lu}}) for Matrix=$_"; > } > > I will look in a while at why the lu_decomp is behaving differently for the > two matrices. It is almost certainly a bug. > > Best regards, > Ed > > From: Luis Mochan<mailto:moc...@icf.unam.mx> > Sent: 11 February 2023 04:20 > To: perldl<mailto:pdl-gene...@lists.sourceforge.net>; > perldl<mailto:pdl-devel@lists.sourceforge.net> > Subject: [Pdl-devel] inconsistent determinant > > I found a curious situation when running pdl, as illustrated in the > following example. I build two singular 3x3 matrices and calculate > their determinant, which is zero. However, in one case I get a PDL > scalar and in the other I get a Perl scalar. I don't know why the > different behaviour: > > # Program > use v5.36; > use PDL; > my $m1=pdl[[-2,-2,-2], [-1,-2,-2], [0,-2,-2]]; > my $m2=pdl[[-2,-2,-2],[-1,-1,-2],[0,0,-2]]; > for($m1, $m2){ > say "Matrix=$_", "Det=", $_->det, "\nRef=", ref $_->det; > } > > > #Results: > Matrix= > [ > [-2 -2 -2] > [-1 -2 -2] > [ 0 -2 -2] > ] > Det=0 > Ref=PDL > Matrix= > [ > [-2 -2 -2] > [-1 -1 -2] > [ 0 0 -2] > ] > Det=0 > Ref= > > I'm currently running PDL v2.081 under Perl 5.36 in Linux/testing. > > Best regards, > Luis > > > > -- > > o > W. Luis Mochán, | tel:(52)(777)329-1734 /<(*) > Instituto de Ciencias Físicas, UNAM | fax:(52)(777)317-5388 `>/ /\ > Av. Universidad s/n CP 62210 | (*)/\/ \ > Cuernavaca, Morelos, México | moc...@fis.unam.mx /\_/\__/ > GPG: 791EB9EB, C949 3F81 6D9B 1191 9A16 C2DF 5F0A C52B 791E B9EB > > > _______________________________________________ > pdl-devel mailing list > pdl-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/pdl-devel > -- o W. Luis Mochán, | tel:(52)(777)329-1734 /<(*) Instituto de Ciencias Físicas, UNAM | fax:(52)(777)317-5388 `>/ /\ Av. Universidad s/n CP 62210 | (*)/\/ \ Cuernavaca, Morelos, México | moc...@fis.unam.mx /\_/\__/ GPG: 791EB9EB, C949 3F81 6D9B 1191 9A16 C2DF 5F0A C52B 791E B9EB _______________________________________________ pdl-devel mailing list pdl-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pdl-devel