Пример #1
0
        /// <inheritdoc />
        IMathematicalMatrix IMathematicalMatrix.Multiply(IMathematicalMatrix matrix)
        {
            #region Validation

            Guard.ArgumentNotNull(matrix, "matrix");

            // Check the dimensions to make sure the operation is a valid one.
            if (noOfColumns != matrix.Rows)
            {
                throw new ArgumentException(IncompatibleMatricesTimes, "matrix");
            }

            #endregion

            var ret = new Matrix(noOfRows, matrix.Columns);

            for (var i = 0; i < noOfRows; i++)
            {
                for (var j = 0; j < matrix.Columns; j++)
                {
                    double sum = 0;

                    for (var k = 0; k < noOfColumns; k++)
                    {
                        sum += (GetValue(i, k) * matrix[k, j]);
                    }

                    ret.SetValue(i, j, sum);
                }
            }

            return(ret);
        }
Пример #2
0
        public void ConcatenateInterface()
        {
            IMathematicalMatrix matrix1 = MatrixTest.GetTestMatrix();
            IMathematicalMatrix matrix2 = GetTestMinusMatrix();

            var concat = matrix1.Concatenate(matrix2);

            Assert.AreEqual(concat.Rows, matrix1.Rows);
            Assert.AreEqual(concat.Columns, matrix1.Columns + matrix2.Columns);

            for (var i = 0; i < matrix1.Rows; i++)
            {
                for (var j = 0; j < matrix1.Columns; j++)
                {
                    Assert.AreEqual(concat[i, j], matrix1[i, j]);
                }
            }

            for (var i = 0; i < matrix2.Rows; i++)
            {
                for (var j = 0; j < matrix2.Columns; j++)
                {
                    Assert.AreEqual(concat[i, j + matrix1.Columns], matrix2[i, j]);
                }
            }
        }
