Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I've been trying to slowly learn PDL over the last few months. While I'm aware of some available documentation (the PDL::* perldoc, The PDL Book, etc.) I've found the beginner documentation to be lacking. Therefore, I thought it would be a good idea to start 'porting' some numpy documentation to PDL for new users such as myself.

I've started with 100 numpy exercises, and this is the work in progress port to Perl/PDL.

As I'm still learning PDL, some solutions may be less than optimal, while others do not currently have solutions as they are outside my of level of competency. Therefore, I'm posting this WIP to PM to ask for comments and contributions.

As with most of Perl, there is more than one way to do it for most of these. I've decided to keep the $var->function() syntax as much as possible to easily be able to chain operations.

Thanks in advance!

100 PDL Exercises

1. Load the PDL library:
use PDL;
2. Print the PDL version:
use PDL::Version; print $PDL::Version::VERSION;
3. Create a null vector of size 10:
my $z = zeros(10); print $z;
4. How to find the memory size of any matrix?
use PDL::Core ':Internal' my $z = zeros(10); my $size = howbig($z->get_datatype) * $z->nelem; print $size;
5. How to get the documentation of the numpy add function from the command line?
# To get top level PDL help perldoc PDL
6. Create a null vector of size 10 and set the fifth value to 1:
my $z = zeros(10); $z->slice(4) .= 1; print $z;

or with PDL::NiceSlice loaded:

my $z = zeros(10); $z(4) .= 1; print $z;

Note: It will be assumed that PDL::NiceSlice will be loaded from now on.

