amari_calculus/lib.rs
1//! # Amari Calculus
2//!
3//! Geometric calculus - a unified framework for differential and integral calculus using geometric algebra.
4//!
5//! ## Overview
6//!
7//! This crate provides geometric calculus operations that unify:
8//! - Vector calculus (gradient, divergence, curl)
9//! - Differential forms
10//! - Tensor calculus
11//! - Covariant derivatives on manifolds
12//!
13//! ## Mathematical Foundation
14//!
15//! Geometric calculus is built on the **vector derivative operator**:
16//!
17//! ```text
18//! ∇ = e^i ∂_i (sum over basis vectors)
19//! ```
20//!
21//! This operator combines:
22//! - Dot product → divergence (∇·F)
23//! - Wedge product → curl (∇∧F)
24//! - Full geometric product → complete derivative (∇F = ∇·F + ∇∧F)
25//!
26//! ## Key Features
27//!
28//! - **Vector Derivative Operator**: The fundamental ∇ operator
29//! - **Classical Operators**: Gradient, divergence, curl, Laplacian
30//! - **Manifold Calculus**: Covariant derivatives, connections, geodesics
31//! - **Lie Derivatives**: Derivatives along vector fields
32//! - **Integration**: Integration on manifolds using amari-measure
33//! - **Fundamental Theorem**: ∫_V (∇F) dV = ∮_∂V F dS
34//!
35//! ## Examples
36//!
37//! ### Gradient of a scalar field
38//!
39//! ```rust
40//! use amari_calculus::{ScalarField, VectorDerivative, CoordinateSystem};
41//! use amari_core::Multivector;
42//!
43//! // Define scalar field f(x, y) = x² + y²
44//! let f = ScalarField::<3, 0, 0>::new(|coords| {
45//! coords[0].powi(2) + coords[1].powi(2)
46//! });
47//!
48//! // Create vector derivative operator
49//! let nabla = VectorDerivative::<3, 0, 0>::new(CoordinateSystem::Cartesian);
50//!
51//! // Compute gradient at point (1, 2)
52//! let grad_f = nabla.gradient(&f, &[1.0, 2.0, 0.0]);
53//!
54//! // Gradient should be approximately (2, 4, 0)
55//! ```
56//!
57//! ### Divergence of a vector field
58//!
59//! ```rust
60//! use amari_calculus::{VectorField, VectorDerivative, CoordinateSystem, vector_from_slice};
61//!
62//! // Define vector field F(x, y, z) = (x, y, z)
63//! let f = VectorField::<3, 0, 0>::new(|coords| {
64//! vector_from_slice(&[coords[0], coords[1], coords[2]])
65//! });
66//!
67//! let nabla = VectorDerivative::<3, 0, 0>::new(CoordinateSystem::Cartesian);
68//!
69//! // Compute divergence (should be 3)
70//! let div_f = nabla.divergence(&f, &[1.0, 1.0, 1.0]);
71//! ```
72//!
73//! ### Curl of a vector field
74//!
75//! ```rust
76//! use amari_calculus::{VectorField, VectorDerivative, CoordinateSystem, vector_from_slice};
77//!
78//! // Define vector field F(x, y, z) = (-y, x, 0) (rotation around z-axis)
79//! let f = VectorField::<3, 0, 0>::new(|coords| {
80//! vector_from_slice(&[-coords[1], coords[0], 0.0])
81//! });
82//!
83//! let nabla = VectorDerivative::<3, 0, 0>::new(CoordinateSystem::Cartesian);
84//!
85//! // Compute curl (should be (0, 0, 2) bivector representing rotation)
86//! let curl_f = nabla.curl(&f, &[0.0, 0.0, 0.0]);
87//! ```
88
89#![cfg_attr(not(feature = "std"), no_std)]
90
91// Error handling
92mod error;
93pub use error::*;
94
95// Core derivative types
96mod derivative;
97pub use derivative::*;
98
99// Field types
100pub mod fields;
101pub use fields::{MultivectorField, ScalarField, VectorField};
102
103// Classical differential operators
104pub mod operators;
105pub use operators::{curl, divergence, gradient, laplacian};
106
107// Covariant derivatives on manifolds
108pub mod manifold;
109pub use manifold::RiemannianManifold;
110
111// Lie derivatives
112mod lie;
113pub use lie::*;
114
115// Integration on manifolds
116mod integration;
117pub use integration::ManifoldIntegrator;
118
119/// Utility function to create a vector multivector from a slice of components
120///
121/// # Arguments
122///
123/// * `components` - Slice of f64 values representing vector components
124///
125/// # Returns
126///
127/// A Multivector containing the vector (grade-1 elements only)
128pub fn vector_from_slice<const P: usize, const Q: usize, const R: usize>(
129 components: &[f64],
130) -> amari_core::Multivector<P, Q, R> {
131 let mut mv = amari_core::Multivector::zero();
132 let dim = P + Q + R;
133 for (i, &val) in components.iter().enumerate() {
134 if i < dim {
135 mv.set_vector_component(i, val);
136 }
137 }
138 mv
139}
140
141/// Coordinate systems for differential operators
142#[derive(Debug, Clone, Copy, PartialEq, Eq)]
143pub enum CoordinateSystem {
144 /// Cartesian coordinates (x, y, z, ...)
145 Cartesian,
146 /// Spherical coordinates (r, θ, φ)
147 Spherical,
148 /// Cylindrical coordinates (ρ, φ, z)
149 Cylindrical,
150 /// Polar coordinates (r, θ) - 2D only
151 Polar,
152}
153
154/// Prelude module for convenient imports
155pub mod prelude {
156 pub use crate::fields::{MultivectorField, ScalarField, VectorField};
157 pub use crate::operators::{curl, divergence, gradient, laplacian};
158 pub use crate::CoordinateSystem;
159 pub use crate::VectorDerivative;
160}
161
162#[cfg(test)]
163mod tests {
164 use super::*;
165
166 #[test]
167 fn test_crate_compiles() {
168 // Basic smoke test to ensure crate compiles
169 let _coords = CoordinateSystem::Cartesian;
170 }
171}