-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
<Complex> and Quaternions/Vectors #947
Comments
I also believe that Zig can shine in scientific computing in general. There is a lot of code abandoned or written once and forgotten in universities or by post-grads for research purposes that carries a lot of value but is written in C or C++(in the C with objects style) or desperately slow Java in a very clumsy manner or after a fierce fight with compiler. Zig can help here to do an incremental port from C-whatever to Zig and apply proper development best practices (without spending time to rewrite the code from scratch or manually transpile). There are other languages out there but Zig can focus on this area, from proteins, genomics to computational fluid dynamics or climate science. Computing is not only or webapps or mobileapps (people got it wrong). |
Completely agree that we need to deeply consider scientific computing! I think however the problem is a two phase issue, the first is to provide some nice utilities that are standardised around handling things like 'bigInts', vectors, matrices and so on. Then have a separate library (probably not official) to handle the more specific needs of scientific computing such as handling equations and the general use 'maple/matlab/whatever' that academics use (well not just academics also data scientists these days). We have to be careful that we don't develop a bloated std like in C++, and that we take a more conservative approach like in C/go (but don't go too far and be too inconsistent like C, where for some reason quick sort exists but almost anything else you have to build yourself). So I think having a well done mathematical library with things like Sedenions and Octonions should be separate to the std kinda like how numpy and scipy are done with python, however I do think that quite a few features in numpy like multidimensional arrays should be in python, but others are just not needed, especially around the region of graphing. |
@BraedonWooding : You have it right. Keep Zig a core part and make everything a library. Community should be able to build everything with Zig. The only requirements of Zig is ergonomics and enablement, no bloat not swiss army knife in the true spirit of C and Scheme. |
Just a note here: if there are built in fundamental types for n-dimensional vectors, complex and quarternion numbers + matrices, then we could have built-in overloading for those types, allowing basic operations to be expressed clearly, e.g. |
@lerno I agree that might make more sense, but my own preference would be to keep fundamental types to those exposed by the LLVM IR. Otherwise, they are just additional features that need not be in the compiler. Furthermore, you might consider allowing operator overloads (like C++ and Ruby) if you prefer that syntax. |
I assumed operator overloading was out for Zig? |
yes this is why, as I understand, he argues that your proposal is a contradiction to the current design decision IF your proposal was accepted then zig would also need/ have op overloading in general because your proposal implies that this is a useful feature/ good tradeoff |
From what I understand C is doing some form of overloading too in its support of complex numbers. The use case is not general operator overloading but specific overloading for certain basic math operations. General operator overloading is a bad idea. |
Thats just super weird IMO, I did not know this but you're definitely right that its a "feature". IMO if zig would acknowledge that op overloading is a useful feature, it should go all the way and make it accessible to all users (what about my own finite field arithmetic?) not only compiler supported types. Thus I don't think this fits onto zig anymore. Maybe one possible option might be to allow op overloading for * / + - ^ or some subset of operations. |
I think the issue with operator overloading is that it is too prone to abuse. So as I said originally, it is probably best to avoid relying on operators unless the type is fundamental. When it comes to fundamentals, the LLVM IR is probably the best way to decide. |
It seems like a lot of people want various built-in features that, while interesting, don't seem to fit with the spirit of Zig as a lean, mean C competitor. I sometimes wonder if the right way to let them all get what they want is to support language extension via third-party package, a bit like GHC's LANGUAGE pragma, but with an API that can support third-party extension and resolution of third-party LANGUAGE packages. I certainly don't care for built-in operator-overloaded linear algebraic features, and I would prefer not to waste the time of core developers with many nice-to-have features. But it would be awesome if there way a way for interested parties to develop syntax packages. @andrewrk any feedback? |
One problem with operator overloading, even for things like matrices, is that you so easily hide what is going on. C++ takes this to an extreme where just seeing "a + b" gives you no real idea of what is going on. Matrix addition? String concatenation? Set union? Since Zig already started down the path of having + and +% and so on, perhaps there is a way to provide for some amount of operator overloading to make these things a little nicer on the eyes but still keep basic operations clear. Perhaps names like "_+_" could be overloaded or something (do not take that particular syntax as a real proposal). Then matrices could be added with "a _+_ b". Not a serious proposal, but perhaps there is some happy medium that allows external packages to provide almost the same syntax as built in things but still allow a very clear indication of when some operation needed more investigation before you automatically assume what it does. If I see "add()", I know it is a function and I need to go look up what it does. If I see "+", I have to know the types of both arguments in C++ and even then this gets hard with all the automatic coercion that goes on. If I saw "_+_" I could treat it like "add()" and make the effort to find out what it really calls. |
In Why Zig When There is Already CPP, D, and Rust?, Zig prides itself for not having hidden control flow, which I believe is a good decision. Something that can help users who want operator overloading but don't want the hidden function calls would be explicit overloaded operators.
var c = a @+ b; This tells the user that there are hidden functions being called here, but we are explicitly stating they are. Allowing structures to override these can make using libraries like a 3rd party Bignum library usable without being painful. |
I used to be a fan of mixfix operators (where overloaded
is hardly better than
and can't be used like a normal
without horrible verbosity. |
I think that @kyle-github raises a very important thing: overloaded operators end up being very confusing because they look fundamental yet they are not. I fully agree with that being a problem. Also, overloading a few operators will still mean quite a few are either using ”wrong” symbols or has to be represented as functions. As an example, consider the dot and cross product of two vectors. The dot product might use ’*’ but what of the cross product? Unless we’re adding crazy stuff like unicode operators (which swift already demonstrated is a horrible idea) I don’t have a perfect solution for this, but if we look at Fortran it already has specialized ways to perform operations across slices of a matrix, eg This is not to suggest Zig copy Fortran, but to show what it would be up against if trying to compete in that domain. |
@lerno the lucky thing with Fortran matrices is that those operations actually correspond to compiler intrinsics. They can be represented as vectors (https://llvm.org/docs/LangRef.html#vector-type), which have special SIMD support (#903). In those cases I strongly recommend the compiler support the type as a fundamental. |
I was just reading in the docs about comptime and saw the max(...) example
this should be a generic function but without overloading
so this relates to anything that uses + - etc. as well which is why I'm bringing this up here although it would fit here as well |
Would it make sense to support the normal operators for arrays (and slices?) of fundamental types? I'm not sure if that's something Zig wants (only on basic operations +, -,*,/ imo). At least for fixed size arrays that would be nice. Then you don't need a big chunk of the Vector3D library stuff. |
Despite it is chinese to me, I found this thread pretty interesting. It is about what makes Fortran so good for numerical computation, and why it continues to be used instead of C, C++, Rust, ... They say things like:
So maybe, even if it is not implemented anytime soon, it looks like it might be a good idea to bear in mind this sort of things in mind. |
I'd like to chime in and say that I think allowing users to define numeric types (not just complex, quaternion, but also dual units, dual quaternions, bivectors, etc) would be hugely beneficial (science, math, computer graphics). Operator overloading can certainly be abused, but for numeric types, they really are the most (and some may say only) way to realistic express more complicated expressions, They obey laws of associativity, commutivity, and precedence as you would expect, and can be self-documenting used judiciously. I'm looking at porting over some code from C++ now, and I have to say, the operator part is the only thing that makes me nervous (compare even |
related #427 |
For math heavy code, it would be really nice to have some form of operator overloading. I'd really rather write Instead of adding full blown operator overloading, how about introducing a new type say the Also, similar to how
The
This would make the functions in I think the biggest issue is error handling. In most cases I think it is fine to require the use to validate input and just panic on bad values, e.g:
However types like BigInt which might allocate memory to grow in size would probably need some way to fail while returning an error. For this case, maybe it could work like this:
This last example seems like it has issues since this expression Anyway just throwing some ideas out there; I'm not fully sure on all this myself. Things look fine for objects with a static memory requirements, but objects with dynamic memory requirements seem a bit hairy. |
Could be possible to constrain overloading to anything without function calls which makes what you can do at the mercy of base operators. That at least forces a complete definition of the overload in one place and makes it painful enough to not go crazy on abuse. It's then not possible to allocate, concat, call out to magic procedures, etc, yet possible to gain convenience in operator use for at least some of the usecases while not violating the hidden control-flow rule. |
for some reason I was thinking about this today. There is something clean about zig not allowing operator overloading for basic operators (each one implies no hidden function calls). But I think the issue is really "maybe we should have some functions that can have infix calls). I'd suggest, say allowing something like "a <+> b" for a generalized addition that you must import into the local scope by "usingnamespace" or something or other. It might be nice if you could also have unicode operators, like "a <⊕> b". You could also require such a thing to have parenthesis #114 Only problem is that this is not necessarily composable, if you have say, a matrix direct sum, and wanted to overload the internal +/- operators for this function (like let's say you want to do matrices over quaternions or GF256)... But I feel like there might be a clever way of doing this with comptime, even. |
Proposal is too vague; closing. |
I think currently one of the big places Zig could be really useful is in data science, for this to occur Zig needs damn good math functionality including stuff like Quaternions and Vectors along with some good complex number support (quaternions are 4 tuple complex numbers if you don't know, furthermore quaternions do need good euler angle support for game dev).
I currently have a brief hackish solution for vectors, which currently only supports a single 3D vector though of course that means it supports 2D vectors (and has a few optimisations in place for those cases such as for getting the angle or doing a cross product). Currently I implemented a quick invsqrt with a doom like fast inv sqrt which is more for game development where you only care about 1-2 decimal points of precision in some cases.
You can find it here.
Anyways at minimum I'm going to make a library that holds them for my stuff, but I think they would be very valuable for the std lib.
The text was updated successfully, but these errors were encountered: