Example #1
0
		public void TestMultiply()
		{
			Matrix m = new Matrix(3, 3);
			m.SetRow(0, -1.1F, 2.6F, -7.1F);
			m.SetRow(1, 4.6F, -3.7F, 9.1F);
			m.SetRow(2, 4.1F, -3.1F, 7.7F);

			Matrix result = new Matrix(3, 3);
			result.SetRow(0, -3.41F, 8.06F, -22.01F);
			result.SetRow(1, 14.26F, -11.47F, 28.21F);
			result.SetRow(2, 12.71F, -9.61F, 23.87F);

			Assert.IsTrue(Matrix.AreEqual(m * 3.1F, result));
		}
        public void TestInc()
        {
            var matrix = new Matrix<double>(5, 5);
            double[] row = { 1, 2, 3, 4, 5 };
            for (int i = 0; i < 5; i++)
                matrix.SetRow(i, row);
            matrix.Inc(3, 4, 2.5);
            Assert.AreEqual(7.5, matrix[3, 4]);

            var matrix1 = new Matrix<double>(5, 5);
            for (int i = 0; i < 5; i++)
                matrix1.SetRow(i, row);
            var matrix2 = new Matrix<double>(5, 5);
            for (int i = 0; i < 5; i++)
                matrix2.SetRow(i, row);
            double[] testrow = { 2, 4, 6, 8, 10 };
            matrix1.Inc(matrix2);
            Assert.AreEqual(testrow, matrix1.GetRow(2));

            var matrix3 = new Matrix<double>(5, 5);
            for (int i = 0; i < 5; i++)
                matrix3.SetRow(i, row);
            matrix3.Inc(1.0);
            for (int j = 0; j < 5; j++)
                Assert.AreEqual(row[j] + 1, matrix3[1, j]);

            var matrix4 = new Matrix<int>(5, 5);
            int[] int_row = { 1, 2, 3, 4, 5 };
            for (int i = 0; i < 5; i++)
                matrix4.SetRow(i, int_row);
            Assert.AreEqual(matrix4[1, 2], 3);
            matrix4.Inc(1, 2);
            Assert.AreEqual(matrix4[1, 2], 4);
        }
Example #3
0
 public void TestInc()
 {
     var matrix = new Matrix<double>(5, 5);
     double[] row = { 1, 2, 3, 4, 5 };
     for (int i = 0; i < 5; i++)
         matrix.SetRow(i, row);
     MatrixUtils.Inc(matrix, 3, 4, 2.5);
     Assert.AreEqual(7.5, matrix[3, 4]);
 }
Example #4
0
 public void TestFrobeniusNorm()
 {
     var matrix = new Matrix<double>(5, 5);
     double[] row = { 1, 2, 3, 4, 5 };
     for (int i = 0; i < 5; i++)
         matrix.SetRow(i, row);
     double result =Math.Sqrt(275.0);
     Assert.AreEqual(result,MatrixUtils.FrobeniusNorm(matrix));
 }
		[Test()] public void TestColumnAverage()
		{
			var matrix = new Matrix<float>(5, 5);
			float[] row = { 1, 2, 3, 4, 5 };
			for (int i = 0; i < 5; i++)
				matrix.SetRow(i, row);
			Assert.AreEqual(2.0, matrix.ColumnAverage(1));
			Assert.AreEqual(5.0, matrix.ColumnAverage(4));
		}
Example #6
0
 public void TestGetSetRow()
 {
     var matrix = new Matrix<int>(5, 5);
     int[] row = { 1, 2, 3, 4, 5 };
     matrix.SetRow(3, row);
     Assert.AreEqual(row, matrix.GetRow(3));
     Assert.AreEqual(0, matrix[0, 0]);
     Assert.AreEqual(1, matrix[3, 0]);
 }
Example #7
0
 public void TestColumnAverage()
 {
     var matrix = new Matrix<double>(5, 5);
     double[] row = { 1, 2, 3, 4, 5 };
     for (int i = 0; i < 5; i++)
         matrix.SetRow(i, row);
     Assert.AreEqual(2.0, MatrixUtils.ColumnAverage(matrix, 1));
     Assert.AreEqual(5.0, MatrixUtils.ColumnAverage(matrix, 4));
 }
Example #8
0
 public void TestSetColumnToOneValue()
 {
     var matrix = new Matrix<int>(5, 5);
     int[] row = { 1, 2, 3, 4, 5 };
     for (int i = 0; i < 5; i++)
         matrix.SetRow(i, row);
     matrix.SetColumnToOneValue(3, 10);
     int[] testcolumn = { 10, 10, 10, 10, 10 };
     Assert.AreEqual(testcolumn, matrix.GetColumn(3));
 }
