Esempio n. 1
0
        /// <summary>Indicates whether or not two matricies contain same values
        /// in an element-wise comparison. Does not verify that the matricies point
        /// to the same underlying data-set or element instances</summary>
        /// <param name="matrix1">Matrix to compare</param>
        /// <param name="matrix2">Matrix to compare</param>
        /// <returns>true, if the matricies are equal on an element-wise basis; otherwise, false</returns>
        public static Boolean Equality(
            MatrixBase <T> matrix1,
            MatrixBase <T> matrix2)
        {
            if ((Object)matrix1 == null || (Object)matrix2 == null)
            {
                return(false);
            }
            if (matrix1.ColumnCount != matrix2.ColumnCount)
            {
                return(false);
            }
            if (matrix1.RowCount != matrix2.RowCount)
            {
                return(false);
            }

            for (Int32 i = 0; i < matrix1.ColumnCount; i++)
            {
                for (Int32 j = 0; j < matrix1.RowCount; j++)
                {
                    if (!matrix1[i, j].Equals(matrix2[i, j]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="view"></param>
        /// <param name="columnRange"></param>
        /// <param name="rowRange"></param>
        /// <returns></returns>
        public static MatrixBase <T> SubMatrix(
            MatrixBase <T> view,
            Int32Range columnRange,
            Int32Range rowRange)
        {
            if (!view.DataColumnRange.Contains(columnRange))
            {
                throw new ArgumentOutOfRangeException("columnRange");
            }
            if (!view.DataRowRange.Contains(rowRange))
            {
                throw new ArgumentOutOfRangeException("rowRange");
            }

            MatrixBase <T> result
                = new MatrixBase <T>(view.DataSource,
                                     new Int32Range(
                                         columnRange.Start + view.DataColumnRange.Start,
                                         columnRange.End + view.DataColumnRange.Start),
                                     new Int32Range(
                                         rowRange.Start + view.DataRowRange.Start,
                                         rowRange.End + view.DataRowRange.Start));

            return(result);
        }
Esempio n. 3
0
 /// <summary>Copies the values of the matrix object into a
 /// larger matrix at the specified offset</summary>
 /// <param name="matrix">Target matrix</param>
 /// <param name="targetColumnOffset">Target matrix column offset</param>
 /// <param name="targetRowOffset">Target matrix row offset</param>
 public virtual void CopyInto(
     MatrixBase <T> targetMatrix,
     Int32 targetColumnOffset,
     Int32 targetRowOffset)
 {
     MatrixBase <T> .ElementWiseCopy(this, targetMatrix, targetColumnOffset, targetRowOffset);
 }
Esempio n. 4
0
 /// <summary>Copies the values of the specified source matrix
 /// into this (larger) matrix at the specified offset</summary>
 /// <param name="sourceMatrix">Source matrix</param>
 /// <param name="targetColumnOffset">Target column offset in this matrix</param>
 /// <param name="targetRowOffset">Target row offset in this matrix</param>
 public virtual void CopyFrom(
     MatrixBase <T> sourceMatrix,
     Int32 targetColumnOffset,
     Int32 targetRowOffset)
 {
     MatrixBase <T> .ElementWiseCopy(sourceMatrix, this, targetColumnOffset, targetRowOffset);
 }
Esempio n. 5
0
        /// <summary>Performs an element-wise binary operation (involving two matricies),
        /// returning the result in a new matrix instance</summary>
        /// <param name="matrix1">First matrix</param>
        /// <param name="matrix2">Second matrix</param>
        /// <param name="operation">Binary operation delegate</param>
        /// <returns>Matrix instance containing the result of the binary operation</returns>
        public static MatrixBase <T> ElementWiseOperation(
            MatrixBase <T> matrix1,
            MatrixBase <T> matrix2,
            ElementBinaryOperationDelegate operation)
        {
            if (MatrixBase <T> .IsNull(matrix1))
            {
                throw new ArgumentNullException("matrix1");
            }
            if (MatrixBase <T> .IsNull(matrix2))
            {
                throw new ArgumentNullException("matrix2");
            }
            if (matrix1.ColumnCount != matrix2.ColumnCount)
            {
                throw new DimensionMismatchException("The number of columns in matrix1 does not equal the number of columns in matrix2");
            }
            if (matrix1.RowCount != matrix2.RowCount)
            {
                throw new DimensionMismatchException("The number of rows in matrix1 does not equal the number of rows in matrix2");
            }

            MatrixBase <T> result = new MatrixBase <T>(matrix1.ColumnCount, matrix1.RowCount);

            for (Int32 i = 0; i < result.ColumnCount; i++)
            {
                for (Int32 j = 0; j < result.RowCount; j++)
                {
                    result[i, j] = operation(matrix1[i, j], matrix2[i, j]);
                }
            }
            return(result);
        }
Esempio n. 6
0
 /// <summary>Indicates whether or not two matricies contain same values
 /// in an element-wise comparison. Does not verify that the matricies point
 /// to the same underlying data-set or element instances</summary>
 /// <param name="matrix1">Matrix to compare</param>
 /// <param name="matrix2">Matrix to compare</param>
 /// <returns>true, if the matricies are equal on an element-wise basis; otherwise, false</returns>
 public static Boolean Equality(DoubleMatrix matrix1, DoubleMatrix matrix2)
 {
     if ((object)matrix1 == null || (object)matrix2 == null)
     {
         return(false);
     }
     return(MatrixBase <Double> .Equality(matrix1.InnerMatrix, matrix2.InnerMatrix));
 }
Esempio n. 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="matricies"></param>
        /// <returns></returns>
        public static DoubleMatrix JoinVertical(IEnumerable <DoubleMatrix> matricies)
        {
            List <MatrixBase <Double> > innerMatricies = new List <MatrixBase <Double> >();

            foreach (DoubleMatrix matrix in matricies)
            {
                innerMatricies.Add(matrix.InnerMatrix);
            }
            return(new DoubleMatrix(MatrixBase <Double> .JoinVertical(innerMatricies)));
        }
Esempio n. 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="matrix"></param>
        /// <param name="row1"></param>
        /// <param name="row2"></param>
        public static void SwapRows(
            MatrixBase <T> matrix,
            Int32 row1,
            Int32 row2)
        {
            MatrixBase <T> rowTemp = matrix.Rows[row1].Clone();

            matrix.Rows[row1] = matrix.Rows[row2];
            matrix.Rows[row2] = rowTemp;
        }
Esempio n. 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="matrix"></param>
        /// <param name="row1"></param>
        /// <param name="row2"></param>
        public static void SwapColumns(
            MatrixBase <T> matrix,
            Int32 column1,
            Int32 column2)
        {
            MatrixBase <T> columnTemp = matrix.Columns[column1].Clone();

            matrix.Columns[column1] = matrix.Columns[column2];
            matrix.Columns[column2] = columnTemp;
        }
Esempio n. 10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="matrix1"></param>
 /// <param name="matrix2"></param>
 /// <returns></returns>
 protected static DoubleMatrix Subtraction(
     DoubleMatrix matrix1,
     DoubleMatrix matrix2)
 {
     return(new DoubleMatrix(
                MatrixBase <Double> .ElementWiseOperation(
                    matrix1.InnerMatrix,
                    matrix2.InnerMatrix,
                    delegate(double element1, double element2)
                    { return element1 - element2; })));
 }
Esempio n. 11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="matrix"></param>
 /// <param name="scalar"></param>
 /// <returns></returns>
 protected static DoubleMatrix Multiplication(
     DoubleMatrix matrix,
     double scalar)
 {
     return(new DoubleMatrix(
                MatrixBase <Double> .ElementWiseOperation(
                    matrix.InnerMatrix,
                    scalar,
                    delegate(double element1, double element2)
                    { return element1 * element2; })));
 }
Esempio n. 12
0
 /// <summary>CopyFrom</summary>
 /// <param name="sourceMatrix"></param>
 /// <param name="targetColumnOffset"></param>
 /// <param name="targetRowOffset"></param>
 public void CopyFrom(
     DoubleMatrix sourceMatrix,
     Int32 targetColumnOffset,
     Int32 targetRowOffset)
 {
     MatrixBase <Double> .ElementWiseCopy(
         sourceMatrix.InnerMatrix,
         this.InnerMatrix,
         targetColumnOffset,
         targetRowOffset);
 }
Esempio n. 13
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="matrix"></param>
 /// <param name="scalar"></param>
 /// <returns></returns>
 protected static DoubleMatrix Addition(
     DoubleMatrix matrix,
     Double scalar)
 {
     return(new DoubleMatrix(
                MatrixBase <Double> .ElementWiseOperation(
                    matrix.InnerMatrix,
                    scalar,
                    delegate(Double element1, Double element2)
                    { return element1 + element2; })));
 }
Esempio n. 14
0
 /// <summary>CopyInto</summary>
 /// <param name="targetMatrix"></param>
 /// <param name="targetColumnOffset"></param>
 /// <param name="targetRowOffset"></param>
 public void CopyInto(
     DoubleMatrix targetMatrix,
     Int32 targetColumnOffset,
     Int32 targetRowOffset)
 {
     MatrixBase <Double> .ElementWiseCopy(
         this.InnerMatrix,
         targetMatrix.InnerMatrix,
         targetColumnOffset,
         targetRowOffset);
 }
Esempio n. 15
0
        /*************************/
        /* PUBLIC STATIC METHODS */
        /*************************/

        /// <summary>Generates a new default matrix</summary>
        /// <param name="columns">Width of matrix</param>
        /// <param name="rows">Height of matrix</param>
        /// <returns>Matrix of specified size with all element values set to 0</returns>
        public static MatrixBase <T> Default(Int32 columns, Int32 rows)
        {
            MatrixBase <T> defaultMatrix = new MatrixBase <T>(columns, rows);

            for (Int32 i = 0; i < columns; i++)
            {
                for (Int32 j = 0; j < rows; j++)
                {
                    defaultMatrix[i, j] = default(T);
                }
            }
            return(defaultMatrix);
        }
Esempio n. 16
0
 /// <summary>Performs element-wise copy of the source matrix into
 /// the target matrix at the specified offset</summary>
 /// <param name="sourceMatrix">Source matrix</param>
 /// <param name="targetMatrix">Target matrix</param>
 /// <param name="targetColumnOffset">Target column offset</param>
 /// <param name="targetRowOffset">Target row offset</param>
 public static void ElementWiseCopy(
     MatrixBase <T> source,
     MatrixBase <T> target,
     Int32 targetColumnOffset,
     Int32 targetRowOffset)
 {
     for (Int32 i = 0; i < source.ColumnCount; i++)
     {
         for (Int32 j = 0; j < source.RowCount; j++)
         {
             target[i + targetColumnOffset, j + targetRowOffset] = source[i, j];
         }
     }
 }
Esempio n. 17
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="matrix1"></param>
 /// <param name="matrix2"></param>
 /// <returns></returns>
 public static Boolean DimensionsMatch(
     MatrixBase <T> matrix1,
     MatrixBase <T> matrix2)
 {
     if (matrix1 == null)
     {
         throw new ArgumentNullException("matrix1");
     }
     if (matrix2 == null)
     {
         throw new ArgumentNullException("matrix2");
     }
     return(matrix1.ColumnCount == matrix2.ColumnCount &&
            matrix1.RowCount == matrix2.RowCount);
 }
Esempio n. 18
0
        /// <summary>Transposes a matrix (reflecting it across the diagonalOffset axis)
        /// swapping all elements (i, i) with elements (i, i).</summary>
        /// <param name="matrix">Matrix to be transposed</param>
        /// <returns>Returns a transposed copy of the input matrix</returns>
        public static MatrixBase <T> Transpose(MatrixBase <T> matrix)
        {
            MatrixBase <T> result
                = new MatrixBase <T>(matrix.RowCount, matrix.ColumnCount);

            for (Int32 i = 0; i < matrix.ColumnCount; i++)
            {
                for (Int32 j = 0; j < matrix.RowCount; j++)
                {
                    result[j, i] = matrix[i, j];
                }
            }

            return(result);
        }
Esempio n. 19
0
            /********************/
            /* PUBLIC ACCESSORS */
            /********************/

            /// <summary>this</summary>
            /// <param name="columnIndex"></param>
            /// <returns></returns>
            public MatrixBase <T> this[Int32 columnIndex]
            {
                get
                {
                    return(this.AsEnumerable().ElementAt(columnIndex));
                }
                set
                {
                    if (value.RowCount != _dataView.RowCount || value.ColumnCount != 1)
                    {
                        throw new DimensionMismatchException();
                    }

                    MatrixBase <T> targetColumn = this.AsEnumerable().ElementAt(columnIndex);
                    for (int j = 0; j < value.RowCount; j++)
                    {
                        targetColumn[0, j] = value[0, j];
                    }
                }
            }
Esempio n. 20
0
        /// <summary>Performs an element-wise unary operation involving a single matrix,
        /// returning the result in a new matrix instance</summary>
        /// <param name="matrix">Matrix</param>
        /// <param name="operation">Unary operation delegate</param>
        /// <returns>Matrix instance containing the result of the unary operation</returns>
        public static MatrixBase <T> ElementWiseOperation(
            MatrixBase <T> matrix,
            ElementPositionalUnaryOperationDelegate operation)
        {
            if (MatrixBase <T> .IsNull(matrix))
            {
                throw new ArgumentNullException("matrix");
            }

            MatrixBase <T> result = new MatrixBase <T>(matrix.ColumnCount, matrix.RowCount);

            for (Int32 i = 0; i < result.ColumnCount; i++)
            {
                for (Int32 j = 0; j < result.RowCount; j++)
                {
                    result[i, j] = operation(matrix[i, j], i, j);
                }
            }
            return(result);
        }
Esempio n. 21
0
            /********************/
            /* PUBLIC ACCESSORS */
            /********************/

            public MatrixBase <T> this[Int32 rowIndex]
            {
                get
                {
                    return(this.AsEnumerable().ElementAt(rowIndex));
                }
                set
                {
                    if (value.ColumnCount != _dataView.ColumnCount || value.RowCount != 1)
                    {
                        throw new DimensionMismatchException();
                    }

                    MatrixBase <T> targetRow = this.AsEnumerable().ElementAt(rowIndex);
                    for (int i = 0; i < value.ColumnCount; i++)
                    {
                        targetRow[i, 0] = value[i, 0];
                    }
                }
            }
Esempio n. 22
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="matricies"></param>
        /// <returns></returns>
        public static MatrixBase <T> JoinVertical(
            IEnumerable <MatrixBase <T> > matricies)
        {
            int?columnCount   = null;
            int totalRowCount = 0;

            foreach (MatrixBase <T> matrix in matricies)
            {
                if (MatrixBase <T> .IsNull(matrix))
                {
                    throw new ArgumentNullException();
                }
                if (columnCount == null)
                {
                    columnCount = matrix.ColumnCount;
                }
                else if ((int)columnCount != matrix.ColumnCount)
                {
                    throw new DimensionMismatchException();
                }
                totalRowCount += matrix.RowCount;
            }

            MatrixBase <T> resultMatrix = new MatrixBase <T>((int)columnCount, totalRowCount);

            int rowOffset = 0;

            foreach (MatrixBase <T> matrix in matricies)
            {
                MatrixBase <T> .ElementWiseCopy(matrix, resultMatrix, 0, rowOffset);

                rowOffset += matrix.RowCount;
            }

            return(resultMatrix);
        }
Esempio n. 23
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="matricies"></param>
        /// <returns></returns>
        public static MatrixBase <T> JoinHorizontal(
            IEnumerable <MatrixBase <T> > matricies)
        {
            int?rowCount         = null;
            int totalColumnCount = 0;

            foreach (MatrixBase <T> matrix in matricies)
            {
                if (MatrixBase <T> .IsNull(matrix))
                {
                    throw new ArgumentNullException();
                }
                if (rowCount == null)
                {
                    rowCount = matrix.RowCount;
                }
                else if ((int)rowCount != matrix.RowCount)
                {
                    throw new DimensionMismatchException();
                }
                totalColumnCount += matrix.ColumnCount;
            }

            MatrixBase <T> resultMatrix = new MatrixBase <T>(totalColumnCount, (int)rowCount);

            int columnOffset = 0;

            foreach (MatrixBase <T> matrix in matricies)
            {
                MatrixBase <T> .ElementWiseCopy(matrix, resultMatrix, columnOffset, 0);

                columnOffset += matrix.ColumnCount;
            }

            return(resultMatrix);
        }
Esempio n. 24
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="columnRange"></param>
 /// <param name="rowRange"></param>
 /// <returns></returns>
 public DoubleMatrix SubMatrix(
     Int32Range columnRange,
     Int32Range rowRange)
 {
     return(new DoubleMatrix(MatrixBase <Double> .SubMatrix(this.InnerMatrix, columnRange, rowRange)));
 }
Esempio n. 25
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="columns"></param>
 /// <param name="rows"></param>
 public DoubleMatrix(int columns, int rows)
 {
     _matrix = new MatrixBase <Double>(columns, rows);
 }
Esempio n. 26
0
 /// <summary>Swaps the order of the two specified rows in the matrix</summary>
 /// <param name="row1">Row to swap</param>
 /// <param name="row2">Row to swap</param>
 public void SwapRows(Int32 row1, Int32 row2)
 {
     MatrixBase <Double> .SwapRows(this, row1, row2);
 }
Esempio n. 27
0
        /******************/
        /* PUBLIC METHODS */
        /******************/

        /// <summary>Swaps the order of the two specified columns in the matrix</summary>
        /// <param name="row1">Column to swap</param>
        /// <param name="row2">Column to swap</param>
        public void SwapColumns(Int32 column1, Int32 column2)
        {
            MatrixBase <Double> .SwapColumns(this, column1, column2);
        }
Esempio n. 28
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="matrix"></param>
 public DoubleMatrix(DoubleMatrix matrix)
 {
     _matrix = matrix.InnerMatrix.Clone();
 }
Esempio n. 29
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="doubleArray"></param>
 public DoubleMatrix(MatrixBase <Double> matrix)
 {
     _matrix = matrix;
 }
Esempio n. 30
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="doubleArray"></param>
 public DoubleMatrix(Double[,] doubleArray)
 {
     _matrix = new MatrixBase <Double>(doubleArray);
 }