예제 #1
0
        public void TestAlmostEquals_ComplexMatrix()
        {
            ComplexMatrix a1 = ComplexMatrix.Random(3, 2, new ContinuousUniformDistribution());
            ComplexMatrix a2 = a1.Clone();
            ComplexMatrix b  = -a1;
            ComplexMatrix c  = a1 * (1.0 + (1e+10 * Number.PositiveEpsilonOf(1.0)));
            ComplexMatrix d  = a1 * (1.0 + (2 * Number.PositiveEpsilonOf(1.0)));

            Helper_TestAlmostEqualityForGenericType(a1, a2, b, c, d);

            // Wrapper
            Assert.That(ComplexMatrix.AlmostEqual(a1, c), Is.False);
            Assert.That(ComplexMatrix.AlmostEqual(a1, c, 1e-10), Is.False);
            Assert.That(ComplexMatrix.AlmostEqual(a1, c, 1e-2), Is.True);

            // reference type -> no boxing
            Assert.That(a1, Is.SameAs(a1));

            // transpose
            Assert.That(a1.Transpose(), Is.Not.EqualTo(a1));
            Assert.That(Number.AlmostEqual(a1, a1.Transpose()), Is.False);
            Assert.That(Number.AlmostEqual(a1, a1.Transpose(), 1e-10), Is.False);

            // hermitian transpose
            Assert.That(a1.HermitianTranspose(), Is.Not.EqualTo(a1));
            Assert.That(Number.AlmostEqual(a1, a1.HermitianTranspose()), Is.False);
            Assert.That(Number.AlmostEqual(a1, a1.HermitianTranspose(), 1e-10), Is.False);
        }
        public void ComplexMatrixConjugate()
        {
            // MATLAB: conj_c = conj(ca3x2)
            ComplexMatrix u = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6 + (12 * j), 0 },
                new Complex[] { 2 + (4 * j), 12 + (24 * j) },
                new Complex[] { 14 + (28 * j), 18 + (36 * j) }
            });

            Assert.That(_ca3X2.Conjugate(), NumericIs.AlmostEqualTo(u), "conj c 1");
            Assert.That(_ca3X2.Conjugate(), Is.Not.SameAs(_ca3X2));
            Assert.That(_ca3X2.Conjugate().GetArray(), Is.Not.SameAs(_ca3X2.GetArray()));

            ComplexMatrix uInplace = _ca3X2.Clone();

            Complex[][] internalArray = uInplace.GetArray();
            uInplace.ConjugateInplace();
            Assert.That(uInplace, NumericIs.AlmostEqualTo(u), "conj c 2");
            Assert.That(internalArray, Is.Not.SameAs(_ca3X2.GetArray()));
            Assert.That(internalArray, Is.SameAs(uInplace.GetArray()));
        }
