Hi Luis,

Any unexpected behaviour is worth asking about, so thank you (as so often)! In 
particular, anything that breaks an expectation of being able to chain 
operations in an OO style is bad.

I’ve just pushed what I believe is a fix on GitHub, so please give that a go if 
you like. I’m holding off on the next release of PDL for another couple of days 
since PDL::OpenCV (which depends on a PDL fix for [o] OtherPars which is on GH, 
and is imminent!) may need another couple of fixes, and I don’t want to cause 
unnecessary work for packagers and users with too-rapid releases.

Best regards,
Ed

From: Luis Mochan<mailto:moc...@icf.unam.mx>
Sent: 14 February 2023 19:05
To: Ed .<mailto:ej...@hotmail.com>
Cc: perldl<mailto:pdl-general@lists.sourceforge.net>; 
perldl<mailto:pdl-de...@lists.sourceforge.net>
Subject: Re: [Pdl-devel] inconsistent determinant

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-general@lists.sourceforge.net>; 
> perldl<mailto:pdl-de...@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-de...@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-general mailing list
pdl-general@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pdl-general

Reply via email to