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)?
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)?
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,
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
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))
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).
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)]
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))
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2024-05-17 00:20:50 +0100
Seen: 414 times
Last updated: May 19