Post Reply 
Apply Elementwise on Matrix
06-07-2024, 08:25 PM
Post: #1
Apply Elementwise on Matrix
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
Find all posts by this user
Quote this message in a reply
06-08-2024, 05:27 AM (This post was last modified: 06-08-2024 07:23 AM by komame.)
Post: #2
RE: Apply Elementwise on Matrix
(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.

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
06-17-2024, 08:05 PM
Post: #3
RE: Apply Elementwise on Matrix
Thanks a lot! Those are exactly the things I need to learn.
Find all posts by this user
Quote this message in a reply
06-22-2024, 09:31 PM
Post: #4
RE: Apply Elementwise on Matrix
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.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)