public void SolveLongMatrixThrowsArgumentException() { var matrix = new SparseMatrix(3, 2); var input = new DenseVector(3); var solver = new TFQMR(); Assert.Throws<ArgumentException>(() => matrix.SolveIterative(input, solver)); }
public void SolveLongMatrixThrowsArgumentException() { var matrix = new SparseMatrix(3, 2); var input = new DenseVector(3); var solver = new BiCgStab(); Assert.That(() => matrix.SolveIterative(input, solver), Throws.ArgumentException); }
public void SolveWideMatrixThrowsArgumentException() { var matrix = new SparseMatrix(2, 3); var input = new DenseVector(2); var solver = new TFQMR(); Assert.That(() => matrix.SolveIterative(input, solver), Throws.ArgumentException); }
public void SolveWideMatrixThrowsArgumentException() { var matrix = new SparseMatrix(2, 3); var input = new DenseVector(2); var solver = new GpBiCg(); Assert.Throws<ArgumentException>(() => matrix.SolveIterative(input, solver)); }
/// <summary> /// Check the result. /// </summary> /// <param name="preconditioner">Specific preconditioner.</param> /// <param name="matrix">Source matrix.</param> /// <param name="vector">Initial vector.</param> /// <param name="result">Result vector.</param> protected override void CheckResult(IPreconditioner<Complex> preconditioner, SparseMatrix matrix, Vector<Complex> vector, Vector<Complex> result) { Assert.AreEqual(typeof(UnitPreconditioner<Complex>), preconditioner.GetType(), "#01"); // Unit preconditioner is doing nothing. Vector and result should be equal for (var i = 0; i < vector.Count; i++) { Assert.IsTrue(vector[i] == result[i], "#02-" + i); } }
/// <summary> /// Create unit matrix. /// </summary> /// <param name="size">Matrix size.</param> /// <returns>New unit matrix.</returns> internal SparseMatrix CreateUnitMatrix(int size) { var matrix = new SparseMatrix(size); for (var i = 0; i < size; i++) { matrix[i, i] = 2; } return matrix; }
/// <summary> /// Check the result. /// </summary> /// <param name="preconditioner">Specific preconditioner.</param> /// <param name="matrix">Source matrix.</param> /// <param name="vector">Initial vector.</param> /// <param name="result">Result vector.</param> protected override void CheckResult(IPreconditioner<Complex> preconditioner, SparseMatrix matrix, Vector<Complex> vector, Vector<Complex> result) { Assert.AreEqual(typeof(Diagonal), preconditioner.GetType(), "#01"); // Compute M * result = product // compare vector and product. Should be equal var product = new DenseVector(result.Count); matrix.Multiply(result, product); for (var i = 0; i < product.Count; i++) { Assert.IsTrue(vector[i].Real.AlmostEqual(product[i].Real, -Epsilon.Magnitude()), "#02-" + i); Assert.IsTrue(vector[i].Imaginary.AlmostEqual(product[i].Imaginary, -Epsilon.Magnitude()), "#03-" + i); } }
public void CanAddSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); var m2 = SparseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); var sum1 = m1 + m2; var sum2 = m2 + m1; Assert.IsTrue(sum1.Equals(m2)); Assert.IsTrue(sum1.Equals(sum2)); var sparseResult = new SparseMatrix(1, 3); sparseResult.Add(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); sparseResult = SparseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); sparseResult.Add(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); sparseResult = SparseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); m1.Add(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); sparseResult = SparseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); sparseResult.Add(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(2*sum1)); var denseResult = new DenseMatrix(1, 3); denseResult.Add(m2, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); denseResult = DenseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); denseResult.Add(m1, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); var m3 = DenseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); var sum3 = m1 + m3; var sum4 = m3 + m1; Assert.IsTrue(sum3.Equals(m3)); Assert.IsTrue(sum3.Equals(sum4)); }
/// <summary> /// Create a matrix based on this vector in row form (one single row). /// </summary> /// <returns>This vector as a row matrix.</returns> public override Matrix<Complex> ToRowMatrix() { var matrix = new SparseMatrix(1, Count); for (var i = 0; i < NonZerosCount; i++) { matrix.At(0, _nonZeroIndices[i], _nonZeroValues[i]); } return matrix; }
/// <summary> /// Create a matrix based on this vector in column form (one single column). /// </summary> /// <returns>This vector as a column matrix.</returns> public override Matrix<Complex> ToColumnMatrix() { var matrix = new SparseMatrix(Count, 1); for (var i = 0; i < NonZerosCount; i++) { matrix.At(_nonZeroIndices[i], 0, _nonZeroValues[i]); } return matrix; }
/// <summary> /// Create a new sparse matrix with the diagonal as a copy of the given array. /// This new matrix will be independent from the array. /// A new memory block will be allocated for storing the matrix. /// </summary> public static SparseMatrix OfDiagonalArray(int rows, int columns, Complex[] diagonal) { var m = new SparseMatrix(rows, columns); m.SetDiagonal(diagonal); return m; }
/// <summary> /// Create a new sparse matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. /// A new memory block will be allocated for storing the matrix. /// </summary> public static SparseMatrix OfDiagonalVector(int rows, int columns, Vector<Complex> diagonal) { var m = new SparseMatrix(rows, columns); m.SetDiagonal(diagonal); return m; }
/// <summary> /// Can multiply a matrix with matrix. /// </summary> /// <param name="nameA">Matrix A name.</param> /// <param name="nameB">Matrix B name.</param> public override void CanMultiplyMatrixWithMatrixIntoResult(string nameA, string nameB) { var matrixA = TestMatrices[nameA]; var matrixB = TestMatrices[nameB]; var matrixC = new SparseMatrix(matrixA.RowCount, matrixB.ColumnCount); matrixA.Multiply(matrixB, matrixC); Assert.AreEqual(matrixC.RowCount, matrixA.RowCount); Assert.AreEqual(matrixC.ColumnCount, matrixB.ColumnCount); for (var i = 0; i < matrixC.RowCount; i++) { for (var j = 0; j < matrixC.ColumnCount; j++) { AssertHelpers.AlmostEqualRelative(matrixA.Row(i)*matrixB.Column(j), matrixC[i, j], 15); } } }
public void CanSubtractSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); var m2 = SparseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); var diff1 = m1 - m2; var diff2 = m2 - m1; Assert.IsTrue(diff1.Equals(m2.Negate())); Assert.IsTrue(diff1.Equals(diff2.Negate())); var sparseResult = new SparseMatrix(1, 3); sparseResult.Subtract(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); sparseResult = SparseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); sparseResult.Subtract(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(diff2)); sparseResult = SparseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); m1.Subtract(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); sparseResult = SparseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); sparseResult.Subtract(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(0*diff1)); var denseResult = new DenseMatrix(1, 3); denseResult.Subtract(m2, denseResult); Assert.IsTrue(denseResult.Equals(diff1)); denseResult = DenseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); denseResult.Subtract(m1, denseResult); Assert.IsTrue(denseResult.Equals(diff2)); var m3 = DenseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); var diff3 = m1 - m3; var diff4 = m3 - m1; Assert.IsTrue(diff3.Equals(m3.Negate())); Assert.IsTrue(diff3.Equals(diff4.Negate())); }
public void CanCreateLargeMatrix() { const int Order = 1000000; var matrix = new SparseMatrix(Order); Assert.AreEqual(Order, matrix.RowCount); Assert.AreEqual(Order, matrix.ColumnCount); Assert.DoesNotThrow(() => matrix[0, 0] = 1); }
public void CanCreateLargeSparseMatrix() { var matrix = new SparseMatrix(500, 1000); var nonzero = 0; var rnd = new System.Random(0); for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) { var value = rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10); if (value != 0) { nonzero++; } matrix[i, j] = value; } } Assert.AreEqual(matrix.NonZerosCount, nonzero); }
/// <summary> /// Create a matrix based on this vector in row form (one single row). /// </summary> /// <returns>This vector as a row matrix.</returns> public override Matrix<Complex> ToRowMatrix() { var matrix = new SparseMatrix(1, Count); for (var i = 0; i < _storage.ValueCount; i++) { matrix.At(0, _storage.Indices[i], _storage.Values[i]); } return matrix; }
/// <summary> /// Create a matrix based on this vector in column form (one single column). /// </summary> /// <returns>This vector as a column matrix.</returns> public override Matrix<Complex> ToColumnMatrix() { var matrix = new SparseMatrix(Count, 1); for (var i = 0; i < _storage.ValueCount; i++) { matrix.At(_storage.Indices[i], 0, _storage.Values[i]); } return matrix; }
/// <summary> /// Outer product of two vectors /// </summary> /// <param name="u">First vector</param> /// <param name="v">Second vector</param> /// <returns>Matrix M[i,j] = u[i]*v[j] </returns> /// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception> public static Matrix<Complex> OuterProduct(SparseVector u, SparseVector v) { if (u == null) { throw new ArgumentNullException("u"); } if (v == null) { throw new ArgumentNullException("v"); } var matrix = new SparseMatrix(u.Count, v.Count); for (var i = 0; i < u.NonZerosCount; i++) { for (var j = 0; j < v.NonZerosCount; j++) { if (u._nonZeroIndices[i] == v._nonZeroIndices[j]) { matrix.At(i, j, u._nonZeroValues[i] * v._nonZeroValues[j]); } } } return matrix; }
/// <summary> /// Returns the transpose of this matrix. /// </summary> /// <returns>The transpose of this matrix.</returns> public override Matrix<Complex> Transpose() { var ret = new SparseMatrix(ColumnCount, RowCount) { _columnIndices = new int[NonZerosCount], _nonZeroValues = new Complex[NonZerosCount] }; // Do an 'inverse' CopyTo iterate over the rows for (var i = 0; i < _rowIndex.Length; i++) { // Get the begin / end index for the current row var startIndex = _rowIndex[i]; var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount; // Get the values for the current row if (startIndex == endIndex) { // Begin and end are equal. There are no values in the row, Move to the next row continue; } for (var j = startIndex; j < endIndex; j++) { ret.SetValueAt(_columnIndices[j], i, _nonZeroValues[j]); } } return ret; }
public void SolvePoissonMatrixAndBackMultiply() { // Create the matrix var matrix = new SparseMatrix(100); // Assemble the matrix. We assume we're solving the Poisson equation // on a rectangular 10 x 10 grid const int GridSize = 10; // The pattern is: // 0 .... 0 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 0 0 ... 0 for (var i = 0; i < matrix.RowCount; i++) { // Insert the first set of -1's if (i > (GridSize - 1)) { matrix[i, i - GridSize] = -1; } // Insert the second set of -1's if (i > 0) { matrix[i, i - 1] = -1; } // Insert the centerline values matrix[i, i] = 4; // Insert the first trailing set of -1's if (i < matrix.RowCount - 1) { matrix[i, i + 1] = -1; } // Insert the second trailing set of -1's if (i < matrix.RowCount - GridSize) { matrix[i, i + GridSize] = -1; } } // Create the y vector var y = Vector<Complex>.Build.Dense(matrix.RowCount, 1); // Create an iteration monitor which will keep track of iterative convergence var monitor = new Iterator<Complex>( new IterationCountStopCriterium<Complex>(MaximumIterations), new ResidualStopCriterium<Complex>(ConvergenceBoundary), new DivergenceStopCriterium<Complex>(), new FailureStopCriterium<Complex>()); var solver = new TFQMR(); // Solve equation Ax = y var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); Assert.AreEqual(y.Count, x.Count, "#03"); // Back multiply the vector var z = matrix.Multiply(x); // Check that the solution converged Assert.IsTrue(monitor.Status == IterationStatus.Converged, "#04"); // Now compare the vectors Assert.LessOrEqual(Distance.Chebyshev(y, z), 2*ConvergenceBoundary); }
/// <summary> /// Creates a new <see cref="SparseMatrix"/> and inserts the given column at the given index. /// </summary> /// <param name="columnIndex">The index of where to insert the column.</param> /// <param name="column">The column to insert.</param> /// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is < zero or > the number of columns.</exception> /// <exception cref="ArgumentException">If the size of <paramref name="column"/> != the number of rows.</exception> public override Matrix<Complex> InsertColumn(int columnIndex, Vector<Complex> column) { if (columnIndex < 0 || columnIndex > ColumnCount) { throw new ArgumentOutOfRangeException("columnIndex"); } if (column.Count != RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column"); } var result = new SparseMatrix(RowCount, ColumnCount + 1); for (var i = 0; i < columnIndex; i++) { result.SetColumn(i, Column(i)); } result.SetColumn(columnIndex, column); for (var i = columnIndex + 1; i < ColumnCount + 1; i++) { result.SetColumn(i, Column(i - 1)); } return result; }
/// <summary> /// Stacks this matrix on top of the given matrix and places the result into the result <see cref="SparseMatrix"/>. /// </summary> /// <param name="lower">The matrix to stack this matrix upon.</param> /// <returns>The combined <see cref="SparseMatrix"/>.</returns> /// <exception cref="ArgumentNullException">If lower is <see langword="null" />.</exception> /// <exception cref="ArgumentException">If <strong>upper.Columns != lower.Columns</strong>.</exception> public override Matrix<Complex> Stack(Matrix<Complex> lower) { if (lower == null) { throw new ArgumentNullException("lower"); } if (lower.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension, "lower"); } var result = new SparseMatrix(RowCount + lower.RowCount, ColumnCount); Stack(lower, result); return result; }
/// <summary> /// Create a new sparse matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. /// A new memory block will be allocated for storing the matrix. /// </summary> public static SparseMatrix OfDiagonalVector(Vector<Complex> diagonal) { var m = new SparseMatrix(diagonal.Count, diagonal.Count); m.SetDiagonal(diagonal); return m; }
/// <summary> /// Check the result. /// </summary> /// <param name="preconditioner">Specific preconditioner.</param> /// <param name="matrix">Source matrix.</param> /// <param name="vector">Initial vector.</param> /// <param name="result">Result vector.</param> protected abstract void CheckResult(IPreconditioner<Complex> preconditioner, SparseMatrix matrix, Vector<Complex> vector, Vector<Complex> result);
/// <summary> /// Creates a matrix that contains the values from the requested sub-matrix. /// </summary> /// <param name="rowIndex">The row to start copying from.</param> /// <param name="rowLength">The number of rows to copy. Must be positive.</param> /// <param name="columnIndex">The column to start copying from.</param> /// <param name="columnLength">The number of columns to copy. Must be positive.</param> /// <returns>The requested sub-matrix.</returns> /// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is /// negative, or greater than or equal to the number of rows.</item> /// <item><paramref name="columnIndex"/> is negative, or greater than or equal to the number /// of columns.</item> /// <item><c>(columnIndex + columnLength) >= Columns</c></item> /// <item><c>(rowIndex + rowLength) >= Rows</c></item></list></exception> /// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/> /// is not positive.</exception> public override Matrix<Complex> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) { if (rowIndex >= RowCount || rowIndex < 0) { throw new ArgumentOutOfRangeException("rowIndex"); } if (columnIndex >= ColumnCount || columnIndex < 0) { throw new ArgumentOutOfRangeException("columnIndex"); } if (rowLength < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength"); } if (columnLength < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength"); } var colMax = columnIndex + columnLength; var rowMax = rowIndex + rowLength; if (rowMax > RowCount) { throw new ArgumentOutOfRangeException("rowLength"); } if (colMax > ColumnCount) { throw new ArgumentOutOfRangeException("columnLength"); } var result = new SparseMatrix(rowLength, columnLength); if (rowIndex > columnIndex && columnIndex + columnLength > rowIndex) { for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++) { result[i, rowIndex - columnIndex + i] = Data[rowIndex + i]; } } else if (rowIndex < columnIndex && rowIndex + rowLength > columnIndex) { for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++) { result[columnIndex - rowIndex + i, i] = Data[columnIndex + i]; } } else { for (var i = 0; i < Math.Min(columnLength, rowLength); i++) { result[i, i] = Data[rowIndex + i]; } } return result; }
/// <summary> /// Initializes a square <see cref="SparseMatrix"/> with all zero's except for ones on the diagonal. /// </summary> /// <param name="order">the size of the square matrix.</param> /// <returns>Identity <c>SparseMatrix</c></returns> /// <exception cref="ArgumentException"> /// If <paramref name="order"/> is less than one. /// </exception> public static SparseMatrix Identity(int order) { var m = new SparseMatrix(order) { NonZerosCount = order, _nonZeroValues = new Complex[order], _columnIndices = new int[order] }; for (var i = 0; i < order; i++) { m._nonZeroValues[i] = 1.0; m._columnIndices[i] = i; m._rowIndex[i] = i; } return m; }
/// <summary> /// Concatenates this matrix with the given matrix. /// </summary> /// <param name="right">The matrix to concatenate.</param> /// <returns>The combined <see cref="SparseMatrix"/>.</returns> public override Matrix<Complex> Append(Matrix<Complex> right) { if (right == null) { throw new ArgumentNullException("right"); } if (right.RowCount != RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); } var result = new SparseMatrix(RowCount, ColumnCount + right.ColumnCount); Append(right, result); return result; }
/// <summary> /// Creates a new <see cref="SparseMatrix"/> and inserts the given row at the given index. /// </summary> /// <param name="rowIndex">The index of where to insert the row.</param> /// <param name="row">The row to insert.</param> /// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is < zero or > the number of rows.</exception> /// <exception cref="ArgumentException">If the size of <paramref name="row"/> != the number of columns.</exception> public override Matrix<Complex> InsertRow(int rowIndex, Vector<Complex> row) { if (rowIndex < 0 || rowIndex > RowCount) { throw new ArgumentOutOfRangeException("rowIndex"); } if (row.Count != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); } var result = new SparseMatrix(RowCount + 1, ColumnCount); for (var i = 0; i < rowIndex; i++) { result.At(i, i, At(i, i)); } result.SetRow(rowIndex, row); for (var i = rowIndex + 1; i < result.RowCount; i++) { result.At(i, i - 1, At(i - 1, i - 1)); } return result; }
/// <summary> /// Diagonally stacks his matrix on top of the given matrix. The new matrix is a M-by-N matrix, /// where M = this.Rows + lower.Rows and N = this.Columns + lower.Columns. /// The values of off the off diagonal matrices/blocks are set to zero. /// </summary> /// <param name="lower">The lower, right matrix.</param> /// <exception cref="ArgumentNullException">If lower is <see langword="null" />.</exception> /// <returns>the combined matrix</returns> public override Matrix<Complex> DiagonalStack(Matrix<Complex> lower) { if (lower == null) { throw new ArgumentNullException("lower"); } var result = new SparseMatrix(RowCount + lower.RowCount, ColumnCount + lower.ColumnCount); DiagonalStack(lower, result); return result; }