# Matrix function

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 close merge delete

Sort by » oldest newest most voted

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,

more

Thank you very much!

( 2024-05-19 15:00:11 +0200 )edit

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

more

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

( 2024-05-17 22:32:40 +0200 )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))

( 2024-05-18 15:44:44 +0200 )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).

( 2024-05-18 18:28:32 +0200 )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)]

( 2024-05-19 12:17:35 +0200 )edit

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))

more

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

( 2024-05-17 11:54:54 +0200 )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))

( 2024-05-19 07:37:02 +0200 )edit