HP Forums
Apply Elementwise on Matrix - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Apply Elementwise on Matrix (/thread-21855.html)



Apply Elementwise on Matrix - NumericDavid - 06-07-2024 08:25 PM

Hi Everybody,

before I ask my silly question: a short introduction to myself since this is my first post in this forum. I'm a theoretical physicist by profession and do calculations on compute clusters in my job where fortran, c++ and octave are my workhorses. I'm new to HP Calculators and now a proud owner of an HP Prime G2. My other (old friends) calculators are a TI NSpire and a TI-84.

Now comes the silly question: is there a shortcut to apply a function elementwise on a matrix/vector? Imagine you have a 10D real vector in M1 and want the vector of sines of each entry.

MAKEMAT(SIN(M1(J)),10) //ugly thing solves my problem

What is the shortest form to obtain "SIN(M1)".

Best wishes,
David


RE: Apply Elementwise on Matrix - komame - 06-08-2024 05:27 AM

(06-07-2024 08:25 PM)NumericDavid Wrote:  What is the shortest form to obtain "SIN(M1)".

Hi David,

there are several ways to do this. The question is whether it needs to be a vector or if it can be a list? Lists by default work the way you want and can also be one-dimensional or multi-dimensional.
So, using a list you can execute: SIN(L1) or directly SIN({1,2,3}) and you will get the result in the form of a list.
However, if it has to be a vector, you can also handle that. You need to know that the HP Prime has two operating modes: HOME and CAS. They have their own dedicated keys on the keyboard. In the second row on the left side, there is the HOME key, and CAS is on the right side. When you turn on CAS mode, you can perform algebraic, symbolic, etc. calculations. In this mode, you can also calculate for a vector: SIN(M1), but when you press enter, an exact (or symbolic) result will appear. To get a numerical result right away, instead of just pressing Enter, press Shift + Enter, which will give you the same effect as in the HOME mode for lists. However, this behavior can be changed because in the CAS settings (Shift + CAS) there is an 'Exact' flag, if you uncheck it, a numerical result will be returned immediately after pressing Enter (without Shift).
There are many different settings in both HOME and CAS modes that affect how the HP Prime works, and once you get to know them, you can truly feel the power of this device.


RE: Apply Elementwise on Matrix - NumericDavid - 06-17-2024 08:05 PM

Thanks a lot! Those are exactly the things I need to learn.


RE: Apply Elementwise on Matrix - Wes Loewer - 06-22-2024 09:31 PM

I realize you asked about vectors, but just to clarify:
If you take the sin(M1) where M1 is a square matrix, then instead of taking the sine of each element, the Prime CAS will take the sine of the matrix as one would in Linear Algebra (diagonalizing, etc), just as the Nspire would.