Matrix-exponentiation operator: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 1,236:
89 55
55 34</lang>
 
=={{header|Kotlin}}==
<lang scala>// version 1.1.3
 
typealias Vector = DoubleArray
typealias Matrix = Array<Vector>
 
operator fun Matrix.times(other: Matrix): Matrix {
val rows1 = this.size
val cols1 = this[0].size
val rows2 = other.size
val cols2 = other[0].size
require(cols1 == rows2)
val result = Matrix(rows1) { Vector(cols2) }
for (i in 0 until rows1) {
for (j in 0 until cols2) {
for (k in 0 until rows2) {
result[i][j] += this[i][k] * other[k][j]
}
}
}
return result
}
 
fun identityMatrix(n: Int): Matrix {
require(n >= 1)
val ident = Matrix(n) { Vector(n) }
for (i in 0 until n) ident[i][i] = 1.0
return ident
}
 
infix fun Matrix.pow(n : Int): Matrix {
require (n >= 0 && this.size == this[0].size)
if (n == 0) return identityMatrix(this.size)
if (n == 1) return this
var pow = identityMatrix(this.size)
var base = this
var e = n
while (e > 0) {
if ((e and 1) == 1) pow *= base
e = e shr 1
base *= base
}
return pow
}
 
fun printMatrix(m: Matrix, n: Int) {
println("** Power of $n **")
for (i in 0 until m.size) println(m[i].contentToString())
println()
}
 
fun main(args: Array<String>) {
val m = arrayOf(
doubleArrayOf(3.0, 2.0),
doubleArrayOf(2.0, 1.0)
)
for (i in 0..10) printMatrix(m pow i, i)
}</lang>
 
{{out}}
<pre>
** Power of 0 **
[1.0, 0.0]
[0.0, 1.0]
 
** Power of 1 **
[3.0, 2.0]
[2.0, 1.0]
 
** Power of 2 **
[13.0, 8.0]
[8.0, 5.0]
 
** Power of 3 **
[55.0, 34.0]
[34.0, 21.0]
 
** Power of 4 **
[233.0, 144.0]
[144.0, 89.0]
 
** Power of 5 **
[987.0, 610.0]
[610.0, 377.0]
 
** Power of 6 **
[4181.0, 2584.0]
[2584.0, 1597.0]
 
** Power of 7 **
[17711.0, 10946.0]
[10946.0, 6765.0]
 
** Power of 8 **
[75025.0, 46368.0]
[46368.0, 28657.0]
 
** Power of 9 **
[317811.0, 196418.0]
[196418.0, 121393.0]
 
** Power of 10 **
[1346269.0, 832040.0]
[832040.0, 514229.0]
</pre>
 
=={{header|Liberty BASIC}}==
9,476

edits