Conversation
src/integer/mat_z/mul.rs
Outdated
| type Output = MatZ; | ||
|
|
||
| /// Implements the [`Mul`] trait for two [`MatZ`] values. | ||
| /// [`Mul`] is implemented for any combination of owned and borrowed [`MatZ`] and any kind of [`VecZ`]. |
There was a problem hiding this comment.
VecZ?
| /// [`Mul`] is implemented for any combination of owned and borrowed [`MatZ`] and any kind of [`VecZ`]. | |
| /// [`Mul`] is implemented for any combination of owned and borrowed [`MatZ`]. |
There was a problem hiding this comment.
This doc-comment is correct. It's about all the combinations of Multiplication of that type that are implemented.
a97d74a to
58631ab
Compare
1bed518 to
4c27d3d
Compare
|
|
||
| use super::MatZ; | ||
| use std::str::FromStr; | ||
|
|
There was a problem hiding this comment.
the tests are all 2x2 matrices, please also add one for different sizes
There was a problem hiding this comment.
Now one 3x3 test was included
src/integer/vec_z/from.rs
Outdated
| /// Creates a new row-vector with `num_rows` rows and | ||
| /// zeros as entries. | ||
| /// | ||
| /// Parameters: | ||
| /// - `num_rows`: number of columns the new vector should have | ||
| /// | ||
| /// Returns a [`VecZ`] or an error, if the number of rows is | ||
| /// less or equal to `0`. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// use math::integer::VecZ; | ||
| /// use math::utils::VectorDirection; | ||
| /// | ||
| /// let vector = VecZ::new(5, VectorDirection::ColumnVector).unwrap(); | ||
| /// ``` | ||
| /// | ||
| /// # Errors and Failures | ||
| /// - Returns a [`MathError`] of type | ||
| /// [`InvalidMatrix`](MathError::InvalidMatrix) | ||
| /// if the number of rows is `0`. | ||
| /// - Returns a [`MathError`] of type [`OutOfBounds`](MathError::OutOfBounds) | ||
| /// if the number of rows is negative or it does not fit into an [`i64`]. |
There was a problem hiding this comment.
Does not fit to definition with vector direction
src/integer/vec_z/from.rs
Outdated
| /// Creates a [`VecZ`] row-vector with entries in [`Z`] from a [`String`]. | ||
| /// The format of that string looks like this `[1,2,3]` for a row vector | ||
| /// with three entries (`1` in the first row, `2` in the second one, ...) |
There was a problem hiding this comment.
does not fit to direction
src/integer/vec_z/from.rs
Outdated
| assert_eq!(Z::from_i64(0), entry1); | ||
| assert_eq!(Z::from_i64(0), entry2); |
There was a problem hiding this comment.
| assert_eq!(Z::from_i64(0), entry1); | |
| assert_eq!(Z::from_i64(0), entry2); | |
| assert_eq!(Z::ZERO entry1); | |
| assert_eq!(Z::ZERO, entry2); |
src/integer/vec_z/from.rs
Outdated
| assert_eq!(Z::from_i64(0), entry1); | ||
| assert_eq!(Z::from_i64(0), entry2); |
There was a problem hiding this comment.
though i prefer using the constant
| assert_eq!(Z::from_i64(0), entry1); | |
| assert_eq!(Z::from_i64(0), entry2); | |
| assert_eq!(Z::from(0), entry1); | |
| assert_eq!(Z::from(0), entry2); |
src/integer/vec_z/transpose.rs
Outdated
| let cmp = VecZ::from_str("[[1],[2],[3]]").unwrap(); | ||
|
|
||
| assert_eq!(cmp, vec.transpose()); | ||
| } |
There was a problem hiding this comment.
Please also test with large entries and with negative entries and different sizes
There was a problem hiding this comment.
several kinds of entries (large, negative etc) are tested in MatZ::transpose, which this function uses. Different sized matrices were now added (4 and 5 entries) to the ones in MatZ:: transpose by these tests
src/integer/vec_z/get.rs
Outdated
| pub fn is_row_vector(&self) -> bool { | ||
| self.matrix.get_num_columns() > 1 | ||
| } |
There was a problem hiding this comment.
would this be more intuitiv?:
| pub fn is_row_vector(&self) -> bool { | |
| self.matrix.get_num_columns() > 1 | |
| } | |
| pub fn is_row_vector(&self) -> bool { | |
| self.matrix.get_num_rows() == 1 | |
| } |
There was a problem hiding this comment.
Now done this way. get_direction changed appropriately
src/integer/vec_z/from.rs
Outdated
| /// Creates a [`VecZ`] row-vector with entries in [`Z`] from a [`String`]. | ||
| /// The format of that string looks like this `[1,2,3]` for a row vector | ||
| /// with three entries (`1` in the first row, `2` in the second one, ...) |
There was a problem hiding this comment.
format does not fit anymore
src/integer/vec_z/from.rs
Outdated
| let entries_string = parse_matrix_string(string)?; | ||
| let (num_rows, num_cols) = find_matrix_dimensions(&entries_string)?; | ||
| let mut vector: VecZ; | ||
| match (num_rows, num_cols) { | ||
| (1, _) => vector = VecZ::new(num_cols, VectorDirection::RowVector)?, | ||
| (_, 1) => vector = VecZ::new(num_rows, VectorDirection::ColumnVector)?, | ||
| (_, _) => return Err(MathError::InvalidStringToVectorInput(String::from( | ||
| "The string either contained more than one column or row, or did have zero entries", | ||
| ))), | ||
| } | ||
|
|
||
| // fill entries of matrix according to entries in string_matrix | ||
| for (row_num, row) in entries_string.iter().enumerate() { | ||
| for (col_num, entry) in row.iter().enumerate() { | ||
| let z_entry = Z::from_str(entry)?; | ||
| vector.matrix.set_entry(row_num, col_num, z_entry)?; | ||
| } | ||
| } | ||
| Ok(vector) |
There was a problem hiding this comment.
Alternatively, what I would prefer:
| let entries_string = parse_matrix_string(string)?; | |
| let (num_rows, num_cols) = find_matrix_dimensions(&entries_string)?; | |
| let mut vector: VecZ; | |
| match (num_rows, num_cols) { | |
| (1, _) => vector = VecZ::new(num_cols, VectorDirection::RowVector)?, | |
| (_, 1) => vector = VecZ::new(num_rows, VectorDirection::ColumnVector)?, | |
| (_, _) => return Err(MathError::InvalidStringToVectorInput(String::from( | |
| "The string either contained more than one column or row, or did have zero entries", | |
| ))), | |
| } | |
| // fill entries of matrix according to entries in string_matrix | |
| for (row_num, row) in entries_string.iter().enumerate() { | |
| for (col_num, entry) in row.iter().enumerate() { | |
| let z_entry = Z::from_str(entry)?; | |
| vector.matrix.set_entry(row_num, col_num, z_entry)?; | |
| } | |
| } | |
| Ok(vector) | |
| let matrix = MatZ::from_str(str)?; | |
| match (matrix.get_num_rows(), matrix.get_num_cols()) { | |
| (a,b) if a ==1 || b== 1 => Ok(VecZ{matrix})} | |
| _ => Err(MathError::InvalidStringToVectorInput(String::from( | |
| "The string either contained more than one column or row, or did have zero entries", | |
| ))), | |
| } |
There was a problem hiding this comment.
Done in slightly different way
src/integer/vec_z.rs
Outdated
| /// # Examples | ||
| #[derive(Debug, PartialEq, Eq, Clone)] | ||
| pub struct VecZ { | ||
| pub(crate) matrix: MatZ, |
There was a problem hiding this comment.
matrix might be misleading
src/utils/parse.rs
Outdated
| @@ -1,4 +1,4 @@ | |||
| // Copyright © 2023 Marcel Luca Schmidt | |||
| // Copyright © 2023 Marcel Luca Schmidt, Niklas Siemer | |||
There was a problem hiding this comment.
| // Copyright © 2023 Marcel Luca Schmidt, Niklas Siemer | |
| // Copyright © 2023 Marcel Luca Schmidt |
| /// assert_eq!(mat.transpose(), cmp); | ||
| /// ``` | ||
| pub fn transpose(&self) -> Self { | ||
| let mut out = Self::new(self.get_num_columns(), self.get_num_rows()).unwrap(); |
There was a problem hiding this comment.
better use "expect" with a simple message that the number of rows or columns was saved wrongly.
There was a problem hiding this comment.
As we know that this can not fail, it's ok to use unwrap
src/integer/vec_z/from.rs
Outdated
| /// [`InvalidMatrix`](MathError::InvalidMatrix) | ||
| /// if the number of rows is `0`. | ||
| /// - Returns a [`MathError`] of type [`OutOfBounds`](MathError::OutOfBounds) | ||
| /// if the number of rows is negative or it does not fit into an [`i64`]. |
There was a problem hiding this comment.
| /// if the number of rows is negative or it does not fit into an [`i64`]. | |
| /// if the number of rows/columns is negative or it does not fit into an [`i64`]. |
There was a problem hiding this comment.
Added at several points
src/integer/vec_z/from.rs
Outdated
| /// # Errors and Failures | ||
| /// - Returns a [`MathError`] of type | ||
| /// [`InvalidMatrix`](MathError::InvalidMatrix) | ||
| /// if the number of rows is `0`. |
There was a problem hiding this comment.
| /// if the number of rows is `0`. | |
| /// if the number of rows/columns is `0`. |
src/integer/vec_z/from.rs
Outdated
| /// Creates a [`VecZ`] row-vector with entries in [`Z`] from a [`String`]. | ||
| /// The format of that string looks like this `[1,2,3]` for a row vector | ||
| /// with three entries (`1` in the first row, `2` in the second one, ...) |
There was a problem hiding this comment.
| /// Creates a [`VecZ`] row-vector with entries in [`Z`] from a [`String`]. | |
| /// The format of that string looks like this `[1,2,3]` for a row vector | |
| /// with three entries (`1` in the first row, `2` in the second one, ...) | |
| /// Creates a [`VecZ`] vector with entries in [`Z`] from a [`String`]. | |
| /// The format of that string looks like this `[[1,2,3]]` for a row vector | |
| /// with three entries (`1` in the first row, `2` in the second one, ...) | |
| /// and like this `[[1],[2],[3]]` for a column vector with the same entries. | |
| /// A string containing a single value (`[[1]]`) is always a row vector. |
There was a problem hiding this comment.
Done, but left out the comment about a 1x1 string as this is the wrong place to put it.
src/integer/vec_z/get.rs
Outdated
| /// assert!(row.is_row_vector()); | ||
| /// ``` | ||
| pub fn is_row_vector(&self) -> bool { | ||
| self.matrix.get_num_columns() > 1 |
There was a problem hiding this comment.
| self.matrix.get_num_columns() > 1 | |
| self.matrix.get_num_rows() == 1 |
There was a problem hiding this comment.
Yes, now it's done like this and commented accordingly
src/integer/vec_z/get.rs
Outdated
| /// assert!(col.is_column_vector()); | ||
| /// ``` | ||
| pub fn is_column_vector(&self) -> bool { | ||
| self.matrix.get_num_rows() > 1 |
There was a problem hiding this comment.
| self.matrix.get_num_rows() > 1 | |
| self.matrix.get_num_rows() != 1 |
There was a problem hiding this comment.
Not a nice solution, the one above is way more appropriate
src/integer/vec_z/get.rs
Outdated
| self.matrix.get_num_columns() > 1 | ||
| } | ||
|
|
||
| /// Returns `true` if the vector is a column vector with more than one row. |
There was a problem hiding this comment.
| /// Returns `true` if the vector is a column vector with more than one row. | |
| /// Returns `true` if the vector is a column vector. |
There was a problem hiding this comment.
Changed according to new implementation
src/integer/vec_z/get.rs
Outdated
| /// Returns [`VectorDirection::ColumnVector`] if the vector is a column vector with more than one row. | ||
| /// Otherwise, returns [`VectorDirection::RowVector`]. |
There was a problem hiding this comment.
| /// Returns [`VectorDirection::ColumnVector`] if the vector is a column vector with more than one row. | |
| /// Otherwise, returns [`VectorDirection::RowVector`]. | |
| /// Returns [`VectorDirection::ColumnVector`] if the vector is a column vector. | |
| /// Otherwise, returns [`VectorDirection::RowVector`]. |
src/integer/vec_z/set.rs
Outdated
| pub fn set_entry<S: TryInto<i64> + Display + Copy, T: Into<Z>>( | ||
| &mut self, | ||
| entry: S, | ||
| value: T, |
There was a problem hiding this comment.
Use updated version with impl instead of S and T
There was a problem hiding this comment.
As I'm not able to use the trait and this is the way it is done in MatZ, I do not see what to do differently here. But I also opened an issue for the set_entry and get_entry trait for Vectors.
src/integer/mat_z/mul.rs
Outdated
| type Output = MatZ; | ||
|
|
||
| /// Implements the [`Mul`] trait for two [`MatZ`] values. | ||
| /// [`Mul`] is implemented for any combination of owned and borrowed [`MatZ`] and any kind of [`VecZ`]. |
* Add macros for cross type arithmetics * Implement multiplication for MatZ * Implement PartialEq for MatZ * Implement transpose for MatZ * Add VecZ * Implement MatZ * VecZ + tests * Add instantiation methods for VecZ * Add getter and transpose to VecZ * Implement multiplication for VecZ in combination with MatZ * Apply requested PR changes and add from_str tests for VecZ * Apply rustfmt + add import of GetEntry for VecZ * Make vector viable as Column and RowVector * Apply requested PR changes to VecZ * Remove vectors
This PR implements
including tests for
MatZ.