feffit 0.1.0

Pure-Rust EXAFS toolkit — data reduction (pre-edge/normalize/AUTOBK), Fourier transforms, FEFF path fitting (feffit), and feff.inp build/run; a port of larch.xafs
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! A small arithmetic expression parser/evaluator covering the subset of
//! `asteval` (the engine `lmfit` uses for constraint expressions) that EXAFS
//! fits need: numeric literals, named variables, the binary operators
//! `+ - * / % **`, unary `+`/`-`, parentheses, and the common math functions.
//!
//! Semantics are matched to asteval/numpy where they differ from naive parsing:
//! `**` is right-associative and binds tighter than unary minus (so `-2**2`
//! is `-4` and `2**-2` is `0.25`), and `log` is the natural logarithm.
//!
//! Not supported (deliberately): comparisons, boolean/conditional expressions,
//! indexing, attribute access, and the EXAFS `sigma2_debye`/`sigma2_eins`
//! helpers (those need their own port).

use std::collections::HashMap;
use std::f64::consts::{E, PI};
use std::fmt;

/// An error from tokenizing, parsing, or evaluating an expression.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExprError {
    /// A character the tokenizer does not recognise.
    BadChar(char),
    /// A malformed number literal.
    BadNumber(String),
    /// The parser reached an unexpected token (or end of input).
    Parse(String),
    /// A variable name not present in the symbol table at eval time.
    UnknownVar(String),
    /// A function name that is not supported.
    UnknownFunc(String),
    /// A supported function called with the wrong number of arguments.
    Arity(String),
}

impl fmt::Display for ExprError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExprError::BadChar(c) => write!(f, "unexpected character '{c}'"),
            ExprError::BadNumber(s) => write!(f, "invalid number '{s}'"),
            ExprError::Parse(s) => write!(f, "parse error: {s}"),
            ExprError::UnknownVar(s) => write!(f, "unknown variable '{s}'"),
            ExprError::UnknownFunc(s) => write!(f, "unknown function '{s}'"),
            ExprError::Arity(s) => write!(f, "wrong number of arguments to '{s}'"),
        }
    }
}

impl std::error::Error for ExprError {}

#[derive(Debug, Clone, PartialEq)]
enum Tok {
    Num(f64),
    Ident(String),
    Plus,
    Minus,
    Star,
    Slash,
    Percent,
    Pow,
    LParen,
    RParen,
    Comma,
}

fn tokenize(s: &str) -> Result<Vec<Tok>, ExprError> {
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;
    let mut out = Vec::new();
    while i < chars.len() {
        let c = chars[i];
        if c.is_whitespace() {
            i += 1;
        } else if c.is_ascii_digit()
            || (c == '.' && i + 1 < chars.len() && chars[i + 1].is_ascii_digit())
        {
            let start = i;
            while i < chars.len() && (chars[i].is_ascii_digit() || chars[i] == '.') {
                i += 1;
            }
            // exponent part: e / E [+/-] digits
            if i < chars.len() && (chars[i] == 'e' || chars[i] == 'E') {
                i += 1;
                if i < chars.len() && (chars[i] == '+' || chars[i] == '-') {
                    i += 1;
                }
                while i < chars.len() && chars[i].is_ascii_digit() {
                    i += 1;
                }
            }
            let lit: String = chars[start..i].iter().collect();
            let val: f64 = lit.parse().map_err(|_| ExprError::BadNumber(lit.clone()))?;
            out.push(Tok::Num(val));
        } else if c.is_alphabetic() || c == '_' {
            let start = i;
            while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
                i += 1;
            }
            out.push(Tok::Ident(chars[start..i].iter().collect()));
        } else {
            match c {
                '+' => out.push(Tok::Plus),
                '-' => out.push(Tok::Minus),
                '*' => {
                    if i + 1 < chars.len() && chars[i + 1] == '*' {
                        out.push(Tok::Pow);
                        i += 1;
                    } else {
                        out.push(Tok::Star);
                    }
                }
                '/' => out.push(Tok::Slash),
                '%' => out.push(Tok::Percent),
                '(' => out.push(Tok::LParen),
                ')' => out.push(Tok::RParen),
                ',' => out.push(Tok::Comma),
                _ => return Err(ExprError::BadChar(c)),
            }
            i += 1;
        }
    }
    Ok(out)
}

