|
| 1 | +package io.github.hexagonnico.cmplxlib.matrix |
| 2 | + |
| 3 | +import io.github.hexagonnico.cmplxlib.Complex |
| 4 | +import io.github.hexagonnico.cmplxlib.vector.Vec2c |
| 5 | +import org.scalatest.funsuite.AnyFunSuite |
| 6 | + |
| 7 | +class Mat2cSuite extends AnyFunSuite { |
| 8 | + |
| 9 | + test("Sum of matrices") { |
| 10 | + val a = Mat2c( |
| 11 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 12 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 13 | + ) |
| 14 | + val b = Mat2c( |
| 15 | + Complex(2.0, 2.5), Complex(3.0, 0.5), |
| 16 | + Complex(1.0, 1.5), Complex(4.0, 3.5) |
| 17 | + ) |
| 18 | + assert(a + b == Mat2c( |
| 19 | + Complex(3.0, 4.5), Complex(4.5, 1.5), |
| 20 | + Complex(1.5, 3.0), Complex(7.0, 5.5) |
| 21 | + )) |
| 22 | + } |
| 23 | + |
| 24 | + test("Negative matrix") { |
| 25 | + assert(Mat2c( |
| 26 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 27 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 28 | + ).negated == Mat2c( |
| 29 | + Complex(-1.0, -2.0), Complex(-1.5, -1.0), |
| 30 | + Complex(-0.5, -1.5), Complex(-3.0, -2.0) |
| 31 | + )) |
| 32 | + } |
| 33 | + |
| 34 | + test("Subtraction of matrices") { |
| 35 | + val a = Mat2c( |
| 36 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 37 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 38 | + ) |
| 39 | + val b = Mat2c( |
| 40 | + Complex(2.0, 2.5), Complex(3.0, 0.5), |
| 41 | + Complex(1.0, 1.5), Complex(4.0, 3.5) |
| 42 | + ) |
| 43 | + assert(a - b == Mat2c( |
| 44 | + Complex(-1.0, -0.5), Complex(-1.5, 0.5), |
| 45 | + Complex(-0.5, 0.0), Complex(-1.0, -1.5) |
| 46 | + )) |
| 47 | + } |
| 48 | + |
| 49 | + test("Matrix multiplied by a scalar") { |
| 50 | + assert(Mat2c( |
| 51 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 52 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 53 | + ) * 1.5 == Mat2c( |
| 54 | + Complex(1.5, 3.0), Complex(2.25, 1.5), |
| 55 | + Complex(0.75, 2.25), Complex(4.5, 3.0) |
| 56 | + )) |
| 57 | + } |
| 58 | + |
| 59 | + test("Matrix multiplied by a scalar commutativity") { |
| 60 | + assert(1.5 * Mat2c( |
| 61 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 62 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 63 | + ) == Mat2c( |
| 64 | + Complex(1.5, 3.0), Complex(2.25, 1.5), |
| 65 | + Complex(0.75, 2.25), Complex(4.5, 3.0) |
| 66 | + )) |
| 67 | + } |
| 68 | + |
| 69 | + test("Matrix-vector product") { |
| 70 | + val mat = Mat2c( |
| 71 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 72 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 73 | + ) |
| 74 | + val vec = Vec2c(Complex(1.0, 1.0), Complex(2.0, 3.0)) |
| 75 | + assert(mat * vec == Vec2c(Complex(-1.0, 9.5), Complex(-1.0, 15.0))) |
| 76 | + } |
| 77 | + |
| 78 | + test("Matrix-vector product by values") { |
| 79 | + val mat = Mat2c( |
| 80 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 81 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 82 | + ) |
| 83 | + assert(mat * (Complex(1.0, 1.0), Complex(2.0, 3.0)) == Vec2c(Complex(-1.0, 9.5), Complex(-1.0, 15.0))) |
| 84 | + } |
| 85 | + |
| 86 | + test("Transposed") { |
| 87 | + assert(Mat2c( |
| 88 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 89 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 90 | + ).transposed == Mat2c( |
| 91 | + Complex(1.0, 2.0), Complex(0.5, 1.5), |
| 92 | + Complex(1.5, 1.0), Complex(3.0, 2.0) |
| 93 | + )) |
| 94 | + } |
| 95 | + |
| 96 | + test("Symmetric matrix") { |
| 97 | + assert(Mat2c( |
| 98 | + Complex.Zero, Complex.One, |
| 99 | + Complex.One, Complex.Zero |
| 100 | + ).isSymmetric) |
| 101 | + } |
| 102 | + |
| 103 | + test("Skew symmetric matrix") { |
| 104 | + assert(Mat2c( |
| 105 | + Complex.Zero, Complex.One, |
| 106 | + -Complex.One, Complex.Zero |
| 107 | + ).isSkewSymmetric) |
| 108 | + } |
| 109 | + |
| 110 | + test("Complex conjugate of a matrix") { |
| 111 | + assert(Mat2c( |
| 112 | + Complex(1.0, 1.0), Complex(2.0, -2.0), |
| 113 | + Complex(-3.0, 3.0), Complex(4.0, 4.0) |
| 114 | + ).conjugate == Mat2c( |
| 115 | + Complex(1.0, -1.0), Complex(2.0, 2.0), |
| 116 | + Complex(-3.0, -3.0), Complex(4.0, -4.0) |
| 117 | + )) |
| 118 | + } |
| 119 | + |
| 120 | + test("Conjugate transposed of a matrix") { |
| 121 | + assert(Mat2c( |
| 122 | + Complex(1.0, 1.0), Complex(2.0, -2.0), |
| 123 | + Complex(-3.0, 3.0), Complex(4.0, 4.0) |
| 124 | + ).hermitian == Mat2c( |
| 125 | + Complex(1.0, -1.0), Complex(-3.0, -3.0), |
| 126 | + Complex(2.0, 2.0), Complex(4.0, -4.0) |
| 127 | + )) |
| 128 | + } |
| 129 | + |
| 130 | + test("Matrix product") { |
| 131 | + val a = Mat2c( |
| 132 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 133 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 134 | + ) |
| 135 | + val b = Mat2c( |
| 136 | + Complex(2.0, 2.5), Complex(3.0, 0.5), |
| 137 | + Complex(1.0, 1.5), Complex(4.0, 3.5) |
| 138 | + ) |
| 139 | + assert(a * b == Mat2c( |
| 140 | + Complex(-3.0, 9.75), Complex(4.5, 15.75), |
| 141 | + Complex(-2.75, 10.75), Complex(5.75, 23.25) |
| 142 | + )) |
| 143 | + } |
| 144 | + |
| 145 | + test("Matrix power") { |
| 146 | + val a = Mat2c( |
| 147 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 148 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 149 | + ) |
| 150 | + assert((a power 3) == (a * a * a)) |
| 151 | + } |
| 152 | + |
| 153 | + test("Matrix determinant") { |
| 154 | + val a = Mat2c( |
| 155 | + Complex(1.0, 2.0), Complex(1.5, 1.0), |
| 156 | + Complex(0.5, 1.5), Complex(3.0, 2.0) |
| 157 | + ) |
| 158 | + assert(a.determinant == Complex(-0.25, 5.25)) |
| 159 | + } |
| 160 | +} |
0 commit comments