7. Create a vector with values ranging from 10 to 49:
my $z = 10 + sequence(40); print $z;
8. Reverse a vector (first element becomes last)
my $z = sequence(10); $z = $z(-1:0); print $z;
9. Create a 3x3 matrix with values from 0 to 8:
my $z = sequence(3,3); print $z;
10. Find indices of non-zero elements in the vector [1,2,0,0,4,0]:
my $vec = pdl [1,2,0,0,4,0]; my $nz = $vec->which; print $nz;
11. Create a 3x3 identity matrix:
my $z = identity(3); print $z;
12. Create a 3x3x3 matrix of random values:
my $z = random(3,3,3); print $z;
13. Create a 10x10 matrix of random values and find the minimum and maximum values:
my $z = random(10,10); print $z->min, $z->max;
14. Create a random vector of size 30 and find the mean value:
my $z = random(30); print $z->avg;
15. Create a 2D matrix with 1 on the border and 0 inside:
my $z = ones(10,10); $z(1:8,1:8) .= 0; print $z;
16. How to add a border (filled with 0's) around an existing matrix?
my $a = random(8,8); my $x = zeros(2 + $a->dim(0), 2 + $a->dim(1)); $x(1:8,1:8) .= $a; print $x;
17. What is the result of the following expression?
n/a
18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal:
my $z = identity(5) * (1 + sequence(5)); $z->where($z > 4) .= 0; $z = $z->transpose->rotate(1)->transpose; print $z;
19. Create a 8x8 matrix and fill it with a checkerboard pattern:
my $z = zeros(8,8); $z("0::2","0::2") .= 1 $z("1::2","1::2") .= 1 print $z;
20. Consider a (6,7,8) shape matrix, what is the index (x,y,z) of the 100th element?
my $z = random(6,7,8); my $hundreth = $z->clump($z->ndims)->(100); # TODO: find index of $hundreth in $z
21. Create a checkerboard 8x8 matrix using the tile function:
n/a
22. Normalize a 5x5 random matrix:
my $z = random(5,5); $z = (($z - $z->min) / ($z->max - $z->min)); print $z;
23. Create a custom dtype that describes a color as four unsigned bytes (RGBA):
n/a
24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product):
my $x = ones(3,5); my $y = ones(2,3); print $x x $y;
25. Given a 1D matrix, negate all elements which are between 3 and 8, in place.
my $z = sequence(10); $z->where($z <= 8 & $z>= 3) *= -1; print $z;
26. What is the output of the following script?
n/a
27. Consider an integer vector Z, which of these expressions are legal?
my $z = sequence(long, 10); $z ** $z; 2 << $z >> 2; $z <- $z; 1j * $z; $z / 1 / 1; $z < $z > $z;
28. What are the result of the following expressions?
print pdl(0) / pdl(0); print pdl(0) // pdl(0); print float int pdl(NaN);
29. How to round away from zero a float matrix?
$z = 20 * random(10) - 10; $z->where($z < 0) .= - $z->where($z < 0)->abs->ceil; $z->where($z > 0) .= $z->where($z > 0)->ceil; print $z;
30. How to find common values between two matrix?
my $z1 = long 256 * random(10); my $z2 = long 256 * random(10); print intersect $z1, $z2;
31. How to ignore all numpy warnings (not recommended)?
n/a for PDL
32. Is the following expressions true?
n/a
33. How to get the dates of yesterday, today and tomorrow?
# No built in PDL time/date functions my $yesterday = time() - (60 * 60 * 24); my $today = time(); my $tomorrow = time() + (60 * 60 * 24);
34. How to get all the dates corresponding to the month of July 2016?
n/a
35. How to compute ((A+B)*(-A/2)) in place?
my $a = ones(3); my $b = 2 * ones(3); my $c = 3 * ones(3); print ($a + $b) * (- $a/2)
36. Extract the integer part of a random matrix using 5 different methods:
my $z = 10 * random(10); print $z->ceil; print $z->floor; print byte $z; print long $z; print longlong $z;
37. Create a 5x5 matrix with row values ranging from 0 to 4:
my $z = xvals zeros(5,5); print $z;
38. Consider a generator function that generates 10 integers and use it to build an matrix:
n/a
39. Create a vector of size 10 with values ranging from 0 to 1, both excluded:
my $z = (sequence(12) / 11)->slice("1:10"); print $z;
40. Create a random vector of size 10 and sort it:
my $z = random(10)->qsort; print $z;
41. How to sum a small matrix faster than np.sum?
n/a
42. Consider two random matrices A and B, check if they are equal:
my $a = random(10); my $b = random(10); print $a == $b;
43. Make an array immutable (read-only):
n/a
44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates:
use PDL::Complex; my $z = random(2,10); my $p = Cr2p($z); print $p;
45. Create random vector of size 10 and replace the maximum value by 0:
my $z = random(10); $z->where($z == $z->max) .= 0; print $z;
46. Create a structured array with x and y coordinates covering the [0,1]x[0,1] area.
n/a
47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)):
TODO
48. Print the minimum and maximum representable value for each data type:
# This cannot be done directly, but you can extract the underlying # C type used for each PDL type: print byte->realctype; print short->realctype; print ushort->realctype; print long->realctype; print longlong->realctype; print indx->realctype; print float->realctype; print double->realctype;
49. How to print all the values of an array?
# Set maximum print limit to one million elements $PDL::toolongtoprint = 1_000_000; $z = zeros(1000,1000); print $z;
50. Find the nearest value from a given value in an array:
TODO
51. Create a structured array representing a position (x,y) and a color (r,g,b):
n/a
52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances:
my $z = random(10,2); my ($x,$y) = ($z(:,0), $z(:,1)); my $d = (($x - $x->transpose)->ipow(2)) + (($y - $y->transpose)->ipow(2)); print $d;
53. How to convert a float (32 bits) array into an integer (32 bits) in place?
my $z = float 1000 * random(10); $z = long $z;
54. Consider the following file:
1,2,3,4,5 6,,,7,8 ,,9,10,11

How to read it?

