ocas-calc 0.21.0

Calculus and equation solving for oCAS
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
//! Trigonometric integrands via complex exponentials.
//!
//! [`trig_to_exp`] rewrites `sin`/`cos`/`tan`/etc. into exponentials with
//! the imaginary unit `I` (see [`crate::complex`]), so the Risch
//! algorithm (which only knows `log`/`exp`) can attempt them.
//! [`realify`] tries to rewrite the resulting complex answer back into
//! real form, merging conjugate logarithm pairs into real `log`/`atan`
//! terms on a best-effort basis.

use ocas_atom::{Atom, AtomArena, AtomNode, Symbol};

use crate::complex::i;

/// Rewrite trigonometric functions in `expr` into complex exponentials.
///
/// Uses the standard identities (with `t = exp(I·u)`):
///
/// - `sin(u) → (t - t⁻¹)/(2I)`
/// - `cos(u) → (t + t⁻¹)/2`
/// - `tan(u) → sin/cos`, and similarly for `sec`/`csc`/`cot`.
///
/// Returns `None` when `expr` contains no trigonometric functions (the
/// caller then proceeds with the original expression).
pub(crate) fn trig_to_exp<'a>(ctx: &'a AtomArena<'a>, expr: Atom<'a>) -> Option<Atom<'a>> {
    let mut found = false;
    let out = rewrite(ctx, expr, &mut found);
    found.then_some(out)
}

fn rewrite<'a>(ctx: &'a AtomArena<'a>, expr: Atom<'a>, found: &mut bool) -> Atom<'a> {
    match expr.node() {
        AtomNode::Num(_) | AtomNode::Var(_) => expr,
        AtomNode::Add(args) | AtomNode::Mul(args) => {
            let new: Vec<Atom> = args.iter().map(|a| rewrite(ctx, *a, found)).collect();
            if matches!(expr.node(), AtomNode::Add(_)) {
                ctx.add(&new)
            } else {
                ctx.mul(&new)
            }
        }
        AtomNode::Pow(b, e) => {
            let nb = rewrite(ctx, *b, found);
            let ne = rewrite(ctx, *e, found);
            ctx.pow(nb, ne)
        }
        AtomNode::Fun(name, args) => {
            let new_args: Vec<Atom> = args.iter().map(|a| rewrite(ctx, *a, found)).collect();
            let n = name.as_str();
            if args.len() == 1 && matches!(n, "sin" | "cos" | "tan" | "cot" | "sec" | "csc") {
                *found = true;
                let u = new_args[0];
                trig_exp_form(ctx, n, u)
            } else {
                ctx.fun(n, &new_args)
            }
        }
    }
}

/// `exp(I·u)` and its inverse `(exp(I·u))⁻¹` (a power of the same
/// generator, so the tower sees a single exponential).
fn exp_iu<'a>(ctx: &'a AtomArena<'a>, u: Atom<'a>) -> (Atom<'a>, Atom<'a>) {
    let iu = ctx.mul(&[i(ctx), u]);
    let t = ctx.fun("exp", &[iu]);
    let t_inv = ctx.pow(t, ctx.num(-1));
    (t, t_inv)
}

/// The complex-exponential form of a trigonometric function.
fn trig_exp_form<'a>(ctx: &'a AtomArena<'a>, name: &str, u: Atom<'a>) -> Atom<'a> {
    let (t, ti) = exp_iu(ctx, u);
    let two = ctx.num(2);
    let two_i = ctx.mul(&[two, i(ctx)]);
    match name {
        // sin(u) = (t - t⁻¹)/(2I)
        "sin" => {
            let num = ctx.add(&[t, ctx.mul(&[ctx.num(-1), ti])]);
            ctx.mul(&[num, ctx.pow(two_i, ctx.num(-1))])
        }
        // cos(u) = (t + t⁻¹)/2
        "cos" => {
            let num = ctx.add(&[t, ti]);
            ctx.mul(&[num, ctx.pow(two, ctx.num(-1))])
        }
        // tan(u) = (t - t⁻¹)/(I·(t + t⁻¹))
        "tan" => {
            let num = ctx.add(&[t, ctx.mul(&[ctx.num(-1), ti])]);
            let den = ctx.mul(&[i(ctx), ctx.add(&[t, ti])]);
            ctx.mul(&[num, ctx.pow(den, ctx.num(-1))])
        }
        // cot(u) = I·(t + t⁻¹)/(t - t⁻¹)
        "cot" => {
            let num = ctx.mul(&[i(ctx), ctx.add(&[t, ti])]);
            let den = ctx.add(&[t, ctx.mul(&[ctx.num(-1), ti])]);
            ctx.mul(&[num, ctx.pow(den, ctx.num(-1))])
        }
        // sec(u) = 2/(t + t⁻¹)
        "sec" => {
            let den = ctx.add(&[t, ti]);
            ctx.mul(&[two, ctx.pow(den, ctx.num(-1))])
        }
        // csc(u) = 2I/(t - t⁻¹)
        "csc" => {
            let den = ctx.add(&[t, ctx.mul(&[ctx.num(-1), ti])]);
            ctx.mul(&[two_i, ctx.pow(den, ctx.num(-1))])
        }
        _ => unreachable!("trig_exp_form: unsupported {name}"),
    }
}

