Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TestableComplexMatrixNegation{TExpected}"/> class.
 /// </summary>
 /// <param name="expected">The expected result or exception.</param>
 /// <param name="operand">The operand.</param>
 public TestableComplexMatrixNegation(
     TExpected expected,
     TestableComplexMatrix operand) :
     base(
         expected,
         operand,
         operandWritableOps:
         new Func <ComplexMatrix, ComplexMatrix>[2] {
     (o) => - o,
     (o) => ComplexMatrix.Negate(o)
 },
         operandReadOnlyOps:
         new Func <ReadOnlyComplexMatrix, ComplexMatrix>[2] {
     (o) => - o,
     (o) => ReadOnlyComplexMatrix.Negate(o)
 }
         )
 {
 }
        public void Main()
        {
            // Create the operand.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var operand = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("operand =");
            Console.WriteLine(operand);

            // Compute the negation of operand.
            var result = -operand;

            Console.WriteLine();
            Console.WriteLine("- operand =");
            Console.WriteLine(result);

            // In .NET languages that do not support overloaded operators,
            // you can use the alternative methods named Negate.
            result = ComplexMatrix.Negate(operand);

            Console.WriteLine();
            Console.WriteLine("DoubleMatrix.Negate(operand) returns");
            Console.WriteLine();
            Console.WriteLine(result);

            // Both operators and alternative methods are overloaded to
            // support read-only matrix arguments.
            // Compute the negation using a read-only wrapper of operand.
            ReadOnlyComplexMatrix readOnlyOperand = operand.AsReadOnly();

            result = -readOnlyOperand;

            Console.WriteLine();
            Console.WriteLine("- readOnlyOperand =");
            Console.WriteLine(result);
        }
        public void ComplexMatrixAddition()
        {
            /*
             * 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
             */

            // ComplexMatrix + ComplexMatrix
            ComplexMatrix sumCmCm = 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) }
            });

            Assert.That(_ca3X2 + _cb3X2, NumericIs.AlmostEqualTo(sumCmCm), "sum cc 1");
            ComplexMatrix sumCmCmInplace = _ca3X2.Clone();

            Assert.That(sumCmCmInplace.Add(_cb3X2), NumericIs.AlmostEqualTo(sumCmCm), "sum cc 2");
            sumCmCmInplace.AddInplace(_cb3X2);
            Assert.That(sumCmCmInplace, NumericIs.AlmostEqualTo(sumCmCm), "sum cc 3");

            // ComplexMatrix - ComplexMatrix
            ComplexMatrix diffCmCm = 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) }
            });

            Assert.That(_ca3X2 - _cb3X2, NumericIs.AlmostEqualTo(diffCmCm), "diff cc 1");
            ComplexMatrix diffCmCmInplace = _ca3X2.Clone();

            Assert.That(diffCmCmInplace.Subtract(_cb3X2), NumericIs.AlmostEqualTo(diffCmCm), "diff cc 2");
            diffCmCmInplace.SubtractInplace(_cb3X2);
            Assert.That(diffCmCmInplace, NumericIs.AlmostEqualTo(diffCmCm), "diff cc 3");

            // ComplexMatrix + Matrix
            ComplexMatrix sumCmM = 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) }
            });

            Assert.That(_ca3X2 + _mb3X2, NumericIs.AlmostEqualTo(sumCmM), "sum cm 1");
            ComplexMatrix sumCmMInplace = _ca3X2.Clone();

            Assert.That(sumCmMInplace.Add(_mb3X2), NumericIs.AlmostEqualTo(sumCmM), "sum cm 2");
            sumCmMInplace.AddInplace(_mb3X2);
            Assert.That(sumCmMInplace, NumericIs.AlmostEqualTo(sumCmM), "sum cm 3");

            // ComplexMatrix - Matrix
            ComplexMatrix diffCmM = 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) }
            });

            Assert.That(_ca3X2 - _mb3X2, NumericIs.AlmostEqualTo(diffCmM), "diff cm 1");
            ComplexMatrix diffCmMInplace = _ca3X2.Clone();

            Assert.That(diffCmMInplace.Subtract(_mb3X2), NumericIs.AlmostEqualTo(diffCmM), "diff cm 2");
            diffCmMInplace.SubtractInplace(_mb3X2);
            Assert.That(diffCmMInplace, NumericIs.AlmostEqualTo(diffCmM), "diff cm 3");

            // ComplexMatrix + Complex
            ComplexMatrix sumCmC = 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) }
            });

            Assert.That(_ca3X2 + _s, NumericIs.AlmostEqualTo(sumCmC), "sum cs 1");
            ComplexMatrix sumCmCInplace = _ca3X2.Clone();

            Assert.That(sumCmCInplace.Add(_s), NumericIs.AlmostEqualTo(sumCmC), "sum cs 2");
            sumCmCInplace.AddInplace(_s);
            Assert.That(sumCmCInplace, NumericIs.AlmostEqualTo(sumCmC), "sum cs 3");

            // ComplexMatrix - Complex
            ComplexMatrix diffCmC = 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) }
            });

            Assert.That(_ca3X2 - _s, NumericIs.AlmostEqualTo(diffCmC), "diff cs 1");
            ComplexMatrix diffCmCInplace = _ca3X2.Clone();

            Assert.That(diffCmCInplace.Subtract(_s), NumericIs.AlmostEqualTo(diffCmC), "diff cs 2");
            diffCmCInplace.SubtractInplace(_s);
            Assert.That(diffCmCInplace, NumericIs.AlmostEqualTo(diffCmC), "diff cs 3");

            // ComplexMatrix Negate
            ComplexMatrix negateCm = 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, NumericIs.AlmostEqualTo(negateCm), "neg c 1");
            ComplexMatrix negCmInplace = _ca3X2.Clone();

            Assert.That(negCmInplace.Negate(), NumericIs.AlmostEqualTo(negateCm), "neg c 2");
            negCmInplace.NegateInplace();
            Assert.That(negCmInplace, NumericIs.AlmostEqualTo(negateCm), "neg c 3");
        }
        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");
        }