Conjugate transpose: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: Add perl 6 example)
m (→‎{{header|Perl 6}}: whitespace tweaks)
Line 1,099: Line 1,099:
=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|2015-12-13}}
{{works with|Rakudo|2015-12-13}}
<lang perl6>for [ # Test Matrices
<lang perl6>for [ # Test Matrices
[ 1, 1+i, 2i],
[ 1, 1+i, 2i],
[ 1-i, 5, -3],
[ 1-i, 5, -3],
[0-2i, -3, 0]
[0-2i, -3, 0]
],
],
[
[
[1, 1, 0],
[1, 1, 0],
[0, 1, 1],
[0, 1, 1],
[1, 0, 1]
[1, 0, 1]
],
],
[
[
[0.707 , 0.707, 0],
[0.707 , 0.707, 0],
[0.707i, 0-0.707i, 0],
[0.707i, 0-0.707i, 0],
[0 , 0, i]
[0 , 0, i]
]
]
-> @m {
-> @m {
say "\nMatrix:";
say "\nMatrix:";
@m.&say-it;
@m.&say-it;
my @t = @m».conj.&mat-trans;
my @t = @m».conj.&mat-trans;
say "\nTranspose:";
say "\nTranspose:";
@t.&say-it;
@t.&say-it;
say "Is Hermitian? {is-Hermitian(@m, @t)}";
say "Is Hermitian? {is-Hermitian(@m, @t)}";
say "Is Normal? {is-Normal(@m, @t)}";
say "Is Normal? {is-Normal(@m, @t)}";
say "Is Unitary? {is-Unitary(@m, @t)}";
say "Is Unitary? {is-Unitary(@m, @t)}";
}
}


sub is-Hermitian (@m, @t, --> Bool) {
sub is-Hermitian (@m, @t, --> Bool) {
Line 1,141: Line 1,141:
sub mat-ident ($n) { [ map { [ flat 0 xx $_, 1, 0 xx $n - 1 - $_ ] }, ^$n ] }
sub mat-ident ($n) { [ map { [ flat 0 xx $_, 1, 0 xx $n - 1 - $_ ] }, ^$n ] }


sub mat-mult(@a, @b, \ε = 1e-15) {
sub mat-mult (@a, @b, \ε = 1e-15) {
my @p;
my @p;
for ^@a X ^@b[0] -> ($r, $c) {
for ^@a X ^@b[0] -> ($r, $c) {