September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,12 +1,32 @@
|
|||
import Data.List
|
||||
import Data.List (transpose)
|
||||
|
||||
xs <+> ys = zipWith (+) xs ys
|
||||
xs <*> ys = sum $ zipWith (*) xs ys
|
||||
(<+>)
|
||||
:: Num a
|
||||
=> [a] -> [a] -> [a]
|
||||
(<+>) = zipWith (+)
|
||||
|
||||
newtype Mat a = Mat [[a]] deriving (Eq, Show)
|
||||
(<*>)
|
||||
:: Num a
|
||||
=> [a] -> [a] -> a
|
||||
(<*>) = (sum .) . zipWith (*)
|
||||
|
||||
instance Num a => Num (Mat a) where
|
||||
newtype Mat a =
|
||||
Mat [[a]]
|
||||
deriving (Eq, Show)
|
||||
|
||||
instance Num a =>
|
||||
Num (Mat a) where
|
||||
negate (Mat x) = Mat $ map (map negate) x
|
||||
Mat x + Mat y = Mat $ zipWith (<+>) x y
|
||||
Mat x * Mat y = Mat [[xs <*> ys | ys <- transpose y] | xs <- x]
|
||||
Mat x + Mat y = Mat $ zipWith (<+>) x y
|
||||
Mat x * Mat y =
|
||||
Mat
|
||||
[ [ xs Main.<*> ys -- Main prefix to distinguish fron applicative operator
|
||||
| ys <- transpose y ]
|
||||
| xs <- x ]
|
||||
abs = undefined
|
||||
fromInteger _ = undefined -- don't know dimension of the desired matrix
|
||||
signum = undefined
|
||||
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
main :: IO ()
|
||||
main = print $ Mat [[1, 2], [0, 1]] ^ 4
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
// 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)
|
||||
}
|
||||
|
|
@ -12,23 +12,22 @@ multi infix:<**> (SqMat $m, Int $n is copy where { $_ >= 0 }) {
|
|||
my $tmp = $m;
|
||||
my $out = [for ^$m -> $i { [ for ^$m -> $j { +($i == $j) } ] } ];
|
||||
loop {
|
||||
$out = $out * $tmp if $n +& 1;
|
||||
last unless $n +>= 1;
|
||||
$tmp = $tmp * $tmp;
|
||||
$out = $out * $tmp if $n +& 1;
|
||||
last unless $n +>= 1;
|
||||
$tmp = $tmp * $tmp;
|
||||
}
|
||||
|
||||
$out;
|
||||
}
|
||||
|
||||
multi show (SqMat $m) {
|
||||
my $size = 1;
|
||||
for ^$m X ^$m -> ($i, $j) { $size max= $m[$i][$j].Str.chars; }
|
||||
.put for @$m».fmt("%{$size}s");
|
||||
my $size = $m.flatmap( *.list».chars ).max;
|
||||
say .fmt("%{$size}s", ' ') for $m.list;
|
||||
}
|
||||
|
||||
my @m = [1, 2, 0],
|
||||
[0, 3, 1],
|
||||
[1, 0, 0];
|
||||
[0, 3, 1],
|
||||
[1, 0, 0];
|
||||
|
||||
for 0 .. 10 -> $order {
|
||||
say "### Order $order";
|
||||
|
|
|
|||
|
|
@ -0,0 +1,55 @@
|
|||
function identity(integer n)
|
||||
sequence res = repeat(repeat(0,n),n)
|
||||
for i=1 to n do
|
||||
res[i][i] = 1
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
function matrix_mul(sequence a, sequence b)
|
||||
sequence c
|
||||
if length(a[1]) != length(b) then
|
||||
return 0
|
||||
else
|
||||
c = repeat(repeat(0,length(b[1])),length(a))
|
||||
for i=1 to length(a) do
|
||||
for j=1 to length(b[1]) do
|
||||
for k=1 to length(a[1]) do
|
||||
c[i][j] += a[i][k]*b[k][j]
|
||||
end for
|
||||
end for
|
||||
end for
|
||||
return c
|
||||
end if
|
||||
end function
|
||||
|
||||
function matrix_exponent(sequence m, integer n)
|
||||
integer l = length(m)
|
||||
if n=0 then return identity(l) end if
|
||||
sequence res = m
|
||||
for i=2 to n do
|
||||
res = matrix_mul(res,m)
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
constant M1 = {{5}}
|
||||
constant M2 = {{3, 2},
|
||||
{2, 1}}
|
||||
constant M3 = {{1, 2, 0},
|
||||
{0, 3, 1},
|
||||
{1, 0, 0}}
|
||||
|
||||
ppOpt({pp_Nest,1})
|
||||
pp(matrix_exponent(M1,0))
|
||||
pp(matrix_exponent(M1,1))
|
||||
pp(matrix_exponent(M1,2))
|
||||
puts(1,"==\n")
|
||||
pp(matrix_exponent(M2,0))
|
||||
pp(matrix_exponent(M2,1))
|
||||
pp(matrix_exponent(M2,2))
|
||||
pp(matrix_exponent(M2,10))
|
||||
puts(1,"==\n")
|
||||
pp(matrix_exponent(M3,10))
|
||||
puts(1,"==\n")
|
||||
pp(matrix_exponent(identity(4),5))
|
||||
Loading…
Add table
Add a link
Reference in a new issue