Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Translating python math to Perl

by bliako (Abbot)
on Aug 25, 2023 at 11:00 UTC ( [id://11154038]=note: print w/replies, xml ) Need Help??


in reply to Translating python math to Perl

  • matrix = np.eye(3,3) creates a 3D 3x3 (thanks hv) matrix with 1's in the diagonal and 0 elsewhere. (or load it from JSON).
  • you mentioned points being byte data, np.frombuffer(points, dtype=np.float32) converts it into a list of float32. And then it swaps the bytes - re: little endian etc.
  • d.reshape(-1,6) converts above list into a matrix with number of cols = 6 and any number of rows it can fit.
  • d[:,2] the 3rd column of matrix d
  • pressure = (d[:,2] / pressure_norm) ** pressure_pow create a pressure vector based on d where each item is divided by pressure_norm and then raise to pressure_pow power. (I guess ** is raising to a power)
  • points.shape[0] is the number of rows of points (remember it was reshaped to have 6 columns and any number of rows it fits).
  • np.ones([points.shape[0],1]), create a vector of 1s (i,e. a matrix of 1 column, that's the '1' in the expression) with as many rows as the reshaped points of the last bullet
  • append that vector into points hence increase the number of rows by 1 adding a vector of 1s at the end.
  • points @ matrix.T I guess it transposes points (rows become columns) it is multiplying the matrix 'points' by the transpose of 'matrix' (see NERDVANA's Re: Translating python math to Perl).
  • points = points[:, :2] get the 3rd column (0-based indexing) of the current points

my python is very limited so all above with caution. All the above can be done with pure perl but perhaps a dedicated module or PDL can be employed.

Replies are listed 'Best First'.
Re^2: Translating python math to Perl
by NERDVANA (Curate) on Aug 25, 2023 at 20:24 UTC
    Did a quick search and found that the '@' operator is matrix multiplication.

    So that line points @ matrix.T is multiplying the matrix 'points' by the transpose of 'matrix'.

    I'd say this really should be translated to PDL, because otherwise it's going to get very verbose. That's the real reason people use matrices in linear algebra - it is just too much fiddly math otherwise, and it's all fairly repetitive. Mathmaticians created "matrix math" just to simplify the notation for these common formulas. Unfortunately I don't know PDL so I can't help there.

      Jeez! my search was talking about overwriting methods.Talk of the snake with the forked tongue. I will update my answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-09-13 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.