Last active
August 29, 2015 14:06
-
-
Save H2CO3/308beaf9fed6eb52546f to your computer and use it in GitHub Desktop.
Playin' around with complex matrices...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Computes conjugate transpose of M | |
let conjTransp = function conjTransp(M) { | |
return map(range(sizeof M[0]), function(row) { | |
return map(range(sizeof M), function(col) { | |
return cplx_conj(M[col][row]); | |
}); | |
}); | |
}; | |
# Helper for cplxMatMul | |
let cplxVecScalarMul = function cplxVecScalarMul(A, B, row, col) { | |
var M = { "re": 0.0, "im": 0.0 }; | |
let N = sizeof A; | |
for (var i = 0; i < N; i++) { | |
let P = cplx_mul(A[row][i], B[i][col]); | |
M = cplx_add(M, P); | |
} | |
return M; | |
}; | |
# Multiplies matrices A and B | |
# A and B are assumed to be square and of the same size, | |
# this condition is not checked. | |
let cplxMatMul = function cplxMatMul(A, B) { | |
var R = {}; | |
let N = sizeof A; | |
for (var row = 0; row < N; row++) { | |
R[row] = {}; | |
for (var col = 0; col < N; col++) { | |
R[row][col] = cplxVecScalarMul(A, B, row, col); | |
} | |
} | |
return R; | |
}; | |
# Helper for creating an array representing a complex number | |
# given its textual representation | |
let _ = function makeComplex(str) { | |
let sep = indexof(str, "+", 1); | |
if sep < 0 { | |
sep = indexof(str, "-", 1); | |
} | |
let reStr = substrto(str, sep); | |
let imStr = substrfrom(str, sep); | |
return { "re": tofloat(reStr), "im": tofloat(imStr) }; | |
}; | |
# Formats a complex matrix | |
let printCplxMat = function printCplxMat(M) { | |
foreach(M, function(i, row) { | |
foreach(row, function(j, elem) { | |
printf(" %.2f%+.2fi", elem.re, elem.im); | |
}); | |
print(); | |
}); | |
}; | |
# A Hermitian matrix | |
let H = { | |
{ _("3+0i"), _("2+1i") }, | |
{ _("2-1i"), _("0+0i") } | |
}; | |
# A normal matrix | |
let N = { | |
{ _("1+0i"), _("1+0i"), _("0+0i") }, | |
{ _("0+0i"), _("1+0i"), _("1+0i") }, | |
{ _("1+0i"), _("0+0i"), _("1+0i") } | |
}; | |
# A unitary matrix | |
let U = { | |
{ _("0.70710678118+0i"), _("0.70710678118+0i"), _("0+0i") }, | |
{ _("0-0.70710678118i"), _("0+0.70710678118i"), _("0+0i") }, | |
{ _("0+0i"), _("0+0i"), _("0+1i") } | |
}; | |
print("Hermitian matrix:\nH = "); | |
printCplxMat(H); | |
print("H* = "); | |
printCplxMat(conjTransp(H)); | |
print(); | |
print("Normal matrix:\nN = "); | |
printCplxMat(N); | |
print("N* = "); | |
printCplxMat(conjTransp(N)); | |
print("N* x N = "); | |
printCplxMat(cplxMatMul(conjTransp(N), N)); | |
print("N x N* = "); | |
printCplxMat(cplxMatMul(N, conjTransp(N))); | |
print(); | |
print("Unitary matrix:\nU = "); | |
printCplxMat(U); | |
print("U* = "); | |
printCplxMat(conjTransp(U)); | |
print("U x U* = "); | |
printCplxMat(cplxMatMul(U, conjTransp(U))); | |
print(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment