From c6f7cae32667e5ff502e9aca756cc46434b64178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Thu, 25 Feb 2021 11:11:29 +0100 Subject: [PATCH] Move COO, CSC, CSR constructor at the top of the impls. --- nalgebra-sparse/src/coo.rs | 9 +++ nalgebra-sparse/src/csc.rs | 121 ++++++++++++++++++------------------- nalgebra-sparse/src/csr.rs | 121 ++++++++++++++++++------------------- 3 files changed, 129 insertions(+), 122 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 6b641513..50c68677 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -61,6 +61,15 @@ impl CooMatrix { } } + /// Construct a zero COO matrix of the given dimensions. + /// + /// Specifically, the collection of triplets - corresponding to explicitly stored entries - + /// is empty, so that the matrix (implicitly) represented by the COO matrix consists of all + /// zero entries. + pub fn zeros(nrows: usize, ncols: usize) -> Self { + Self::new(nrows, ncols) + } + /// Try to construct a COO matrix from the given dimensions and a collection of /// (i, j, v) triplets. /// diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index fb454a43..3a4d3f6f 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -127,6 +127,17 @@ pub struct CscMatrix { } impl CscMatrix { + /// Constructs a CSC representation of the (square) `n x n` identity matrix. + #[inline] + pub fn identity(n: usize) -> Self + where + T: Scalar + One, + { + Self { + cs: CsMatrix::identity(n), + } + } + /// Create a zero CSC matrix with no explicitly stored entries. pub fn zeros(nrows: usize, ncols: usize) -> Self { Self { @@ -134,6 +145,51 @@ impl CscMatrix { } } + /// Try to construct a CSC matrix from raw CSC data. + /// + /// It is assumed that each column contains unique and sorted row indices that are in + /// bounds with respect to the number of rows in the matrix. If this is not the case, + /// an error is returned to indicate the failure. + /// + /// An error is returned if the data given does not conform to the CSC storage format. + /// See the documentation for [CscMatrix](struct.CscMatrix.html) for more information. + pub fn try_from_csc_data( + num_rows: usize, + num_cols: usize, + col_offsets: Vec, + row_indices: Vec, + values: Vec, + ) -> Result { + let pattern = SparsityPattern::try_from_offsets_and_indices( + num_cols, + num_rows, + col_offsets, + row_indices, + ) + .map_err(pattern_format_error_to_csc_error)?; + Self::try_from_pattern_and_values(pattern, values) + } + + /// Try to construct a CSC matrix from a sparsity pattern and associated non-zero values. + /// + /// Returns an error if the number of values does not match the number of minor indices + /// in the pattern. + pub fn try_from_pattern_and_values( + pattern: SparsityPattern, + values: Vec, + ) -> Result { + if pattern.nnz() == values.len() { + Ok(Self { + cs: CsMatrix::from_pattern_and_values(pattern, values), + }) + } else { + Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::InvalidStructure, + "Number of values and row indices must be the same", + )) + } + } + /// The number of rows in the matrix. #[inline] pub fn nrows(&self) -> usize { @@ -180,51 +236,6 @@ impl CscMatrix { self.cs.values_mut() } - /// Try to construct a CSC matrix from raw CSC data. - /// - /// It is assumed that each column contains unique and sorted row indices that are in - /// bounds with respect to the number of rows in the matrix. If this is not the case, - /// an error is returned to indicate the failure. - /// - /// An error is returned if the data given does not conform to the CSC storage format. - /// See the documentation for [CscMatrix](struct.CscMatrix.html) for more information. - pub fn try_from_csc_data( - num_rows: usize, - num_cols: usize, - col_offsets: Vec, - row_indices: Vec, - values: Vec, - ) -> Result { - let pattern = SparsityPattern::try_from_offsets_and_indices( - num_cols, - num_rows, - col_offsets, - row_indices, - ) - .map_err(pattern_format_error_to_csc_error)?; - Self::try_from_pattern_and_values(pattern, values) - } - - /// Try to construct a CSC matrix from a sparsity pattern and associated non-zero values. - /// - /// Returns an error if the number of values does not match the number of minor indices - /// in the pattern. - pub fn try_from_pattern_and_values( - pattern: SparsityPattern, - values: Vec, - ) -> Result { - if pattern.nnz() == values.len() { - Ok(Self { - cs: CsMatrix::from_pattern_and_values(pattern, values), - }) - } else { - Err(SparseFormatError::from_kind_and_msg( - SparseFormatErrorKind::InvalidStructure, - "Number of values and row indices must be the same", - )) - } - } - /// An iterator over non-zero triplets (i, j, v). /// /// The iteration happens in column-major fashion, meaning that j increases monotonically, @@ -485,28 +496,16 @@ impl CscMatrix { cs: self.cs.diagonal_as_matrix(), } } -} -impl CscMatrix -where - T: Scalar, -{ /// Compute the transpose of the matrix. - pub fn transpose(&self) -> CscMatrix { + pub fn transpose(&self) -> CscMatrix + where + T: Scalar, + { CsrMatrix::from(self).transpose_as_csc() } } -impl CscMatrix { - /// Constructs a CSC representation of the (square) `n x n` identity matrix. - #[inline] - pub fn identity(n: usize) -> Self { - Self { - cs: CsMatrix::identity(n), - } - } -} - /// Convert pattern format errors into more meaningful CSC-specific errors. /// /// This ensures that the terminology is consistent: we are talking about rows and columns, diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 2e95460f..ded189eb 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -127,6 +127,17 @@ pub struct CsrMatrix { } impl CsrMatrix { + /// Constructs a CSR representation of the (square) `n x n` identity matrix. + #[inline] + pub fn identity(n: usize) -> Self + where + T: Scalar + One, + { + Self { + cs: CsMatrix::identity(n), + } + } + /// Create a zero CSR matrix with no explicitly stored entries. pub fn zeros(nrows: usize, ncols: usize) -> Self { Self { @@ -134,6 +145,51 @@ impl CsrMatrix { } } + /// Try to construct a CSR matrix from raw CSR data. + /// + /// It is assumed that each row contains unique and sorted column indices that are in + /// bounds with respect to the number of columns in the matrix. If this is not the case, + /// an error is returned to indicate the failure. + /// + /// An error is returned if the data given does not conform to the CSR storage format. + /// See the documentation for [CsrMatrix](struct.CsrMatrix.html) for more information. + pub fn try_from_csr_data( + num_rows: usize, + num_cols: usize, + row_offsets: Vec, + col_indices: Vec, + values: Vec, + ) -> Result { + let pattern = SparsityPattern::try_from_offsets_and_indices( + num_rows, + num_cols, + row_offsets, + col_indices, + ) + .map_err(pattern_format_error_to_csr_error)?; + Self::try_from_pattern_and_values(pattern, values) + } + + /// Try to construct a CSR matrix from a sparsity pattern and associated non-zero values. + /// + /// Returns an error if the number of values does not match the number of minor indices + /// in the pattern. + pub fn try_from_pattern_and_values( + pattern: SparsityPattern, + values: Vec, + ) -> Result { + if pattern.nnz() == values.len() { + Ok(Self { + cs: CsMatrix::from_pattern_and_values(pattern, values), + }) + } else { + Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::InvalidStructure, + "Number of values and column indices must be the same", + )) + } + } + /// The number of rows in the matrix. #[inline] pub fn nrows(&self) -> usize { @@ -182,51 +238,6 @@ impl CsrMatrix { self.cs.values_mut() } - /// Try to construct a CSR matrix from raw CSR data. - /// - /// It is assumed that each row contains unique and sorted column indices that are in - /// bounds with respect to the number of columns in the matrix. If this is not the case, - /// an error is returned to indicate the failure. - /// - /// An error is returned if the data given does not conform to the CSR storage format. - /// See the documentation for [CsrMatrix](struct.CsrMatrix.html) for more information. - pub fn try_from_csr_data( - num_rows: usize, - num_cols: usize, - row_offsets: Vec, - col_indices: Vec, - values: Vec, - ) -> Result { - let pattern = SparsityPattern::try_from_offsets_and_indices( - num_rows, - num_cols, - row_offsets, - col_indices, - ) - .map_err(pattern_format_error_to_csr_error)?; - Self::try_from_pattern_and_values(pattern, values) - } - - /// Try to construct a CSR matrix from a sparsity pattern and associated non-zero values. - /// - /// Returns an error if the number of values does not match the number of minor indices - /// in the pattern. - pub fn try_from_pattern_and_values( - pattern: SparsityPattern, - values: Vec, - ) -> Result { - if pattern.nnz() == values.len() { - Ok(Self { - cs: CsMatrix::from_pattern_and_values(pattern, values), - }) - } else { - Err(SparseFormatError::from_kind_and_msg( - SparseFormatErrorKind::InvalidStructure, - "Number of values and column indices must be the same", - )) - } - } - /// An iterator over non-zero triplets (i, j, v). /// /// The iteration happens in row-major fashion, meaning that i increases monotonically, @@ -485,28 +496,16 @@ impl CsrMatrix { cs: self.cs.diagonal_as_matrix(), } } -} -impl CsrMatrix -where - T: Scalar, -{ /// Compute the transpose of the matrix. - pub fn transpose(&self) -> CsrMatrix { + pub fn transpose(&self) -> CsrMatrix + where + T: Scalar, + { CscMatrix::from(self).transpose_as_csr() } } -impl CsrMatrix { - /// Constructs a CSR representation of the (square) `n x n` identity matrix. - #[inline] - pub fn identity(n: usize) -> Self { - Self { - cs: CsMatrix::identity(n), - } - } -} - /// Convert pattern format errors into more meaningful CSR-specific errors. /// /// This ensures that the terminology is consistent: we are talking about rows and columns,