Example #9
0
		public void TestSubtract()
		{
			Matrix m1 = new Matrix(3, 3);
			m1.SetColumn(0, 2.2F, -6.1F, -7.6F);
			m1.SetColumn(1, -3.4F, 7.2F, 8.7F);
			m1.SetColumn(2, 1.6F, 5.5F, -9.8F);

			Matrix m2 = new Matrix(3, 3);
			m2.SetRow(0, -1.1F, 2.6F, -7.1F);
			m2.SetRow(1, 4.6F, -3.7F, 9.1F);
			m2.SetRow(2, 4.1F, -3.1F, 7.7F);

			Matrix result = new Matrix(3, 3);
			result.SetRow(0, 3.3F, -6F, 8.7F);
			result.SetRow(1, -10.7F, 10.9F, -3.6F);
			result.SetRow(2, -11.7F, 11.8F, -17.5F);

			Assert.IsTrue(Matrix.AreEqual(m1 - m2, result));
		}
		[Test()] public void TestMultiply()
		{
			var matrix = new Matrix<float>(5, 5);
			float[] row = { 1, 2, 3, 4, 5 };
			for (int i = 0; i < 5; i++)
				matrix.SetRow(i, row);
			matrix.Multiply(2.5f);
			float[] testrow = { 2.5f, 5f, 7.5f, 10f, 12.5f };
			Assert.AreEqual(testrow, matrix.GetRow(3));
		}
Example #11
0
		public void TestAdd()
		{
			Matrix m1 = new Matrix(3, 3);
			m1.SetColumn(0, 2.2F, -6.1F, -7.6F);
			m1.SetColumn(1, -3.4F, 7.2F, 8.7F);
			m1.SetColumn(2, 1.6F, 5.5F, -9.8F);

			Matrix m2 = new Matrix(3, 3);
			m2.SetRow(0, -1.1F, 2.6F, -7.1F);
			m2.SetRow(1, 4.6F, -3.7F, 9.1F);
			m2.SetRow(2, 4.1F, -3.1F, 7.7F);

			Matrix result = new Matrix(3, 3);
			result.SetRow(0, 1.1F, -0.8F, -5.5F);
			result.SetRow(1, -1.5F, 3.5F, 14.6F);
			result.SetRow(2, -3.5F, 5.6F, -2.1F);

			Assert.IsTrue(Matrix.AreEqual(m1 + m2, result));
		}
Example #12
0
		public void TestConstructor()
		{
			var m = new Matrix(3, 3);
			m.SetColumn(0, 0, 0, 0);
			m.SetColumn(1, 0, 0, 0);
			m.SetColumn(2, 0, 0, 0);
			Assert.IsTrue(Matrix.AreEqual(m, new Matrix(3, 3)));

			m.SetRow(0, 1, 2, 3);
			m.SetRow(1, 4, 5, 6);
			m.SetRow(2, 7, 8, 9);
			Assert.IsTrue(Matrix.AreEqual(m, new Matrix(new float[,] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}})));
			Assert.IsTrue(Matrix.AreEqual(m, new Matrix(new Matrix(new float[,] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}))));

			try
			{
				new Matrix(0, 0);
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentException) {}

			try
			{
				new Matrix(new Matrix(4, -5));
				Assert.Fail("Expected an exception");
			}
            catch (ArgumentException) { }

			try
			{
				new Matrix((Matrix) null);
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentNullException) {}

			try
			{
				new Matrix((float[,]) null);
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentNullException) {}
		}
Example #13
0
        public void TestInc2()
        {
            var matrix1 = new Matrix<double>(5, 5);
            double[] row = { 1, 2, 3, 4, 5 };
            for (int i = 0; i < 5; i++)
                matrix1.SetRow(i, row);

            var matrix2 = new Matrix<double>(5, 5);
            for (int i = 0; i < 5; i++)
                matrix2.SetRow(i, row);
            double[] testrow = {2, 4, 6, 8, 10};
            MatrixUtils.Inc(matrix1, matrix2);
            Assert.AreEqual(testrow, matrix1.GetRow(2));
        }
Example #14
0
		public void TestRowSetter()
		{
			Matrix m = new Matrix(1, 3);
			m.SetRow(0, 1F, 2F, 3F);

			try
			{
				//not enough.
				m.SetRow(0, 1F, 2F);
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentException) {}
		}
