/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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)); }
/// <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; }
/// <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; }
/// <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))); }
/// <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); }
/// <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; }))); }
/// <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; }))); }
/// <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; }))); }
/// <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); }
/*************************/ /* 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); }
/// <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]; } } }
/// <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); }
/// <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); }
/********************/ /* 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]; } } }
/// <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); }
/********************/ /* 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]; } } }
/// <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); }
/// <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); }
/// <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)); }
/// <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); }
/// <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 })); }
/// <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 })); }
/// <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); }
/// <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); }
/// <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); }