nalgebra-macros-0.3.0/.cargo_vcs_info.json0000644000000002000000000000100140610ustar { "git": { "sha1": "631064a3bcdf0b307f23ab0853b8dadad52a7d37", "dirty": true }, "path_in_vcs": "nalgebra-macros" }nalgebra-macros-0.3.0/Cargo.lock0000644000000117150000000000100120510ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "approx" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" dependencies = [ "num-traits", ] [[package]] name = "autocfg" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "bytemuck" version = "1.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef657dfab802224e671f5818e9a4935f9b1957ed18e58292690cc39e7a4092a3" [[package]] name = "matrixmultiply" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a" dependencies = [ "autocfg", "rawpointer", ] [[package]] name = "nalgebra" version = "0.33.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26aecdf64b707efd1310e3544d709c5c0ac61c13756046aaaba41be5c4f66a3b" dependencies = [ "approx", "matrixmultiply", "nalgebra-macros 0.2.2", "num-complex", "num-rational", "num-traits", "simba", "typenum", ] [[package]] name = "nalgebra-macros" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "nalgebra-macros" version = "0.3.0" dependencies = [ "nalgebra", "proc-macro2", "quote", "syn", ] [[package]] name = "num-bigint" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" dependencies = [ "num-integer", "num-traits", ] [[package]] name = "num-complex" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "num-traits", ] [[package]] name = "num-integer" version = "0.1.46" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" dependencies = [ "num-traits", ] [[package]] name = "num-rational" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" dependencies = [ "num-bigint", "num-integer", "num-traits", ] [[package]] name = "num-traits" version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] [[package]] name = "paste" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "proc-macro2" version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" dependencies = [ "proc-macro2", ] [[package]] name = "rawpointer" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" [[package]] name = "safe_arch" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96b02de82ddbe1b636e6170c21be622223aea188ef2e139be0a5b219ec215323" dependencies = [ "bytemuck", ] [[package]] name = "simba" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3a386a501cd104797982c15ae17aafe8b9261315b5d07e3ec803f2ea26be0fa" dependencies = [ "approx", "num-complex", "num-traits", "paste", "wide", ] [[package]] name = "syn" version = "2.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "typenum" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "unicode-ident" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" [[package]] name = "wide" version = "0.7.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41b5576b9a81633f3e8df296ce0063042a73507636cbe956c61133dd7034ab22" dependencies = [ "bytemuck", "safe_arch", ] nalgebra-macros-0.3.0/Cargo.toml0000644000000025110000000000100120660ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2024" name = "nalgebra-macros" version = "0.3.0" authors = [ "Andreas Longva", "Sébastien Crozet ", ] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Procedural macros for nalgebra" homepage = "https://nalgebra.rs" documentation = "https://www.nalgebra.rs/docs" readme = "README.md" keywords = [ "linear", "algebra", "matrix", "vector", "math", ] categories = [ "science", "mathematics", ] license = "Apache-2.0" repository = "https://github.com/dimforge/nalgebra" resolver = "2" [lib] name = "nalgebra_macros" path = "src/lib.rs" proc-macro = true [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" [dependencies.syn] version = "2.0" features = ["full"] [dev-dependencies.nalgebra] version = "0.33" nalgebra-macros-0.3.0/Cargo.toml.orig000064400000000000000000000012031046102023000155440ustar 00000000000000[package] name = "nalgebra-macros" version = "0.3.0" authors = ["Andreas Longva", "Sébastien Crozet "] edition = "2024" description = "Procedural macros for nalgebra" documentation = "https://www.nalgebra.rs/docs" homepage = "https://nalgebra.rs" repository = "https://github.com/dimforge/nalgebra" readme = "../README.md" categories = ["science", "mathematics"] keywords = ["linear", "algebra", "matrix", "vector", "math"] license = "Apache-2.0" [lib] proc-macro = true [dependencies] syn = { version = "2.0", features = ["full"] } quote = "1.0" proc-macro2 = "1.0" [dev-dependencies] nalgebra = { version = "0.33" } nalgebra-macros-0.3.0/LICENSE000064400000000000000000000261231046102023000136720ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2020 Sébastien Crozet Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. nalgebra-macros-0.3.0/README.md000064400000000000000000000015441046102023000141440ustar 00000000000000

