Conjugate transpose: Difference between revisions

Updated both D entries
m (→‎{{header|REXX}}: removed STYLE from the PRE html tag.)
(Updated both D entries)
Line 308:
std.numeric;
 
T[][] conjugateTranspose(T)(in T[][] m) pure nothrow @safe {
auto r = new typeof(return)(m[0].length, m.length);
foreach (immutable nr, const row; m)
Line 316:
}
 
bool isRectangular(T)(in T[][] M) pure nothrow @safe @nogc {
return M.all!(row => row.length == M[0].length);
}
 
T[][] matMul(T)(in T[][] A, in T[][] B) pure nothrow /*@safe*/
in {
assert(A.isRectangular && B.isRectangular &&
Line 341:
/// some bits of mantissa.
bool areEqual(T)(in Complex!T[][][] matrices, in size_t nBits=20)
pure nothrow /*@safe*/ {
static bool allSame(U)(in U[] v) pure nothrow @nogc {
return v[1 .. $].all!(c => c == v[0]);
Line 348:
bool allNearSame(in Complex!T[] v) pure nothrow @nogc {
auto v0 = v[0].Complex!T; // To avoid another cast.
return v[1 .. $].all!(c => feqrel(v0.re, cast()c.re) >= nBits &&
feqrel(v0.im, cast()c.im) >= nBits);
}
 
Line 366:
 
bool isHermitian(T)(in Complex!T[][] m, in Complex!T[][] ct)
pure nothrow /*@safe*/ {
return [m, ct].areEqual;
}
 
bool isNormal(T)(in Complex!T[][] m, in Complex!T[][] ct)
pure nothrow /*@safe*/ {
return [matMul(m, ct), matMul(ct, m)].areEqual;
}
 
auto complexIdentitymatrix(in size_t side) pure nothrow /*@safe*/ {
return side.iota.map!(r => side.iota.map!(c => complex(r == c)).array).array;
.map!((in r) => side.iota.map!(c => complex(r == c)).array)
.array;
}
 
bool isUnitary(T)(in Complex!T[][] m, in Complex!T[][] ct)
pure nothrow /*@safe*/ {
immutable mct = matMul(m, ct);
immutable ident = mct.length.complexIdentitymatrix;
Line 388 ⟶ 386:
}
 
void main() /*@safe*/ {
alias C = complex;
immutable x = 2 ^^ 0.5 / 2;
Line 459 ⟶ 457:
auto conjugateTranspose(T)(in Complex!T[][] m) pure nothrow /*@safe*/
if (!hasIndirections!T) {
return iota(m[0].length).map!(i => m.transversal(i).map!conj.array).array;
.map!(i => m.transversal(i).map!conj.array)
.array
.assumeUnique; //*
}
 
T[][] matMul(T)(immutable T[][] A, immutable T[][] B) pure nothrow /*@safe*/ {
immutable Bt = B[0].length.iota.map!(i => B.transversal(i).array).array;
return A.map!(a => Bt.map!(b => a.dotProduct(b)).array).array;
return A.map!((in a) => Bt.map!(b => a.dotProduct(b)).array).array;
}
 
Line 481 ⟶ 475:
bool allNearSame(in Complex!T[] v) pure nothrow @nogc @safe {
auto v0 = v[0].Complex!T; // To avoid another cast.
return v[1 .. $].all!(c => feqrel(v0.re, cast()c.re) >= nBits &&
feqrel(v0.im, cast()c.im) >= nBits);
}
 
Line 509 ⟶ 503:
 
auto complexIdentitymatrix(in size_t side) pure nothrow /*@safe*/ {
return side.iota.map!(r => side.iota.map!(c => complex(r == c)).array).array;
.map!((in r) => side.iota.map!(c => complex(r == c)).array)
.array;
}