Example #15
0
		public void TestInverse4()
		{
			// test identity inverse
			var m = Matrix.GetIdentity(4);

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), new Matrix(new float[,] {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}})));

			// test a known inverse
			m = new Matrix(new[,]
			               	{
			               		{0.100f, 0.200f, 0.300f, 1.300f},
			               		{-0.400f, 0.500f, 0.600f, 1.400f},
			               		{0.700f, 0.800f, 0.900f, 1.500f},
			               		{1.000f, 1.100f, 1.200f, -1.600f}
			               	});

			var r = new Matrix(new[,]
			                   	{
			                   		{0.62500f, -1.25000f, 0.62500f, 0.00000f},
			                   		{-18.12500f, 2.50000f, 9.37500f, -3.75000f},
			                   		{15.8854167f, -1.25000f, -8.4895833f, 3.854167f},
			                   		{-0.15625f, 0.00000f, 0.46875f, -0.31250f}
			                   	});

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), r, _tolerance));

			// test inverse multiplied against original is the identity
			m.SetRow(0, -1.1F, 2.6F, -7.1F, 2.2F);
			m.SetRow(1, 4.6F, -3.7F, 9.1F, 6.9F);
			m.SetRow(2, 4.1F, -3.1F, 7.7F, 7.1F);
			m.SetRow(3, -9.9F, 0.2F, 4.3F, 5.5F);

			Assert.IsTrue(Matrix.AreEqual(m*m.Invert(), Matrix.GetIdentity(4), _tolerance));
			Assert.IsTrue(Matrix.AreEqual(m.Invert()*m, Matrix.GetIdentity(4), _tolerance));

			// test non-invertible
			m.SetRow(0, 1, 0, 0, 0);
			m.SetRow(1, 0, 1, 0, 0);
			m.SetRow(2, 0, 0, 1, 0);
			m.SetRow(3, 0, 0, 5, 0);

			try
			{
				m.Invert();
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentException) {}
		}
Example #16
0
		public void TestClone()
		{
			Matrix m = new Matrix(3, 3);
			m.SetRow(0, -3.41F, 8.06F, -22.01F);
			m.SetRow(1, 14.26F, -11.47F, 28.21F);
			m.SetRow(2, 12.71F, -9.61F, 23.87F);

			Assert.IsTrue(Matrix.AreEqual(m, m.Clone()));
		}
Example #17
0
		public void TestRowSetter()
		{
			Matrix m = new Matrix(1, 3);
			m.SetRow(0, 1F, 2F, 3F);

			//not enough.
			m.SetRow(0, 1F, 2F);
		}
		[Test()] public void TestRowDifference()
		{
			var matrix1 = new Matrix<float>(5, 5);
			float[] row = { 1, 2, 3, 4, 5 };
			for (int i = 0; i < 5; i++)
				matrix1.SetRow(i, row);
			var matrix2 = new Matrix<float>(5, 5);
			for (int i = 0; i < 5; i++)
				matrix2.SetRow(i, row);

			var result = MatrixExtensions.RowDifference(matrix1, 2, matrix2, 3);
			for (int i = 0; i < 5; i++)
				Assert.AreEqual(0, result[0]);
		}
Example #19
0
		public void TestTranspose()
		{
			Matrix m = new Matrix(2, 3);
			m.SetRow(0, -1.1F, 2.6F, -7.1F);
			m.SetRow(1, 4.6F, -3.7F, 9.1F);

			Matrix result = new Matrix(3, 2);
			result.SetColumn(0, -1.1F, 2.6F, -7.1F);
			result.SetColumn(1, 4.6F, -3.7F, 9.1F);

			Assert.IsTrue(Matrix.AreEqual(m.Transpose(), result));
		}
Example #20
0
		public void TestAccessor()
		{
			Matrix m = new Matrix(4, 4);
			m.SetRow(0, 1F, 0F, 0F, 0F);
			m.SetRow(1, 0F, 2F, 0F, 0F);
			m.SetRow(2, 0F, 0F, 3F, 0F);
			m.SetRow(3, 0F, 0F, 0F, 4F);

			Assert.AreEqual(m[0, 0], 1F);
			Assert.AreEqual(m[1, 1], 2F);
			Assert.AreEqual(m[2, 2], 3F);
			Assert.AreEqual(m[3, 3], 4F);

			try
			{
				float outOfRange = m[0, 4];
				Assert.Fail("Expected an exception, but instead for a value of {0}", outOfRange);
			}
			catch (ArgumentOutOfRangeException) {}
		}
		[Test()] public void TestFrobeniusNorm()
		{
			var matrix = new Matrix<float>(5, 5);
			float[] row = { 1, 2, 3, 4, 5 };
			for (int i = 0; i < 5; i++)
				matrix.SetRow(i, row);
			float result = (float) Math.Sqrt(275.0);
			Assert.AreEqual(result, matrix.FrobeniusNorm());
		}
