mathcore 0.3.1

Symbolic math library and computer algebra system for Rust
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
use crate::calculus::Calculus;
use crate::engine::Engine;
use crate::types::{BinaryOp, Expr, MathError};
use nalgebra::DMatrix;
use std::collections::HashMap;

pub struct Optimization;

impl Optimization {
    pub fn gradient(expr: &Expr, vars: &[String]) -> Result<Vec<Expr>, MathError> {
        let mut gradient = Vec::new();

        for var in vars {
            let partial = Calculus::differentiate(expr, var)?;
            gradient.push(partial);
        }

        Ok(gradient)
    }

    pub fn hessian(expr: &Expr, vars: &[String]) -> Result<Vec<Vec<Expr>>, MathError> {
        let mut hessian = vec![vec![Expr::zero(); vars.len()]; vars.len()];

        for (i, var_i) in vars.iter().enumerate() {
            for (j, var_j) in vars.iter().enumerate() {
                let first_deriv = Calculus::differentiate(expr, var_i)?;
                let second_deriv = Calculus::differentiate(&first_deriv, var_j)?;
                hessian[i][j] = second_deriv;
            }
        }

        Ok(hessian)
    }

    pub fn gradient_descent(
        loss_fn: &Expr,
        initial_params: HashMap<String, f64>,
        learning_rate: f64,
        iterations: usize,
    ) -> Result<HashMap<String, f64>, MathError> {
        let engine = Engine::new();
        let mut params = initial_params.clone();
        let var_names: Vec<String> = params.keys().cloned().collect();

        let gradients = Self::gradient(loss_fn, &var_names)?;

        for _ in 0..iterations {
            let mut updates = HashMap::new();

            for (var_name, grad_expr) in var_names.iter().zip(gradients.iter()) {
                let grad_value = match engine.evaluate_with_vars(grad_expr, &params)? {
                    Expr::Number(n) => n,
                    _ => {
                        return Err(MathError::InvalidOperation(
                            "Gradient must be numeric".to_string(),
                        ))
                    }
                };

                let current_value = params[var_name];
                updates.insert(var_name.clone(), current_value - learning_rate * grad_value);
            }

            params = updates;
        }

        Ok(params)
    }

    pub fn automatic_differentiation(
        expr: &Expr,
        var: &str,
        value: f64,
    ) -> Result<(f64, f64), MathError> {
        let engine = Engine::new();
        let mut vars = HashMap::new();
        vars.insert(var.to_string(), value);

        let function_value = match engine.evaluate_with_vars(expr, &vars)? {
            Expr::Number(n) => n,
            _ => {
                return Err(MathError::InvalidOperation(
                    "Expression must evaluate to number".to_string(),
                ))
            }
        };

        let derivative = Calculus::differentiate(expr, var)?;
        let derivative_value = match engine.evaluate_with_vars(&derivative, &vars)? {
            Expr::Number(n) => n,
            _ => {
                return Err(MathError::InvalidOperation(
                    "Derivative must evaluate to number".to_string(),
                ))
            }
        };

        Ok((function_value, derivative_value))
    }

    pub fn jacobian(
        functions: &[Expr],
        vars: &[String],
        point: &HashMap<String, f64>,
    ) -> Result<DMatrix<f64>, MathError> {
        let engine = Engine::new();
        let mut jacobian = DMatrix::zeros(functions.len(), vars.len());

        for (i, func) in functions.iter().enumerate() {
            for (j, var) in vars.iter().enumerate() {
                let partial = Calculus::differentiate(func, var)?;
                let value = match engine.evaluate_with_vars(&partial, point)? {
                    Expr::Number(n) => n,
                    _ => {
                        return Err(MathError::InvalidOperation(
                            "Jacobian entry must be numeric".to_string(),
                        ))
                    }
                };
                jacobian[(i, j)] = value;
            }
        }

        Ok(jacobian)
    }

