Conjugate transpose: Difference between revisions

Content added Content deleted
(Updated second D entry)
Line 457: Line 457:
// alias CM(T) = Complex!T[][]; // Not yet useful.
// alias CM(T) = Complex!T[][]; // Not yet useful.


auto conjugateTranspose(T)(in Complex!T[][] m) /*pure*/ nothrow
auto conjugateTranspose(T)(in Complex!T[][] m) pure nothrow /*@safe*/
if (!hasIndirections!T) {
if (!hasIndirections!T) {
return iota(m[0].length)
return iota(m[0].length)
Line 465: Line 465:
}
}


T[][] matMul(T)(immutable T[][] A, immutable T[][] B) pure nothrow {
T[][] matMul(T)(immutable T[][] A, immutable T[][] B) pure nothrow /*@safe*/ {
immutable Bt = B[0].length.iota.map!(i=> B.transversal(i).array)
immutable Bt = B[0].length.iota.map!(i=> B.transversal(i).array)
.array;
.array;
Line 474: Line 474:
/// some bits of mantissa.
/// some bits of mantissa.
bool areEqual(T)(in Complex!T[][][] matrices, in size_t nBits=20)
bool areEqual(T)(in Complex!T[][][] matrices, in size_t nBits=20)
pure nothrow {
pure nothrow /*@safe*/ {
static bool allSame(U)(in U[] v) pure nothrow @nogc {
static bool allSame(U)(in U[] v) pure nothrow @nogc @safe {
return v[1 .. $].all!(c => c == v[0]);
return v[1 .. $].all!(c => c == v[0]);
}
}


bool allNearSame(in Complex!T[] v) pure nothrow @nogc {
bool allNearSame(in Complex!T[] v) pure nothrow @nogc @safe {
auto v0 = v[0].Complex!T; // To avoid another cast.
auto v0 = v[0].Complex!T; // To avoid another cast.
return v[1 .. $].all!(c=> feqrel(v0.re, cast()c.re) >= nBits &&
return v[1 .. $].all!(c=> feqrel(v0.re, cast()c.re) >= nBits &&
Line 499: Line 499:


bool isHermitian(T)(in Complex!T[][] m, in Complex!T[][] ct)
bool isHermitian(T)(in Complex!T[][] m, in Complex!T[][] ct)
pure nothrow {
pure nothrow /*@safe*/ {
return [m, ct].areEqual;
return [m, ct].areEqual;
}
}


bool isNormal(T)(immutable Complex!T[][] m, immutable Complex!T[][] ct)
bool isNormal(T)(immutable Complex!T[][] m, immutable Complex!T[][] ct)
pure nothrow {
pure nothrow /*@safe*/ {
return [matMul(m, ct), matMul(ct, m)].areEqual;
return [matMul(m, ct), matMul(ct, m)].areEqual;
}
}


auto complexIdentitymatrix(in size_t side) pure nothrow {
auto complexIdentitymatrix(in size_t side) pure nothrow /*@safe*/ {
return side.iota
return side.iota
.map!((in r) => side.iota.map!(c => complex(r == c)).array)
.map!((in r) => side.iota.map!(c => complex(r == c)).array)
Line 515: Line 515:


bool isUnitary(T)(immutable Complex!T[][] m, immutable Complex!T[][] ct)
bool isUnitary(T)(immutable Complex!T[][] m, immutable Complex!T[][] ct)
pure nothrow {
pure nothrow /*@safe*/ {
immutable mct = matMul(m, ct);
immutable mct = matMul(m, ct);
immutable ident = mct.length.complexIdentitymatrix;
immutable ident = mct.length.complexIdentitymatrix;