Matrix-exponentiation operator: Difference between revisions

Content added Content deleted
(Added Kotlin)
(+Stata)
Line 2,424: Line 2,424:


Domain:[http://fricas.github.io/api/Matrix.html?highlight=matrix Matrix(R)]
Domain:[http://fricas.github.io/api/Matrix.html?highlight=matrix Matrix(R)]

=={{header|Stata}}==

This implementation uses [https://en.wikipedia.org/wiki/Exponentiation_by_squaring Exponentiation by squaring] to compute a^n for a matrix a and an integer n (which may be positive, negative or zero).

<lang stata>real matrix matpow(real matrix a, real scalar n) {
real matrix p, x
real scalar i, s
s = n<0
n = abs(n)
x = a
p = I(rows(a))
for (i=n; i>0; i=floor(i/2)) {
if (mod(i,2)==1) p = p*x
x = x*x
}
return(s?luinv(p):p)
}</lang>

Here is an example to compute Fibonacci numbers:

<lang stata>: matpow((0,1\1,1),10)
[symmetric]
1 2
+-----------+
1 | 34 |
2 | 55 89 |
+-----------+</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==