in reply to Re: first stumbling steps in PDL
in thread first stumbling steps in PDL

A slightly more concise way to get a lower triangular matrix of 1s:
use PDL::LinearAlgebra; sub ltm { my ($size, $strictly) = @_; my $ltm = ones($size,$size)->tricpy(1); $strictly ? $ltm - identity($size) : $ltm; }

Replies are listed 'Best First'.
Re^3: first stumbling steps in PDL
by jo37 (Curate) on Feb 02, 2024 at 20:54 UTC

    Is there an advantage of using tricpy over mtri? At least mtri may be called without argument when an upper triangle is requested, while with tricpy it is always required.

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      I'd say use whatever you find works! tricpy should clearly allow a default value of 0. Feel like making a PR? You'd have to make a PMCode function :-)

        Opened an PR.

        PS:
        The second upvote for this node promoted me to Deacon.
        Thank you all!

        Greetings,
        -jo

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

        Now I have two different solutions to cope with a missing argument to tricpy. Both meet the target, as all these statements are valid and produce the same result:

        tricpy($m, 0, $c); $c = $m->tricpy(0); $c = $m->tricpy
        Though I like the first solution more, it raises a documentation issue.

        First:

        pp_def( 'tricpy', Pars => 'A(m,n);[o] C(m,n)', OtherPars => 'int uplo', OtherParsDefaults => {uplo => 0}, ArgOrder => [qw(A uplo C)], Code => ' ... ', Doc => <<EOT =for ref Copy triangular part to another matrix. If uplo == 0 copy upper triang +ular part. =cut EOT

        Second:

        pp_def( 'tricpy', Pars => 'A(m,n);uplo();[o] C(m,n)', PMCode => 'sub PDL::tricpy { return PDL::_tricpy_int(@_) if @_ == 3; my ($A, $uplo) = @_; PDL::_tricpy_int($A, $uplo // 0, my $C = PDL->null); $C; }', Code => ' ... ', Doc => <<EOT =for ref Copy triangular part to another matrix. If uplo == 0 copy upper triang +ular part. =cut EOT );

        The generated POD for the first version is misleading, as the signature doesn't conform to the actual argument order:

        tricpy Signature: (A(m,n);[o] C(m,n); int uplo) Copy triangular part to another matrix. If uplo == 0 copy upper triangular part. tricpy does not process bad values. It will set the bad-value flag + of all output ndarrays if the flag is set for any of the input ndarra +ys.

        Any suggestions?

        Greetings,
        -jo

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
        Feel like making a PR?

        Sounds like an interesting challenge. I'll give it a try.

        Greetings,
        -jo

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re^3: first stumbling steps in PDL
by etj (Priest) on Feb 04, 2024 at 23:07 UTC
    Today it was bugging me that there is no tritosquare, to provide another way to make a lower-triangular matrix. Now there is, on git master and soonish to be released:
    pdl> p ones(10)->tritosquare [ [1 0 0 0] [1 1 0 0] [1 1 1 0] [1 1 1 1] ]
Re^3: first stumbling steps in PDL
by jo37 (Curate) on Feb 06, 2024 at 08:30 UTC

    A slightly different approach for a strictly lower triangular matrix:

    use PDL::LinearAlgebra::Real ... tricpy(zeros($size,$size), 0, my $sltm = ones($size, $size));

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      Beautiful, with fewer operations!