# HP Forums

Full Version: Sqrt of matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there a command for finding a SQRT of all elements in the matrix?

Like if I have a matrix (that varies in size)

[attachment=10582]

Like in this Example if I have 3x3 Matrix (M1)-> then I run cas.diag(M1) i will get matrix 1,9,16
now Is there a command to automatically find SQRT values of 1,9,16 ?
So the answer ends up being a matrix with values 1, 3, 4 ?
The cas sqrt(matrix) should help.
Code:
`apply(sqrt,[[4,1],[2,3]],matrix)`
If you run sqrt() on a matrix, it will return a matrix that equals the argument once squared.
I think what Amer7 wants is a matrix containing the square root of each element of another matrix. If that's true then here's an example of how to do that:

[[1,2],[3,4]]▶M1
MAKEMAT(√(M1(I,J)),2,2)▶M2

Now M2 contains:

[[1,1.41421356237],[1.73205080757,2]]
Yes, that's precisely what apply(...) does, sqrt on each element.
@StephenG1CMZ
This works when In CAS
@ parisse
It works but I have to know the size of the Matrix.

The issue with my programs is when I get an assignment I never hav the same MatrixDimensions. There are a lot of calculations between the Matrices, that's why I have to write these programs.

How can I call CAS in normal program. Like if I have:

Code:
```Begin Local Nn3,Mm3; Nn3:=M1+M2; Now do I write? #CAS begin Mm3:=SQRT(diag(Nn3)); END; EDITMAT(Mm3,"SQRT values of diagonal elements of Matrix Nn3"); END;```
Is this syntax of any help?
[code]
EXPORT RT()
BEGIN
M1:=[1,3,9];
M1:=CAS("sqrt(M1)");
PRINT(M1);

END;
[\code]
(04-11-2022 06:07 PM)StephenG1CMZ Wrote: [ -> ]Is this syntax of any help?
[code]
EXPORT RT()
BEGIN
M1:=[1,3,9];
M1:=CAS("sqrt(M1)");
PRINT(M1);

END;
[\code]

Yes this works
CAS("sqrt(Matrix variable)");

Thank you this will make my calculations much faster. Thank you all once again!
(04-11-2022 04:13 PM)parisse Wrote: [ -> ]Yes, that's precisely what apply(...) does, sqrt on each element.

Of course you are right. I failed to mention that I was referring to StephenG1CMZ's post.
(04-11-2022 08:35 AM)Amer7 Wrote: [ -> ]now Is there a command to automatically find SQRT values of 1,9,16 ?
So the answer ends up being a matrix with values 1, 3, 4 ?

HOME> [[1, 9, 16]] .^ 0.5      → [[1, 3, 4]]

This has an advantage by not going thru CAS, which may make sqrt inaccurate.

HOME> M1 := [[0.398, 0.571]]
HOME> M1 .^ 0.5

[[0.630872411824, 0.755645419493]]

HOME> CAS("apply(sqrt, M1)")

[[0.630872411823, 0.755645419492]]
(04-11-2022 06:24 PM)Amer7 Wrote: [ -> ]
(04-11-2022 06:07 PM)StephenG1CMZ Wrote: [ -> ]Is this syntax of any help?
[code]
EXPORT RT()
BEGIN
M1:=[1,3,9];
M1:=CAS("sqrt(M1)");
PRINT(M1);

END;
[\code]

Yes this works
CAS("sqrt(Matrix variable)");

Thank you this will make my calculations much faster. Thank you all once again!

Note: Whilst sqrt(matrix) can be useful, it's not the same as applying sqrt to each element.
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :