Sqrt of matrix
04-11-2022, 08:35 AM
Post: #1
 Amer7 Member Posts: 72 Joined: Dec 2020
Sqrt of matrix
Is there a command for finding a SQRT of all elements in the matrix?

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

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 ?
04-11-2022, 12:43 PM
Post: #2
 StephenG1CMZ Senior Member Posts: 1,042 Joined: May 2015
RE: Sqrt of matrix
The cas sqrt(matrix) should help.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
04-11-2022, 01:09 PM
Post: #3
 parisse Senior Member Posts: 1,327 Joined: Dec 2013
RE: Sqrt of matrix
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.
04-11-2022, 01:12 PM
Post: #4
 roadrunner Senior Member Posts: 449 Joined: Jun 2015
RE: Sqrt of matrix
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]]
04-11-2022, 04:13 PM
Post: #5
 parisse Senior Member Posts: 1,327 Joined: Dec 2013
RE: Sqrt of matrix
Yes, that's precisely what apply(...) does, sqrt on each element.
04-11-2022, 04:24 PM (This post was last modified: 04-11-2022 04:26 PM by Amer7.)
Post: #6
 Amer7 Member Posts: 72 Joined: Dec 2020
RE: Sqrt of matrix
@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;
04-11-2022, 06:07 PM
Post: #7
 StephenG1CMZ Senior Member Posts: 1,042 Joined: May 2015
RE: Sqrt of matrix
Is this syntax of any help?
[code]
EXPORT RT()
BEGIN
M1:=[1,3,9];
M1:=CAS("sqrt(M1)");
PRINT(M1);

END;
[\code]

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
04-11-2022, 06:24 PM
Post: #8
 Amer7 Member Posts: 72 Joined: Dec 2020
RE: Sqrt of matrix
(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-12-2022, 12:58 AM
Post: #9
 roadrunner Senior Member Posts: 449 Joined: Jun 2015
RE: Sqrt of matrix
(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-12-2022, 12:39 PM
Post: #10
 Albert Chan Senior Member Posts: 2,712 Joined: Jul 2018
RE: Sqrt of matrix
(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-12-2022, 01:50 PM
Post: #11
 StephenG1CMZ Senior Member Posts: 1,042 Joined: May 2015
RE: Sqrt of matrix
(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.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
 « Next Oldest | Next Newest »

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