Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
101
Task/Conjugate-transpose/Sparkling/conjugate-transpose.sparkling
Normal file
101
Task/Conjugate-transpose/Sparkling/conjugate-transpose.sparkling
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
# 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();
|
||||
Loading…
Add table
Add a link
Reference in a new issue