public void MatrixNegateNull(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Assert.Throws <NullReferenceException>(() => Matrix.Negate(null));
            Matrix m1 = null;

            Assert.Throws <NullReferenceException>(() => m1.Negate());
            Assert.Throws <NullReferenceException>(() => - m1);
        }
        public void MatrixMultiplyMatrixNull(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            Assert.Throws <NullReferenceException>(() => Matrix.Multiply(testMatrix1, null));
            Matrix m1 = null;

            Assert.Throws <NullReferenceException>(() => testMatrix1 * m1);
            Assert.Throws <NullReferenceException>(() => testMatrix1.Multiply(m1));
        }
        public void MatrixAddMatrixScalarFluent(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            double scalar   = 3.0;
            Matrix m        = new Matrix(testMatrix1).Add(scalar).Add(scalar);
            Matrix expected = testMatrix1 + (scalar * 2);

            Assert.Equal(expected, m);
        }
Exemplo n.º 4
0
        public void MatrixDivideNullScalar(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            double scalar = 2.0;

            Assert.Throws <NullReferenceException>(() => Matrix.Divide(null, scalar));
            Matrix m = null;

            Assert.Throws <NullReferenceException>(() => m.Divide(scalar));
            Assert.Throws <NullReferenceException>(() => m / scalar);
        }
        public void MatrixSubtractMatrixNull(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            Assert.Throws <NullReferenceException>(() => Matrix.Subtract(testMatrix1, null));
            Matrix m1 = new Matrix(testMatrix1);

            Assert.Throws <NullReferenceException>(() => m1.Subtract(null));
            Assert.Throws <NullReferenceException>(() => m1 - null);
        }
Exemplo n.º 6
0
        public void MatrixElementOperationInvalidDimensions(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();
            Matrix testMatrix3 = Setup.GetTestMatrix3();

            Assert.Throws <InvalidMatrixDimensionsException>(() => Matrix.ElementOperation(testMatrix1, testMatrix3, (a, b) => a + b));
            Matrix m1 = new Matrix(testMatrix1);

            Assert.Throws <InvalidMatrixDimensionsException>(() => m1.ElementOperation(testMatrix3, (a, b) => a + b));
        }
        public void MatrixMultiplyNullScalar(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            double scalar = 3.0;

            Assert.Throws <NullReferenceException>(() => Matrix.Multiply(null, scalar));
            Matrix m1 = null;

            Assert.Throws <NullReferenceException>(() => m1.Multiply(scalar));
            Assert.Throws <NullReferenceException>(() => m1 * scalar);
        }
        public void MatrixAddNullMatrix(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            Assert.Throws <NullReferenceException>(() => Matrix.Add(null, testMatrix1));
            Matrix m1 = null;

            Assert.Throws <NullReferenceException>(() => m1.Add(testMatrix1));
            Assert.Throws <NullReferenceException>(() => m1 + testMatrix1);
        }
        public void MatrixSubtractMatrixMatrixInvalidDimensions(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();
            Matrix testMatrix3 = Setup.GetTestMatrix3();

            Assert.Throws <InvalidMatrixDimensionsException>(() => Matrix.Subtract(testMatrix1, testMatrix3));
            Matrix m1 = new Matrix(testMatrix1);

            Assert.Throws <InvalidMatrixDimensionsException>(() => m1.Subtract(testMatrix3));
            Assert.Throws <InvalidMatrixDimensionsException>(() => testMatrix1 - testMatrix3);
        }
        public void MatrixMultiplyMatrixMatrixInvalidDimensions(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();
            Matrix testMatrix2 = Setup.GetTestMatrix2();

            Assert.Throws <InvalidMatrixDimensionsException>(() => Matrix.Multiply(testMatrix1, testMatrix2));
            Matrix m1 = new Matrix(testMatrix1);

            Assert.Throws <InvalidMatrixDimensionsException>(() => testMatrix1 * testMatrix2);
            Assert.Throws <InvalidMatrixDimensionsException>(() => testMatrix1.Multiply(testMatrix2));
        }
        public void MatrixSubtractNullScalar(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            double scalar = 3.0;

            Assert.Throws <NullReferenceException>(() => Matrix.Subtract(null, scalar));
            Matrix m1 = null;

            Assert.Throws <NullReferenceException>(() => m1.Subtract(scalar));
            Assert.Throws <NullReferenceException>(() => m1 - scalar);
            Assert.Throws <NullReferenceException>(() => scalar - m1);
        }
        public void MatrixAddNullScalar(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            double scalar = 3.0;

            Assert.Throws <NullReferenceException>(() => Matrix.Add(null, scalar));
            Matrix m1 = null;

            Assert.Throws <NullReferenceException>(() => m1.Add(scalar));
            Assert.Throws <NullReferenceException>(() => m1 + scalar);
            Assert.Throws <NullReferenceException>(() => scalar + m1);
        }
        public void MatrixMultiplyMatrixScalarFluent(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            double scalar = 3.0;
            Matrix m      = new Matrix(testMatrix1);

            m.Multiply(scalar).Multiply(scalar);

            Matrix expected = testMatrix1 * (scalar * scalar);

            Assert.Equal(expected, m);
        }