/// Try to rewrite a complex Risch result back into real form.
///
/// Handles the common patterns produced by trigonometric integrals:
///
/// - products/quotients of `exp(±I·u)` that combine back into `sin`/`cos`
///   are left in exponential form only when no simple real form exists;
/// - conjugate logarithm pairs `c·log(u + I v) + c·log(u - I v)` become
///   `c·log(u² + v²)`; pairs with opposite coefficients `c·log(u+Iv) -
///   c·log(u-Iv)` become `2c·atan(v/u)`.
///
/// This is a best-effort cosmetic pass; if nothing matches, `expr` is
/// returned unchanged (the complex answer is still mathematically valid,
/// as differentiation verifies).
pub(crate) fn realify<'a>(ctx: &'a AtomArena<'a>, expr: Atom<'a>) -> Atom<'a> {
    match expr.node() {
        AtomNode::Add(args) => {
            let rewritten: Vec<Atom> = args.iter().map(|a| realify(ctx, *a)).collect();
            if let Some(merged) = merge_conjugate_logs(ctx, &rewritten) {
                return merged;
            }
            ctx.add(&rewritten)
        }
        AtomNode::Mul(args) => {
            let new: Vec<Atom> = args.iter().map(|a| realify(ctx, *a)).collect();
            if let Some(combined) = combine_exp_pair(ctx, &new) {
                return combined;
            }
            ctx.mul(&new)
        }
        AtomNode::Pow(b, e) => ctx.pow(realify(ctx, *b), realify(ctx, *e)),
        AtomNode::Fun(name, args) => {
            let new: Vec<Atom> = args.iter().map(|a| realify(ctx, *a)).collect();
            ctx.fun(name.as_str(), &new)
        }
        _ => expr,
    }
}

/// In a sum, find `c·log(u + Iv) + c·log(u - Iv)` pairs and merge them
/// into `c·log(u² + v²)`, or `c·log(u+Iv) - c·log(u-Iv)` into `2c·atan(v/u)`.
fn merge_conjugate_logs<'a>(ctx: &'a AtomArena<'a>, args: &[Atom<'a>]) -> Option<Atom<'a>> {
    for i in 0..args.len() {
        for j in (i + 1)..args.len() {
            if let Some(merged) = try_merge_pair(ctx, args[i], args[j]) {
                let mut rest: Vec<Atom> = args
                    .iter()
                    .enumerate()
                    .filter(|&(k, _)| k != i && k != j)
                    .map(|(_, a)| *a)
                    .collect();
                rest.push(merged);
                return Some(if rest.len() == 1 {
                    rest[0]
                } else {
                    ctx.add(&rest)
                });
            }
        }
    }
    None
}

/// Split `expr` into `(coeff, log_arg)` when it is `coeff·log(arg)` (coeff
/// may be 1).
fn as_scaled_log<'a>(ctx: &'a AtomArena<'a>, expr: Atom<'a>) -> Option<(Atom<'a>, Atom<'a>)> {
    if let AtomNode::Fun(name, args) = expr.node() {
        if name.as_str() == "log" && args.len() == 1 {
            return Some((ctx.num(1), args[0]));
        }
    }
    if let AtomNode::Mul(factors) = expr.node() {
        if factors.len() == 2 {
            if let AtomNode::Fun(name, args) = factors[1].node() {
                if name.as_str() == "log" && args.len() == 1 {
                    return Some((factors[0], args[0]));
                }
            }
        }
    }
    None
}