/// A parsed expression (abstract syntax tree).
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
    Num(f64),
    Var(String),
    Neg(Box<Expr>),
    Bin(BinOp, Box<Expr>, Box<Expr>),
    Call(String, Vec<Expr>),
}

/// A binary operator in an [`Expr`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinOp {
    Add,
    Sub,
    Mul,
    Div,
    Rem,
    Pow,
}

struct Parser {
    toks: Vec<Tok>,
    pos: usize,
}

impl Parser {
    fn peek(&self) -> Option<&Tok> {
        self.toks.get(self.pos)
    }
    fn next(&mut self) -> Option<Tok> {
        let t = self.toks.get(self.pos).cloned();
        if t.is_some() {
            self.pos += 1;
        }
        t
    }
    fn expect(&mut self, t: &Tok) -> Result<(), ExprError> {
        match self.next() {
            Some(ref got) if got == t => Ok(()),
            other => Err(ExprError::Parse(format!("expected {t:?}, found {other:?}"))),
        }
    }

    // expr := add
    fn parse_expr(&mut self) -> Result<Expr, ExprError> {
        self.parse_add()
    }

    // add := mul (('+'|'-') mul)*
    fn parse_add(&mut self) -> Result<Expr, ExprError> {
        let mut lhs = self.parse_mul()?;
        while let Some(op) = match self.peek() {
            Some(Tok::Plus) => Some(BinOp::Add),
            Some(Tok::Minus) => Some(BinOp::Sub),
            _ => None,
        } {
            self.next();
            let rhs = self.parse_mul()?;
            lhs = Expr::Bin(op, Box::new(lhs), Box::new(rhs));
        }
        Ok(lhs)
    }

    // mul := unary (('*'|'/'|'%') unary)*
    fn parse_mul(&mut self) -> Result<Expr, ExprError> {
        let mut lhs = self.parse_unary()?;
        while let Some(op) = match self.peek() {
            Some(Tok::Star) => Some(BinOp::Mul),
            Some(Tok::Slash) => Some(BinOp::Div),
            Some(Tok::Percent) => Some(BinOp::Rem),
            _ => None,
        } {
            self.next();
            let rhs = self.parse_unary()?;
            lhs = Expr::Bin(op, Box::new(lhs), Box::new(rhs));
        }
        Ok(lhs)
    }

    // unary := ('+'|'-') unary | power
    // (so that ** binds tighter than a leading unary minus: -2**2 == -4)
    fn parse_unary(&mut self) -> Result<Expr, ExprError> {
        match self.peek() {
            Some(Tok::Plus) => {
                self.next();
                self.parse_unary()
            }
            Some(Tok::Minus) => {
                self.next();
                Ok(Expr::Neg(Box::new(self.parse_unary()?)))
            }
            _ => self.parse_power(),
        }
    }

    // power := atom ('**' unary)?   (right-associative; RHS may be unary)
    fn parse_power(&mut self) -> Result<Expr, ExprError> {
        let base = self.parse_atom()?;
        if let Some(Tok::Pow) = self.peek() {
            self.next();
            let exp = self.parse_unary()?;
            Ok(Expr::Bin(BinOp::Pow, Box::new(base), Box::new(exp)))
        } else {
            Ok(base)
        }
    }

    // atom := number | name | name '(' args ')' | '(' expr ')'
    fn parse_atom(&mut self) -> Result<Expr, ExprError> {
        match self.next() {
            Some(Tok::Num(v)) => Ok(Expr::Num(v)),
            Some(Tok::LParen) => {
                let e = self.parse_expr()?;
                self.expect(&Tok::RParen)?;
                Ok(e)
            }
            Some(Tok::Ident(name)) => {
                if let Some(Tok::LParen) = self.peek() {
                    self.next();
                    let mut args = Vec::new();
                    if self.peek() != Some(&Tok::RParen) {
                        loop {
                            args.push(self.parse_expr()?);
                            match self.peek() {
                                Some(Tok::Comma) => {
                                    self.next();
                                }
                                _ => break,
                            }
                        }
                    }
                    self.expect(&Tok::RParen)?;
                    Ok(Expr::Call(name, args))
                } else {
                    Ok(Expr::Var(name))
                }
            }
            other => Err(ExprError::Parse(format!("unexpected {other:?}"))),
        }
    }
}

