/// <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="ArgumentNullException">If <paramref name="row"/> is <see langword="null" />. </exception> /// <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 <double> InsertRow(int rowIndex, Vector <double> row) { if (row == null) { throw new ArgumentNullException("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> /// Get the 1D Laplacian matrix (with Dirichlet boundary conditions). /// </summary> /// <param name="nx">Grid size.</param> /// <param name="eigenvalues">Vector to store eigenvalues (optional).</param> /// <returns>Laplacian sparse matrix.</returns> public static SparseMatrix Laplacian(int nx, DenseVector eigenvalues = null) { if (nx == 1) { // Handle special case n = 1. var A = new SparseMatrix(nx, nx); A.At(0, 0, 2.0); return(A); } var C = new CoordinateStorage <double>(nx, nx); for (int i = 0; i < nx; i++) { C.At(i, i, 2.0); if (i == 0) { C.At(i, i + 1, -1.0); } else if (i == (nx - 1)) { C.At(i, i - 1, -1.0); } else { C.At(i, i - 1, -1.0); C.At(i, i + 1, -1.0); } } if (eigenvalues != null) { // Compute eigenvalues. int count = Math.Min(nx, eigenvalues.Count); var eigs = new double[nx]; for (int i = 0; i < count; i++) { eigs[i] = 4 * Math.Pow(Math.Sin((i + 1) * Math.PI / (2 * (nx + 1))), 2); } Array.Sort(eigs); for (int i = 0; i < count; ++i) { eigenvalues.At(i, eigs[i]); } } return((SparseMatrix)C.ToSparseMatrix()); }
/// <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 <double> 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._storage.ValueCount; i++) { for (var j = 0; j < v._storage.ValueCount; j++) { matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]); } } return(matrix); }
/// <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="ArgumentNullException">If <paramref name="row"/> is <see langword="null" />. </exception> /// <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<double> InsertRow(int rowIndex, Vector<double> row) { if (row == null) { throw new ArgumentNullException("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> /// 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<double> 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._storage.ValueCount; i++) { for (var j = 0; j < v._storage.ValueCount; j++) { matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]); } } return matrix; }
/// <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<double> 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<double> ToColumnMatrix() { var matrix = new SparseMatrix(Count, 1); for (var i = 0; i < NonZerosCount; i++) { matrix.At(_nonZeroIndices[i], 0, _nonZeroValues[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> /*SparseMatrix*/ public static Matrix 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> /// 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<double> ToRowMatrix() { var indices = _storage.Indices; var values = _storage.Values; var matrix = new SparseMatrix(1, Count); for (var i = 0; i < _storage.ValueCount; i++) { matrix.At(0, indices[i], 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<double> ToColumnMatrix() { var indices = _storage.Indices; var values = _storage.Values; var matrix = new SparseMatrix(Count, 1); for (var i = 0; i < _storage.ValueCount; i++) { matrix.At(indices[i], 0, values[i]); } return matrix; }