Example #22
0
		public void TestAreEqual()
		{
			var m1 = new Matrix(3, 3);
			m1.SetColumn(0, 1, 4, 7);
			m1.SetColumn(1, 2, 5, 8);
			m1.SetColumn(2, 3, 6, 9);

			var m2 = new Matrix(3, 3);
			m2.SetRow(0, 1, 2, 3);
			m2.SetRow(1, 4, 5, 6);
			m2.SetRow(2, 7, 8, 9);

			Assert.IsTrue(Matrix.AreEqual(m1, m2));

			m2[1, 1] = 0;
			Assert.IsFalse(Matrix.AreEqual(m1, m2));
		}
		[Test()] public void TestScalarProductWithRowDifference()
		{
			var matrix1 = new Matrix<float>(5, 5);
			float[] row = { 1, 2, 3, 4, 5 };
			for (int i = 0; i < 5; i++)
				matrix1.SetRow(i, row);
			var matrix2 = new Matrix<float>(5, 5);
			for (int i = 0; i < 5; i++)
				matrix2.SetRow(i, row);
			var matrix3 = new Matrix<float>(5, 5);
			MatrixExtensions.Inc(matrix3, 1.0f);

			Assert.AreEqual(40, MatrixExtensions.RowScalarProductWithRowDifference(matrix1, 2, matrix2, 3, matrix3, 1));
		}
Example #24
0
 public void TestRowScalarProduct()
 {
     var matrix = new Matrix<double>(5, 5);
     double[] row = { 1, 2, 3, 4, 5 };
     for (int i = 0; i < 5; i++)
         matrix.SetRow(i, row);
     double[] vector = { 1, 2, 3, 4, 5 };
     double result = 55;
     Assert.AreEqual(result, MatrixUtils.RowScalarProduct(matrix, 2, vector));
 }
Example #25
0
		public void TestAccessor()
		{
			Matrix m = new Matrix(4, 4);
			m.SetRow(0, 1F, 0F, 0F, 0F);
			m.SetRow(1, 0F, 2F, 0F, 0F);
			m.SetRow(2, 0F, 0F, 3F, 0F);
			m.SetRow(3, 0F, 0F, 0F, 4F);

			Assert.AreEqual(m[0, 0], 1F);
			Assert.AreEqual(m[1, 1], 2F);
			Assert.AreEqual(m[2, 2], 3F);
			Assert.AreEqual(m[3, 3], 4F);

			float outOfRange = m[0, 4];
		}
