Ask Your Question
0

Matrix function

asked 2024-05-17 00:20:50 +0100

Roman1988 gravatar image

updated 2024-05-17 00:55:19 +0100

How do you calculate a matrix function in Sage? For example, exp is easily calculated as exp(A) or A.exp(). And how do you calculate sin(A), сos(A), ln(A)?

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
1

answered 2024-05-19 14:19:38 +0100

Emmanuel Charpentier gravatar image

updated 2024-05-19 14:22:24 +0100

It turns out that Sage's various interpreters do have ways to get matrix trig functions :

giac has matrix trig functions :

sage: matrix(giac.sin(A)._sage_())
[              sin(2)     -sin(2) + sin(1) -3*sin(2) + 3*sin(1)]
[-3*sin(2) + 3*sin(1)  3*sin(2) - 2*sin(1)  9*sin(2) - 9*sin(1)]
[     sin(2) - sin(1)     -sin(2) + sin(1) -3*sin(2) + 4*sin(1)]

sympy has not matrix trig functions. But it has matrix exponential (and logarithm), which can be used to find matrix trigonoimetric functions, as noted by @Max Alekseyev :

sage: matrix([[u for u in v] for v in ((I*A)._sympy_().exp()-(-I*A)._sympy_().ex
....: p())._sage_()/2/I]).apply_map(lambda u:u.demoivre(force=True))
[              sin(2)     -sin(2) + sin(1) -3*sin(2) + 3*sin(1)]
[-3*sin(2) + 3*sin(1)  3*sin(2) - 2*sin(1)  9*sin(2) - 9*sin(1)]
[     sin(2) - sin(1)     -sin(2) + sin(1) -3*sin(2) + 4*sin(1)]

You can cheat and use Mathematica, or the (gratis but not free) Wolfram engine :

sage: matrix(sin._mathematica_().MatrixFunction(A).sage())
[              sin(2)     -sin(2) + sin(1) -3*sin(2) + 3*sin(1)]
[-3*sin(2) + 3*sin(1)  3*sin(2) - 2*sin(1)  9*sin(2) - 9*sin(1)]
[     sin(2) - sin(1)     -sin(2) + sin(1) -3*sin(2) + 4*sin(1)]

One notes that conversions of matrices to/from interpreters may be problematic which implies some workarounds in the previous examples...

HTH,

edit flag offensive delete link more

Comments

Thank you very much!

Roman1988 gravatar imageRoman1988 ( 2024-05-19 15:00:11 +0100 )edit
0

answered 2024-05-17 15:48:44 +0100

Max Alekseyev gravatar image

updated 2024-05-17 15:51:06 +0100

You can define trigonometric functions via exp() as:

matsin = lambda M: ((I*M).exp() - (-I*M).exp())/2/I
matcos = lambda M: ((I*M).exp() + (-I*M).exp())/2

For logarithm, please see this Q&A: https://math.stackexchange.com/q/3116315

edit flag offensive delete link more

Comments

Thanks, but it doesn't compute in all cases. For example, for the matrix A = matrix(QQ, 3, [2, -1, -3, -3, 4, 9, 1, -1, -2]) error: RuntimeError: ECL says: Unable to find the spectral representation

Roman1988 gravatar imageRoman1988 ( 2024-05-17 22:32:40 +0100 )edit

It's a known issue with Maxima - see https://ask.sagemath.org/question/23784/

A workaround is to define A over CDF, or change its ring on fly:

matcos(A.change_ring(CDF))
Max Alekseyev gravatar imageMax Alekseyev ( 2024-05-18 15:44:44 +0100 )edit

Thanks, that helps, although it gives an approximate result. Is there no way to get an exact solution in Sage? The matrix has integer eigenvalues and sin(A) = {{Sin[2], Sin[1] - Sin[2], 3 Sin[1] - 3 Sin[2]}, {3 Sin[1] - 3 Sin[2], -2 Sin[1] + 3 Sin[2], -9 Sin[1] + 9 Sin[2]}, {-Sin[1] + Sin[2], Sin[1] - Sin[2], 4 Sin[1] - 3 Sin[2]}} (computed in Wolfram).

Roman1988 gravatar imageRoman1988 ( 2024-05-18 18:28:32 +0100 )edit

FWIW, sympy has matrix exponential and logarithm (but no matrix trig...). These can be used by Sage :

sage: A = matrix(QQ, 3, [2, -1, -3, -3, 4, 9, 1, -1, -2]) ; A
[ 2 -1 -3]
[-3  4  9]
[ 1 -1 -2]
sage: (((I*A)._sympy_().exp()-(-I*A)._sympy_().exp())._sage_()/2/I).apply_map(lambda u:u.demoivre(force=True))
[              sin(2)     -sin(2) + sin(1) -3*sin(2) + 3*sin(1)]
[-3*sin(2) + 3*sin(1)  3*sin(2) - 2*sin(1)  9*sin(2) - 9*sin(1)]
[     sin(2) - sin(1)     -sin(2) + sin(1) -3*sin(2) + 4*sin(1)]
Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-05-19 12:17:35 +0100 )edit
-1

answered 2024-05-17 11:22:04 +0100

tolga gravatar image

Mapping the function to the matrix can be useful:

A = matrix([[0.1,0.2],[0.3,0.4]])
matsin(x)=sin(x)
matlog(x)=log(x)
print(A.apply_map(matsin))
print(A.apply_map(matlog))
edit flag offensive delete link more

Comments

Thank you, but that's not it. This is simply calculating the function from each element of the matrix.

Roman1988 gravatar imageRoman1988 ( 2024-05-17 11:54:54 +0100 )edit

You can use Python modules in Sagemath. What about the following solution?

import numpy as np

A = matrix(QQ, 3, [2, -1, -3, -3, 4, 9, 1, -1, -2])
print(np.sin(A))
print(np.cos(A))
print(np.log(A))
tolga gravatar imagetolga ( 2024-05-19 07:37:02 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2024-05-17 00:20:50 +0100

Seen: 436 times

Last updated: May 19