Example #1
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));
		}
Example #2
0
 public void TestGetSetColumn()
 {
     var matrix = new Matrix<int>(5, 5);
     int[] column = { 1, 2, 3, 4, 5 };
     matrix.SetColumn(3, column);
     Assert.AreEqual(column, matrix.GetColumn(3));
     Assert.AreEqual(0, matrix[0, 0]);
     Assert.AreEqual(1, matrix[0, 3]);
 }
Example #3
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));
		}
Example #4
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 #5
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 #6
0
 public Matrix Transpose()
 {
     Matrix m = new Matrix(this.Length, 1);
     m.SetColumn(1, this.m_data);
     return m;
 }
Example #7
0
		public void TestColumnSetter()
		{
			Matrix m = new Matrix(3, 1);
			m.SetColumn(0, 1F, 2F, 3F);

			//too many.
			m.SetColumn(0, 1F, 2F, 3F, 4F);
		}
Example #8
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 #9
0
        void LearnUserAttributeToFactorMapping()
        {
            // no mapping of no user attributes present
            if (user_attributes.NumberOfEntries == 0)
                return;

            // create attribute-to-factor weight matrix
            this.user_attribute_to_factor = new Matrix<float>(NumUserAttributes + 1, num_factors);
            Console.Error.WriteLine("num_user_attributes=" + NumUserAttributes);
            // store the results of the different runs in the following array
            var old_user_attribute_to_factor = new Matrix<float>[num_init_mapping];

            Console.Error.WriteLine("Will use {0} examples ...", num_iter_mapping * MaxUserID);

            var old_rmse_per_factor = new double[num_init_mapping][];

            for (int h = 0; h < num_init_mapping; h++)
            {
                user_attribute_to_factor.InitNormal(InitMean, InitStdDev);
                Console.Error.WriteLine("----");

                for (int i = 0; i < num_iter_mapping * MaxUserID; i++)
                    UpdateUserMapping();
                ComputeUserMappingFit();

                old_user_attribute_to_factor[h] = new Matrix<float>(user_attribute_to_factor);
                old_rmse_per_factor[h] = ComputeUserMappingFit();
            }

            var min_rmse_per_factor = new double[num_factors];
            for (int i = 0; i < num_factors; i++)
                min_rmse_per_factor[i] = double.MaxValue;
            var best_factor_init = new int[num_factors];

            // find best factor mappings:
            for (int i = 0; i < num_init_mapping; i++)
            {
                for (int j = 0; j < num_factors; j++)
                {
                    if (old_rmse_per_factor[i][j] < min_rmse_per_factor[j])
                    {
                        min_rmse_per_factor[j] = old_rmse_per_factor[i][j];
                        best_factor_init[j]    = i;
                    }
                }
            }

            // set the best weight combinations for each factor mapping
            for (int i = 0; i < num_factors; i++)
            {
                Console.Error.WriteLine("Factor {0}, pick {1}", i, best_factor_init[i]);
                user_attribute_to_factor.SetColumn(i, old_user_attribute_to_factor[best_factor_init[i]].GetColumn(i));
            }

            Console.Error.WriteLine("----");
            ComputeUserMappingFit();

            ComputeFactorsForNewUsers();
        }
Example #10
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;
        }
Example #11
0
		public void TestColumnSetter()
		{
			Matrix m = new Matrix(3, 1);
			m.SetColumn(0, 1F, 2F, 3F);

			try
			{
				//too many.
				m.SetColumn(0, 1F, 2F, 3F, 4F);
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentException) {}
		}