/// Parse an expression string into an [`Expr`] AST.
pub fn parse(s: &str) -> Result<Expr, ExprError> {
    let toks = tokenize(s)?;
    let mut p = Parser { toks, pos: 0 };
    let e = p.parse_expr()?;
    if p.pos != p.toks.len() {
        return Err(ExprError::Parse(format!(
            "trailing tokens after expression: {:?}",
            &p.toks[p.pos..]
        )));
    }
    Ok(e)
}

/// Resolver for application-specific functions not built into the evaluator.
///
/// feffit uses this to expose `sigma2_eins`/`sigma2_debye`, which close over a
/// Feff path's geometry. [`Expr::eval_ctx`] consults `call` before the built-in
/// functions; returning `None` means "not my function — fall through to the
/// built-ins" (and then to [`ExprError::UnknownFunc`]).
pub trait FuncCtx {
    /// Evaluate function `name` with already-evaluated argument values, or
    /// `None` if this resolver does not handle `name`.
    fn call(&self, name: &str, args: &[f64]) -> Option<Result<f64, ExprError>>;
}

/// A [`FuncCtx`] that resolves nothing: only the built-in functions apply.
pub struct NoCtx;

impl FuncCtx for NoCtx {
    fn call(&self, _name: &str, _args: &[f64]) -> Option<Result<f64, ExprError>> {
        None
    }
}

impl Expr {
    /// Collect the variable names referenced by this expression (for dependency
    /// resolution). Function names are not included.
    pub fn vars(&self, out: &mut Vec<String>) {
        match self {
            Expr::Num(_) => {}
            Expr::Var(n) => out.push(n.clone()),
            Expr::Neg(e) => e.vars(out),
            Expr::Bin(_, a, b) => {
                a.vars(out);
                b.vars(out);
            }
            Expr::Call(_, args) => {
                for a in args {
                    a.vars(out);
                }
            }
        }
    }

    /// Evaluate against a symbol table. Built-in constants `pi`/`e` are used as
    /// fallbacks only when not shadowed by the table.
    pub fn eval(&self, sym: &HashMap<String, f64>) -> Result<f64, ExprError> {
        self.eval_ctx(sym, &NoCtx)
    }

    /// Like [`Expr::eval`], but `ctx` may resolve application-specific functions
    /// (e.g. feffit's `sigma2_eins`/`sigma2_debye`) before the built-ins.
    pub fn eval_ctx(
        &self,
        sym: &HashMap<String, f64>,
        ctx: &dyn FuncCtx,
    ) -> Result<f64, ExprError> {
        match self {
            Expr::Num(v) => Ok(*v),
            Expr::Var(n) => {
                if let Some(v) = sym.get(n) {
                    Ok(*v)
                } else {
                    // built-in constants, used only when not shadowed by the table
                    match n.as_str() {
                        "pi" => Ok(PI),
                        "e" => Ok(E),
                        _ => Err(ExprError::UnknownVar(n.clone())),
                    }
                }
            }
            Expr::Neg(e) => Ok(-e.eval_ctx(sym, ctx)?),
            Expr::Bin(op, a, b) => {
                let x = a.eval_ctx(sym, ctx)?;
                let y = b.eval_ctx(sym, ctx)?;
                Ok(match op {
                    BinOp::Add => x + y,
                    BinOp::Sub => x - y,
                    BinOp::Mul => x * y,
                    BinOp::Div => x / y,
                    // Python/numpy float `%`: result takes the sign of the divisor
                    BinOp::Rem => x - y * (x / y).floor(),
                    BinOp::Pow => x.powf(y),
                })
            }
            Expr::Call(name, args) => {
                let vals: Result<Vec<f64>, _> = args.iter().map(|a| a.eval_ctx(sym, ctx)).collect();
                let vals = vals?;
                if let Some(r) = ctx.call(name, &vals) {
                    return r;
                }
                call_func(name, &vals)
            }
        }
    }

