Dynamic matrices to implement VectorSpace
- Dominant language
- Rust
- Stars
- 4.8k
- Forks
- 565
- PR merge metrics
- No merged PRs in 30d
Description
While discussing integration of sprs and alga, a few questions arose about how to turn dynamically-sized sparse vectors into a vector space. The only practical issue so far seems to be that the type has too many zeros, namely one for each dimension.
Looking into this further and comparing to nalgebra, it became clear that nalgebra's `DVector` does not implement `VectorSpace` either. I am suggesting to solve the too-many-zeros issue and thereby enabling `DVector` and `DMatrix` to implement `VectorSpace` and related traits.
# Motivation
`VectorSpace` and its subtraits are extremely useful abstractions for practical applications. They comprise all the normal operations you want to do with a vector. There are a lot of applications, where the size of of a vector is not known statically, yet you still want to operate on it like on a real vector space.
# Proposal
This is based on the work done on sprs here: vbarielle/sprs#120. I took the approach of introducing an empty vector of dimension zero as a universal zero, more explicitly:
```rust
DVector::from_fn(0, |_| unreachable!())
```
Say, we allow instantiating this using the `Zero` trait from alga. All operations like addition, scalar multiplication, etc. should be implemented in a way that it acts like the real zero for the other operand:
```rust
let a = DVector::from_element(3, 1.0);
assert_eq!(DVector::zero() + a, a);
assert_eq!(a + DVector::zero(), a);
assert_eq!(DVector::zero() * 3, DVector::zero());
```
All the other aspects of a vector space should be straight forward to implement, as far as I can tell.
From the point of view of abstract algebra, the question is probably whether `DVector` (and `DMatrix`) is one vector space or many vector spaces. The above suggestion is based on the idea that it consists of many vector spaces, that are incompatible with each other. As long as operations take place inside one of these vector spaces (say vectors of length 20, for example), everything behaves correctly. If we cross the border between different vector spaces, we see runtime errors.
# Unresolved questions
There is still a slight oddity that for each of these vector spaces of length `n`, there will then be two values with the same properties as a zero, namely `DVector::zero()` and `DVector::from_element(n, zero())`. This brings in a question about, whether equality checks should these two as equal, too, to avoid confusion.
cc @sebcrozet, I would love to hear your opinion on that.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.