Пример #3
0
        public bool Equals(IMathematicalMatrix other, double precision)
        {
            if (other == null)
            {
                return(false);
            }

            if (other.Rows != Rows)
            {
                return(false);
            }

            if (other.Columns != Columns)
            {
                return(false);
            }

            for (var i = 0; i < Rows; i++)
            {
                for (var j = 0; j < Columns; j++)
                {
                    if (!GetValue(i, j).IsSimilarTo(other[i, j], precision))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #4
0
        /// <inheritdoc />
        IMathematicalMatrix IMathematicalMatrix.Subtract(IMathematicalMatrix matrix)
        {
            #region Validation

            Guard.ArgumentNotNull(matrix, "matrix");

            if ((noOfRows != matrix.Rows) || (noOfColumns != matrix.Columns))
            {
                throw new ArgumentException(IncompatibleMatrices, "matrix");
            }

            #endregion

            var ret = new Matrix(noOfRows, noOfColumns);

            for (var i = 0; i < noOfRows; i++)
            {
                for (var j = 0; j < noOfColumns; j++)
                {
                    ret.SetValue(i, j, GetValue(i, j) - matrix[i, j]);
                }
            }

            return(ret);
        }
Пример #5
0
        public void MinorArbitraryInterface()
        {
            IMathematicalMatrix matrix = MatrixTest.GetTestMatrix();

            var rows    = matrix.Rows;
            var columns = matrix.Columns;

            var minor = matrix.Minor(2, 3);

            Assert.AreEqual(minor.Columns, columns - 1);
            Assert.AreEqual(minor.Rows, rows - 1);

            for (var i = 0; i < minor.Rows; i++)
            {
                for (var j = 0; j < minor.Columns; j++)
                {
                    var addAmount = 0;

                    if (i >= 2)
                    {
                        addAmount++;
                    }

                    if (j >= 3)
                    {
                        addAmount++;
                    }

                    Assert.AreEqual(minor[i, j], i + j + addAmount);
                }
            }

            Assert.IsTrue(MatrixTest.IsOriginalTestMatrix(matrix));
        }
Пример #6
0
        public bool Equals(IMathematicalMatrix other)
        {
            if (other == null)
            {
                return(false);
            }

            if (other.Rows != Rows)
            {
                return(false);
            }

            if (other.Columns != Columns)
            {
                return(false);
            }

            for (var i = 0; i < Rows; i++)
            {
                for (var j = 0; j < Columns; j++)
                {
                    if (GetValue(i, j) != other[i, j])
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #7
0
 /// <summary>
 ///   Adds to matrices according to the linear algebra operator +.
 /// </summary>
 /// <param name="matrix"> The result of the addition. </param>
 /// <returns> The result of the plus operation. </returns>
 IMathematicalMatrix IMathematicalMatrix.Plus(IMathematicalMatrix matrix)
 {
     if (matrix.GetType() != typeof(Matrix))
     {
         throw new ArgumentException(Resources.InvalidOperationWrongMatrixType);
     }
     return(Plus((Matrix)matrix));
 }
Пример #8
0
        public void ExceptionIncompatibleMatrices()
        {
            IMathematicalMatrix matrix1 = new Matrix(2, 3);
            matrix1[0, 0] = 1;
            matrix1[0, 1] = 2;
            matrix1[0, 2] = -4;
            matrix1[1, 0] = 0;
            matrix1[1, 1] = 3;
            matrix1[1, 2] = -1;

            IMathematicalMatrix matrix2 = MatrixTest.GetTestMatrix();

            matrix1.Add(matrix2);
        }
Пример #9
0
        public static bool IsOriginalTestMatrix(IMathematicalMatrix matrix)
        {
            for (var i = 0; i < matrix.Rows; i++)
            {
                for (var j = 0; j < matrix.Columns; j++)
                {
                    if (matrix[i, j] != i + j)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #10
0
        public static bool IsOriginalTestMatrix(IMathematicalMatrix matrix)
        {
            for (var i = 0; i < matrix.Rows; i++)
            {
                for (var j = 0; j < matrix.Columns; j++)
                {
                    if (matrix[i, j] != i + j)
                    {
                        return false;
                    }
                }
            }

            return true;
        }
Пример #11
0
        public void ExceptionIncompatibleMatrices2()
        {
            IMathematicalMatrix matrix1 = new Matrix(2, 3);

            matrix1[0, 0] = 1;
            matrix1[0, 1] = 2;
            matrix1[0, 2] = -4;
            matrix1[1, 0] = 0;
            matrix1[1, 1] = 3;
            matrix1[1, 2] = -1;

            IMathematicalMatrix matrix2 = MatrixTest.GetTestMatrix();

            Assert.Throws <ArgumentException>(() => matrix1.Subtract(matrix2));
        }
Пример #12
0
        public void ColumnArbitraryInterface()
        {
            IMathematicalMatrix matrix = MatrixTest.GetTestMatrix();

            matrix.MultiplyColumn(3, 3);

            for (var i = 0; i < matrix.Rows; i++)
            {
                for (var j = 0; j < matrix.Columns; j++)
                {
                    if (j == 3)
                    {
                        Assert.AreEqual(matrix[i, j], 3 * (i + j));
                    }
                    else
                    {
                        Assert.AreEqual(matrix[i, j], i + j);
                    }
                }
            }
        }
Пример #13
0
        public void MinorBottomRightInterface()
        {
            IMathematicalMatrix matrix = MatrixTest.GetTestMatrix();

            var rows    = matrix.Rows;
            var columns = matrix.Columns;

            var minor = matrix.Minor(matrix.Rows - 1, matrix.Columns - 1);

            Assert.AreEqual(minor.Columns, columns - 1);
            Assert.AreEqual(minor.Rows, rows - 1);

            for (var i = 0; i < minor.Rows; i++)
            {
                for (var j = 0; j < minor.Columns; j++)
                {
                    Assert.AreEqual(minor[i, j], i + j);
                }
            }

            Assert.IsTrue(MatrixTest.IsOriginalTestMatrix(matrix));
        }
Пример #14
0
        public void ExceptionInterfaceTimesNull()
        {
            IMathematicalMatrix matrix = MatrixTest.GetTestMatrix();

            matrix.Multiply(null);
        }
Пример #15
0
 public void ExceptionPlusNull()
 {
     IMathematicalMatrix matrix = MatrixTest.GetTestMatrix();
     matrix.Add(null);
 }
Пример #16
0
        public void ExceptionInterfaceTimesNull()
        {
            IMathematicalMatrix matrix = MatrixTest.GetTestMatrix();

            Assert.Throws <ArgumentNullException>(() => matrix.Multiply(null));
        }
Пример #17
0
 public void ExceptionInterfaceMinusNull()
 {
     IMathematicalMatrix matrix = MatrixTest.GetTestMatrix();
     matrix.Subtract(null);
 }
Пример #18
0
        public void ExceptionInterfaceMinusNull()
        {
            IMathematicalMatrix matrix = MatrixTest.GetTestMatrix();

            Assert.Throws <ArgumentNullException>(() => matrix.Subtract(null));
        }
Пример #19
0
        public void ExceptionPlusNull()
        {
            IMathematicalMatrix matrix = MatrixTest.GetTestMatrix();

            Assert.Throws <ArgumentNullException>(() => matrix.Add(null));
        }
Пример #20
0
 /// <inheritdoc />
 IMathematicalMatrix IMathematicalMatrix.Add(IMathematicalMatrix matrix)
 {
     return(AddInternal(this, matrix));
 }
Пример #21
0
 /// <inheritdoc />
 IMathematicalMatrix IMathematicalMatrix.Concatenate(IMathematicalMatrix rightMatrix)
 {
     return(ConcatenateInternal(this, rightMatrix));
 }