示例#1
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);
        }
示例#2
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);
        }
示例#3
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);
        }
示例#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);
 }
示例#5
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);
 }
示例#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));
 }
示例#7
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;
        }
示例#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;
        }
示例#9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="matricies"></param>
        /// <returns></returns>
        public static DoubleMatrix JoinVertical(IEnumerable <DoubleMatrix> matricies)
        {
            List <MatrixBase <Double> > innerMatrices = new List <MatrixBase <Double> >();

            foreach (DoubleMatrix matrix in matricies)
            {
                innerMatrices.Add(matrix.InnerMatrix);
            }
            return(new DoubleMatrix(MatrixBase <Double> .JoinVertical(innerMatrices)));
        }
示例#10
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);
 }
示例#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; })));
 }
示例#12
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; })));
 }
示例#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; })));
 }
示例#14
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);
 }
示例#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);
        }
示例#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];
         }
     }
 }
示例#17
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);
        }
示例#18
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);
 }
示例#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];
                    }
                }
            }
示例#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);
        }
示例#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];
                    }
                }
            }
示例#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);
        }
示例#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);
        }
示例#24
0
 /// <summary>Creates a new matrix pointing at the same underlying data
 /// set, windowed to the specified column and row ranges</summary>
 /// <param name="columnRange">Sub-matrix column range</param>
 /// <param name="rowRange">Sub-matrix row range</param>
 /// <returns></returns>
 public virtual MatrixBase <T> SubMatrix(
     Int32Range columnRange,
     Int32Range rowRange)
 {
     return(MatrixBase <T> .SubMatrix(this, columnRange, rowRange));
 }
示例#25
0
 /// <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 virtual void SwapColumns(Int32 column1, Int32 column2)
 {
     MatrixBase <T> .SwapColumns(this, column1, column2);
 }
示例#26
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="topMatrix"></param>
 /// <param name="bottomMatrix"></param>
 /// <returns></returns>
 public static MatrixBase <T> JoinVertical(
     MatrixBase <T> topMatrix,
     MatrixBase <T> bottomMatrix)
 {
     return(MatrixBase <T> .JoinVertical(new MatrixBase <T>[] { topMatrix, bottomMatrix }));
 }
示例#27
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="leftMatrix"></param>
 /// <param name="rightMatrix"></param>
 /// <returns></returns>
 public static MatrixBase <T> JoinHorizontal(
     MatrixBase <T> leftMatrix,
     MatrixBase <T> rightMatrix)
 {
     return(MatrixBase <T> .JoinHorizontal(new MatrixBase <T>[] { leftMatrix, rightMatrix }));
 }
示例#28
0
 /// <summary>Indicates whether the specified Matrix is null or is empty (of rank 0)</summary>
 /// <param name="matrix">Matrix</param>
 /// <returns>true, if specified matrix is null or empty (of rank == 0); otherwise, false</returns>
 public static Boolean IsNullOrEmpty(MatrixBase <T> matrix)
 {
     return(((object)matrix == null) || matrix.IsEmpty);
 }
示例#29
0
 /// <summary>Indicates whether the specified matrix is null</summary>
 /// <param name="matrix">Matrix</param>
 /// <returns>true, if specified matrix is null; otherwise, false</returns>
 public static Boolean IsNull(MatrixBase <T> matrix)
 {
     return((object)matrix == null);
 }
示例#30
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 virtual void SwapRows(Int32 row1, Int32 row2)
 {
     MatrixBase <T> .SwapRows(this, row1, row2);
 }