    /// Forward-mode automatic differentiation: evaluate the expression and its
    /// gradient with respect to a basis of `nvar` variables. `sym` holds each
    /// symbol's value; `grads` holds the gradient vector for symbols that depend
    /// on the basis (a symbol absent from `grads` is treated as a constant, with
    /// zero gradient). This is the first-order error propagation larch performs
    /// with the `uncertainties` package: `stderr(f) = sqrt(gᵀ C g)`.
    pub fn eval_dual(
        &self,
        sym: &HashMap<String, f64>,
        grads: &HashMap<String, Vec<f64>>,
        nvar: usize,
    ) -> Result<(f64, Vec<f64>), ExprError> {
        self.eval_dual_ctx(sym, grads, nvar, &NoCtx)
    }

    /// Like [`Expr::eval_dual`], but `ctx` may resolve application-specific
    /// functions. Their partial derivatives are taken numerically (central
    /// differences), matching how larch's `uncertainties` package propagates
    /// through opaque functions such as `sigma2_eins`/`sigma2_debye`.
    pub fn eval_dual_ctx(
        &self,
        sym: &HashMap<String, f64>,
        grads: &HashMap<String, Vec<f64>>,
        nvar: usize,
        ctx: &dyn FuncCtx,
    ) -> Result<(f64, Vec<f64>), ExprError> {
        match self {
            Expr::Num(v) => Ok((*v, vec![0.0; nvar])),
            Expr::Var(n) => {
                let v = if let Some(v) = sym.get(n) {
                    *v
                } else {
                    match n.as_str() {
                        "pi" => PI,
                        "e" => E,
                        _ => return Err(ExprError::UnknownVar(n.clone())),
                    }
                };
                let g = grads.get(n).cloned().unwrap_or_else(|| vec![0.0; nvar]);
                Ok((v, g))
            }
            Expr::Neg(e) => {
                let (v, g) = e.eval_dual_ctx(sym, grads, nvar, ctx)?;
                Ok((-v, g.iter().map(|x| -x).collect()))
            }
            Expr::Bin(op, a, b) => {
                let (x, xg) = a.eval_dual_ctx(sym, grads, nvar, ctx)?;
                let (y, yg) = b.eval_dual_ctx(sym, grads, nvar, ctx)?;
                let g: Vec<f64> = match op {
                    BinOp::Add => (0..nvar).map(|i| xg[i] + yg[i]).collect(),
                    BinOp::Sub => (0..nvar).map(|i| xg[i] - yg[i]).collect(),
                    BinOp::Mul => (0..nvar).map(|i| x * yg[i] + y * xg[i]).collect(),
                    BinOp::Div => (0..nvar)
                        .map(|i| (xg[i] * y - x * yg[i]) / (y * y))
                        .collect(),
                    // d/d of (x - y*floor(x/y)): floor is locally constant
                    BinOp::Rem => {
                        let fl = (x / y).floor();
                        (0..nvar).map(|i| xg[i] - fl * yg[i]).collect()
                    }
                    // d(x^y) = y*x^(y-1) dx + x^y*ln(x) dy; the ln term only
                    // applies where the exponent itself varies (yg != 0)
                    BinOp::Pow => {
                        let val = x.powf(y);
                        let d_dx = y * x.powf(y - 1.0);
                        let lnx = if x > 0.0 { x.ln() } else { 0.0 };
                        (0..nvar)
                            .map(|i| {
                                let mut gi = d_dx * xg[i];
                                if yg[i] != 0.0 {
                                    gi += val * lnx * yg[i];
                                }
                                gi
                            })
                            .collect()
                    }
                };
                let v = match op {
                    BinOp::Add => x + y,
                    BinOp::Sub => x - y,
                    BinOp::Mul => x * y,
                    BinOp::Div => x / y,
                    BinOp::Rem => x - y * (x / y).floor(),
                    BinOp::Pow => x.powf(y),
                };
                Ok((v, g))
            }
            Expr::Call(name, args) => {
                let evaled: Result<Vec<(f64, Vec<f64>)>, _> = args
                    .iter()
                    .map(|a| a.eval_dual_ctx(sym, grads, nvar, ctx))
                    .collect();
                let evaled = evaled?;
                let vals: Vec<f64> = evaled.iter().map(|(v, _)| *v).collect();
                if let Some(r) = ctx.call(name, &vals) {
                    let fval = r?;
                    // Context functions are opaque: take partials numerically by
                    // central difference, then chain through each argument's
                    // gradient. Mirrors larch's `uncertainties` propagation
                    // through `sigma2_eins`/`sigma2_debye`.
                    let mut g = vec![0.0; nvar];
                    for (i, (ai, ai_grad)) in evaled.iter().enumerate() {
                        if ai_grad.iter().all(|&d| d == 0.0) {
                            continue; // argument does not vary -> no contribution
                        }
                        let h = 1.0e-6 * ai.abs().max(1.0);
                        let mut ap = vals.clone();
                        let mut am = vals.clone();
                        ap[i] = ai + h;
                        am[i] = ai - h;
                        let unknown = || ExprError::UnknownFunc(name.clone());
                        let fp = ctx.call(name, &ap).ok_or_else(unknown)??;
                        let fm = ctx.call(name, &am).ok_or_else(unknown)??;
                        let dfi = (fp - fm) / (2.0 * h);
                        for (gk, &gik) in g.iter_mut().zip(ai_grad.iter()) {
                            *gk += dfi * gik;
                        }
                    }
                    return Ok((fval, g));
                }
                call_func_dual(name, &evaled, nvar)
            }
        }
    }
}