my $z = rcols "data.csv", { COLSEP => ',' }, []; $z = $z->transpose; # optional (PDL is column major) print $z;
55. What is the equivalent of enumerate for numpy arrays?
n/a
56. Generate a generic 2D Gaussian-like array:
my $z = grandom(10,10); # correct? print $z;
57. How to randomly place p elements in a 2D array?
my $p = 3; my $z = zeros(10,10); my $i = indx $z->nelem * random($p) $z->clump($z->ndims)->($i) .= 1; print $z;
58. Subtract the mean of each row of a matrix:
my $z = random(5, 10); $z = $z - $z->avgover->transpose; print $z;
59. How to I sort an array by the nth column?
TODO
60. How to tell if a given 2D array has null columns?
TODO
61. Find the nearest value from a given value in an array:
TODO
62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator?
n/a
63. Create an array class that has a name attribute:
n/a
64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)?
TODO
65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)?
TODO
66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors:
my ($w, $h) = (16, 16); my $i = byte 256 * random($w, $h, 3); my $uniqcol = $i->uniq->nelem; print $uniqcol;
67. Considering a four dimensions array, how to get sum over the last two axis at once?
TODO
68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices?
TODO
69. How to get the diagonal of a dot product?
my $z1 = random(10, 10); my $z2 = random(10, 10); print $z1->inner($z2);
70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value ?
my $z = pdl [1,2,3,4,5]; my $nz = 3; my $x = zeros($z->dim(0) * $nz); $x("0::$nz") .= $z; print $x;
71. Consider an array of dimension (5,5,3), how to multiply it by an array with dimensions (5,5)?
my $z1 = ones(5,5,3); my $z2 = 2 * ones(5,5); print $z1 * $z2;
72. How to swap two rows of an array?
my $z = sequence(5,5); $z(0:1,) .= $z(1:0,)->sever; print $z;
73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles:
TODO
74. Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C?
TODO
75. How to compute averages using a sliding window over an array?
TODO
76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1])
TODO
77. How to negate a boolean, or to change the sign of a float inplace?
my $z = long 2 * random(10); $z = not $z; print $z; $z = -5 + sequence(10); $z = -1 * $z; print $z;
78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])?
TODO
79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])?
TODO
80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a fill value when necessary):
TODO
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]]:
$z = 10 * random(15); $len = 4; my @r = (); push @r, $z($_:$_ + $len-1) for (0 .. $z->nelem - $len) $r = pdl @r; print $r;
82. Compute a matrix rank:
my $z = 10 * random(10,10); my ($u, $s, $v) = $z->svd; my $rank = $s->where($s > 1e-10); print $rank;
83. How to find the most frequent value in an array?
TODO
84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix:
my $z = long 5 * random(10,10); my $dim = 3; my (@out, $out); for my $i ( 0 .. $z->dim(0) - $dim - 1) { for my $j ( 0 .. $z->dim(1) - $dim - 1) { push @out, $z($i:$i+$dim,$j:$j+$dim); } } $out = pdl @out; print $out;
85. Create a 2D array subclass such that Z[i,j] == Z[j,i]:
TODO
86. Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1))
TODO
87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)?
TODO
88. How to implement the Game of Life using PDL arrays?
TODO
89. How to get the n largest values of an array:
my $z = 10 * random(20); my $n = 3; print $z->qsort->(-$n:);
90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item)
TODO
91. How to create a record array from a regular array?
n/a?
92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods:
my $z = random(5e7); $z ** 3; $z->ipow(3); $z->power(3,0);
93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B?
TODO
94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]):
TODO
95. Convert a vector of ints into a matrix binary representation:
my $z = pdl [0,1,2,3,15,16,32,64,128]; my $bits = ($z->transpose & (2 ** xvals(9))); $bits->where($bits > 0) .= 1; print $bits;
96. Given a two dimensional array, how to extract unique rows?
my $z = long 2 * random(3,6); print $z->uniqvec;
97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function:
TODO
98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples:
TODO
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:
TODO
100. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means):
TODO

edit: link to PDL for those who do not know what it is


In reply to RFC: 100 PDL Exercises (ported from numpy) by mxb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-28 18:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found