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

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

Open
dingxiangfei2009 opened this issue Sep 3, 2019 · 2 comments

Comments

@dingxiangfei2009
Copy link

dingxiangfei2009 commented Sep 3, 2019

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
Copy link
Member

sebcrozet commented Sep 3, 2019

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
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants