Пример #1
0
        public virtual void set(Matrix original)
        {
            DMatrix m = (DMatrix)original;

            if (m.getNumCols() == 1 && m.getNumRows() == 5)
            {
                a1 = m.get(0, 0);
                a2 = m.get(1, 0);
                a3 = m.get(2, 0);
                a4 = m.get(3, 0);
                a5 = m.get(4, 0);
            }
            else if (m.getNumRows() == 1 && m.getNumCols() == 5)
            {
                a1 = m.get(0, 0);
                a2 = m.get(0, 1);
                a3 = m.get(0, 2);
                a4 = m.get(0, 3);
                a5 = m.get(0, 4);
            }
            else
            {
                throw new ArgumentException("Incompatible shape");
            }
        }
Пример #2
0
 /**
  * Checks to see if every element in A is countable.  A doesn't have any element with
  * a value of NaN or infinite.
  *
  * @param A Matrix
  */
 public static void assertCountable(DMatrix A)
 {
     for (int i = 0; i < A.getNumRows(); i++)
     {
         for (int j = 0; j < A.getNumCols(); j++)
         {
             assertTrue(!double.IsNaN(A.get(i, j)), "NaN found at " + i + " " + j);
             assertTrue(!double.IsInfinity(A.get(i, j)), "Infinite found at " + i + " " + j);
         }
     }
 }
Пример #3
0
        public virtual void set(Matrix original)
        {
            if (original.getNumCols() != 2 || original.getNumRows() != 2)
            {
                throw new ArgumentException("Rows and/or columns do not match");
            }
            DMatrix m = (DMatrix)original;

            a11 = m.get(0, 0);
            a12 = m.get(0, 1);
            a21 = m.get(1, 0);
            a22 = m.get(1, 1);
        }
Пример #4
0
        /**
         * Assert equals with a relative error
         */
        public static void assertRelativeEquals(DMatrix A, DMatrix B, double tol)
        {
            assertShape(A, B);

            for (int i = 0; i < A.getNumRows(); i++)
            {
                for (int j = 0; j < A.getNumCols(); j++)
                {
                    double valA = A.get(i, j);
                    double valB = B.get(i, j);

                    if ((double.IsNaN(valA) != double.IsNaN(valB)) ||
                        (double.IsInfinity(valA) != double.IsInfinity(valB)))
                    {
                        throw new AssertFailedException("At (" + i + "," + j + ") A = " + valA + " B = " + valB);
                    }
                    double max   = Math.Max(Math.Abs(valA), Math.Abs(valB));
                    double error = Math.Abs(valA - valB) / max;
                    if (error > tol)
                    {
                        Console.WriteLine("------------  A  -----------");
                        A.print();
                        Console.WriteLine("\n------------  B  -----------");
                        B.print();
                        throw new AssertFailedException("At (" + i + "," + j + ") A = " + valA + " B = " + valB +
                                                        "   error = " + error);
                    }
                }
            }
        }
Пример #5
0
        /**
         * <p>
         * Checks to see if each element in the matrix is within tolerance of each other:
         * </p>
         *
         * <p>
         * The two matrices are identical with in tolerance if:<br>
         * |a<sub>ij</sub> - b<sub>ij</sub>| &le; tol
         * </p>
         *
         * <p>
         * In addition if an element is NaN or infinite in one matrix it must be the same in the other.
         * </p>
         *
         * @param A Matrix A
         * @param B Matrix B
         * @param tol Tolerance
         */
        public static void assertEqualsUncountable(DMatrix A, DMatrix B, double tol)
        {
            assertShape(A, B);

            for (int i = 0; i < A.getNumRows(); i++)
            {
                for (int j = 0; j < A.getNumCols(); j++)
                {
                    double valA = A.get(i, j);
                    double valB = B.get(i, j);

                    if (double.IsNaN(valA))
                    {
                        assertTrue(double.IsNaN(valB), "At (" + i + "," + j + ") A = " + valA + " B = " + valB);
                    }
                    else if (double.IsInfinity(valA))
                    {
                        assertTrue(double.IsInfinity(valB), "At (" + i + "," + j + ") A = " + valA + " B = " + valB);
                    }
                    else
                    {
                        double diff = Math.Abs(valA - valB);
                        assertTrue(diff <= tol, "At (" + i + "," + j + ") A = " + valA + " B = " + valB);
                    }
                }
            }
        }
