Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added into_vec method for Matrix #3

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
26 changes: 26 additions & 0 deletions src/base/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2357,3 +2357,29 @@ impl<T> super::alias::Matrix1<T> {
scalar
}
}

alexandruradovici marked this conversation as resolved.
Show resolved Hide resolved
/// Provides methods for transforming a matrix into a vector with different algorithms
impl<T, R: Dim, C: Dim, S: Storage<T, R, C>> Matrix<T, R, C, S>
where
T: Clone,
{
/// Converts matrix into a vector by concatenating rows
pub fn into_vec(&self) -> Vec<T> {
let (num_rows, num_columns) = self.shape();
let mut resulted_vector = Vec::with_capacity(num_rows * num_columns);

for i in 0..num_rows {
for j in 0..num_columns {
// SAFETY: by design, self is in valid state to call self.get_unchecked.
// Since (num_rows, num_columns) is the size of the matrix, loops
// assure that i and j are in bounds, so the use of get_unchecked will
// optimize the program, without creating memory issues.
resulted_vector.push(unsafe { self.get_unchecked((i, j)) }.clone());
alexandruradovici marked this conversation as resolved.
Show resolved Hide resolved
}
}

resulted_vector
}
}