예제 #1
0
        public static AnyRectangularMatrix Repmat(AnyRectangularMatrix source, int verticalTiles, int horizontalTiles)
        {
            RectangularMatrix target = new RectangularMatrix(
                (int)Math.Floor(source.RowCount * (double)verticalTiles),
                (int)Math.Floor(source.ColumnCount * (double)horizontalTiles));

            int rows = source.RowCount;
            int cols = source.ColumnCount;

            for (int v = 0; v < verticalTiles; v++)
            {
                for (int h = 0; h < horizontalTiles; h++)
                {
                    for (int r = 0; r < rows; r++)
                    {
                        for (int c = 0; c < cols; c++)
                        {
                            target[(v * rows) + r, (h * cols) + c] = source[r, c];
                        }
                    }
                }
            }

            return(target);
        }
예제 #2
0
        public void RepmatTest()
        {
            AnyRectangularMatrix source = this.ThreeByThreeSquare;
            int verticalTiles           = 2;
            int horizontalTiles         = 3;

            AnyRectangularMatrix expected = new RectangularMatrix(new[, ]
            {
                { 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0 },
                { 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0 },
                { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
                { 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0 },
                { 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0 },
                { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }
            });

            AnyRectangularMatrix actual = Utilities.Repmat(source, verticalTiles, horizontalTiles);

            for (int r = 0; r < expected.RowCount; r++)
            {
                for (int c = 0; c < expected.ColumnCount; c++)
                {
                    Assert.AreEqual(expected[r, c], actual[r, c]);
                }
            }
        }
예제 #3
0
        public static bool IsNearlyEqual(AnyRectangularMatrix x, AnyRectangularMatrix y, EvaluationSettings s)
        {
            double m = x.FrobeniusNorm() + y.FrobeniusNorm();
            double e = s.AbsolutePrecision + m * s.RelativePrecision;
            AnyRectangularMatrix D = x - y;

            return(D.FrobeniusNorm() <= e);
        }
예제 #4
0
 public static void PrintMatrix(AnyRectangularMatrix M)
 {
     for (int r = 0; r < M.RowCount; r++) {
         for (int c = 0; c < M.ColumnCount; c++) {
             Console.Write("{0,12:g8} ", M[r, c]);
         }
         Console.WriteLine();
     }
     Console.WriteLine("--");
 }
예제 #5
0
 private void WriteMatrix(AnyRectangularMatrix A)
 {
     Console.WriteLine("--");
     for (int r = 0; r < A.RowCount; r++)
     {
         for (int c = 0; c < A.ColumnCount; c++)
         {
             Console.Write("{0}  ", A[r, c]);
         }
         Console.WriteLine();
     }
 }
예제 #6
0
 public static void PrintMatrix(string name, AnyRectangularMatrix M)
 {
     Console.WriteLine($"{name}=");
     for (int r = 0; r < M.RowCount; r++)
     {
         for (int c = 0; c < M.ColumnCount; c++)
         {
             Console.Write("{0,10:g4}", M[r, c]);
         }
         Console.WriteLine();
     }
 }
예제 #7
0
        public void AnyRectangularMatrixOperations()
        {
            AnyRectangularMatrix M1 = R;
            AnyRectangularMatrix M2 = -R;

            RectangularMatrix MS = M1 + M2;

            Assert.IsTrue(MS == 0.0 * M1);
            RectangularMatrix MD = M1 - M2;

            Assert.IsTrue(MD == 2.0 * M1);
        }
예제 #8
0
 public static void PrintMatrix(AnyRectangularMatrix M)
 {
     for (int r = 0; r < M.RowCount; r++)
     {
         for (int c = 0; c < M.ColumnCount; c++)
         {
             Console.Write("{0,12:g8} ", M[r, c]);
         }
         Console.WriteLine();
     }
     Console.WriteLine("--");
 }
예제 #9
0
        /*
         * public static bool IsNearlyEigenvalue (ISquareMatrix M, ColumnVector v, double c) {
         *  double n = MatrixNorm(M);
         *  double ep = e;
         *  if (Math.Abs(n / c) < 0.1) ep = e / n;
         *
         *
         * }
         */

        private static double MatrixNorm(AnyRectangularMatrix M)
        {
            double n = 0.0;

            for (int r = 0; r < M.RowCount; r++)
            {
                for (int c = 0; c < M.ColumnCount; c++)
                {
                    n += Math.Abs(M[r, c]);
                }
            }
            return(n);
        }
예제 #10
0
        public static bool IsNearlyEqual(AnyRectangularMatrix A, AnyRectangularMatrix B, double e)
        {
            double nA = MatrixNorm(A);
            double nB = MatrixNorm(B);

            for (int r = 0; r < A.RowCount; r++)
            {
                for (int c = 0; c < A.ColumnCount; c++)
                {
                    if (Math.Abs(A[r, c] - B[r, c]) > e * (nA + nB))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
예제 #11
0
        public void GetColumnsTest()
        {
            AnyRectangularMatrix matrix        = this.ThreeByThreeSquare;
            IEnumerable <int>    columnIndices = new[] { 0, 2 };
            AnyRectangularMatrix expected      = new RectangularMatrix(new[, ] {
                { 0.0, 2.0 }, { 3.0, 5.0 }, { 0.0, 0.0 }
            });

            AnyRectangularMatrix actual = Utilities.GetColumns(matrix, columnIndices);

            for (int r = 0; r < expected.RowCount; r++)
            {
                for (int c = 0; c < expected.ColumnCount; c++)
                {
                    Assert.AreEqual(expected, actual);
                }
            }
        }
예제 #12
0
        public static AnyRectangularMatrix GetColumns(AnyRectangularMatrix matrix, IEnumerable <int> columnIndices)
        {
            RectangularMatrix target = new RectangularMatrix(matrix.RowCount, columnIndices.Count());

            int columnIndex = 0;

            foreach (int col in columnIndices)
            {
                for (int r = 0; r < matrix.RowCount; r++)
                {
                    target[r, columnIndex] = matrix[r, col];
                }

                columnIndex++;
            }

            return(target);
        }
예제 #13
0
        public static SquareMatrix ConvertToSquare(AnyRectangularMatrix matrix, int dimension)
        {
            if (matrix.RowCount != matrix.ColumnCount)
            {
                return(null);
            }

            SquareMatrix square = new SquareMatrix(dimension);

            for (int r = 0; r < dimension; r++)
            {
                for (int c = 0; c < dimension; c++)
                {
                    if ((r < matrix.RowCount) && (c < matrix.ColumnCount))
                    {
                        square[r, c] = matrix[r, c];
                    }
                }
            }

            return(square);
        }
예제 #14
0
 public static bool IsNearlyEqual(AnyRectangularMatrix x, AnyRectangularMatrix y, EvaluationSettings s)
 {
     double m = x.FrobeniusNorm() + y.FrobeniusNorm();
     double e = s.AbsolutePrecision + m * s.RelativePrecision;
     AnyRectangularMatrix D = x - y;
     return (D.FrobeniusNorm() <= e);
 }
예제 #15
0
 public static bool IsNearlyEqual(AnyRectangularMatrix A, AnyRectangularMatrix B)
 {
     return(IsNearlyEqual(A, B, TargetPrecision));
 }
예제 #16
0
 private static void WriteMatrix(AnyRectangularMatrix A)
 {
     for (int r = 0; r < A.RowCount; r++) {
         for (int c = 0; c < A.ColumnCount; c++) {
             Console.Write("{0} ", A[r, c]);
         }
         Console.WriteLine();
     }
 }
예제 #17
0
 public static bool IsNearlyEqual(AnyRectangularMatrix A, AnyRectangularMatrix B, double e)
 {
     double nA = MatrixNorm(A);
     double nB = MatrixNorm(B);
     for (int r = 0; r < A.RowCount; r++) {
         for (int c = 0; c < A.ColumnCount; c++) {
             if (Math.Abs(A[r, c] - B[r, c]) > e * (nA + nB)) return (false);
         }
     }
     return (true);
 }
예제 #18
0
 public static bool IsNearlyEqual(AnyRectangularMatrix A, AnyRectangularMatrix B)
 {
     return (IsNearlyEqual(A, B, TargetPrecision));
 }
예제 #19
0
        /*
        public static bool IsNearlyEigenvalue (ISquareMatrix M, ColumnVector v, double c) {
            double n = MatrixNorm(M);
            double ep = e;
            if (Math.Abs(n / c) < 0.1) ep = e / n;

        }
        */
        private static double MatrixNorm(AnyRectangularMatrix M)
        {
            double n = 0.0;
            for (int r = 0; r < M.RowCount; r++) {
                for (int c = 0; c < M.ColumnCount; c++) {
                    n += Math.Abs(M[r, c]);
                }
            }
            return (n);
        }