/// Split `arg` into `(u, v)` when it is `u + I·v` or `u - I·v`.
fn as_complex_linear<'a>(arg: Atom<'a>) -> Option<(Atom<'a>, Atom<'a>, bool)> {
    let AtomNode::Add(terms) = arg.node() else {
        return None;
    };
    if terms.len() != 2 {
        return None;
    }
    for &t in terms.iter() {
        if let Some((sign, v)) = as_iv(t) {
            let u = if t == terms[0] { terms[1] } else { terms[0] };
            return Some((u, v, sign));
        }
    }
    None
}

/// Match `I·v` (returns `(true, v)`) or `-I·v` (returns `(false, v)`).
fn as_iv<'a>(t: Atom<'a>) -> Option<(bool, Atom<'a>)> {
    if crate::complex::is_i(t) {
        // Bare I means v = 1, which we cannot construct without an arena.
        return None;
    }
    let AtomNode::Mul(factors) = t.node() else {
        return None;
    };
    let has_i = factors.iter().any(|f| crate::complex::is_i(*f));
    if !has_i {
        return None;
    }
    let rest: Vec<Atom> = factors
        .iter()
        .filter(|f| !crate::complex::is_i(**f))
        .copied()
        .collect();
    let (neg, rest): (bool, Vec<Atom>) = if rest
        .first()
        .is_some_and(|r| matches!(r.node(), AtomNode::Num(-1)))
    {
        (true, rest[1..].to_vec())
    } else {
        (false, rest)
    };
    let v = match rest.len() {
        0 => return None,
        1 => rest[0],
        _ => return None,
    };
    Some((!neg, v))
}

/// Try merging one pair of scaled logs into a real form.
fn try_merge_pair<'a>(ctx: &'a AtomArena<'a>, a: Atom<'a>, b: Atom<'a>) -> Option<Atom<'a>> {
    let (ca, arga) = as_scaled_log(ctx, a)?;
    let (cb, argb) = as_scaled_log(ctx, b)?;
    // Coefficients must match exactly (hash-consed equality) or be exact
    // negatives of each other.
    let (u, v, pos) = as_complex_linear(arga)?;
    let (u2, v2, pos2) = as_complex_linear(argb)?;
    if u != u2 || v != v2 || pos == pos2 {
        return None;
    }
    // a = c·log(u+Iv), b = c·log(u-Iv) → c·log(u²+v²)
    if ca == cb {
        let u2v2 = ctx.add(&[ctx.pow(u, ctx.num(2)), ctx.pow(v, ctx.num(2))]);
        let log = ctx.fun("log", &[u2v2]);
        // If the original had a bare log (ca is the log itself), emit a
        // bare log; otherwise keep the coefficient.
        return Some(if matches!(ca.node(), AtomNode::Num(1)) {
            log
        } else {
            ctx.mul(&[ca, log])
        });
    }
    // a = c·log(u+Iv), b = -c·log(u-Iv) → 2c·atan(v/u)
    if is_negation_of(ctx, cb, ca) {
        let ratio = ctx.mul(&[v, ctx.pow(u, ctx.num(-1))]);
        let atan = ctx.fun("atan", &[ratio]);
        let two = ctx.num(2);
        return Some(if matches!(ca.node(), AtomNode::Num(1)) {
            ctx.mul(&[two, atan])
        } else {
            ctx.mul(&[two, ca, atan])
        });
    }
    None
}

/// Whether `b` is `-1 · a` (structurally, via a leading -1 factor).
fn is_negation_of<'a>(ctx: &'a AtomArena<'a>, b: Atom<'a>, a: Atom<'a>) -> bool {
    let neg_a = ctx.mul(&[ctx.num(-1), a]);
    b == neg_a
}