fn call_func(name: &str, a: &[f64]) -> Result<f64, ExprError> {
    let one = || -> Result<f64, ExprError> {
        if a.len() == 1 {
            Ok(a[0])
        } else {
            Err(ExprError::Arity(name.to_string()))
        }
    };
    let two = || -> Result<(f64, f64), ExprError> {
        if a.len() == 2 {
            Ok((a[0], a[1]))
        } else {
            Err(ExprError::Arity(name.to_string()))
        }
    };
    Ok(match name {
        "sin" => one()?.sin(),
        "cos" => one()?.cos(),
        "tan" => one()?.tan(),
        "asin" | "arcsin" => one()?.asin(),
        "acos" | "arccos" => one()?.acos(),
        "atan" | "arctan" => one()?.atan(),
        "sinh" => one()?.sinh(),
        "cosh" => one()?.cosh(),
        "tanh" => one()?.tanh(),
        "exp" => one()?.exp(),
        "log" | "ln" => one()?.ln(),
        "log10" => one()?.log10(),
        "sqrt" => one()?.sqrt(),
        "abs" | "fabs" => one()?.abs(),
        "floor" => one()?.floor(),
        "ceil" => one()?.ceil(),
        "atan2" | "arctan2" => {
            let (y, x) = two()?;
            y.atan2(x)
        }
        "pow" => {
            let (x, y) = two()?;
            x.powf(y)
        }
        "min" => {
            if a.is_empty() {
                return Err(ExprError::Arity(name.to_string()));
            }
            a.iter().copied().fold(f64::INFINITY, f64::min)
        }
        "max" => {
            if a.is_empty() {
                return Err(ExprError::Arity(name.to_string()));
            }
            a.iter().copied().fold(f64::NEG_INFINITY, f64::max)
        }
        _ => return Err(ExprError::UnknownFunc(name.to_string())),
    })
}