    pub fn taylor_series(
        expr: &Expr,
        var: &str,
        center: f64,
        order: usize,
    ) -> Result<Expr, MathError> {
        let engine = Engine::new();
        let mut series = Expr::zero();
        let mut current_deriv = expr.clone();
        let mut factorial = 1.0;

        for n in 0..=order {
            if n > 0 {
                current_deriv = Calculus::differentiate(&current_deriv, var)?;
                factorial *= n as f64;
            }

            let mut eval_point = HashMap::new();
            eval_point.insert(var.to_string(), center);

            let coeff_value = match engine.evaluate_with_vars(&current_deriv, &eval_point)? {
                Expr::Number(v) => v / factorial,
                _ => continue,
            };

            let x_minus_center = Expr::Binary {
                op: BinaryOp::Subtract,
                left: Box::new(Expr::Symbol(var.to_string())),
                right: Box::new(Expr::Number(center)),
            };

            let power_term = if n == 0 {
                Expr::Number(coeff_value)
            } else if n == 1 {
                Expr::Binary {
                    op: BinaryOp::Multiply,
                    left: Box::new(Expr::Number(coeff_value)),
                    right: Box::new(x_minus_center),
                }
            } else {
                let power = Expr::Binary {
                    op: BinaryOp::Power,
                    left: Box::new(x_minus_center),
                    right: Box::new(Expr::Number(n as f64)),
                };
                Expr::Binary {
                    op: BinaryOp::Multiply,
                    left: Box::new(Expr::Number(coeff_value)),
                    right: Box::new(power),
                }
            };

            series = Expr::Binary {
                op: BinaryOp::Add,
                left: Box::new(series),
                right: Box::new(power_term),
            };
        }

        Ok(series)
    }

    pub fn optimize_newton(
        func: &Expr,
        var: &str,
        initial: f64,
        tolerance: f64,
        max_iterations: usize,
    ) -> Result<f64, MathError> {
        let engine = Engine::new();
        let first_deriv = Calculus::differentiate(func, var)?;
        let second_deriv = Calculus::differentiate(&first_deriv, var)?;

        let mut x = initial;

        for _ in 0..max_iterations {
            let mut vars = HashMap::new();
            vars.insert(var.to_string(), x);

            let f_prime = match engine.evaluate_with_vars(&first_deriv, &vars)? {
                Expr::Number(n) => n,
                _ => {
                    return Err(MathError::InvalidOperation(
                        "Derivative must be numeric".to_string(),
                    ))
                }
            };

            if f_prime.abs() < tolerance {
                return Ok(x);
            }

            let f_double_prime = match engine.evaluate_with_vars(&second_deriv, &vars)? {
                Expr::Number(n) => n,
                _ => {
                    return Err(MathError::InvalidOperation(
                        "Second derivative must be numeric".to_string(),
                    ))
                }
            };

            if f_double_prime.abs() < 1e-10 {
                return Err(MathError::InvalidOperation(
                    "Second derivative too small".to_string(),
                ));
            }

            x -= f_prime / f_double_prime;
        }

        Ok(x)
    }

    pub fn lagrange_multipliers(
        objective: &Expr,
        constraints: &[Expr],
        vars: &[String],
    ) -> Result<Vec<Expr>, MathError> {
        let mut lagrangian = objective.clone();
        let mut lambda_vars = Vec::new();

        for (i, constraint) in constraints.iter().enumerate() {
            let lambda_var = format!("λ{}", i);
            lambda_vars.push(lambda_var.clone());

            let lambda_term = Expr::Binary {
                op: BinaryOp::Multiply,
                left: Box::new(Expr::Symbol(lambda_var)),
                right: Box::new(constraint.clone()),
            };

            lagrangian = Expr::Binary {
                op: BinaryOp::Add,
                left: Box::new(lagrangian),
                right: Box::new(lambda_term),
            };
        }

        let mut all_vars = vars.to_vec();
        all_vars.extend(lambda_vars);

        Self::gradient(&lagrangian, &all_vars)
    }
}

