Example #1
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 #2
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 #3
0
        private bool canBeThere(Matrix m, int row, int column, int digit)
        {
            int[] temp = m.GetRow(row);
            foreach (int i in temp)
                if (i == digit)
                    return false;
            temp = m.GetColumn(column);
            foreach (int i in temp)
                if (i == digit)
                    return false;
            temp = m.GetRegion(row, column);
            foreach (int i in temp)
                if (i == digit)
                    return false;

            return true;
        }
Example #4
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 #5
0
        internal static Vector Variance(Matrix value, double[] means)
        {
            Vector variance = new Vector(value.Columns);

            for (int i = 0; i < value.Columns; i++)
            {
                //TODO: Substitute this with the complete matrix variance algorithm
                variance[i] = Variance(value.GetColumn(i), means[i]);
            }

            return variance;
        }
Example #6
0
        internal static Matrix Covariance(Matrix matrix, double[] mean)
        {
            if (matrix.Rows == 1)
              {
                  throw new ArgumentException("Sample has only one observation.","matrix");
              }

              Matrix cov = new Matrix(matrix.Columns);

              for (int i = 0; i < cov.Columns; i++)
              {
                  Vector column = matrix.GetColumn(i);
                  cov[i, i] = Variance(column, mean[i]); // diagonal has the variances

                  for (int j = i + 1; j < cov.Columns; j++)
                  {
                      cov[i, j] = Covariance(column, matrix.GetColumn(j));
                      cov[j, i] = cov[i, j]; // Matrix is symmetric
                  }
              }
              return cov;
        }
Example #7
0
        /// <summary>Calculates the matrix Modes vector.</summary>
        /// <param name="m">A matrix whose modes will be calculated.</param>
        /// <returns>Returns a vector containing the modes of the given matrix.</returns>
        public static Vector Mode(Matrix matrix)
        {
            Vector mode = new Vector(matrix.Columns);

            for (int i = 0; i < mode.Length; i++)
            {
                mode[i] = Mode(matrix.GetColumn(i));
            }

            return mode;
        }
Example #8
0
        /// <summary>Calculates the matrix Medians vector.</summary>
        /// <param name="m">A matrix whose deviations will be calculated.</param>
        /// <returns>Returns a vector containing the medians of the given matrix.</returns>
        public static Vector Median(Matrix value)
        {
            Vector medians = new Vector(value.Columns);

            for (int i = 0; i < value.Columns; i++)
            {
                medians[i] = Median(value.GetColumn(i));
            }

            return medians;
        }