/// Forward-mode AD companion to [`call_func`]: returns the function value and
/// its gradient given each argument's `(value, gradient)`.
fn call_func_dual(
    name: &str,
    a: &[(f64, Vec<f64>)],
    nvar: usize,
) -> Result<(f64, Vec<f64>), ExprError> {
    // chain rule for a single-argument function f: g = f'(u) * ug
    let unary = |fval: f64, fprime: f64, ug: &[f64]| -> (f64, Vec<f64>) {
        (fval, ug.iter().map(|d| fprime * d).collect())
    };
    let need1 = || -> Result<(f64, &Vec<f64>), ExprError> {
        if a.len() == 1 {
            Ok((a[0].0, &a[0].1))
        } else {
            Err(ExprError::Arity(name.to_string()))
        }
    };
    Ok(match name {
        "sin" => {
            let (u, ug) = need1()?;
            unary(u.sin(), u.cos(), ug)
        }
        "cos" => {
            let (u, ug) = need1()?;
            unary(u.cos(), -u.sin(), ug)
        }
        "tan" => {
            let (u, ug) = need1()?;
            let t = u.tan();
            unary(t, 1.0 + t * t, ug)
        }
        "asin" | "arcsin" => {
            let (u, ug) = need1()?;
            unary(u.asin(), 1.0 / (1.0 - u * u).sqrt(), ug)
        }
        "acos" | "arccos" => {
            let (u, ug) = need1()?;
            unary(u.acos(), -1.0 / (1.0 - u * u).sqrt(), ug)
        }
        "atan" | "arctan" => {
            let (u, ug) = need1()?;
            unary(u.atan(), 1.0 / (1.0 + u * u), ug)
        }
        "sinh" => {
            let (u, ug) = need1()?;
            unary(u.sinh(), u.cosh(), ug)
        }
        "cosh" => {
            let (u, ug) = need1()?;
            unary(u.cosh(), u.sinh(), ug)
        }
        "tanh" => {
            let (u, ug) = need1()?;
            let t = u.tanh();
            unary(t, 1.0 - t * t, ug)
        }
        "exp" => {
            let (u, ug) = need1()?;
            let ex = u.exp();
            unary(ex, ex, ug)
        }
        "log" | "ln" => {
            let (u, ug) = need1()?;
            unary(u.ln(), 1.0 / u, ug)
        }
        "log10" => {
            let (u, ug) = need1()?;
            unary(u.log10(), 1.0 / (u * std::f64::consts::LN_10), ug)
        }
        "sqrt" => {
            let (u, ug) = need1()?;
            let s = u.sqrt();
            unary(s, 0.5 / s, ug)
        }
        "abs" | "fabs" => {
            let (u, ug) = need1()?;
            unary(u.abs(), u.signum(), ug)
        }
        "floor" => {
            let (u, ug) = need1()?;
            unary(u.floor(), 0.0, ug)
        }
        "ceil" => {
            let (u, ug) = need1()?;
            unary(u.ceil(), 0.0, ug)
        }
        "atan2" | "arctan2" => {
            if a.len() != 2 {
                return Err(ExprError::Arity(name.to_string()));
            }
            let (y, yg) = (a[0].0, &a[0].1);
            let (x, xg) = (a[1].0, &a[1].1);
            let denom = x * x + y * y;
            let g = (0..nvar).map(|i| (x * yg[i] - y * xg[i]) / denom).collect();
            (y.atan2(x), g)
        }
        "pow" => {
            if a.len() != 2 {
                return Err(ExprError::Arity(name.to_string()));
            }
            let (x, xg) = (a[0].0, &a[0].1);
            let (y, yg) = (a[1].0, &a[1].1);
            let val = x.powf(y);
            let d_dx = y * x.powf(y - 1.0);
            let lnx = if x > 0.0 { x.ln() } else { 0.0 };
            let g = (0..nvar)
                .map(|i| {
                    let mut gi = d_dx * xg[i];
                    if yg[i] != 0.0 {
                        gi += val * lnx * yg[i];
                    }
                    gi
                })
                .collect();
            (val, g)
        }
        "min" | "max" => {
            if a.is_empty() {
                return Err(ExprError::Arity(name.to_string()));
            }
            // gradient passes through the selected argument
            let pick = if name == "min" {
                a.iter()
                    .enumerate()
                    .fold(0, |best, (i, c)| if c.0 < a[best].0 { i } else { best })
            } else {
                a.iter()
                    .enumerate()
                    .fold(0, |best, (i, c)| if c.0 > a[best].0 { i } else { best })
            };
            (a[pick].0, a[pick].1.clone())
        }
        _ => return Err(ExprError::UnknownFunc(name.to_string())),
    })
}