pub struct SymbolicIntegration;

impl SymbolicIntegration {
    pub fn integrate_by_parts(u: &Expr, dv: &Expr, var: &str) -> Result<Expr, MathError> {
        let du = Calculus::differentiate(u, var)?;
        let v = Calculus::integrate(dv, var)?;

        let uv = Expr::Binary {
            op: BinaryOp::Multiply,
            left: Box::new(u.clone()),
            right: Box::new(v.clone()),
        };

        let vdu = Expr::Binary {
            op: BinaryOp::Multiply,
            left: Box::new(v),
            right: Box::new(du),
        };

        let integral_vdu = Calculus::integrate(&vdu, var)?;

        Ok(Expr::Binary {
            op: BinaryOp::Subtract,
            left: Box::new(uv),
            right: Box::new(integral_vdu),
        })
    }

    pub fn substitution_rule(
        expr: &Expr,
        old_var: &str,
        substitution: &Expr,
        new_var: &str,
    ) -> Result<Expr, MathError> {
        let engine = Engine::new();
        let substituted = engine.substitute(expr, old_var, substitution)?;

        let du = Calculus::differentiate(substitution, new_var)?;

        let integrand = Expr::Binary {
            op: BinaryOp::Multiply,
            left: Box::new(substituted),
            right: Box::new(du),
        };

        Calculus::integrate(&integrand, new_var)
    }

    pub fn partial_fractions(
        numerator: &Expr,
        denominator: &Expr,
        var: &str,
    ) -> Result<Vec<Expr>, MathError> {
        use crate::solver::Solver;

        let roots = Solver::solve(denominator, var)?;
        let mut fractions = Vec::new();

        for root in roots {
            if let Expr::Number(r) = root {
                let factor = Expr::Binary {
                    op: BinaryOp::Subtract,
                    left: Box::new(Expr::Symbol(var.to_string())),
                    right: Box::new(Expr::Number(r)),
                };

                let residue = Self::compute_residue(numerator, denominator, var, r)?;

                let fraction = Expr::Binary {
                    op: BinaryOp::Divide,
                    left: Box::new(residue),
                    right: Box::new(factor),
                };

                fractions.push(fraction);
            }
        }

        Ok(fractions)
    }

    fn compute_residue(
        numerator: &Expr,
        denominator: &Expr,
        var: &str,
        pole: f64,
    ) -> Result<Expr, MathError> {
        let engine = Engine::new();
        let factor = Expr::Binary {
            op: BinaryOp::Subtract,
            left: Box::new(Expr::Symbol(var.to_string())),
            right: Box::new(Expr::Number(pole)),
        };

        let reduced_den = Expr::Binary {
            op: BinaryOp::Divide,
            left: Box::new(denominator.clone()),
            right: Box::new(factor),
        };

        let ratio = Expr::Binary {
            op: BinaryOp::Divide,
            left: Box::new(numerator.clone()),
            right: Box::new(reduced_den),
        };

        let mut vars = HashMap::new();
        vars.insert(var.to_string(), pole);

        engine.evaluate_with_vars(&ratio, &vars)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::Parser;

    #[test]
    fn test_gradient() {
        let expr = Parser::parse("x^2 + y^2").unwrap();
        let vars = vec!["x".to_string(), "y".to_string()];
        let grad = Optimization::gradient(&expr, &vars).unwrap();

        assert_eq!(grad.len(), 2);
        println!("Gradient of x^2 + y^2: [{}, {}]", grad[0], grad[1]);
    }

    #[test]
    fn test_taylor_series() {
        let expr = Parser::parse("sin(x)").unwrap();
        let taylor = Optimization::taylor_series(&expr, "x", 0.0, 5).unwrap();
        println!("Taylor series of sin(x): {}", taylor);
    }
}