Exemplo n.º 14
0
        public void MatrixDivideMatrixScalarFluent(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();
            double scalar      = 2.0;

            Matrix m = new Matrix(testMatrix1);

            m.Divide(scalar).Divide(scalar);
            Matrix expected = new Matrix(new double[, ] {
                { 0.25, 0.5, 0.75 }, { 1.0, 1.25, 1.5 }
            });

            Assert.Equal(expected, m);
        }
Exemplo n.º 15
0
        public void MatrixElementOperation(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix m1 = new Matrix(2, 2);

            m1.Fill(3.0);
            Matrix m2 = Matrix.ElementOperation(m1, a => Math.Sin(a));

            m1.ElementOperation(a => Math.Sin(a));
            double expectedElement = Math.Sin(3.0);
            Matrix expected        = new Matrix(2, 2, expectedElement);

            Assert.True(expected == m1);
            Assert.True(expected == m2);
        }
Exemplo n.º 16
0
        public void MatrixElementOperationScalar(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix m1 = new Matrix(2, 2);

            m1.Fill(3.0);
            Matrix m2 = Matrix.ElementOperation(m1, 2.0, (a, b) => Math.Pow(a, b));

            m1.ElementOperation(2.0, (a, b) => Math.Pow(a, b));
            double expectedElement = Math.Pow(3.0, 2.0);
            Matrix expected        = new Matrix(2, 2, expectedElement);

            Assert.True(expected == m1);
            Assert.True(expected == m2);
        }
Exemplo n.º 17
0
        public void MatrixElementOperationMatrix(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            double x = 3.0, y = 4.0;
            Matrix m1 = new Matrix(2, 2);
            Matrix m2 = new Matrix(2, 2);

            m1.Fill(x);
            m2.Fill(y);
            Matrix m3 = Matrix.ElementOperation(m1, m2, (a, b) => a + b);

            m1.ElementOperation(m2, (a, b) => a + b);
            Matrix expected = new Matrix(2, 2, x + y);

            Assert.True(expected == m1);
            Assert.True(expected == m3);
        }
        public void MatrixNegate(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            Matrix m1 = Matrix.Negate(testMatrix1);
            Matrix m2 = new Matrix(testMatrix1); m2.Negate();
            Matrix m3 = -testMatrix1;

            Matrix expected = new Matrix(new double[, ] {
                { -1.0, -2.0, -3.0 }, { -4.0, -5.0, -6.0 }
            });

            Assert.Equal(expected, m1);
            Assert.Equal(expected, m2);
            Assert.Equal(expected, m3);
        }