예제 #3
0
        public void TestComplexMatrix_AdditiveTranspose()
        {
            /*
             * MATLAB:
             * sum_cc = ca3x2 + cb3x2
             * diff_cc = ca3x2 - cb3x2
             * sum_cm = ca3x2 + mb3x2
             * diff_cm = ca3x2 - mb3x2
             * sum_cs = ca3x2 + s
             * diff_cs = ca3x2 - s
             * neg_c = -ca3x2
             * conj_c = conj(ca3x2)
             * trans_c = ca3x2.'
             * htrans_c = ca3x2'
             * trans_c2 = cc2x2.'
             * htrans_c2 = cc2x2'
             */

            // ComplexMatrix + ComplexMatrix
            ComplexMatrix sum_cc = new ComplexMatrix(new Complex[][] {
                new Complex[] { 15 + 72 * j, 1.5 + 16.5 * j },
                new Complex[] { -2 - 37 * j, 9.5 - 43.5 * j },
                new Complex[] { 32 + 137 * j, 11 - 96 * j }
            });

            NumericAssert.AreAlmostEqual(sum_cc, ca3x2 + cb3x2, "sum cc 1");
            ComplexMatrix sum_cc_inplace = ca3x2.Clone();

            NumericAssert.AreAlmostEqual(sum_cc, sum_cc_inplace.Add(cb3x2), "sum cc 2");
            sum_cc_inplace.AddInplace(cb3x2);
            NumericAssert.AreAlmostEqual(sum_cc, sum_cc_inplace, "sum cc 3");

            // ComplexMatrix - ComplexMatrix
            ComplexMatrix diff_cc = new ComplexMatrix(new Complex[][] {
                new Complex[] { -3 - 96 * j, -1.5 - 16.5 * j },
                new Complex[] { 6 + 29 * j, 14.5 - 4.5 * j },
                new Complex[] { -4 - 193 * j, 25 + 24 * j }
            });

            NumericAssert.AreAlmostEqual(diff_cc, ca3x2 - cb3x2, "diff cc 1");
            ComplexMatrix diff_cc_inplace = ca3x2.Clone();

            NumericAssert.AreAlmostEqual(diff_cc, diff_cc_inplace.Subtract(cb3x2), "diff cc 2");
            diff_cc_inplace.SubtractInplace(cb3x2);
            NumericAssert.AreAlmostEqual(diff_cc, diff_cc_inplace, "diff cc 3");

            // ComplexMatrix + Matrix
            ComplexMatrix sum_cm = new ComplexMatrix(new Complex[][] {
                new Complex[] { 16 - 12 * j, 2.5 },
                new Complex[] { -1 - 4 * j, 10.5 - 24 * j },
                new Complex[] { 33 - 28 * j, 12 - 36 * j }
            });

            NumericAssert.AreAlmostEqual(sum_cm, ca3x2 + mb3x2, "sum cm 1");
            ComplexMatrix sum_cm_inplace = ca3x2.Clone();

            NumericAssert.AreAlmostEqual(sum_cm, sum_cm_inplace.Add(mb3x2), "sum cm 2");
            sum_cm_inplace.AddInplace(mb3x2);
            NumericAssert.AreAlmostEqual(sum_cm, sum_cm_inplace, "sum cm 3");

            // ComplexMatrix - Matrix
            ComplexMatrix diff_cm = new ComplexMatrix(new Complex[][] {
                new Complex[] { -4 - 12 * j, -2.5 },
                new Complex[] { 5 - 4 * j, 13.5 - 24 * j },
                new Complex[] { -5 - 28 * j, 24 - 36 * j }
            });

            NumericAssert.AreAlmostEqual(diff_cm, ca3x2 - mb3x2, "diff cm 1");
            ComplexMatrix diff_cm_inplace = ca3x2.Clone();

            NumericAssert.AreAlmostEqual(diff_cm, diff_cm_inplace.Subtract(mb3x2), "diff cm 2");
            diff_cm_inplace.SubtractInplace(mb3x2);
            NumericAssert.AreAlmostEqual(diff_cm, diff_cm_inplace, "diff cm 3");

            // ComplexMatrix + Complex
            ComplexMatrix sum_cs = new ComplexMatrix(new Complex[][] {
                new Complex[] { 7 - 11 * j, 1 + j },
                new Complex[] { 3 - 3 * j, 13 - 23 * j },
                new Complex[] { 15 - 27 * j, 19 - 35 * j }
            });

            NumericAssert.AreAlmostEqual(sum_cs, ca3x2 + s, "sum cs 1");
            ComplexMatrix sum_cs_inplace = ca3x2.Clone();

            NumericAssert.AreAlmostEqual(sum_cs, sum_cs_inplace.Add(s), "sum cs 2");
            sum_cs_inplace.AddInplace(s);
            NumericAssert.AreAlmostEqual(sum_cs, sum_cs_inplace, "sum cs 3");

            // ComplexMatrix - Complex
            ComplexMatrix diff_cs = new ComplexMatrix(new Complex[][] {
                new Complex[] { 5 - 13 * j, -1 - j },
                new Complex[] { 1 - 5 * j, 11 - 25 * j },
                new Complex[] { 13 - 29 * j, 17 - 37 * j }
            });

            NumericAssert.AreAlmostEqual(diff_cs, ca3x2 - s, "diff cs 1");
            ComplexMatrix diff_cs_inplace = ca3x2.Clone();

            NumericAssert.AreAlmostEqual(diff_cs, diff_cs_inplace.Subtract(s), "diff cs 2");
            diff_cs_inplace.SubtractInplace(s);
            NumericAssert.AreAlmostEqual(diff_cs, diff_cs_inplace, "diff cs 3");

            // ComplexMatrix Negate
            ComplexMatrix neg_c = new ComplexMatrix(new Complex[][] {
                new Complex[] { -6 + 12 * j, 0 },
                new Complex[] { -2 + 4 * j, -12 + 24 * j },
                new Complex[] { -14 + 28 * j, -18 + 36 * j }
            });

            NumericAssert.AreAlmostEqual(neg_c, -ca3x2, "neg c 1");
            ComplexMatrix neg_c_inplace = ca3x2.Clone();

            NumericAssert.AreAlmostEqual(neg_c, neg_c_inplace.Negate(), "neg c 2");
            neg_c_inplace.NegateInplace();
            NumericAssert.AreAlmostEqual(neg_c, neg_c_inplace, "neg c 3");

            // ComplexMatrix Conjugate
            ComplexMatrix conj_c = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6 + 12 * j, 0 },
                new Complex[] { 2 + 4 * j, 12 + 24 * j },
                new Complex[] { 14 + 28 * j, 18 + 36 * j }
            });

            NumericAssert.AreAlmostEqual(conj_c, ca3x2.Conjugate(), "conj c 1");
            ComplexMatrix conj_c_inplace = ca3x2.Clone();

            conj_c_inplace.ConjugateInplace();
            NumericAssert.AreAlmostEqual(conj_c, conj_c_inplace, "conj c 2");

            // ComplexMatrix Transpose (Non-Conjugated)
            ComplexMatrix trans_c = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6 - 12 * j, 2 - 4 * j, 14 - 28 * j },
                new Complex[] { 0, 12 - 24 * j, 18 - 36 * j }
            });

            NumericAssert.AreAlmostEqual(trans_c, ca3x2.Transpose(), "trans c 1");

            // ComplexMatrix Hermitian Transpose (Conjugated)
            ComplexMatrix htrans_c = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6 + 12 * j, 2 + 4 * j, 14 + 28 * j },
                new Complex[] { 0, 12 + 24 * j, 18 + 36 * j }
            });

            NumericAssert.AreAlmostEqual(htrans_c, ca3x2.HermitianTranspose(), "htrans c 1");

            // ComplexMatrix Transpose (Non-Conjugated) (Square)
            ComplexMatrix trans_c2 = new ComplexMatrix(new Complex[][] {
                new Complex[] { 8 - 24 * j, 10 - 30 * j },
                new Complex[] { 9 - 27 * j, 11 - 33 * j }
            });

            NumericAssert.AreAlmostEqual(trans_c2, cc2x2.Transpose(), "trans c2 1");
            ComplexMatrix trans_c2_inplace = cc2x2.Clone();

            trans_c2_inplace.TransposeInplace();
            NumericAssert.AreAlmostEqual(trans_c2, trans_c2_inplace, "trans c2 2");

            // ComplexMatrix Hermitian Transpose (Conjugated) (Square)
            ComplexMatrix htrans_c2 = new ComplexMatrix(new Complex[][] {
                new Complex[] { 8 + 24 * j, 10 + 30 * j },
                new Complex[] { 9 + 27 * j, 11 + 33 * j }
            });

            NumericAssert.AreAlmostEqual(htrans_c2, cc2x2.HermitianTranspose(), "htrans c2 1");
            ComplexMatrix htrans_c2_inplace = cc2x2.Clone();

            htrans_c2_inplace.HermitianTransposeInplace();
            NumericAssert.AreAlmostEqual(htrans_c2, htrans_c2_inplace, "htrans c2 2");
        }