-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Divide Num into separate traits #8049
Conversation
…OrderedRing. These names follow the mathematical conventions. The greater fidelity of these traits allows them to be used on other mathematical structures such as vectors and quaternions.
Please wait for further discussion. Don't r+ yet. |
@brson yeah, this could be a tad contentious. |
These traits are not useful for vector fields as written, you need both a vector and a scalar type. EDIT 1: This is a point trait from actual code. A point is a vector in a normal euclidean space.
EDIT 2 So vector fields don't have Also we need to sure if we want Ring or Commutative Ring/CRing (most commonly used). A group trait at all is maybe not useful since each set has many ways of forming groups over different operators. An |
@blake2-ppc could you expand on that? What changes would you suggest? Could you post a snippet? |
@huonw @jensnockert: thoughts? |
@blake2-ppc Thanks for the input. The difficulty is that we can't enforce commutativity in Rust so If we were to add Commutativity, it might be better to have them as separate lang item traits with the associated unit tests:
See my reddit comment on Guy Steele's lecture. |
I'd factor it like this for vector algebra. I don't know if a separate CRing trait is useful; the simplest (least composite) traits are normally the most useful.
|
I'm fully in support of following the mathematical algebraic structures hierarchy. Pros
Cons
(Although I think @blake2-ppc's pub trait Module<R: Ring>: AdditiveAbelianGroup + ScalarMultiplication<R> {}
pub trait Vector<F: Field>: Module<F> {} </bikeshed>) |
Also, I like the approach of some how indicating at least commutativity and associativity, to allow parallel map-reduce algorithms to Just Work. (Even if this is just by convention/naming: "we told you, it's your fault that this algorithm doesn't return what you expect".) |
@huonw: I agree with using @blake2-ppc / @bjz: Would defining these algebraic structures prevent us from being able to use @nikomatsakis's double-dispatch overloading trick so we could support operator overloading Matrix_Matrix and Matrix_Vector multiplication? |
I like the name http://en.wikipedia.org/wiki/Quasigroup, it doesn't imply associative. |
Regarding Vector Space and Module, @huonw, I'm very against traits that do not have real use case. This includes some of the traits I listed in association with the Vector space; I'm not sure I favor adding all of those at all, I just wanted to elaborate for bjz (since vector spaces were mentioned in the PR text). |
@blake2-ppc I definitely think VectorSpace doesn't belong in the std. We just want a structure that folks can extend in the future. The challenge is to avoid the issues that have plagued the Haskell numerics. |
Might be a good reference: http://www.haskell.org/haskellwiki/Numeric_Prelude |
@erickt btw I don't think that the double dispatch trick should be necessary. Plan is to lift the limitation that requires it, though of course we haven't done so yet. |
This seems like a good candidate for a library in the "incubator", a la Clojure. The rapid evolution of libraries like scalaz shows that these abstract algebra libraries take time to mature. I'm totally in favor of this kind of experimentation, don't get me wrong, especially when it's motivated by a use case--I just don't want to freeze the interface too early. |
@bjz one note about that numerical prelude lib in haskell that you've linked to is that the Haddock type docs are unreadable, you'll have to read the lib in source form to get the info out of it |
I am worried that this is making the num traits too detailed and complex, in a way that is not particularly useful but to a small audience. Frankly most of the names suggested here are kind of outrageous as core numeric traits for a general programming audience. I imagine there is not a lot of precedent for this factoring of the numeric types in other standard libraries, but I'd be happy to see some comparisons. |
@brson: I agree, we should try to minimize the complexity of the trait hierarchy, and only keep those traits that map to types we are likely to use, not approximations mathematical structures. Using names such as Field confuses everyone since most types implementing Field won't actually be fields. |
Excellent feedback everybody! I greatly appreciate the honest critiques.
@pcwalton I'm now thinking now it might be useful to have an external, generalized numeric prelude. #[no_implicit_prelude];
extern mod numeric;
use numeric::prelude::*; I don't know if this should be in extern, but I'm willing to set up my own repo for now. I agree this needs to be more nimble than what |
Ok, in the light of the highly reasonable arguments against, I am going to close this pull, for now. |
Add test for pattern_type_mismatch. This issue has been fixed by [commit](rust-lang/rust-clippy@8c1c763) This PR is used for close rust-lang#7946(Fixes rust-lang#7946). changelog: Add test for pattern_type_mismatch.
As part of the numeric trait reform (tracked in issue #4819), I have replaced
Num
with three traits, named according to mathematical conventions.You might think this is a tad excessive, but it's useful when you want to define these traits on other mathematical structures such as vectors and quaternions that don't satisfy all the operations required by the old
Num
.I have also renamed
Orderable
toOrderedRing
, which is the correct mathematical name. It should really requireTotalOrd
, but we are constrained by the fact that it is not impled by floats.