arael-sym 0.5.2

Symbolic math library: expression trees, automatic differentiation, simplification, CSE, code generation
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::collections::HashMap;
use std::fmt;
use std::ops;
use super::{AsVarName, E, constant};

// ============================================================
// SymVec
// ============================================================

/// Symbolic vector of expressions.
///
/// Supports element-wise arithmetic, dot product, differentiation,
/// evaluation, and code generation. Indexing is zero-based.
#[derive(Debug, Clone, PartialEq)]
pub struct SymVec(pub Vec<E>);

impl SymVec {
    /// Create a symbolic vector from a list of expressions. Accepts
    /// any iterable of `Into<E>` so you can pass bare numeric
    /// literals: `SymVec::new([1.0, 2.0, 3.0])`.
    pub fn new<I>(elems: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<E>,
    {
        SymVec(elems.into_iter().map(Into::into).collect())
    }

    /// Return the number of elements.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Return true if the vector has no elements.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Get a reference to the element at index `i`.
    pub fn get(&self, i: usize) -> &E {
        &self.0[i]
    }

    /// Compute the dot product with another symbolic vector.
    pub fn dot(&self, other: &SymVec) -> E {
        assert_eq!(self.len(), other.len(), "dot product: length mismatch");
        let mut terms: Vec<E> = Vec::with_capacity(self.len());
        for i in 0..self.len() {
            terms.push(self.0[i].clone() * other.0[i].clone());
        }
        terms.into_iter().reduce(|a, b| a + b).unwrap_or_else(|| constant(0.0))
    }

    /// Differentiate each element with respect to a variable.
    pub fn diff(&self, var: impl AsVarName) -> SymVec {
        let v = var.var_name();
        SymVec(self.0.iter().map(|e| e.diff(v)).collect())
    }

    /// Evaluate each element numerically given variable bindings.
    pub fn eval(&self, vars: &HashMap<&str, f64>) -> Result<Vec<f64>, String> {
        self.0.iter().map(|e| e.eval(vars)).collect()
    }

    /// Simplify each element.
    pub fn simplify(&self) -> SymVec {
        SymVec(self.0.iter().map(|e| e.simplify()).collect())
    }

    /// Expand each element (distribute products over sums).
    pub fn expand(&self) -> SymVec {
        SymVec(self.0.iter().map(|e| e.expand()).collect())
    }

    /// Substitute a variable in each element.
    pub fn subs(&self, var: impl AsVarName, replacement: &E) -> SymVec {
        let name = var.var_name();
        SymVec(self.0.iter().map(|e| e.subs(name, replacement)).collect())
    }

    /// Format the vector as a LaTeX column vector (pmatrix).
    pub fn to_latex(&self) -> String {
        let mut buf = String::from("\\begin{pmatrix} ");
        for (i, e) in self.0.iter().enumerate() {
            if i > 0 { buf.push_str(" \\\\ "); }
            buf.push_str(&e.to_latex());
        }
        buf.push_str(" \\end{pmatrix}");
        buf
    }

    /// Generate Rust source code for the vector as a literal array.
    pub fn to_rust(&self, ft: &str) -> String {
        let mut buf = String::from("[");
        for (i, e) in self.0.iter().enumerate() {
            if i > 0 { buf.push_str(", "); }
            buf.push_str(&e.to_rust(ft));
        }
        buf.push(']');
        buf
    }
}

impl ops::Index<usize> for SymVec {
    type Output = E;
    fn index(&self, i: usize) -> &E {
        &self.0[i]
    }
}

impl ops::Add for SymVec {
    type Output = SymVec;
    fn add(self, rhs: SymVec) -> SymVec {
        assert_eq!(self.len(), rhs.len(), "SymVec add: length mismatch");
        SymVec(
            self.0.into_iter().zip(rhs.0)
                .map(|(a, b)| a + b)
                .collect()
        )
    }
}

impl ops::Mul<E> for SymVec {
    type Output = SymVec;
    fn mul(self, rhs: E) -> SymVec {
        SymVec(self.0.into_iter().map(|e| e * rhs.clone()).collect())
    }
}

impl ops::Mul<SymVec> for E {
    type Output = SymVec;
    fn mul(self, rhs: SymVec) -> SymVec {
        SymVec(rhs.0.into_iter().map(|e| self.clone() * e).collect())
    }
}

impl fmt::Display for SymVec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[")?;
        for (i, e) in self.0.iter().enumerate() {
            if i > 0 { write!(f, ", ")?; }
            fmt::Display::fmt(e, f)?;
        }
        write!(f, "]")
    }
}