Example #26
0
        private Matrix Hminired(Matrix A)
        {
            //function A=hminired(A)
            //%HMINIRED Initial reduction of cost matrix for the Hungarian method.
            //%
            //%B=assredin(A)
            //%A - the unreduced cost matris.
            //%B - the reduced cost matrix with linked zeros in each row.

            //% v1.0  96-06-13. Niclas Borlin, [email protected]

            //[m,n]=size(A);
            int m = A.Rows, n = A.Columns;

            //% Subtract column-minimum values from each column.
            //colMin=min(A);
            var colMin = new DenseVector(A.GetColumns().Select(col => col.Min()).ToArray());
            //A=A-colMin(ones(n,1),:);
            for (int i = 0; i < A.Rows; ++i) {
                A.SetRow(i, A.GetRow(i) - colMin);
            }

            //% Subtract row-minimum values from each row.
            //rowMin=min(A')';
            var rowMin = new DenseVector(A.GetRows().Select(row => row.Min()).ToArray());
            //A=A-rowMin(:,ones(1,n));
            for (int j = 0; j < A.Rows; ++j) {
                A.SetColumn(j, A.GetColumn(j) - rowMin);
            }

            //% Get positions of all zeros.
            //[i,j]=find(A==0);
            List<int> ilist = new List<int>();
            List<int> jlist = new List<int>();
            A.EachT((v, i, j) => {
                if (v == 0) {
                    ilist.Add(i);
                    jlist.Add(j);
                }
            });

            //% Extend A to give room for row zero list header column.
            //A(1,n+1)=0;
            Matrix tmp = Zeros(n, n + 1);
            tmp.SetSubMatrix(0, n, 0, n, A);
            //for k=1:n
            for (int k = 0; k < n; ++k) {
                //    % Get all column in this row.
                //    cols=j(k==i)';
                var cols = new List<int>();
                cols.Add(n);
                for (int i = 0; i < ilist.Count; ++i) {
                    if (ilist[i] == k) {
                        cols.Add(jlist[i]);
                    }
                }
                cols.Add(-1);

                //    % Insert pointers in matrix.
                //    A(k,[n+1 cols])=[-cols 0];
                for (int i = 0; i < cols.Count - 1; ++i) {
                    tmp[k, cols[i]] = -(cols[i + 1]) - 1;
                } // TODO 不知道对不对了
                //result[k, cols[cols.Count - 1]] = 0;
                //end
            }
            var result = tmp.Each(v => {
                if (v < 0) return v + 1;
                else if (v == 0) return NoMatch;
                else return v;
            });

            return result;
        }
		[Test()] public void TestRowScalarProduct()
		{
			var matrix = new Matrix<float>(5, 5);
			float[] row = { 1, 2, 3, 4, 5 };
			for (int i = 0; i < 5; i++)
				matrix.SetRow(i, row);
			float[] vector = { 1, 2, 3, 4, 5 };
			Assert.AreEqual(55, MatrixExtensions.RowScalarProduct(matrix, 2, vector));

			var matrix2 = new Matrix<float>(5, 5);
			for (int i = 0; i < 5; i++)
				matrix2.SetRow(i, row);
			Assert.AreEqual(55, MatrixExtensions.RowScalarProduct(matrix, 2, matrix2, 3));
		}
Example #28
0
		public void TestMultipleMatrix()
		{
			const float a11 = 1.1f;
			const float a12 = 2.1f;
			const float a13 = 3.1f;
			const float a21 = 1.2f;
			const float a31 = 1.3f;
			const float a22 = 2.2f;
			const float a32 = 2.3f;
			const float a23 = 3.2f;
			const float a33 = 3.3f;
			const float b11 = 9.1f;
			const float b12 = 8.1f;
			const float b13 = 7.1f;
			const float b21 = 9.2f;
			const float b22 = 8.2f;
			const float b23 = 7.2f;
			const float b31 = 9.3f;
			const float b32 = 8.3f;
			const float b33 = 7.3f;

			var m1 = new Matrix(new[,] {{a11, a12, a13}, {a21, a22, a23}, {a31, a32, a33}});
			var m2 = new Matrix(new[,] {{b11, b12, b13}, {b21, b22, b23}, {b31, b32, b33}});

			var result = new Matrix(3, 3);
			result.SetRow(0,
			              a11*b11 + a12*b21 + a13*b31,
			              a11*b12 + a12*b22 + a13*b32,
			              a11*b13 + a12*b23 + a13*b33);
			result.SetRow(1,
			              a21*b11 + a22*b21 + a23*b31,
			              a21*b12 + a22*b22 + a23*b32,
			              a21*b13 + a22*b23 + a23*b33);
			result.SetRow(2,
			              a31*b11 + a32*b21 + a33*b31,
			              a31*b12 + a32*b22 + a33*b32,
			              a31*b13 + a32*b23 + a33*b33);

			Assert.IsTrue(Matrix.AreEqual(m1*m2, result));
		}
Example #29
0
 public void TestMultiply()
 {
     var matrix = new Matrix<double>(5, 5);
     double[] row = { 1, 2, 3, 4, 5 };
     for (int i = 0; i<5; i++)
         matrix.SetRow(i, row);
     MatrixUtils.Multiply(matrix, 2.5);
     double[] testrow = { 2.5, 5, 7.5, 10, 12.5 };
     Assert.AreEqual(testrow, matrix.GetRow(3));
 }
Example #30
0
		public void TestIdentity()
		{
			Matrix identity = new Matrix(4, 4);
			identity.SetRow(0, 1F, 0F, 0F, 0F);
			identity.SetRow(1, 0F, 1F, 0F, 0F);
			identity.SetRow(2, 0F, 0F, 1F, 0F);
			identity.SetRow(3, 0F, 0F, 0F, 1F);

			Assert.IsTrue(identity.IsSquare);
			Assert.IsTrue(identity.IsIdentity);
			Assert.IsTrue(Matrix.AreEqual(identity, Matrix.GetIdentity(4)));

			identity[0, 1] = 1F;
			Assert.IsFalse(identity.IsIdentity);
		}