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

add implementation of From trait for SMatrix<->DMatrix, SVector<->DVector #1324

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Lishen1
Copy link

@Lishen1 Lishen1 commented Nov 21, 2023

No description provided.

@Andlon
Copy link
Collaborator

Andlon commented Apr 3, 2024

Thanks for the PR @Lishen1, I think this kind of functionality is sorely needed in nalgebra. Converting between different storages often requires different approaches depending on what needs to be done. A more unified approach to conversion would be great.

That said, I think the current approach is a little bit too narrow. For example, it implements directly on DMatrix<T>, which excludes e.g. DVector<T>.

I wonder if we instead could do something like From and TryFrom impls that just forward to the underlying Storage, and then implement the proper conversion routines for storages instead. Essentially, we'd have TryFrom<VecStorage> for ArrayStorage and From<ArrayStorage> for VecStorage> assuming that dimensions satisfy something like DimEq (or ideally SameNumberOfRows/SameNumberOfColumns if possible, for more granular type mismatch errors).

Do you have any thoughts on this?

@Lishen1
Copy link
Author

Lishen1 commented Apr 4, 2024

probably we can combine implementation for vector and matrix into one implementation like:

impl<'a, T: Scalar, const R0: usize, const C0: usize, R1: Dim, C1: Dim>
    From<&'a Matrix<T, R1, C1, VecStorage<T, R1, C1>>>
    for Matrix<T, Const<R0>, Const<C0>, ArrayStorage<T, R0, C0>>
{
    fn from(_m: &'a Matrix<T, R1, C1, VecStorage<T, R1, C1>>) -> Self {
    ...
    }
}

i don't know how we can use SameNumberOfRows/SameNumberOfColumns since R1: Dim, C1: Dim both can be Dyn and we can't proof shape match in compile time.
anyway, i'll think how to improve implementation with yor suggestions

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

Successfully merging this pull request may close these issues.

2 participants