dimforge / alga

Abstract algebra for Rust.
194 stars 39 forks source link

FiniteDimVectorSpace forces every type of vectors to have the same rank #84

Open dingxiangfei2009 opened 5 years ago

dingxiangfei2009 commented 5 years ago

Recall this definition:

pub trait FiniteDimVectorSpace: VectorSpace + Index<usize, Output = Self::Field> + IndexMut<usize, Output = Self::Field> {
    fn dimension() -> usize;
    fn canonical_basis_element(i: usize) -> Self;
    fn dot(&self, other: &Self) -> Self::Field;
    unsafe fn component_unchecked(&self, i: usize) -> &Self::Field;
    unsafe fn component_unchecked_mut(&mut self, i: usize) -> &mut Self::Field;

    fn canonical_basis<F: FnMut(&Self) -> bool>(f: F) { ... }
}

Note that FiniteDimVectorSpace::dimension is independent of &self. This forces all implementors of this trait to return constant value. Therefore, Vec<T> where T: Field is not a vector over the field T.

This is an awkward definition. It might make some sense for vector space of a certain dimension known at compile time, such as Vec2 and Vec3 in the examples. However, it prohibits implementations in general.

The dimension associated method in the definition of SquareMatrix makes much less sense in this context. Recall its definition:

pub trait SquareMatrix: Matrix<Row = Self::Vector, Column = Self::Vector, Transpose = Self> + MultiplicativeMonoid {
type Vector: FiniteDimVectorSpace<Field = Self::Field>;
    fn diagonal(&self) -> Self::Vector;
    fn determinant(&self) -> Self::Field;
    fn try_inverse(&self) -> Option<Self>;

    fn dimension(&self) -> usize { ... }
    fn transpose_mut(&mut self) { ... }
}

The dimension associated method in this case takes &self, but it is in fact independent of &self once Self::Vector has constant rank at compile time.

The proposed change is to allow FiniteDimVectorSpace::dimension to accept &self.

sebcrozet commented 5 years ago

Hi!

Note that FiniteDimVectorSpace::dimension is independent of &self. This forces all implementors of this trait to return constant value. Therefore, Vec where T: Field is not a vector over the field T.

This is actually the reason why it is independent of &self. The type Vec<T> where T: Field cannot represent a vector space over the field T because two vectors of different dimensions are not part of the same vector space.

The dimension associated method in this case takes &self, but it is in fact independent of &self once Self::Vector has constant rank at compile time. The proposed change is to allow FiniteDimVectorSpace::dimension to accept &self.

I agree this does not make sense. Though I think the actual fix here is to make SquareMatrix::dimension independent from self so it is in line with FiniteDimVectorSpace.

dingxiangfei2009 commented 5 years ago

@sebcrozet

I agree this does not make sense. Though I think the actual fix here is to make SquareMatrix::dimension independent from self so it is in line with FiniteDimVectorSpace.

Agree, if FiniteDimVectorSpace::dimension stays independent of &self.

So, allow me to explain the context. I am trying to manipulate matrices and vectors over an abstract field. The problem is some of the matrices/linear operators do not have the dimension of the vector spaces at compile time. For instance, an instance of permutation matrix has a finite dimension of the target vector space, but it is not necessarily fixed. With the current trait definition, these Permutation matrices must have the same size, or a constant generic perimeter in the type signature. It is a bit inflexible.