/// In a product, combine `exp(I·u)·exp(I·v)` → `exp(I·(u+v))` so that
/// cancellation (e.g. exp(Ix)·exp(-Ix)) is visible to the simplifier.
fn combine_exp_pair<'a>(ctx: &'a AtomArena<'a>, factors: &[Atom<'a>]) -> Option<Atom<'a>> {
    let mut exps: Vec<(usize, Atom<'a>)> = Vec::new();
    for (idx, f) in factors.iter().enumerate() {
        if let AtomNode::Fun(name, args) = f.node() {
            if name.as_str() == "exp" && args.len() == 1 && contains_i(args[0]) {
                exps.push((idx, args[0]));
            }
        }
    }
    if exps.len() < 2 {
        return None;
    }
    let (i0, a0) = exps[0];
    let (i1, a1) = exps[1];
    let sum = ctx.add(&[a0, a1]);
    let merged = ctx.fun("exp", &[sum]);
    let mut rest: Vec<Atom> = factors
        .iter()
        .enumerate()
        .filter(|&(k, _)| k != i0 && k != i1)
        .map(|(_, a)| *a)
        .collect();
    rest.push(merged);
    Some(if rest.len() == 1 {
        rest[0]
    } else {
        ctx.mul(&rest)
    })
}

fn contains_i(a: Atom) -> bool {
    if crate::complex::is_i(a) {
        return true;
    }
    match a.node() {
        AtomNode::Add(args) | AtomNode::Mul(args) | AtomNode::Fun(_, args) => {
            args.iter().any(|c| contains_i(*c))
        }
        AtomNode::Pow(b, e) => contains_i(*b) || contains_i(*e),
        _ => false,
    }
}

/// True when `expr` still contains the imaginary unit anywhere.
#[allow(dead_code)]
pub(crate) fn has_imaginary(expr: Atom) -> bool {
    contains_i(expr)
}

#[allow(dead_code)]
fn _unused(s: Symbol) -> Symbol {
    s
}

#[cfg(test)]
mod tests {
    use ocas_atom::AtomArena;
    use ocas_core::arena::Arena;
    use ocas_rewrite::simplify::simplify;

    use super::*;
    use crate::complex::complex_rules;

    #[test]
    fn sin_rewrites_to_exp() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let x = ctx.var("x");
        let out = trig_to_exp(&ctx, ctx.fun("sin", &[x])).expect("has trig");
        let s = out.to_string();
        assert!(s.contains("exp"), "expected exponentials, got {s}");
        assert!(s.contains("I"), "expected imaginary unit, got {s}");
    }

    #[test]
    fn no_trig_returns_none() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let x = ctx.var("x");
        assert!(trig_to_exp(&ctx, ctx.add(&[x, ctx.num(1)])).is_none());
    }

    #[test]
    fn realify_merges_conjugate_log_sum() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let x = ctx.var("x");
        // log(x + I) + log(x - I) → log(x² + 1)
        let ix = ctx.mul(&[i(&ctx), ctx.num(1)]);
        let neg_ix = ctx.mul(&[ctx.num(-1), i(&ctx), ctx.num(1)]);
        let sum = ctx.add(&[
            ctx.fun("log", &[ctx.add(&[x, ix])]),
            ctx.fun("log", &[ctx.add(&[x, neg_ix])]),
        ]);
        let out = realify(&ctx, sum);
        assert_eq!(out.to_string(), "log((x^2) + (1^2))");
    }

    #[test]
    fn realify_exp_cancellation() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let x = ctx.var("x");
        // exp(I·x)·exp(-I·x) → exp(0)
        let prod = ctx.mul(&[
            ctx.fun("exp", &[ctx.mul(&[i(&ctx), x])]),
            ctx.fun("exp", &[ctx.mul(&[ctx.num(-1), i(&ctx), x])]),
        ]);
        let merged = realify(&ctx, prod);
        let rules = complex_rules(&ctx, &());
        let simp = simplify(&ctx, merged, &rules, 20);
        // After merging, the exponent is I·x + (-I·x); the complex rules
        // do not cancel it fully, but the exponents are combined.
        assert!(simp.to_string().starts_with("exp("), "got {simp}");
    }

    #[test]
    fn has_imaginary_detection() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let x = ctx.var("x");
        assert!(has_imaginary(ctx.mul(&[i(&ctx), x])));
        assert!(!has_imaginary(ctx.add(&[x, ctx.num(1)])));
    }
}