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

Remove add_row function from Matrix #59

Open
rspencer01 opened this issue Aug 20, 2021 · 0 comments
Open

Remove add_row function from Matrix #59

rspencer01 opened this issue Aug 20, 2021 · 0 comments
Assignees
Labels
high priority Should be fixed as a next course of action performance Could make things faster/use less memory rustiness Make code more rusty - sometimes may improve performance

Comments

@rspencer01
Copy link
Collaborator

I am really not a fan of adding rows to matrices for three reasons:

  • Complexity: this clones the entire matrix every time just to add a single row
  • Clarity: it is very unclear why you should be able to add an arbitrary length row to a matrix with no rows and no columns. The 0 x 0 matrix should not be a special case (it is after all still a linear map from the 0D vector space to itself).
  • Necessity: you should be able to just use a n x m matrix and keep track of how many rows you have set. If you are really only interested in a list of vectors, this can also be kept as a [Vec<i64>] or a Vec<Vec<i64>> (note a list of vectors is conceptually different from a matrix)

pub fn add_row(&self, v: Vec<i64>) -> Matrix {
let mut ans: Matrix;
if self.data.len() == 0 {
ans = Matrix::new(1, v.len());
for i in 0..v.len() {
ans[(0, i)] = v[i];
}
} else {
assert!(v.len() == self.cols);
ans = Matrix::new(self.rows + 1, self.cols);
for i in 0..self.rows + 1 {
for j in 0..self.cols {
if i < self.rows {
ans[(i, j)] = self.data[i * self.cols + j];
} else {
ans[(i, j)] = v[j];
}
}
}
}
return ans;
}

@rspencer01 rspencer01 added high priority Should be fixed as a next course of action performance Could make things faster/use less memory rustiness Make code more rusty - sometimes may improve performance labels Aug 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
high priority Should be fixed as a next course of action performance Could make things faster/use less memory rustiness Make code more rusty - sometimes may improve performance
Projects
None yet
Development

No branches or pull requests

2 participants