Jump to content

Conjugate transpose: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 992:
Unitary? True
</pre>
 
=={{header|J}}==
 
Line 1,187 ⟶ 1,188:
 
Unitary example: true</lang>
 
 
=={{header|Julia}}==
Line 1,543:
Is normal? TRUE
Is unitary? TRUE
</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2015-12-13}}
<lang perl6>for [ # Test Matrices
[ 1, 1+i, 2i],
[ 1-i, 5, -3],
[0-2i, -3, 0]
],
[
[1, 1, 0],
[0, 1, 1],
[1, 0, 1]
],
[
[0.707 , 0.707, 0],
[0.707i, 0-0.707i, 0],
[0 , 0, i]
]
-> @m {
say "\nMatrix:";
@m.&say-it;
my @t = @m».conj.&mat-trans;
say "\nTranspose:";
@t.&say-it;
say "Is Hermitian?\t{is-Hermitian(@m, @t)}";
say "Is Normal?\t{is-Normal(@m, @t)}";
say "Is Unitary?\t{is-Unitary(@m, @t)}";
}
 
sub is-Hermitian (@m, @t, --> Bool) {
so @m».Complex eqv @t».Complex
}
 
sub is-Normal (@m, @t, --> Bool) {
so mat-mult(@m, @t)».Complex eqv mat-mult(@t, @m)».Complex
}
 
sub is-Unitary (@m, @t, --> Bool) {
so mat-mult(@m, @t, 1e-3)».Complex eqv mat-ident(+@m)».Complex;
}
 
sub mat-trans (@m) { map { [ @m[*;$_] ] }, ^@m[0] }
 
sub mat-ident ($n) { [ map { [ flat 0 xx $_, 1, 0 xx $n - 1 - $_ ] }, ^$n ] }
 
sub mat-mult (@a, @b, \ε = 1e-15) {
my @p;
for ^@a X ^@b[0] -> ($r, $c) {
@p[$r][$c] += @a[$r][$_] * @b[$_][$c] for ^@b;
@p[$r][$c].=round(ε); # avoid floating point math errors
}
@p
}
 
sub say-it (@array) { $_».fmt("%9s").say for @array }</lang>
{{out}}
<pre>Matrix:
[ 1 1+1i 0+2i]
[ 1-1i 5 -3]
[ 0-2i -3 0]
 
Transpose:
[ 1 1+1i 0+2i]
[ 1-1i 5 -3]
[ 0-2i -3 0]
Is Hermitian? True
Is Normal? True
Is Unitary? False
 
Matrix:
[ 1 1 0]
[ 0 1 1]
[ 1 0 1]
 
Transpose:
[ 1 0 1]
[ 1 1 0]
[ 0 1 1]
Is Hermitian? False
Is Normal? True
Is Unitary? False
 
Matrix:
[ 0.707 0.707 0]
[ 0+0.707i 0-0.707i 0]
[ 0 0 0+1i]
 
Transpose:
[ 0.707 0-0.707i 0]
[ 0.707 0+0.707i 0]
[ 0 0 0-1i]
Is Hermitian? False
Is Normal? True
Is Unitary? True
</pre>
 
Line 2,172 ⟶ 2,077:
#f
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015-12-13}}
<lang perl6>for [ # Test Matrices
[ 1, 1+i, 2i],
[ 1-i, 5, -3],
[0-2i, -3, 0]
],
[
[1, 1, 0],
[0, 1, 1],
[1, 0, 1]
],
[
[0.707 , 0.707, 0],
[0.707i, 0-0.707i, 0],
[0 , 0, i]
]
-> @m {
say "\nMatrix:";
@m.&say-it;
my @t = @m».conj.&mat-trans;
say "\nTranspose:";
@t.&say-it;
say "Is Hermitian?\t{is-Hermitian(@m, @t)}";
say "Is Normal?\t{is-Normal(@m, @t)}";
say "Is Unitary?\t{is-Unitary(@m, @t)}";
}
 
sub is-Hermitian (@m, @t, --> Bool) {
so @m».Complex eqv @t».Complex
}
 
sub is-Normal (@m, @t, --> Bool) {
so mat-mult(@m, @t)».Complex eqv mat-mult(@t, @m)».Complex
}
 
sub is-Unitary (@m, @t, --> Bool) {
so mat-mult(@m, @t, 1e-3)».Complex eqv mat-ident(+@m)».Complex;
}
 
sub mat-trans (@m) { map { [ @m[*;$_] ] }, ^@m[0] }
 
sub mat-ident ($n) { [ map { [ flat 0 xx $_, 1, 0 xx $n - 1 - $_ ] }, ^$n ] }
 
sub mat-mult (@a, @b, \ε = 1e-15) {
my @p;
for ^@a X ^@b[0] -> ($r, $c) {
@p[$r][$c] += @a[$r][$_] * @b[$_][$c] for ^@b;
@p[$r][$c].=round(ε); # avoid floating point math errors
}
@p
}
 
sub say-it (@array) { $_».fmt("%9s").say for @array }</lang>
{{out}}
<pre>Matrix:
[ 1 1+1i 0+2i]
[ 1-1i 5 -3]
[ 0-2i -3 0]
 
Transpose:
[ 1 1+1i 0+2i]
[ 1-1i 5 -3]
[ 0-2i -3 0]
Is Hermitian? True
Is Normal? True
Is Unitary? False
 
Matrix:
[ 1 1 0]
[ 0 1 1]
[ 1 0 1]
 
Transpose:
[ 1 0 1]
[ 1 1 0]
[ 0 1 1]
Is Hermitian? False
Is Normal? True
Is Unitary? False
 
Matrix:
[ 0.707 0.707 0]
[ 0+0.707i 0-0.707i 0]
[ 0 0 0+1i]
 
Transpose:
[ 0.707 0-0.707i 0]
[ 0.707 0+0.707i 0]
[ 0 0 0-1i]
Is Hermitian? False
Is Normal? True
Is Unitary? True
</pre>
 
=={{header|REXX}}==
Line 2,324 ⟶ 2,325:
end</lang>
Note: Ruby 1.9 had a bug in the Matrix#hermitian? method. It's fixed in 2.0.
 
=={{header|Rust}}==
Uses external crate 'num', version 0.1.34
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.