コード例 #1
0
        public void MatrixSumTest()
        {
            var symM = new SymmetricMatrix <int>(3);

            symM.SetElements(new int[, ]
            {
                { 1, 2, 3 },
                { 2, 3, 1 },
                { 3, 1, 2 }
            });

            var diagM = new DiagonalMatrix <int>(3);

            diagM.SetElements(new int[, ]
            {
                { 1, 0, 0 },
                { 0, 2, 0 },
                { 0, 0, 3 }
            });

            var correctSum = new SquareMatrix <int>(3);

            correctSum.SetElements(new int[, ]
            {
                { 2, 2, 3 },
                { 2, 5, 1 },
                { 3, 1, 5 }
            });

            var incorrectSum = new SquareMatrix <int>(3);

            incorrectSum.SetElements(new int[, ]
            {
                { 0, 0, 0 },
                { 0, 0, 0 },
                { 0, 0, 0 }
            });

            var result = symM.AddMatrix(diagM, delegate(int a, int b) { return(a + b); });

            Assert.That(result, Is.EqualTo(correctSum));
            Assert.That(result, Is.Not.EqualTo(incorrectSum));
        }
コード例 #2
0
        public void AddTest()
        {
            SymmetricMatrix <int> firstMatrix = new SymmetricMatrix <int>(new int[, ] {
                { 1, 1, 1 }, { 1, 2, 1 }, { 1, 1, 3 }
            });
            DiagonalMatrix <int> secondMatrix = new DiagonalMatrix <int>(new int[, ] {
                { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 }
            });
            SquareMatrix <int> expectedMatrix = new SquareMatrix <int>(new int[, ] {
                { 2, 1, 1 }, { 1, 4, 1 }, { 1, 1, 6 }
            });

            AbstractMatrix <int> resultMatrix = firstMatrix.AddMatrix(secondMatrix);

            for (int i = 0; i < firstMatrix.Dimension; i++)
            {
                for (int j = 0; j < firstMatrix.Dimension; j++)
                {
                    Assert.AreEqual(expectedMatrix[i, j], resultMatrix[i, j]);
                }
            }
        }