// ============================================================
// SymMat
// ============================================================

/// Symbolic matrix of expressions.
///
/// Stored in row-major order. Supports transpose, matrix multiplication,
/// matrix-vector product, element-wise differentiation, and code generation.
#[derive(Debug, Clone, PartialEq)]
pub struct SymMat {
    /// Number of rows.
    pub rows: usize,
    /// Number of columns.
    pub cols: usize,
    /// Row-major element data.
    pub data: Vec<E>,
}

impl SymMat {
    /// Create a matrix from dimensions and row-major data. Accepts
    /// any iterable of `Into<E>` so you can pass bare numeric literals:
    /// `SymMat::new(2, 2, [1.0, 2.0, 3.0, 4.0])`.
    pub fn new<I>(rows: usize, cols: usize, data: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<E>,
    {
        let data: Vec<E> = data.into_iter().map(Into::into).collect();
        assert_eq!(data.len(), rows * cols, "SymMat::new: data size mismatch");
        SymMat { rows, cols, data }
    }

    /// Create a zero matrix of the given dimensions.
    pub fn zeros(rows: usize, cols: usize) -> Self {
        SymMat {
            rows,
            cols,
            data: vec![constant(0.0); rows * cols],
        }
    }

    /// Create an n-by-n identity matrix.
    pub fn identity(n: usize) -> Self {
        let mut data = vec![constant(0.0); n * n];
        for i in 0..n {
            data[i * n + i] = constant(1.0);
        }
        SymMat { rows: n, cols: n, data }
    }

    /// Get a reference to the element at row `i`, column `j`.
    pub fn get(&self, i: usize, j: usize) -> &E {
        &self.data[i * self.cols + j]
    }

    /// Set the element at row `i`, column `j`.
    pub fn set(&mut self, i: usize, j: usize, val: E) {
        self.data[i * self.cols + j] = val;
    }

    /// Return the transpose of this matrix.
    pub fn transpose(&self) -> SymMat {
        let mut data = Vec::with_capacity(self.rows * self.cols);
        for j in 0..self.cols {
            for i in 0..self.rows {
                data.push(self.get(i, j).clone());
            }
        }
        SymMat { rows: self.cols, cols: self.rows, data }
    }

    /// Differentiate every element with respect to a variable.
    pub fn diff(&self, var: impl AsVarName) -> SymMat {
        let v = var.var_name();
        SymMat {
            rows: self.rows,
            cols: self.cols,
            data: self.data.iter().map(|e| e.diff(v)).collect(),
        }
    }

    /// Evaluate every element numerically, returning a nested `Vec<Vec<f64>>`.
    pub fn eval(&self, vars: &HashMap<&str, f64>) -> Result<Vec<Vec<f64>>, String> {
        let mut result = Vec::with_capacity(self.rows);
        for i in 0..self.rows {
            let mut row = Vec::with_capacity(self.cols);
            for j in 0..self.cols {
                row.push(self.get(i, j).eval(vars)?);
            }
            result.push(row);
        }
        Ok(result)
    }

    /// Simplify every element.
    pub fn simplify(&self) -> SymMat {
        SymMat {
            rows: self.rows,
            cols: self.cols,
            data: self.data.iter().map(|e| e.simplify()).collect(),
        }
    }

    /// Expand every element (distribute products over sums).
    pub fn expand(&self) -> SymMat {
        SymMat {
            rows: self.rows,
            cols: self.cols,
            data: self.data.iter().map(|e| e.expand()).collect(),
        }
    }

    /// Substitute a variable in every element.
    pub fn subs(&self, var: impl AsVarName, replacement: &E) -> SymMat {
        let name = var.var_name();
        SymMat {
            rows: self.rows,
            cols: self.cols,
            data: self.data.iter().map(|e| e.subs(name, replacement)).collect(),
        }
    }

    /// Format the matrix as a LaTeX pmatrix.
    pub fn to_latex(&self) -> String {
        let mut buf = String::from("\\begin{pmatrix} ");
        for i in 0..self.rows {
            if i > 0 { buf.push_str(" \\\\ "); }
            for j in 0..self.cols {
                if j > 0 { buf.push_str(" & "); }
                buf.push_str(&self.get(i, j).to_latex());
            }
        }
        buf.push_str(" \\end{pmatrix}");
        buf
    }

    /// Generate Rust source code for the matrix as a nested array literal.
    pub fn to_rust(&self, ft: &str) -> String {
        let mut buf = String::from("[");
        for i in 0..self.rows {
            if i > 0 { buf.push_str(", "); }
            buf.push('[');
            for j in 0..self.cols {
                if j > 0 { buf.push_str(", "); }
                buf.push_str(&self.get(i, j).to_rust(ft));
            }
            buf.push(']');
        }
        buf.push(']');
        buf
    }
}

// SymMat + SymMat
impl ops::Add for SymMat {
    type Output = SymMat;
    fn add(self, rhs: SymMat) -> SymMat {
        assert_eq!((self.rows, self.cols), (rhs.rows, rhs.cols), "SymMat add: dimension mismatch");
        SymMat {
            rows: self.rows,
            cols: self.cols,
            data: self.data.into_iter().zip(rhs.data)
                .map(|(a, b)| a + b)
                .collect(),
        }
    }
}

// SymMat * SymMat
impl ops::Mul for SymMat {
    type Output = SymMat;
    fn mul(self, rhs: SymMat) -> SymMat {
        assert_eq!(self.cols, rhs.rows, "SymMat mul: dimension mismatch");
        let mut data = Vec::with_capacity(self.rows * rhs.cols);
        for i in 0..self.rows {
            for j in 0..rhs.cols {
                let mut sum: Option<E> = None;
                for k in 0..self.cols {
                    let prod = self.get(i, k).clone() * rhs.get(k, j).clone();
                    sum = Some(match sum {
                        Some(acc) => acc + prod,
                        None => prod,
                    });
                }
                data.push(sum.unwrap_or_else(|| constant(0.0)));
            }
        }
        SymMat { rows: self.rows, cols: rhs.cols, data }
    }
}

// SymMat * SymVec
impl ops::Mul<SymVec> for SymMat {
    type Output = SymVec;
    fn mul(self, rhs: SymVec) -> SymVec {
        assert_eq!(self.cols, rhs.len(), "SymMat * SymVec: dimension mismatch");
        let mut result = Vec::with_capacity(self.rows);
        for i in 0..self.rows {
            let mut sum: Option<E> = None;
            for j in 0..self.cols {
                let prod = self.get(i, j).clone() * rhs[j].clone();
                sum = Some(match sum {
                    Some(acc) => acc + prod,
                    None => prod,
                });
            }
            result.push(sum.unwrap_or_else(|| constant(0.0)));
        }
        SymVec(result)
    }
}

// SymMat * E (scalar)
impl ops::Mul<E> for SymMat {
    type Output = SymMat;
    fn mul(self, rhs: E) -> SymMat {
        SymMat {
            rows: self.rows,
            cols: self.cols,
            data: self.data.into_iter().map(|e| e * rhs.clone()).collect(),
        }
    }
}

// E * SymMat (scalar)
impl ops::Mul<SymMat> for E {
    type Output = SymMat;
    fn mul(self, rhs: SymMat) -> SymMat {
        SymMat {
            rows: rhs.rows,
            cols: rhs.cols,
            data: rhs.data.into_iter().map(|e| self.clone() * e).collect(),
        }
    }
}

impl fmt::Display for SymMat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[")?;
        for i in 0..self.rows {
            if i > 0 { write!(f, "; ")?; }
            for j in 0..self.cols {
                if j > 0 { write!(f, ", ")?; }
                fmt::Display::fmt(self.get(i, j), f)?;
            }
        }
        write!(f, "]")
    }
}

// ============================================================
// Jacobian
// ============================================================

/// Compute the Jacobian matrix: partial derivatives of each expression with
/// respect to each variable.
///
/// Returns a [`SymMat`] with `exprs.len()` rows and `vars.len()` columns,
/// where element (i, j) is `d(exprs[i]) / d(vars[j])`.
pub fn jacobian(exprs: &[E], vars: &[&str]) -> SymMat {
    let rows = exprs.len();
    let cols = vars.len();
    let mut data = Vec::with_capacity(rows * cols);
    for expr in exprs {
        for var in vars {
            data.push(expr.diff(var));
        }
    }
    SymMat { rows, cols, data }
}