Conjugate transpose: Difference between revisions

m
Added PowerShell
(Added Scala section)
m (Added PowerShell)
Line 1,337:
Matrix is unitary
Matrix is unitary
</pre>
 
=={{header|PowerShell}}==
<lang PowerShell>
function conjugate-transpose($a) {
$arr = @()
if($a) {
$n = $a.count - 1
if(0 -lt $n) {
$m = ($a | foreach {$_.count} | measure-object -Minimum).Minimum - 1
if( 0 -le $m) {
if (0 -lt $m) {
$arr =@(0)*($m+1)
foreach($i in 0..$m) {
$arr[$i] = foreach($j in 0..$n) {@([System.Numerics.complex]::Conjugate($a[$j][$i]))}
}
} else {$arr = foreach($row in $a) {[System.Numerics.complex]::Conjugate($row[0])}}
}
} else {$arr = foreach($row in $a) {[System.Numerics.complex]::Conjugate($row[0])}}
}
$arr
}
 
function complex-multarrays($a, $b) {
$c = @()
if($a -and $b) {
$n = $a.count - 1
$m = $b[0].count - 1
$c = @([System.Numerics.complex]::new(0,0))*($n+1)
foreach ($i in 0..$n) {
$c[$i] = foreach ($j in 0..$m) {
[System.Numerics.complex]$sum = [System.Numerics.complex]::new(0,0)
foreach ($k in 0..$n){$sum = [System.Numerics.complex]::Add($sum, ([System.Numerics.complex]::Multiply($a[$i][$k],$b[$k][$j])))}
$sum
}
}
}
$c
}
 
function identity($n) {
if(0 -lt $n) {
$array = @(0) * $n
foreach ($i in 0..($n-1)) {
$array[$i] = @(0) * $n
$array[$i][$i] = 1
}
$array
} else { @() }
}
 
function are-eq ($a,$b) { -not (Compare-Object $a $b -SyncWindow 0)}
 
function show($a) {
if($a) {
0..($a.Count - 1) | foreach{ if($a[$_]){"$($a[$_])"}else{""} }
}
}
function complex($a,$b) {[System.Numerics.complex]::new($a,$b)}
$id2 = identity 2
$m = @(@((complex 2 7), (complex 9 -5)),@((complex 3 4), (complex 8 -6)))
$hm = conjugate-transpose $m
$mhm = complex-multarrays $m $hm
$hmm = complex-multarrays $hm $m
"`$m ="
show $m
""
"`$hm = conjugate-transpose `$m ="
show $hm
""
"`$m * `$hm ="
show $mhm
""
"`$hm * `$m ="
show $hmm
""
"Hermitian? `$m = $(are-eq $m $hm)"
"Normal? `$m = $(are-eq $mhm $hmm)"
"Unitary? `$m = $((are-eq $id2 $hmm) -and (are-eq $id2 $mhm))"
</lang>
<b>Output:</b>
<pre>
$m =
(2, 7) (9, -5)
(3, 4) (8, -6)
 
$hm = conjugate-transpose $m =
(2, -7) (3, -4)
(9, 5) (8, 6)
 
$m * $hm =
(159, 0) (136, 27)
(136, -27) (125, 0)
 
$hm * $m =
(78, 0) (-17, -123)
(-17, 123) (206, 0)
 
Hermitian? $m = False
Normal? $m = False
Unitary? $m = False
</pre>
 
678

edits