1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! **ARAEL** -- Algorithms for Robust Autonomy, Estimation, and Localization.
//!
//! Nonlinear optimization framework with compile-time symbolic differentiation.
//!
//! Define model structs with optimizable parameters, write constraints as
//! symbolic expressions, and the framework symbolically differentiates at
//! compile time, applies common subexpression elimination, and generates
//! compiled cost/gradient/hessian code.
//!
//! # Features
//!
//! - **Symbolic math** -- expression trees with automatic differentiation,
//! simplification, LaTeX/Rust code generation (via `arael-sym`)
//! - **Compile-time constraint code generation** -- write constraints
//! symbolically, get compiled derivative code with CSE
//! - **Levenberg-Marquardt solver** -- with robust error suppression
//! via the Starship method `gamma * atan(r / gamma)`
//! - **Multiple solver backends** via `LmSolver` trait:
//! - Dense Cholesky (nalgebra) -- fixed-size dispatch up to 9x9
//! - Band Cholesky -- pure Rust O(n*kd^2) for block-tridiagonal systems
//! - Sparse Cholesky (faer, pure Rust) -- for general sparse hessians
//! - Eigen SimplicialLLT and CHOLMOD (SuiteSparse) -- optional C++ backends via FFI
//! - LAPACK band -- optional dpbsv/spbsv backend
//! - **Indexed sparse assembly** -- precomputed position lists for
//! zero-overhead hessian assembly after first iteration
//! - **f32 and f64 precision** -- `#[arael(root)]` for f64,
//! `#[arael(root, f32)]` for f32 throughout
//! - **Model trait** -- hierarchical serialize/deserialize/update protocol
//! - **Type-safe references** -- `Ref<T>`, `Vec<T>`, `Deque<T>`, `Arena<T>`
//! - **Hessian blocks** -- `SelfBlock<A>` and `CrossBlock<A, B>` generic
//! over float type
//! - **Gimbal-lock-free rotations** -- `EulerAngleParam` optimizes a small
//! delta around a reference rotation matrix
//! - **WASM/browser support** -- compiles to WebAssembly; the `arael-sketch`
//! constraint editor runs in the browser via eframe/egui
//!
//! # Example: Symbolic Math
//!
//! ```ignore
//! use arael::sym::*;
//!
//! arael::sym! {
//! let x = symbol("x");
//! let f = sin(x) * x + 1.0;
//!
//! println!("f(x) = {}", f); // sin(x) * x + 1
//! println!("f'(x) = {}", f.diff("x")); // x * cos(x) + sin(x)
//!
//! let vars = std::collections::HashMap::from([("x", 2.0)]);
//! println!("f(2.0) = {}", f.eval(&vars)); // 2.8185...
//! }
//! ```
//!
//! The `sym!` macro auto-inserts `.clone()` on variable reuse, so you write
//! natural math without ownership boilerplate.
//! See [docs/SYM.md](https://github.com/harakas/arael/blob/master/docs/SYM.md)
//! for the full symbolic math reference.
//!
//! # Example: Robust Linear Regression
//!
//! Define a model with optimizable parameters and a residual expression.
//! The Starship method `gamma * atan(plain_r / gamma)` suppresses outlier
//! influence while preserving smooth differentiability:
//!
//! ```ignore
//! #[arael::model]
//! struct DataEntry { x: f32, y: f32 }
//!
//! #[arael::model]
//! #[arael(fit(data, |e| {
//! let plain_r = (a * e.x + b - e.y) / sigma;
//! gamma * atan(plain_r / gamma)
//! }))]
//! struct LinearModel {
//! a: Param<f32>,
//! b: Param<f32>,
//! data: Vec<DataEntry>,
//! sigma: f32,
//! gamma: f32,
//! }
//! ```
//!
//! The macro generates `calc_cost()`, `calc_grad_hessian()`, and `fit()`
//! with symbolically differentiated, CSE-optimized compiled code.
//! The robust fit ignores outliers while tracking the inlier data:
//!
//! 
//!
//! See [examples/linear_demo.rs](https://github.com/harakas/arael/blob/master/examples/linear_demo.rs) for the full source.
//!
//! # 2D Sketch Editor
//!
//! The `arael-sketch` crate provides an interactive constraint-based 2D sketch
//! editor built on the optimization framework. Runs natively and in the browser
//! via WebAssembly.
//!
//! [](https://sketch.mare.ee/)
//!
//! # Example: SLAM Constraints
//!
//! For multi-body optimization (SLAM, bundle adjustment), define model
//! hierarchies with constraints. The macro handles symbolic differentiation,
//! reference resolution, and code generation.
//!
//! 
//!
//! The sparsity pattern shows pose-pose blocks (upper-left), pose-landmark
//! coupling (off-diagonal), and landmark self-blocks (lower-right). Sparse
//! Cholesky exploits this for large speedups over dense.
//!
//! ```ignore
//! #[arael::model]
//! #[arael(constraint(hb_pose, guard = self.info.gps.is_some(), {
//! // GPS constraint (guarded -- only when data present)
//! let raw = pose.pos - pose.info.gps.pos;
//! let whitened = pose.info.gps.cov_r.transpose() * raw;
//! [gamma * atan(whitened.x * pose.info.gps.cov_isigma.x / gamma), ...]
//! }))]
//! #[arael(constraint(hb_pose, {
//! // Tilt sensor -- accelerometer constrains roll and pitch
//! [(pose.ea.x - pose.info.tilt_roll) * path.tilt_isigma,
//! (pose.ea.y - pose.info.tilt_pitch) * path.tilt_isigma]
//! }))]
//! struct Pose {
//! pos: Param<vect3f>,
//! ea: SimpleEulerAngleParam<f32>,
//! info: PoseInfo,
//! hb_pose: SelfBlock<Pose>,
//! }
//!
//! // Observation linking a landmark to a pose
//! #[arael::model]
//! #[arael(constraint(hb, parent=lm, {
//! let mr2w = pose.ea.rotation_matrix();
//! let lm_r = mr2w.transpose() * (lm.pos - pose.pos);
//! let r_f = feature.mf2r.transpose() * (lm_r - feature.camera_pos);
//! [gamma * atan(atan2(r_f.y, r_f.x) * feature.isigma.x / gamma),
//! gamma * atan(atan2(r_f.z, r_f.x) * feature.isigma.y / gamma)]
//! }))]
//! struct PointFrine {
//! #[arael(ref = root.poses)]
//! pose: Ref<Pose>,
//! #[arael(ref = pose.info.features)]
//! feature: Ref<PointFeature>,
//! hb: CrossBlock<PointLandmark, Pose>,
//! }
//! ```
//!
//! See [examples/slam_demo.rs](https://github.com/harakas/arael/blob/master/examples/slam_demo.rs) for the full 60-pose, 240-landmark SLAM demo
//! with GPS, odometry, tilt sensor, graduated optimization, and covariance
//! estimation.
//! See [docs/SLAM.md](https://github.com/harakas/arael/blob/master/docs/SLAM.md) for the full walkthrough.
//!
//! # Example: Localization
//!
//! Same model as SLAM but landmarks are fixed (known map). With only pose
//! parameters the hessian is block-tridiagonal, so the band solver gives
//! O(n) scaling -- 9.4x faster than dense at 500 poses.
//! See [examples/loc_demo.rs](https://github.com/harakas/arael/blob/master/examples/loc_demo.rs).
//!
//! # Crate structure
//!
//! - `arael-sym` -- symbolic math engine (expression trees, differentiation, CSE)
//! - `arael-macros` -- proc macros (`#[arael::model]`, `#[derive(Model)]`)
//! - `arael` (this crate) -- runtime: model traits, solvers, geometry, vectors
/// Numeric traits (`Float`).
/// 2D and 3D vector types.
/// 3x3 and 2x2 matrix types with rotation and linear algebra.
/// Quaternion type for 3D rotations.
/// Type-safe indexed collections: `Ref`, `Vec`, `Deque`, `Arena`.
/// Re-export of the `arael-sym` symbolic math crate.
pub use arael_sym as sym;
/// Model trait, parameter types, and Hessian blocks.
/// Levenberg-Marquardt solver with dense, band, and sparse backends.
/// Camera model and geometric utilities.
/// The `sym!` auto-clone macro for symbolic expressions (from `arael-sym`).
pub use sym;
/// Derive macro for the `Model` trait.
pub use Model;
/// Attribute macro: `#[arael::model]`.
pub use model;