-
-
Notifications
You must be signed in to change notification settings - Fork 477
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
Matrix macro #886
Matrix macro #886
Conversation
This allows us to avoid injecting unsafe code into every macro invocation, which seems desirable.
These methods enable safe & const construction of DMatrix/DVector from a given VecStorage.
I'm missing one or more tests that verify that the macros work for all built-in types. I will add this as soon as I have time. |
I'm also missing tests where instead of just numbers, we have arbitrary Rust expressions (though one of the doc tests has something like this). I will add this later too. |
What's the case for |
Semantics, mostly. Although a vector is also a matrix, matrices and vectors often play different roles in applications, so at least in my opinion it benefits readability to distinguish them at the syntax level (although you can indeed use Second, |
All right, I finished up the things I wanted to address, so I suppose this PR should be ready now. |
Hey @Andlon, this is an amazing PR. Thank you! I will review this within the next few days.
I agree. I will take care of this later this month. |
Thanks, @sebcrozet! I also wanted to add that I don't have much experience with proc macros, so if there's any experienced proc macro person with some good suggestions for improvement I'd also be happy to hear about that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you again for this PR. This looks great. I'm not a proc-macro expert myself so I can't really judge the quality of the implementation. But it looks OK to me.
The proc macro stuff LGTM. |
@sebcrozet: I moved the impl blocks, but I needed to add some conditional compilation for the @Ralith: thanks for taking a look! |
@Andlon Looks greatk thanks! |
This PR introduces four new macros to
nalgebra
. They are available under the (now default)macros
feature, which makesnalgebra
export the macros from the procedural macro cratenalgebra-macros
. The macros are:matrix![]
for constructing fixed-size matrices with a MATLAB-like syntax.dmatrix![]
for constructing dynamic matrices with the same syntax asmatrix!
vector![]
for constructing small fixed-size vectors (syntax basically the same asvec!
)dvector![]
for constructing dynamically allocated vectors with the same syntax asvector
.The intention here is to simplify matrix/vector construction by improving readability, ease-of-use and ensuring that the matrix construction is as fast as possible.
Example:
Benefits over e.g.
MatrixRxC::new
constructors:#[rustfmt::skip::macros(matrix)]
to preventrustfmt
from messing up carefully aligned syntax.new
constructors.Disadvantages:
syn
andquote
to the dependency tree. On my 6-7 year old laptop, this adds ~10 secs to clean build compile time. Note that if you useserde
or possibly some of the other optional dependencies, you might already havesyn
/quote
in your dependency tree, so you pay effectively no penalty. If users don't need the macro, it's also possible to opt out by disabling themacros
feature.The user guide should probably be updated to accommodate these macros. My suggestion is to make these macros the de-facto way of constructing matrices with
nalgebra
.Resolves #873.