Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: RFC: 100 PDL Exercises (ported from numpy)

by vr (Curate)
on May 03, 2018 at 17:12 UTC ( #1214017=note: print w/replies, xml ) Need Help??


in reply to RFC: 100 PDL Exercises (ported from numpy)

I'll pick an easy one of "TODO" first and will add some more later, in no order -- so there'll be a few edits, return for more :-)

99. Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n:

use strict; use warnings; use PDL; my $x = pdl([1.0, 0.0, 3.0, 8.0], [2.0, 0.0, 1.0, 1.0], [1.5, 2.5, 1.0, 0.0]); my $n = 4; my $mask = ( $x == $x-> rint )-> andover & ( $x-> sumover == $n ); print $x-> transpose -> whereND( $mask ) -> transpose;

--------------------------

Added:

--------------------------

print $PDL::Version::VERSION;

Is it officially recommended way? What's the benefit over usual $PDL::VERSION;? I also wonder what's the $VERSION = eval $VERSION; in that tiny module.

--------------------------

4. How to find the memory size of any matrix?

print $z-> info( '%M' );

--------------------------

35. How to compute ((A+B)*(-A/2)) in place?

There's maybe a typo or two in Python solution (what's the "C"?). But to operate inplace, I'd do this:

my $a = ones(3); my $b = 2 * ones(3); $b += $a; $a /= -2; $b *= $a; print $b;

IIRC, combined assignment operators are overloaded to work inplace, but I can't find a reference right now, will do it later.

--------------------------

53. How to convert a float (32 bits) array into an integer (32 bits) in place?

Good question. The ceil and floor convert double to long inplace. Not sure if PDL allows to do so for 32-bit types.

--------------------------

45. Create random vector of size 10 and replace the maximum value by 0:

More efficient:

my $z = random( 10 ); $z( $z-> maximum_ind ) .= 0; print $z;

--------------------------

64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)?

my $z = zeroes( 10 ); my $i = pdl( 1, 3, 5, 3, 1 ); indadd( 1, $i, $z ); print $z;

--------------------------

81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]:

my $z = 1 + sequence 14; my $len = 4; print $z-> lags( 0, 1, 1 + $z-> nelem - $len ) -> slice( '','-1:0' );

--------------------------

87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)?

If I understand the task correctly, and now that I've learned about lags:

my $x = sequence 16, 16; print $x-> lags( 1, 4, 4 ) -> slice( '', '', '-1:0' ) -> xchg( 0, 1 ) -> sumover -> lags( 0, 4, 4 ) -> slice( '', '-1:0' ) -> sumover;

(Sigh...) Utilizing benefits of idle commuting and thinking things over:

my $x = sequence 16, 16; print $x-> reshape( 4, 4, 4, 4 ) -> reorder( 0, 2, 1, 3 ) -> clump( 2 ) -> sumover

Replies are listed 'Best First'.
Re^2: RFC: 100 PDL Exercises (ported from numpy)
by mxb (Pilgrim) on May 04, 2018 at 07:37 UTC

    Thanks for all of these

    I assume you wouldn't mind me merging the solutions into my 'master' POD, with credit of course.

    Just from reading your solutions I learned a number of new functions, including maximum_ind (and it's sister functions maximum_n_ind, minimum_ind, minimum_n_ind and max2d_ind) and lags. This is what I hoped would happen, as everyone seems to know a different subset of PDL functionality.

      Hi, mxb, of course I woudn't mind.

      + Note, your recipe #66 doesn't do what's expected, -- but to shift bytes left manually, as Python guys do, isn't nice neither. I'd do this:

      pdl> $x = sequence 2,2,3 # "2x2 planar RGB" image, 4 unique colors pdl> $x = $x-> glue( 0, $x ) # "4x2 RGB" image, 4 unique colors pdl> $x = $x-> glue( 1, $x ) # "4x4 RGB" image, still 4 unique colors pdl> $x-> set( 2,2,2, 100 ) # make them 5 pdl> p$x [ [ [0 1 0 1] [2 3 2 3] [0 1 0 1] [2 3 2 3] ] [ [4 5 4 5] [6 7 6 7] [4 5 4 5] [6 7 6 7] ] [ [ 8 9 8 9] [ 10 11 10 11] [ 8 9 100 9] [ 10 11 10 11] ] ] pdl> p $x-> clump(2)-> transpose-> uniqvec-> getdim( 1 ) 5

      -----------------

      As to combined assignment operators working in-place, here is simple experiment (Windows), either line #1 or #2 un-commented on different runs:

      use strict; use warnings; use feature 'say'; use PDL; my $x = zeroes 1e8; my $y = ones 1e8; $x = $x + $y; # 1 #$x += $y; # 2 say qx{ typeperf "\\Process(perl)\\Working Set Peak" -sc 1 } =~ /.+"(.+)"/s; __END__ >perl pdl180504.pl 2427752448.000000 >perl pdl180504.pl 1627779072.000000

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2020-07-12 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?