September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
125
Task/Conjugate-transpose/Kotlin/conjugate-transpose.kotlin
Normal file
125
Task/Conjugate-transpose/Kotlin/conjugate-transpose.kotlin
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
// version 1.1.3
|
||||
|
||||
typealias C = Complex
|
||||
typealias Vector = Array<C>
|
||||
typealias Matrix = Array<Vector>
|
||||
|
||||
class Complex(val real: Double, val imag: Double) {
|
||||
|
||||
operator fun plus(other: Complex) =
|
||||
Complex(this.real + other.real, this.imag + other.imag)
|
||||
|
||||
operator fun times(other: Complex) =
|
||||
Complex(this.real * other.real - this.imag * other.imag,
|
||||
this.real * other.imag + this.imag * other.real)
|
||||
|
||||
fun conj() = Complex(this.real, -this.imag)
|
||||
|
||||
/* tolerable equality allowing for rounding of Doubles */
|
||||
infix fun teq(other: Complex) =
|
||||
Math.abs(this.real - other.real) <= 1e-14 &&
|
||||
Math.abs(this.imag - other.imag) <= 1e-14
|
||||
|
||||
override fun toString() = "${"%.3f".format(real)} " + when {
|
||||
imag > 0.0 -> "+ ${"%.3f".format(imag)}i"
|
||||
imag == 0.0 -> "+ 0.000i"
|
||||
else -> "- ${"%.3f".format(-imag)}i"
|
||||
}
|
||||
}
|
||||
|
||||
fun Matrix.conjTranspose(): Matrix {
|
||||
val rows = this.size
|
||||
val cols = this[0].size
|
||||
return Matrix(cols) { i -> Vector(rows) { j -> this[j][i].conj() } }
|
||||
}
|
||||
|
||||
operator fun Matrix.times(other: Matrix): Matrix {
|
||||
val rows1 = this.size
|
||||
val cols1 = this[0].size
|
||||
val rows2 = other.size
|
||||
val cols2 = other[0].size
|
||||
require(cols1 == rows2)
|
||||
val result = Matrix(rows1) { Vector(cols2) { C(0.0, 0.0) } }
|
||||
for (i in 0 until rows1) {
|
||||
for (j in 0 until cols2) {
|
||||
for (k in 0 until rows2) {
|
||||
result[i][j] += this[i][k] * other[k][j]
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/* tolerable matrix equality using the same concept as for complex numbers */
|
||||
infix fun Matrix.teq(other: Matrix): Boolean {
|
||||
if (this.size != other.size || this[0].size != other[0].size) return false
|
||||
for (i in 0 until this.size) {
|
||||
for (j in 0 until this[0].size) if (!(this[i][j] teq other[i][j])) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun Matrix.isHermitian() = this teq this.conjTranspose()
|
||||
|
||||
fun Matrix.isNormal(): Boolean {
|
||||
val ct = this.conjTranspose()
|
||||
return (this * ct) teq (ct * this)
|
||||
}
|
||||
|
||||
fun Matrix.isUnitary(): Boolean {
|
||||
val ct = this.conjTranspose()
|
||||
val prod = this * ct
|
||||
val ident = identityMatrix(prod.size)
|
||||
val prod2 = ct * this
|
||||
return (prod teq ident) && (prod2 teq ident)
|
||||
}
|
||||
|
||||
fun Matrix.print() {
|
||||
val rows = this.size
|
||||
val cols = this[0].size
|
||||
for (i in 0 until rows) {
|
||||
for (j in 0 until cols) {
|
||||
print(this[i][j])
|
||||
print(if(j < cols - 1) ", " else "\n")
|
||||
}
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
fun identityMatrix(n: Int): Matrix {
|
||||
require(n >= 1)
|
||||
val ident = Matrix(n) { Vector(n) { C(0.0, 0.0) } }
|
||||
for (i in 0 until n) ident[i][i] = C(1.0, 0.0)
|
||||
return ident
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val x = Math.sqrt(2.0) / 2.0
|
||||
val matrices = arrayOf(
|
||||
arrayOf(
|
||||
arrayOf(C(3.0, 0.0), C(2.0, 1.0)),
|
||||
arrayOf(C(2.0, -1.0), C(1.0, 0.0))
|
||||
),
|
||||
arrayOf(
|
||||
arrayOf(C(1.0, 0.0), C(1.0, 0.0), C(0.0, 0.0)),
|
||||
arrayOf(C(0.0, 0.0), C(1.0, 0.0), C(1.0, 0.0)),
|
||||
arrayOf(C(1.0, 0.0), C(0.0, 0.0), C(1.0, 0.0))
|
||||
),
|
||||
arrayOf(
|
||||
arrayOf(C(x, 0.0), C(x, 0.0), C(0.0, 0.0)),
|
||||
arrayOf(C(0.0, -x), C(0.0, x), C(0.0, 0.0)),
|
||||
arrayOf(C(0.0, 0.0), C(0.0, 0.0), C(0.0, 1.0))
|
||||
)
|
||||
)
|
||||
|
||||
for (m in matrices) {
|
||||
println("Matrix:")
|
||||
m.print()
|
||||
val mct = m.conjTranspose()
|
||||
println("Conjugate transpose:")
|
||||
mct.print()
|
||||
println("Hermitian? ${mct.isHermitian()}")
|
||||
println("Normal? ${mct.isNormal()}")
|
||||
println("Unitary? ${mct.isUnitary()}\n")
|
||||
}
|
||||
}
|
||||
120
Task/Conjugate-transpose/Phix/conjugate-transpose.phix
Normal file
120
Task/Conjugate-transpose/Phix/conjugate-transpose.phix
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
enum REAL, IMAG
|
||||
|
||||
type complex(sequence s)
|
||||
return length(s)=2 and atom(s[REAL]) and atom(s[IMAG])
|
||||
end type
|
||||
|
||||
function c_add(complex a, complex b)
|
||||
return sq_add(a,b)
|
||||
end function
|
||||
|
||||
function c_mul(complex a, complex b)
|
||||
return {a[REAL] * b[REAL] - a[IMAG] * b[IMAG],
|
||||
a[REAL] * b[IMAG] + a[IMAG] * b[REAL]}
|
||||
end function
|
||||
|
||||
function c_conj(complex a)
|
||||
return {a[REAL],-a[IMAG]}
|
||||
end function
|
||||
|
||||
function c_print(complex a)
|
||||
if a[IMAG]=0 then return sprintf("%g",a[REAL]) end if
|
||||
return sprintf("%g%+gi",a)
|
||||
end function
|
||||
|
||||
procedure m_print(sequence a)
|
||||
integer l = length(a)
|
||||
for i=1 to l do
|
||||
for j=1 to l do
|
||||
a[i][j] = c_print(a[i][j])
|
||||
end for
|
||||
a[i] = "["&join(a[i],",")&"]"
|
||||
end for
|
||||
puts(1,join(a,"\n")&"\n")
|
||||
end procedure
|
||||
|
||||
|
||||
function conjugate_transpose(sequence a)
|
||||
sequence res = a
|
||||
integer l = length(a)
|
||||
for i=1 to l do
|
||||
for j=1 to l do
|
||||
res[i][j] = c_conj(a[j][i])
|
||||
end for
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
function m_unitary(sequence act)
|
||||
-- note: a was normal and act = a*ct already
|
||||
integer l = length(act)
|
||||
for i=1 to l do
|
||||
for j=1 to l do
|
||||
atom {re,im} = act[i,j]
|
||||
-- round to nearest billionth
|
||||
-- (powers of 2 help the FPU out)
|
||||
re = round(re,1024*1024*1024)
|
||||
im = round(im,1024*1024*1024)
|
||||
if im!=0
|
||||
or (i=j and re!=1)
|
||||
or (i!=j and re!=0) then
|
||||
return 0
|
||||
end if
|
||||
end for
|
||||
end for
|
||||
return 1
|
||||
end function
|
||||
|
||||
function m_mul(sequence a, sequence b)
|
||||
sequence res = sq_mul(a,0)
|
||||
integer l = length(a)
|
||||
for i=1 to l do
|
||||
for j=1 to l do
|
||||
for k=1 to l do
|
||||
res[i][j] = c_add(res[i][j],c_mul(a[i][k],b[k][j]))
|
||||
end for
|
||||
end for
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
procedure test(sequence a)
|
||||
sequence ct = conjugate_transpose(a)
|
||||
printf(1,"Original matrix:\n")
|
||||
m_print(a)
|
||||
printf(1,"Conjugate transpose:\n")
|
||||
m_print(ct)
|
||||
-- note: rounding similar to that in m_unitary may be rqd (in a similar
|
||||
-- loop in a new m_equal function) on these two equality tests,
|
||||
-- but as it is, all tests pass with the builtin = operator.
|
||||
printf(1,"Hermitian?: %s\n",{iff(a=ct?"TRUE":"FALSE")}) -- (this one)
|
||||
sequence act = m_mul(a,ct), cta = m_mul(ct,a)
|
||||
bool normal = act=cta -- (&this one)
|
||||
printf(1,"Normal?: %s\n",{iff(normal?"TRUE":"FALSE")})
|
||||
printf(1,"Unitary?: %s\n\n",{iff(normal and m_unitary(act)?"TRUE":"FALSE")})
|
||||
end procedure
|
||||
|
||||
constant x = sqrt(2)/2
|
||||
|
||||
constant tests = {{{{3, 0},{2,1}},
|
||||
{{2,-1},{1,0}}},
|
||||
|
||||
{{{ 1, 0},{ 1, 1},{ 0, 2}},
|
||||
{{ 1,-1},{ 5, 0},{-3, 0}},
|
||||
{{ 0,-2},{-3, 0},{ 0, 0}}},
|
||||
|
||||
{{{0.5,+0.5},{0.5,-0.5}},
|
||||
{{0.5,-0.5},{0.5,+0.5}}},
|
||||
|
||||
{{{ 1, 0},{ 1, 0},{ 0, 0}},
|
||||
{{ 0, 0},{ 1, 0},{ 1, 0}},
|
||||
{{ 1, 0},{ 0, 0},{ 1, 0}}},
|
||||
|
||||
{{{x, 0},{x, 0},{0, 0}},
|
||||
{{0,-x},{0, x},{0, 0}},
|
||||
{{0, 0},{0, 0},{0, 1}}},
|
||||
|
||||
{{{2,7},{9,-5}},
|
||||
{{3,4},{8,-6}}}}
|
||||
|
||||
for i=1 to length(tests) do test(tests[i]) end for
|
||||
66
Task/Conjugate-transpose/Sidef/conjugate-transpose.sidef
Normal file
66
Task/Conjugate-transpose/Sidef/conjugate-transpose.sidef
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
func is_Hermitian (Array m, Array t) -> Bool { m == t }
|
||||
|
||||
func mat_mult (Array a, Array b, Number ε = -3) {
|
||||
var p = []
|
||||
for r, c in (^a ~X ^b[0]) {
|
||||
for k in (^b) {
|
||||
p[r][c] := 0 += (a[r][k] * b[k][c]) -> round!(ε)
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func mat_trans (Array m) {
|
||||
var r = []
|
||||
for i,j in (^m ~X ^m[0]) {
|
||||
r[j][i] = m[i][j]
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func mat_ident (Number n) {
|
||||
^n -> map {|i|
|
||||
[i.of(0)..., 1, (n - i - 1).of(0)...]
|
||||
}
|
||||
}
|
||||
|
||||
func is_Normal (Array m, Array t) -> Bool {
|
||||
mat_mult(m, t) == mat_mult(t, m)
|
||||
}
|
||||
|
||||
func is_Unitary (Array m, Array t) -> Bool {
|
||||
mat_mult(m, t) == mat_ident(m.len)
|
||||
}
|
||||
|
||||
func say_it (Array a) {
|
||||
a.each {|b|
|
||||
b.map { "%9s" % _ }.join(' ').say
|
||||
}
|
||||
}
|
||||
|
||||
[
|
||||
[
|
||||
[ 1, 1+1i, 2i],
|
||||
[1-1i, 5, -3],
|
||||
[0-2i, -3, 0]
|
||||
],
|
||||
[
|
||||
[1, 1, 0],
|
||||
[0, 1, 1],
|
||||
[1, 0, 1]
|
||||
],
|
||||
[
|
||||
[0.707 , 0.707, 0],
|
||||
[0.707i, -0.707i, 0],
|
||||
[0 , 0, 1i]
|
||||
]
|
||||
].each { |m|
|
||||
say "\nMatrix:"
|
||||
say_it(m)
|
||||
var t = mat_trans(m.map{.map{.conj}})
|
||||
say "\nTranspose:"
|
||||
say_it(t)
|
||||
say "Is Hermitian?\t#{is_Hermitian(m, t)}"
|
||||
say "Is Normal?\t#{is_Normal(m, t)}"
|
||||
say "Is Unitary?\t#{is_Unitary(m, t)}"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue