This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,12 @@
proc **(a, e) {
// create result matrix of same dimensions
var r:[a.domain] a.eltType;
// and initialize to identity matrix
forall ij in r.domain do
r(ij) = if ij(1) == ij(2) then 1 else 0;
for 1..e do
r *= a;
return r;
}

View file

@ -0,0 +1,11 @@
var m:[1..3, 1..3] int;
m(1,1) = 1; m(1,2) = 2; m(1,3) = 0;
m(2,1) = 0; m(2,2) = 3; m(2,3) = 1;
m(3,1) = 1; m(3,2) = 0; m(3,3) = 0;
config param n = 10;
for i in 0..n do {
writeln("Order ", i);
writeln(m ** i, "\n");
}

View file

@ -1,8 +1,8 @@
import std.stdio, std.string, std.math, std.array;
struct SquareMat(T = creal) {
static public string fmt = "%8.3f";
private alias T[][] TM;
public static string fmt = "%8.3f";
private alias TM = T[][];
private TM a;
public this(in size_t side) pure nothrow
@ -15,25 +15,25 @@ struct SquareMat(T = creal) {
public this(in TM m) pure nothrow
in {
assert(!m.empty);
foreach (row; m)
foreach (const row; m)
assert(m.length == m[0].length);
} body {
a.length = m.length;
foreach (i, row; m)
//a[i] = row.dup; // not nothrow
a[i] = row ~ []; // slower
foreach (immutable i, const row; m)
//a[i] = row.dup; // Not nothrow.
a[i] = row ~ []; // Slower.
}
string toString() const {
return xformat("<%(%(" ~ fmt ~ ", %)\n %)>", a);
return format("<%(%(" ~ fmt ~ ", %)\n %)>", a);
}
public static SquareMat identity(in size_t side) pure nothrow {
SquareMat m;
m.a.length = side;
foreach (r, ref row; m.a) {
foreach (immutable r, ref row; m.a) {
row.length = side;
foreach (c; 0 .. side)
foreach (immutable c; 0 .. side)
row[c] = cast(T)(r == c ? 1 : 0);
}
return m;
@ -46,10 +46,10 @@ struct SquareMat(T = creal) {
immutable size_t side = other.a.length;
SquareMat d;
d.a = new TM(side, side);
foreach (r; 0 .. side)
foreach (c; 0 .. side) {
foreach (immutable r; 0 .. side)
foreach (immutable c; 0 .. side) {
d.a[r][c] = cast(T)0;
foreach (k, ark; a[r])
foreach (immutable k, immutable ark; a[r])
d.a[r][c] += ark * other.a[k][c];
}
return d;
@ -70,12 +70,12 @@ struct SquareMat(T = creal) {
}
void main() {
alias SquareMat!() M;
alias M = SquareMat!();
immutable real q = sqrt(0.5);
auto m = M([[ q + 0*1.0Li, q + 0*1.0Li, 0.0L + 0.0Li],
[0.0L - q*1.0Li, 0.0L + q*1.0Li, 0.0L + 0.0Li],
[0.0L + 0.0Li, 0.0L + 0.0Li, 0.0L + 1.0Li]]);
immutable m = M([[ q + 0*1.0Li, q + 0*1.0Li, 0.0L + 0.0Li],
[0.0L - q*1.0Li, 0.0L + q*1.0Li, 0.0L + 0.0Li],
[0.0L + 0.0Li, 0.0L + 0.0Li, 0.0L + 1.0Li]]);
M.fmt = "%5.2f";
foreach (p; [0, 1, 23, 24])
foreach (immutable p; [0, 1, 23, 24])
writefln("m ^^ %d =\n%s", p, m ^^ p);
}

View file

@ -0,0 +1,4 @@
julia> [1 1 ; 1 0]^10
2x2 Array{Int64,2}:
89 55
55 34