rust-ndarray / ndarray

ndarray: an N-dimensional array with array views, multidimensional slicing, and efficient operations
https://docs.rs/ndarray/
Apache License 2.0
3.61k stars 306 forks source link

Alga integration #410

Closed milibopp closed 1 year ago

milibopp commented 6 years ago

Would you be open to integrate ndarray with alga? The point of this would be to enable people to write generic code in terms of vector spaces etc. and be able to pass ndarray's Array type in.

I have been looking at this for various libraries now and as ndarray already supports broadcasting the integration should be a bit easier. But I would have to look into the details, of course.

bluss commented 6 years ago

I don't know alga but thanks for reaching out — algebra is not my prioritized approach to generic programming in numerics. We have some basics to take care of first — for example being able to treat f64 and Complex<f64> both as complex numbers.

What does integrating with alga mean for us?

milibopp commented 6 years ago

Ah, I see… alga does have abstractions for fields, too. ("field" is not meant too mathematically here, as floating point numbers do not strictly satisfy the algebraic properties exactly.)

In addition, it does go into a lot of detail modelling the whole tower of algebraic structures. There are abstractions for linear algebra such as a vector spaces, which are compositions of the more fundamental structures.

In practice, integrating with alga would mean to implement the traits provided by alga (possibly as an optional feature). Not all of them have to be implemented explicitly, as some are mere compositions with blanket implementations.

I would be willing to put in the work to see whether it fits and, if so, do that implementation. But it is a bit of churn and so I wanted reach out first to see whether integrating this into ndarray is an option at all. My motivation is to be able to write generic algorithms for linear algebra that can used with different algebra crates.

bluss commented 6 years ago

Alga's model looks great. Just very detailed, I hope you have some tricks that help with the implementation. My impression is that nalgebra has a matrix slice which is now very similar to ndarray's own data model. Random question I had, is what does “stable” mean in the definition of MatrixMut?

I would much prefer looser coupling, but I know that Rust doesn't make it easy. As in less coupling than an optional dependency (See ndarray-parallel as an example).

I don't want to say no, but I also can't promise to contribute much to this. The general goals for such an integration in ndarray would for me be that it has loose coupling and does not limit refactoring of ndarray.

milibopp commented 6 years ago

Completely agree with you on the loose coupling issue. It would be nice if Rust allowed an integration like this to be a separate crate. One option to do this out of crate is to make a wrapper, which has a ton of downsides, too.

I have started doing a wrapper implementation as a small prototype to test the integration. This is also a good way to make sure one only relies on the public API of ndarray. Thus there should not be any extra barrier to refactoring – unless you want to do API-breaking refactorings, of course, in which case all bets are off, especially if the semantics change.

To be honest I can only speculate on the meaning of "stable" in the MatrixMut docs. By definition of the trait it should mean those matrices, where these methods are fine to expose. As a counter example, if you had a SymmetricMatrix, it should probably not implement such a trait.

bluss commented 6 years ago

We have goals and we have compromises - no point in wrappers if it is a torture to impl or use.

nilgoyette commented 1 year ago

Closing this issue