crates.io

crates.io

Users guide | Documentation

-----

Linear algebra library for the Rust programming language.

----- nalgebra-macros-0.3.0/src/lib.rs000064400000000000000000000210601046102023000145630ustar 00000000000000//! Macros for `nalgebra`. //! //! This crate is not intended for direct consumption. Instead, the macros are re-exported by //! `nalgebra` if the `macros` feature is enabled (enabled by default). #![deny( nonstandard_style, unused, missing_docs, rust_2018_idioms, rust_2024_compatibility, future_incompatible, missing_copy_implementations, missing_debug_implementations, clippy::all )] mod matrix_vector_impl; mod stack_impl; use matrix_vector_impl::{Matrix, Vector}; use crate::matrix_vector_impl::{dmatrix_impl, dvector_impl, matrix_impl, vector_impl}; use proc_macro::TokenStream; use quote::quote; use stack_impl::stack_impl; use syn::parse_macro_input; /// Construct a fixed-size matrix directly from data. /// /// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// This macro facilitates easy construction of matrices when the entries of the matrix are known /// (either as constants or expressions). This macro produces an instance of `SMatrix`. This means /// that the data of the matrix is stored on the stack, and its dimensions are fixed at /// compile-time. If you want to construct a dynamic matrix, use [`dmatrix!`] instead. /// /// `matrix!` is intended to be both the simplest and most efficient way to construct (small) /// matrices, and can also be used in *const fn* contexts. /// /// The syntax is MATLAB-like. Column elements are separated by a comma (`,`), and a semi-colon /// (`;`) designates that a new row begins. /// /// # Examples /// ``` /// use nalgebra::matrix; /// /// // Produces a Matrix3<_> == SMatrix<_, 3, 3> /// let a = matrix![1, 2, 3; /// 4, 5, 6; /// 7, 8, 9]; /// ``` /// /// You can construct matrices with arbitrary expressions for its elements: /// /// ``` /// use nalgebra::{matrix, Matrix2}; /// let theta = 0.45f64; /// /// let r = matrix![theta.cos(), - theta.sin(); /// theta.sin(), theta.cos()]; /// ``` #[proc_macro] pub fn matrix(stream: TokenStream) -> TokenStream { matrix_impl(stream) } /// Construct a dynamic matrix directly from data. /// /// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// The syntax is exactly the same as for [`matrix!`], but instead of producing instances of /// `SMatrix`, it produces instances of `DMatrix`. At the moment it is not usable /// in `const fn` contexts. /// /// # Example /// ``` /// use nalgebra::dmatrix; /// /// // Produces a DMatrix<_> /// let a = dmatrix![1, 2, 3; /// 4, 5, 6; /// 7, 8, 9]; /// ``` #[proc_macro] pub fn dmatrix(stream: TokenStream) -> TokenStream { dmatrix_impl(stream) } /// Construct a fixed-size column vector directly from data. /// /// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// Similarly to [`matrix!`], this macro facilitates easy construction of fixed-size vectors. /// However, whereas the [`matrix!`] macro expects each row to be separated by a semi-colon, /// the syntax of this macro is instead similar to `vec!`, in that the elements of the vector /// are simply listed consecutively. /// /// `vector!` is intended to be the most readable and performant way of constructing small, /// fixed-size vectors, and it is usable in `const fn` contexts. /// /// # Example /// ``` /// use nalgebra::vector; /// /// // Produces a Vector3<_> == SVector<_, 3> /// let v = vector![1, 2, 3]; /// ``` #[proc_macro] pub fn vector(stream: TokenStream) -> TokenStream { vector_impl(stream) } /// Construct a dynamic column vector directly from data. /// /// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// The syntax is exactly the same as for [`vector!`], but instead of producing instances of /// `SVector`, it produces instances of `DVector`. At the moment it is not usable /// in `const fn` contexts. /// /// # Example /// ``` /// use nalgebra::dvector; /// /// // Produces a DVector<_> /// let v = dvector![1, 2, 3]; /// ``` #[proc_macro] pub fn dvector(stream: TokenStream) -> TokenStream { dvector_impl(stream) } /// Construct a fixed-size point directly from data. /// /// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// Similarly to [`vector!`], this macro facilitates easy construction of points. /// /// `point!` is intended to be the most readable and performant way of constructing small, /// points, and it is usable in `const fn` contexts. /// /// # Example /// ``` /// use nalgebra::point; /// /// // Produces a Point3<_> /// let v = point![1, 2, 3]; /// ``` #[proc_macro] pub fn point(stream: TokenStream) -> TokenStream { let vector = parse_macro_input!(stream as Vector); let len = vector.len(); let array_tokens = vector.to_array_tokens(); let output = quote! { nalgebra::Point::<_, #len> { coords: nalgebra::SVector::<_, #len> ::from_array_storage(nalgebra::ArrayStorage([#array_tokens])) } }; proc_macro::TokenStream::from(output) } /// Construct a new matrix by stacking matrices in a block matrix. /// /// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// This macro facilitates the construction of /// [block matrices](https://en.wikipedia.org/wiki/Block_matrix) /// by stacking blocks (matrices) using the same MATLAB-like syntax as the [`matrix!`] and /// [`dmatrix!`] macros: /// /// ```rust /// # use nalgebra::stack; /// # /// # fn main() { /// # let [a, b, c, d] = std::array::from_fn(|_| nalgebra::Matrix1::new(0)); /// // a, b, c and d are matrices /// let block_matrix = stack![ a, b; /// c, d ]; /// # } /// ``` /// /// The resulting matrix is stack-allocated if the dimension of each block row and column /// can be determined at compile-time, otherwise it is heap-allocated. /// This is the case if, for every row, there is at least one matrix with a fixed number of rows, /// and, for every column, there is at least one matrix with a fixed number of columns. /// /// [`stack!`] also supports special syntax to indicate zero blocks in a matrix: /// /// ```rust /// # use nalgebra::stack; /// # /// # fn main() { /// # let [a, b, c, d] = std::array::from_fn(|_| nalgebra::Matrix1::new(0)); /// // a and d are matrices /// let block_matrix = stack![ a, 0; /// 0, d ]; /// # } /// ``` /// Here, the `0` literal indicates a zero matrix of implicitly defined size. /// In order to infer the size of the zero blocks, there must be at least one matrix /// in every row and column of the matrix. /// In other words, no row or column can consist entirely of implicit zero blocks. /// /// # Panics /// /// Panics if dimensions are inconsistent and it cannot be determined at compile-time. /// /// # Examples /// /// ``` /// use nalgebra::{matrix, SMatrix, stack}; /// /// let a = matrix![1, 2; /// 3, 4]; /// let b = matrix![5, 6; /// 7, 8]; /// let c = matrix![9, 10]; /// /// let block_matrix = stack![ a, b; /// c, 0 ]; /// /// assert_eq!(block_matrix, matrix![1, 2, 5, 6; /// 3, 4, 7, 8; /// 9, 10, 0, 0]); /// /// // Verify that the resulting block matrix is stack-allocated /// let _: SMatrix<_, 3, 4> = block_matrix; /// ``` /// /// The example above shows how stacking stack-allocated matrices results in a stack-allocated /// block matrix. If all row and column dimensions can not be determined at compile-time, /// the result is instead a dynamically allocated matrix: /// /// ``` /// use nalgebra::{dmatrix, DMatrix, Dyn, matrix, OMatrix, SMatrix, stack, U3}; /// /// # let a = matrix![1, 2; 3, 4]; let c = matrix![9, 10]; /// // a and c as before, but b is a dynamic matrix this time /// let b = dmatrix![5, 6; /// 7, 8]; /// /// // In this case, the number of rows can be statically inferred to be 3 (U3), /// // but the number of columns cannot, hence it is dynamic /// let block_matrix: OMatrix<_, U3, Dyn> = stack![ a, b; /// c, 0 ]; /// /// // If necessary, a fully dynamic matrix (DMatrix) can be obtained by reshaping /// let dyn_block_matrix: DMatrix<_> = block_matrix.reshape_generic(Dyn(3), Dyn(4)); /// ``` /// Note that explicitly annotating the types of `block_matrix` and `dyn_block_matrix` is /// only made for illustrative purposes, and is not generally necessary. /// #[proc_macro] pub fn stack(stream: TokenStream) -> TokenStream { let matrix = parse_macro_input!(stream as Matrix); proc_macro::TokenStream::from(stack_impl(matrix).unwrap_or_else(syn::Error::into_compile_error)) } nalgebra-macros-0.3.0/src/matrix_vector_impl.rs000064400000000000000000000140521046102023000177270ustar 00000000000000use proc_macro::TokenStream; use quote::{ToTokens, TokenStreamExt, quote}; use std::ops::Index; use syn::Expr; use syn::parse::{Error, Parse, ParseStream}; use syn::punctuated::Punctuated; use syn::spanned::Spanned; use syn::{Token, parse_macro_input}; use proc_macro2::{Delimiter, Spacing, TokenStream as TokenStream2, TokenTree}; use proc_macro2::{Group, Punct}; /// A matrix of expressions pub struct Matrix { // Represent the matrix data in row-major format data: Vec, nrows: usize, ncols: usize, } impl Index<(usize, usize)> for Matrix { type Output = Expr; fn index(&self, (row, col): (usize, usize)) -> &Self::Output { let linear_idx = self.ncols * row + col; &self.data[linear_idx] } } impl Matrix { pub fn nrows(&self) -> usize { self.nrows } pub fn ncols(&self) -> usize { self.ncols } /// Produces a stream of tokens representing this matrix as a column-major nested array. pub fn to_col_major_nested_array_tokens(&self) -> TokenStream2 { let mut result = TokenStream2::new(); for j in 0..self.ncols() { let mut col = TokenStream2::new(); let col_iter = (0..self.nrows()).map(|i| &self[(i, j)]); col.append_separated(col_iter, Punct::new(',', Spacing::Alone)); result.append(Group::new(Delimiter::Bracket, col)); result.append(Punct::new(',', Spacing::Alone)); } TokenStream2::from(TokenTree::Group(Group::new(Delimiter::Bracket, result))) } /// Produces a stream of tokens representing this matrix as a column-major flat array /// (suitable for representing e.g. a `DMatrix`). pub fn to_col_major_flat_array_tokens(&self) -> TokenStream2 { let mut data = TokenStream2::new(); for j in 0..self.ncols() { for i in 0..self.nrows() { self[(i, j)].to_tokens(&mut data); data.append(Punct::new(',', Spacing::Alone)); } } TokenStream2::from(TokenTree::Group(Group::new(Delimiter::Bracket, data))) } } type MatrixRowSyntax = Punctuated; impl Parse for Matrix { fn parse(input: ParseStream<'_>) -> syn::Result { let mut data = Vec::new(); let mut ncols = None; let mut nrows = 0; while !input.is_empty() { let row = MatrixRowSyntax::parse_separated_nonempty(input)?; let row_span = row.span(); if let Some(ncols) = ncols { if row.len() != ncols { let error_msg = format!( "Unexpected number of entries in row {}. Expected {}, found {} entries.", nrows, ncols, row.len() ); return Err(Error::new(row_span, error_msg)); } } else { ncols = Some(row.len()); } data.extend(row.into_iter()); nrows += 1; // We've just read a row, so if there are more tokens, there must be a semi-colon, // otherwise the input is malformed if !input.is_empty() { input.parse::()?; } } Ok(Self { data, nrows, ncols: ncols.unwrap_or(0), }) } } pub struct Vector { elements: Vec, } impl Vector { pub fn to_array_tokens(&self) -> TokenStream2 { let mut data = TokenStream2::new(); data.append_separated(&self.elements, Punct::new(',', Spacing::Alone)); TokenStream2::from(TokenTree::Group(Group::new(Delimiter::Bracket, data))) } pub fn len(&self) -> usize { self.elements.len() } } impl Parse for Vector { fn parse(input: ParseStream<'_>) -> syn::Result { // The syntax of a vector is just the syntax of a single matrix row if input.is_empty() { Ok(Self { elements: Vec::new(), }) } else { let elements = MatrixRowSyntax::parse_terminated(input)? .into_iter() .collect(); Ok(Self { elements }) } } } pub fn matrix_impl(stream: TokenStream) -> TokenStream { let matrix = parse_macro_input!(stream as Matrix); let row_dim = matrix.nrows(); let col_dim = matrix.ncols(); let array_tokens = matrix.to_col_major_nested_array_tokens(); // TODO: Use quote_spanned instead?? let output = quote! { nalgebra::SMatrix::<_, #row_dim, #col_dim> ::from_array_storage(nalgebra::ArrayStorage(#array_tokens)) }; proc_macro::TokenStream::from(output) } pub fn dmatrix_impl(stream: TokenStream) -> TokenStream { let matrix = parse_macro_input!(stream as Matrix); let row_dim = matrix.nrows(); let col_dim = matrix.ncols(); let array_tokens = matrix.to_col_major_flat_array_tokens(); // TODO: Use quote_spanned instead?? let output = quote! { nalgebra::DMatrix::<_> ::from_vec_storage(nalgebra::VecStorage::new( nalgebra::Dyn(#row_dim), nalgebra::Dyn(#col_dim), vec!#array_tokens)) }; proc_macro::TokenStream::from(output) } pub fn vector_impl(stream: TokenStream) -> TokenStream { let vector = parse_macro_input!(stream as Vector); let len = vector.len(); let array_tokens = vector.to_array_tokens(); let output = quote! { nalgebra::SVector::<_, #len> ::from_array_storage(nalgebra::ArrayStorage([#array_tokens])) }; proc_macro::TokenStream::from(output) } pub fn dvector_impl(stream: TokenStream) -> TokenStream { let vector = parse_macro_input!(stream as Vector); let len = vector.len(); let array_tokens = vector.to_array_tokens(); let output = quote! { nalgebra::DVector::<_> ::from_vec_storage(nalgebra::VecStorage::new( nalgebra::Dyn(#len), nalgebra::Const::<1>, vec!#array_tokens)) }; proc_macro::TokenStream::from(output) } nalgebra-macros-0.3.0/src/stack_impl.rs000064400000000000000000000351061046102023000161510ustar 00000000000000use crate::Matrix; use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::{format_ident, quote, quote_spanned}; use syn::spanned::Spanned; use syn::{Error, Expr, Lit}; #[allow(clippy::too_many_lines)] pub fn stack_impl(matrix: Matrix) -> syn::Result { // The prefix is used to construct variable names // that are extremely unlikely to collide with variable names used in e.g. expressions // by the user. Although we could use a long, pseudo-random string, this makes the generated // code very painful to parse, so we settle for something more semantic that is still // very unlikely to collide let prefix = "___na"; let n_block_rows = matrix.nrows(); let n_block_cols = matrix.ncols(); let mut output = quote! {}; // First assign data and shape for each matrix entry to variables // (this is important so that we, for example, don't evaluate an expression more than once) for i in 0..n_block_rows { for j in 0..n_block_cols { let expr = &matrix[(i, j)]; if !is_literal_zero(expr) { let ident_block = format_ident!("{prefix}_stack_{i}_{j}_block"); let ident_shape = format_ident!("{prefix}_stack_{i}_{j}_shape"); output.extend(std::iter::once(quote_spanned! {expr.span()=> let ref #ident_block = #expr; let #ident_shape = #ident_block.shape_generic(); })); } } } // Determine the number of rows (dimension) in each block row, // and write out variables that define block row dimensions and offsets into the // output matrix for i in 0..n_block_rows { // The dimension of the block row is the result of trying to unify the row shape of // all blocks in the block row let dim = (0 ..n_block_cols) .filter_map(|j| { let expr = &matrix[(i, j)]; if !is_literal_zero(expr) { let mut ident_shape = format_ident!("{prefix}_stack_{i}_{j}_shape"); ident_shape.set_span(ident_shape.span().located_at(expr.span())); Some(quote_spanned!{expr.span()=> #ident_shape.0 }) } else { None } }).reduce(|a, b| { let expect_msg = format!("All blocks in block row {i} must have the same number of rows"); quote_spanned!{b.span()=> >::representative(#a, #b) .expect(#expect_msg) } }).ok_or(Error::new(Span::call_site(), format!("Block row {i} cannot consist entirely of implicit zero blocks.")))?; let dim_ident = format_ident!("{prefix}_stack_row_{i}_dim"); let offset_ident = format_ident!("{prefix}_stack_row_{i}_offset"); let offset = if i == 0 { quote! { 0 } } else { let prev_offset_ident = format_ident!("{prefix}_stack_row_{}_offset", i - 1); let prev_dim_ident = format_ident!("{prefix}_stack_row_{}_dim", i - 1); quote! { #prev_offset_ident + <_ as nalgebra::Dim>::value(&#prev_dim_ident) } }; output.extend(std::iter::once(quote! { let #dim_ident = #dim; let #offset_ident = #offset; })); } // Do the same thing for the block columns for j in 0..n_block_cols { let dim = (0 ..n_block_rows) .filter_map(|i| { let expr = &matrix[(i, j)]; if !is_literal_zero(expr) { let mut ident_shape = format_ident!("{prefix}_stack_{i}_{j}_shape"); ident_shape.set_span(ident_shape.span().located_at(expr.span())); Some(quote_spanned!{expr.span()=> #ident_shape.1 }) } else { None } }).reduce(|a, b| { let expect_msg = format!("All blocks in block column {j} must have the same number of columns"); quote_spanned!{b.span()=> >::representative(#a, #b) .expect(#expect_msg) } }).ok_or(Error::new(Span::call_site(), format!("Block column {j} cannot consist entirely of implicit zero blocks.")))?; let dim_ident = format_ident!("{prefix}_stack_col_{j}_dim"); let offset_ident = format_ident!("{prefix}_stack_col_{j}_offset"); let offset = if j == 0 { quote! { 0 } } else { let prev_offset_ident = format_ident!("{prefix}_stack_col_{}_offset", j - 1); let prev_dim_ident = format_ident!("{prefix}_stack_col_{}_dim", j - 1); quote! { #prev_offset_ident + <_ as nalgebra::Dim>::value(&#prev_dim_ident) } }; output.extend(std::iter::once(quote! { let #dim_ident = #dim; let #offset_ident = #offset; })); } // Determine number of rows and cols in output matrix, // by adding together dimensions of all block rows/cols let num_rows = (0..n_block_rows) .map(|i| { let ident = format_ident!("{prefix}_stack_row_{i}_dim"); quote! { #ident } }) .reduce(|a, b| { quote! { <_ as nalgebra::DimAdd<_>>::add(#a, #b) } }) .unwrap_or(quote! { nalgebra::dimension::U0 }); let num_cols = (0..n_block_cols) .map(|j| { let ident = format_ident!("{prefix}_stack_col_{j}_dim"); quote! { #ident } }) .reduce(|a, b| { quote! { <_ as nalgebra::DimAdd<_>>::add(#a, #b) } }) .unwrap_or(quote! { nalgebra::dimension::U0 }); // It should be possible to use `uninitialized_generic` here instead // however that would mean that the macro needs to generate unsafe code // which does not seem like a great idea. output.extend(std::iter::once(quote! { let mut matrix = nalgebra::Matrix::zeros_generic(#num_rows, #num_cols); })); for i in 0..n_block_rows { for j in 0..n_block_cols { let row_dim = format_ident!("{prefix}_stack_row_{i}_dim"); let col_dim = format_ident!("{prefix}_stack_col_{j}_dim"); let row_offset = format_ident!("{prefix}_stack_row_{i}_offset"); let col_offset = format_ident!("{prefix}_stack_col_{j}_offset"); let expr = &matrix[(i, j)]; if !is_literal_zero(expr) { let expr_ident = format_ident!("{prefix}_stack_{i}_{j}_block"); output.extend(std::iter::once(quote! { let start = (#row_offset, #col_offset); let shape = (#row_dim, #col_dim); let input_view = #expr_ident.generic_view((0, 0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); })); } } } Ok(quote! { { #output matrix } }) } fn is_literal_zero(expr: &Expr) -> bool { matches!(expr, Expr::Lit(syn::ExprLit { lit: Lit::Int(integer_literal), .. }) if integer_literal.base10_digits() == "0") } #[cfg(test)] mod tests { use crate::Matrix; use crate::stack_impl::stack_impl; use quote::quote; #[test] fn stack_simple_generation() { let input: Matrix = syn::parse_quote![ a, 0; 0, b; ]; let result = stack_impl(input).unwrap(); let expected = quote! {{ let ref ___na_stack_0_0_block = a; let ___na_stack_0_0_shape = ___na_stack_0_0_block.shape_generic(); let ref ___na_stack_1_1_block = b; let ___na_stack_1_1_shape = ___na_stack_1_1_block.shape_generic(); let ___na_stack_row_0_dim = ___na_stack_0_0_shape.0; let ___na_stack_row_0_offset = 0; let ___na_stack_row_1_dim = ___na_stack_1_1_shape.0; let ___na_stack_row_1_offset = ___na_stack_row_0_offset + <_ as nalgebra::Dim>::value(&___na_stack_row_0_dim); let ___na_stack_col_0_dim = ___na_stack_0_0_shape.1; let ___na_stack_col_0_offset = 0; let ___na_stack_col_1_dim = ___na_stack_1_1_shape.1; let ___na_stack_col_1_offset = ___na_stack_col_0_offset + <_ as nalgebra::Dim>::value(&___na_stack_col_0_dim); let mut matrix = nalgebra::Matrix::zeros_generic( <_ as nalgebra::DimAdd<_>>::add(___na_stack_row_0_dim, ___na_stack_row_1_dim), <_ as nalgebra::DimAdd<_>>::add(___na_stack_col_0_dim, ___na_stack_col_1_dim) ); let start = (___na_stack_row_0_offset, ___na_stack_col_0_offset); let shape = (___na_stack_row_0_dim, ___na_stack_col_0_dim); let input_view = ___na_stack_0_0_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); let start = (___na_stack_row_1_offset, ___na_stack_col_1_offset); let shape = (___na_stack_row_1_dim, ___na_stack_col_1_dim); let input_view = ___na_stack_1_1_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); matrix }}; assert_eq!(format!("{result}"), format!("{}", expected)); } #[test] fn stack_complex_generation() { let input: Matrix = syn::parse_quote![ a, 0, b; 0, c, d; e, 0, 0; ]; let result = stack_impl(input).unwrap(); let expected = quote! {{ let ref ___na_stack_0_0_block = a; let ___na_stack_0_0_shape = ___na_stack_0_0_block.shape_generic(); let ref ___na_stack_0_2_block = b; let ___na_stack_0_2_shape = ___na_stack_0_2_block.shape_generic(); let ref ___na_stack_1_1_block = c; let ___na_stack_1_1_shape = ___na_stack_1_1_block.shape_generic(); let ref ___na_stack_1_2_block = d; let ___na_stack_1_2_shape = ___na_stack_1_2_block.shape_generic(); let ref ___na_stack_2_0_block = e; let ___na_stack_2_0_shape = ___na_stack_2_0_block.shape_generic(); let ___na_stack_row_0_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfRows < _ , _ >> :: representative (___na_stack_0_0_shape . 0 , ___na_stack_0_2_shape . 0) . expect ("All blocks in block row 0 must have the same number of rows") ; let ___na_stack_row_0_offset = 0; let ___na_stack_row_1_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfRows < _ , _ >> :: representative (___na_stack_1_1_shape . 0 , ___na_stack_1_2_shape . 0) . expect ("All blocks in block row 1 must have the same number of rows") ; let ___na_stack_row_1_offset = ___na_stack_row_0_offset + <_ as nalgebra::Dim>::value(&___na_stack_row_0_dim); let ___na_stack_row_2_dim = ___na_stack_2_0_shape.0; let ___na_stack_row_2_offset = ___na_stack_row_1_offset + <_ as nalgebra::Dim>::value(&___na_stack_row_1_dim); let ___na_stack_col_0_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfColumns < _ , _ >> :: representative (___na_stack_0_0_shape . 1 , ___na_stack_2_0_shape . 1) . expect ("All blocks in block column 0 must have the same number of columns") ; let ___na_stack_col_0_offset = 0; let ___na_stack_col_1_dim = ___na_stack_1_1_shape.1; let ___na_stack_col_1_offset = ___na_stack_col_0_offset + <_ as nalgebra::Dim>::value(&___na_stack_col_0_dim); let ___na_stack_col_2_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfColumns < _ , _ >> :: representative (___na_stack_0_2_shape . 1 , ___na_stack_1_2_shape . 1) . expect ("All blocks in block column 2 must have the same number of columns") ; let ___na_stack_col_2_offset = ___na_stack_col_1_offset + <_ as nalgebra::Dim>::value(&___na_stack_col_1_dim); let mut matrix = nalgebra::Matrix::zeros_generic( <_ as nalgebra::DimAdd<_>>::add( <_ as nalgebra::DimAdd<_>>::add(___na_stack_row_0_dim, ___na_stack_row_1_dim), ___na_stack_row_2_dim ), <_ as nalgebra::DimAdd<_>>::add( <_ as nalgebra::DimAdd<_>>::add(___na_stack_col_0_dim, ___na_stack_col_1_dim), ___na_stack_col_2_dim ) ); let start = (___na_stack_row_0_offset, ___na_stack_col_0_offset); let shape = (___na_stack_row_0_dim, ___na_stack_col_0_dim); let input_view = ___na_stack_0_0_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); let start = (___na_stack_row_0_offset, ___na_stack_col_2_offset); let shape = (___na_stack_row_0_dim, ___na_stack_col_2_dim); let input_view = ___na_stack_0_2_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); let start = (___na_stack_row_1_offset, ___na_stack_col_1_offset); let shape = (___na_stack_row_1_dim, ___na_stack_col_1_dim); let input_view = ___na_stack_1_1_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); let start = (___na_stack_row_1_offset, ___na_stack_col_2_offset); let shape = (___na_stack_row_1_dim, ___na_stack_col_2_dim); let input_view = ___na_stack_1_2_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); let start = (___na_stack_row_2_offset, ___na_stack_col_0_offset); let shape = (___na_stack_row_2_dim, ___na_stack_col_0_dim); let input_view = ___na_stack_2_0_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); matrix }}; assert_eq!(format!("{result}"), format!("{}", expected)); } }