Exemplo n.º 19
0
        public void MatrixDivideMatrixScalar(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();
            double scalar      = 2.0;

            Matrix m1       = Matrix.Divide(testMatrix1, scalar);
            Matrix m2       = new Matrix(testMatrix1); m2.Divide(scalar);
            Matrix m3       = testMatrix1 / scalar;
            Matrix expected = new Matrix(new double[, ] {
                { 0.5, 1.0, 1.5 }, { 2.0, 2.5, 3.0 }
            });

            Assert.Equal(expected, m1);
            Assert.Equal(expected, m2);
            Assert.Equal(expected, m3);
        }
        public void MatrixSubtractMatrixMatrix(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();
            Matrix testMatrix2 = Setup.GetTestMatrix2();

            Matrix m1       = Matrix.Subtract(testMatrix2, testMatrix1);
            Matrix m2       = new Matrix(testMatrix2); m2.Subtract(testMatrix1);
            Matrix m3       = testMatrix2 - testMatrix1;
            Matrix expected = new Matrix(new double[, ] {
                { 6.0, 6.0, 6.0 }, { 6.0, 6.0, 6.0 }
            });

            Assert.Equal(expected, m1);
            Assert.Equal(expected, m2);
            Assert.Equal(expected, m3);
        }
        public void MatrixMultipyMatrixMatrixFluent(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();
            Matrix testMatrix3 = Setup.GetTestMatrix3();

            Matrix m1 = new Matrix(testMatrix1);
            Matrix m2 = new Matrix(new double[, ] {
                { 22.0, 28.0 }, { 49.0, 64.0 }
            });
            Matrix expected = new Matrix(new double[, ] {
                { 1856.0, 2408.0 }, { 4214.0, 5468.0 }
            });

            m1.Multiply(testMatrix3).Multiply(m2);
            Assert.Equal(expected, m1);
        }
        public void MatrixAddMatrixMatrix(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();
            Matrix testMatrix2 = Setup.GetTestMatrix2();

            Matrix m1 = Matrix.Add(testMatrix1, testMatrix2);
            Matrix m2 = new Matrix(testMatrix1);

            m2.Add(testMatrix2);
            Matrix m3       = testMatrix1 + testMatrix2;
            Matrix expected = new Matrix(new double[, ] {
                { 8.0, 10.0, 12.0 }, { 14.0, 16.0, 18.0 }
            });

            Assert.Equal(expected, m1);
            Assert.Equal(expected, m2);
            Assert.Equal(expected, m3);
        }
        public void MatrixMultiplyMatrixScalar(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            double scalar   = 3.0;
            Matrix m1       = Matrix.Multiply(testMatrix1, scalar);
            Matrix m2       = new Matrix(testMatrix1); m2.Multiply(scalar);
            Matrix m3       = testMatrix1 * scalar;
            Matrix m4       = scalar * testMatrix1;
            Matrix expected = new Matrix(new double[, ] {
                { 3.0, 6.0, 9.0 }, { 12.0, 15.0, 18.0 }
            });

            Assert.Equal(expected, m1);
            Assert.Equal(expected, m2);
            Assert.Equal(expected, m3);
            Assert.Equal(expected, m4);
        }
        public void MatrixAddMatrixScalar(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            double scalar = 3.0;
            Matrix m1     = Matrix.Add(testMatrix1, scalar);
            Matrix m2     = new Matrix(testMatrix1); m2.Add(scalar);
            Matrix m3     = testMatrix1 + scalar;
            Matrix m4     = scalar + testMatrix1;

            Matrix expected = new Matrix(new double[, ] {
                { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 }
            });

            Assert.Equal(expected, m1);
            Assert.Equal(expected, m2);
            Assert.Equal(expected, m3);
            Assert.Equal(expected, m4);
        }
        public void MatrixSubtractMatrixScalar(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            double scalar = 3.0;
            Matrix m1     = Matrix.Subtract(testMatrix1, scalar);
            Matrix m2     = new Matrix(testMatrix1); m2.Subtract(scalar);
            Matrix m3     = testMatrix1 - scalar;
            Matrix m4     = scalar - testMatrix1;

            Matrix expected = new Matrix(new double[, ] {
                { -2.0, -1.0, 0.0 }, { 1.0, 2.0, 3.0 }
            });

            Assert.Equal(expected, m1);
            Assert.Equal(expected, m2);
            Assert.Equal(expected, m3);
            Assert.Equal(expected, m4);
        }
        public void MatrixMultiplyMatrixMatrix(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();
            Matrix testMatrix3 = Setup.GetTestMatrix3();

            Matrix m1 = Matrix.Multiply(testMatrix1, testMatrix3);
            Matrix m2 = testMatrix1 * testMatrix3;
            Matrix m3 = new Matrix(testMatrix1);

            m3.Multiply(testMatrix3);

            Matrix expected = new Matrix(new double[, ] {
                { 22.0, 28.0 }, { 49.0, 64.0 }
            });

            Assert.Equal(expected, m1);
            Assert.Equal(expected, m2);
            Assert.Equal(expected, m3);
        }
        public void MatrixAddMatrixMatrixFluent(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            Matrix m1 = new Matrix(testMatrix1);

            m1.Add(testMatrix1).Add(testMatrix1);

            Matrix m2 = new Matrix(testMatrix1);

            m2.Add(m2).Add(m2);

            Matrix m3 = Matrix.Add(testMatrix1, testMatrix1).Add(testMatrix1);

            Matrix expected1 = testMatrix1 * 3;
            Matrix expected2 = testMatrix1 * 4;

            Assert.Equal(expected1, m1);
            Assert.Equal(expected2, m2);
            Assert.Equal(expected1, m3);
        }
        public void MatrixSubtractMatrixMatrixFluent(IMatrixArithmetic arithmetic)
        {
            Matrix.Arithmetic = arithmetic;
            Matrix testMatrix1 = Setup.GetTestMatrix1();

            Matrix m1 = new Matrix(testMatrix1);

            m1.Subtract(testMatrix1).Subtract(testMatrix1);

            Matrix m2 = new Matrix(testMatrix1);

            m2.Subtract(m2).Subtract(m2); // After the first subtraction, m2 is zero, so further subtractions have no effect

            Matrix m3 = Matrix.Subtract(testMatrix1, testMatrix1).Subtract(testMatrix1);

            Matrix expected1 = -testMatrix1;
            Matrix expected2 = new Matrix(testMatrix1).Zeros();

            Assert.Equal(expected1, m1);
            Assert.Equal(expected2, m2);
            Assert.Equal(expected1, m3);
        }