Пример #6
0
 /// <summary>
 /// Creates a new DMatrixRMaj which contains the same information as the provided Matrix64F.
 /// </summary>
 /// <param name="mat"> Matrix whose values will be copied.  Not modified. </param>
 //JAVA TO C# CONVERTER WARNING: The following constructor is declared outside of its associated class:
 //ORIGINAL LINE: public DMatrixRMaj(DMatrix mat)
 public DMatrixRMaj(DMatrix mat) : this(mat.NumRows, mat.NumCols)
 {
     for (int i = 0; i < numRows; i++)
     {
         for (int j = 0; j < numCols; j++)
         {
             set(i, j, mat.get(i, j));
         }
     }
 }
Пример #7
0
        public static bool isEquivalentTriangle(bool upper, DMatrix a, DMatrix b, double tol)
        {
            if (a.getNumRows() != b.getNumRows() || a.getNumCols() != b.getNumCols())
            {
                return(false);
            }

            if (upper)
            {
                for (int i = 0; i < a.getNumRows(); i++)
                {
                    for (int j = i; j < a.getNumCols(); j++)
                    {
                        double diff = Math.Abs(a.get(i, j) - b.get(i, j));

                        if (diff > tol)
                        {
                            return(false);
                        }
                    }
                }
            }
            else
            {
                for (int j = 0; j < a.getNumCols(); j++)
                {
                    for (int i = j; i < a.getNumRows(); i++)
                    {
                        double diff = Math.Abs(a.get(i, j) - b.get(i, j));

                        if (diff > tol)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Пример #8
0
        public static void copy(DMatrix from, DMatrix to)
        {
            int numCols = from.getNumCols();
            int numRows = from.getNumRows();

            for (int i = 0; i < numRows; i++)
            {
                for (int j = 0; j < numCols; j++)
                {
                    to.set(i, j, from.get(i, j));
                }
            }
        }
        /**
         * <p>
         * Checks to see if each element in the upper or lower triangular portion of the two matrices are within tolerance of
         * each other: tol &ge; |a<sub>ij</sub> - b<sub>ij</sub>|.
         * <p>
         *
         * <p>
         * NOTE: If any of the elements are not countable then false is returned.<br>
         * NOTE: If a tolerance of zero is passed in this is equivalent to calling
         * {@link #isEquals(DMatrixD1, DMatrixD1)}
         * </p>
         *
         * @param a A matrix. Not modified.
         * @param b A matrix. Not modified.
         * @param upper true of upper triangular and false for lower.
         * @param tol How close to being identical each element needs to be.
         * @return true if equals and false otherwise.
         */
        public static bool isEqualsTriangle(DMatrix a, DMatrix b, bool upper, double tol)
        {
            if (a.NumRows != b.NumRows || a.NumCols != b.NumCols)
            {
                return(false);
            }

            if (upper)
            {
                for (int i = 0; i < a.NumRows; i++)
                {
                    for (int j = i; j < a.NumCols; j++)
                    {
                        if (Math.Abs(a.get(i, j) - b.get(i, j)) > tol)
                        {
                            return(false);
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < a.NumRows; i++)
                {
                    int end = Math.Min(i, a.NumCols - 1);

                    for (int j = 0; j <= end; j++)
                    {
                        if (Math.Abs(a.get(i, j) - b.get(i, j)) > tol)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Пример #10
0
 /**
  * Returns true if the provided matrix is has a value of 1 along the diagonal
  * elements and zero along all the other elements.
  *
  * @param a Matrix being inspected.
  * @param tol How close to zero or one each element needs to be.
  * @return If it is within tolerance to an identity matrix.
  */
 public static bool isIdentity(DMatrix a, double tol)
 {
     for (int i = 0; i < a.getNumRows(); i++)
     {
         for (int j = 0; j < a.getNumCols(); j++)
         {
             if (i == j)
             {
                 if (Math.Abs(a.get(i, j) - 1.0) > tol)
                 {
                     return(false);
                 }
             }
             else
             {
                 if (Math.Abs(a.get(i, j)) > tol)
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
 public static void extract(DMatrix src,
                            int srcY0, int srcX0,
                            DMatrix dst,
                            int dstY0, int dstX0,
                            int numRows, int numCols)
 {
     for (int y = 0; y < numRows; y++)
     {
         for (int x = 0; x < numCols; x++)
         {
             double v = src.get(y + srcY0, x + srcX0);
             dst.set(dstY0 + y, dstX0 + x, v);
         }
     }
 }
Пример #12
0
        public static void print(Stream output, DMatrix mat, string format,
                                 int row0, int row1, int col0, int col1)
        {
            Console.WriteLine("Type = submatrix , rows " + row0 + " to " + row1 + "  columns " + col0 + " to " + col1);

            format += " ";

            for (int y = row0; y < row1; y++)
            {
                for (int x = col0; x < col1; x++)
                {
                    Console.Write(format, mat.get(y, x));
                }
                Console.WriteLine();
            }
        }
Пример #13
0
        public static void print(Stream output, DMatrix mat, string format)
        {
            string type = mat is ReshapeMatrix ? "dense64" : "dense64 fixed";

            Console.WriteLine("Type = " + type + " real , numRows = " + mat.getNumRows() + " , numCols = " +
                              mat.getNumCols());

            format += " ";

            for (int y = 0; y < mat.getNumRows(); y++)
            {
                for (int x = 0; x < mat.getNumCols(); x++)
                {
                    Console.Write(format, mat.get(y, x));
                }
                Console.WriteLine();
            }
        }
Пример #14
0
        /**
         * Saves a matrix to disk using in a Column Space Value (CSV) format. For a
         * description of the format see {@link MatrixIO#loadCSV(String)}.
         *
         * @param A The matrix being saved.
         * @param fileName Name of the file its being saved at.
         * @throws java.io.IOException
         */
        public static void saveCSV(DMatrix A, string fileName)
        {
            var sb = new StringBuilder();

            sb = sb.AppendLine(A.getNumRows() + " " + A.getNumCols() + " real");
            for (int i = 0; i < A.getNumRows(); i++)
            {
                for (int j = 0; j < A.getNumCols(); j++)
                {
                    sb = sb.Append(A.get(i, j) + " ");
                }
                sb = sb.AppendLine();
            }
            using (var stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write(sb.ToString());
                }
        }
Пример #15
0
        public override void set(Matrix original)
        {
            if (original is DMatrixRBlock)
            {
                set((DMatrixRBlock)original);
            }
            else
            {
                DMatrix m = (DMatrix)original;

                for (int i = 0; i < numRows; i++)
                {
                    for (int j = 0; j < numCols; j++)
                    {
                        set(i, j, m.get(i, j));
                    }
                }
            }
        }
Пример #16
0
        /**
         * <p>
         * Checks to see if the transpose of B is equal to A and countable:
         * </p>
         *
         * <p>
         * |a<sub>ij</sub> - b<sub>ji</sub>| &le; tol
         * </p>
         *
         * <p>
         * The test will fail if any element in either matrix is NaN or infinite.
         * </p>
         *
         * @param A Matrix A
         * @param B Matrix B
         * @param tol Tolerance
         */
        public static void assertEqualsTrans(DMatrix A, DMatrix B, double tol)
        {
            assertShape(A, B.getNumCols(), B.getNumRows());

            for (int i = 0; i < A.getNumRows(); i++)
            {
                for (int j = 0; j < A.getNumCols(); j++)
                {
                    double valA = A.get(i, j);
                    double valB = B.get(j, i);

                    assertTrue(!double.IsNaN(valA) && !double.IsNaN(valB),
                               "A(" + i + "," + j + ") = " + valA + ") B(" + j + "," + i + ") = " + valB);
                    assertTrue(!double.IsInfinity(valA) && !double.IsInfinity(valB),
                               "A(" + i + "," + j + ") = " + valA + ") B(" + j + "," + i + ") = " + valB);
                    assertTrue(Math.Abs(valA - valB) <= tol,
                               "A(" + i + "," + j + ") = " + valA + ") B(" + j + "," + i + ") = " + valB);
                }
            }
        }
Пример #17
0
        public override void set(Matrix original)
        {
            DMatrix m = (DMatrix)original;

            reshape(original.getNumRows(), original.getNumCols());

            if (original is DMatrixRMaj)
            {
                // do a faster copy if its of type DMatrixRMaj
                Array.Copy(((DMatrixRMaj)m).data, 0, data, 0, numRows * numCols);
            }
            else
            {
                int index = 0;
                for (int i = 0; i < numRows; i++)
                {
                    for (int j = 0; j < numCols; j++)
                    {
                        data[index++] = m.get(i, j);
                    }
                }
            }
        }
Пример #18
0
        public virtual void set(Matrix original)
        {
            if (original.getNumCols() != 6 || original.getNumRows() != 6)
            {
                throw new ArgumentException("Rows and/or columns do not match");
            }
            DMatrix m = (DMatrix)original;

            a11 = m.get(0, 0);
            a12 = m.get(0, 1);
            a13 = m.get(0, 2);
            a14 = m.get(0, 3);
            a15 = m.get(0, 4);
            a16 = m.get(0, 5);
            a21 = m.get(1, 0);
            a22 = m.get(1, 1);
            a23 = m.get(1, 2);
            a24 = m.get(1, 3);
            a25 = m.get(1, 4);
            a26 = m.get(1, 5);
            a31 = m.get(2, 0);
            a32 = m.get(2, 1);
            a33 = m.get(2, 2);
            a34 = m.get(2, 3);
            a35 = m.get(2, 4);
            a36 = m.get(2, 5);
            a41 = m.get(3, 0);
            a42 = m.get(3, 1);
            a43 = m.get(3, 2);
            a44 = m.get(3, 3);
            a45 = m.get(3, 4);
            a46 = m.get(3, 5);
            a51 = m.get(4, 0);
            a52 = m.get(4, 1);
            a53 = m.get(4, 2);
            a54 = m.get(4, 3);
            a55 = m.get(4, 4);
            a56 = m.get(4, 5);
            a61 = m.get(5, 0);
            a62 = m.get(5, 1);
            a63 = m.get(5, 2);
            a64 = m.get(5, 3);
            a65 = m.get(5, 4);
            a66 = m.get(5, 5);
        }
Пример #19
0
        public virtual void set(Matrix original)
        {
            if (original.getNumCols() != 4 || original.getNumRows() != 4)
            {
                throw new ArgumentException("Rows and/or columns do not match");
            }
            DMatrix m = (DMatrix)original;

            a11 = m.get(0, 0);
            a12 = m.get(0, 1);
            a13 = m.get(0, 2);
            a14 = m.get(0, 3);
            a21 = m.get(1, 0);
            a22 = m.get(1, 1);
            a23 = m.get(1, 2);
            a24 = m.get(1, 3);
            a31 = m.get(2, 0);
            a32 = m.get(2, 1);
            a33 = m.get(2, 2);
            a34 = m.get(2, 3);
            a41 = m.get(3, 0);
            a42 = m.get(3, 1);
            a43 = m.get(3, 2);
            a44 = m.get(3, 3);
        }