Conjugate transpose: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 3,507: | Line 3,507: | ||
}</lang> |
}</lang> |
||
<!-- Wot, no demonstration? --> |
<!-- Wot, no demonstration? --> |
||
=={{header|Wren}}== |
|||
{{libheader|Wren-fmt}} |
|||
{{libheader|Wren-complex}} |
|||
Although the third example is in fact a unitary matrix, the ''isUnitary'' method of the above module returns false. |
|||
This is because the methods in the module work as accurately as they can within the confines of 64-bit floating point arithmetic and don't therefore allow for the small rounding error that occurs due to the use of the irrational number, sqrt(2). |
|||
However, if we use the ''almostEquals'' method with the default tolerance of 1.0e-14, then we do get a ''true'' result. |
|||
<lang ecmascript>import "/complex" for Complex, CMatrix |
|||
import "/fmt" for Fmt |
|||
var cm1 = CMatrix.new( |
|||
[ |
|||
[Complex.new(3), Complex.new(2, 1)], |
|||
[Complex.new(2, -1), Complex.one ] |
|||
] |
|||
) |
|||
var cm2 = CMatrix.fromReals([ [1, 1, 0], [0, 1, 1], [1, 0, 1] ]) |
|||
var x = 2.sqrt/2 |
|||
var cm3 = CMatrix.new( |
|||
[ |
|||
[Complex.new(x), Complex.new(x), Complex.zero], |
|||
[Complex.new(0, -x), Complex.new(0, x), Complex.zero], |
|||
[Complex.zero, Complex.zero, Complex.imagOne] |
|||
] |
|||
) |
|||
for (cm in [cm1, cm2, cm3]) { |
|||
System.print("Matrix:") |
|||
Fmt.mprint(cm, 5, 3) |
|||
System.print("\nConjugate transpose:") |
|||
Fmt.mprint(cm.conjTranspose, 5, 3) |
|||
System.print("\nHermitian : %(cm.isHermitian)") |
|||
System.print("Normal : %(cm.isNormal)") |
|||
System.print("Unitary : %(cm.isUnitary)") |
|||
System.print() |
|||
} |
|||
System.print("For the final example if we use a tolerance of 1e-14:") |
|||
var cm4 = cm3 * cm3.conjTranspose |
|||
var id = CMatrix.identity(3) |
|||
System.print("Unitary : %(cm4.almostEquals(id))")</lang> |
|||
{{out}} |
|||
<pre> |
|||
Matrix: |
|||
|3.000 + 0.000i 2.000 + 1.000i| |
|||
|2.000 - 1.000i 1.000 + 0.000i| |
|||
Conjugate transpose: |
|||
|3.000 + 0.000i 2.000 + 1.000i| |
|||
|2.000 - 1.000i 1.000 + 0.000i| |
|||
Hermitian : true |
|||
Normal : true |
|||
Unitary : false |
|||
Matrix: |
|||
|1.000 + 0.000i 1.000 + 0.000i 0.000 + 0.000i| |
|||
|0.000 + 0.000i 1.000 + 0.000i 1.000 + 0.000i| |
|||
|1.000 + 0.000i 0.000 + 0.000i 1.000 + 0.000i| |
|||
Conjugate transpose: |
|||
|1.000 + 0.000i 0.000 + 0.000i 1.000 + 0.000i| |
|||
|1.000 + 0.000i 1.000 + 0.000i 0.000 + 0.000i| |
|||
|0.000 + 0.000i 1.000 + 0.000i 1.000 + 0.000i| |
|||
Hermitian : false |
|||
Normal : true |
|||
Unitary : false |
|||
Matrix: |
|||
|0.707 + 0.000i 0.707 + 0.000i 0.000 + 0.000i| |
|||
|0.000 - 0.707i 0.000 + 0.707i 0.000 + 0.000i| |
|||
|0.000 + 0.000i 0.000 + 0.000i 0.000 + 1.000i| |
|||
Conjugate transpose: |
|||
|0.707 + 0.000i 0.000 + 0.707i 0.000 + 0.000i| |
|||
|0.707 + 0.000i 0.000 - 0.707i 0.000 + 0.000i| |
|||
|0.000 + 0.000i 0.000 + 0.000i 0.000 - 1.000i| |
|||
Hermitian : false |
|||
Normal : true |
|||
Unitary : false |
|||
For the final example if we use a tolerance of 1e-14: |
|||
Unitary : true |
|||
</pre> |