solvr 0.2.0-beta.2

Advanced computing library for real-world problem solving - optimization, differential equations, interpolation, statistics, and more
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Autograd-based Jacobian computation shared across solvr.
//!
//! Provides exact Jacobian computation using numr's forward-mode automatic
//! differentiation. This is used by:
//! - `optimize/roots` - Newton's method for root finding
//! - `integrate/ode` - Implicit ODE solvers (BDF, Radau, LSODA)
//!
//! # Why Autograd over Finite Differences?
//!
//! Finite differences have several drawbacks:
//! - Truncation error from finite step size
//! - Numerical instability for ill-conditioned problems
//! - Requires tuning epsilon parameter
//! - O(n) function evaluations per Jacobian
//!
//! Autograd provides:
//! - Exact derivatives (to machine precision)
//! - No epsilon tuning required
//! - More robust for stiff problems
//! - Same O(n) cost but exact results
//!
//! # Usage
//!
//! User must write their function using `DualTensor` and `dual_*` operations:
//!
//! ```no_run
//! # use numr::runtime::cpu::{CpuClient, CpuDevice, CpuRuntime};
//! # use numr::tensor::Tensor;
//! use numr::autograd::DualTensor;
//! # let device = CpuDevice::new();
//! # let client = CpuClient::new(device.clone());
//! # let y = Tensor::from_slice(&[1.0, 1.0], &[2], &device);
//!
//! // f(y) = [y[0]^2 - y[1], y[0] * y[1] - 1]
//! let jacobian = solvr::common::jacobian::jacobian_autograd(&client, |_y: &DualTensor<CpuRuntime>, _c: &CpuClient| {
//!     // Use dual operations for automatic differentiation
//!     unimplemented!()
//! }, &y)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
use crate::DType;

use numr::autograd::{DualTensor, Var, backward, jacobian_forward, jvp, var_mul, var_sum};
use numr::error::Result;
use numr::ops::TensorOps;
use numr::runtime::{Runtime, RuntimeClient};
use numr::tensor::Tensor;

/// Compute the Jacobian matrix using forward-mode automatic differentiation.
///
/// For a function F: ℝⁿ → ℝᵐ, computes the m×n Jacobian matrix J where
/// `J[i,j]` = ∂Fᵢ/∂xⱼ.
///
/// This uses numr's forward-mode AD, which computes n JVPs (one per input
/// dimension). For square systems (n = m), this is optimal.
///
/// # Arguments
///
/// * `client` - Runtime client for tensor operations
/// * `f` - Function using `DualTensor` and `dual_*` operations
/// * `x` - Point at which to evaluate the Jacobian
///
/// # Returns
///
/// Jacobian matrix of shape `[m, n]`
///
/// # Example
///
/// ```no_run
/// use numr::runtime::cpu::{CpuClient, CpuDevice, CpuRuntime};
/// use numr::tensor::Tensor;
/// use numr::autograd::DualTensor;
/// let device = CpuDevice::new();
/// let client = CpuClient::new(device.clone());
/// let x = Tensor::from_slice(&[1.0], &[1], &device);
/// // F(x) = x² (element-wise), Jacobian = diag(2x)
/// let jacobian = solvr::common::jacobian::jacobian_autograd(
///     &client,
///     |dual_x: &DualTensor<CpuRuntime>, c: &CpuClient| { /* dual ops */ unimplemented!() },
///     &x,
/// )?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn jacobian_autograd<R, C, F>(client: &C, f: F, x: &Tensor<R>) -> Result<Tensor<R>>
where
    R: Runtime<DType = DType>,
    C: TensorOps<R> + RuntimeClient<R>,
    F: Fn(&DualTensor<R>, &C) -> Result<DualTensor<R>>,
{
    jacobian_forward(f, x, client)
}

/// Compute Jacobian-vector product J @ v using forward-mode AD.
///
/// For a function F: ℝⁿ → ℝᵐ, computes J(x) @ v without forming the full
/// Jacobian matrix. This is useful for:
/// - Newton-Krylov methods (iterative linear solvers)
/// - Large systems where forming J is expensive
/// - Memory-constrained environments
///
/// # Arguments
///
/// * `client` - Runtime client
/// * `f` - Function using `DualTensor` operations
/// * `x` - Point at which to evaluate
/// * `v` - Vector to multiply with Jacobian
///
/// # Returns
///
/// Tuple of (F(x), J(x) @ v)
pub fn jvp_autograd<R, C, F>(
    client: &C,
    f: F,
    x: &Tensor<R>,
    v: &Tensor<R>,
) -> Result<(Tensor<R>, Tensor<R>)>
where
    R: Runtime<DType = DType>,
    C: TensorOps<R> + RuntimeClient<R>,
    F: FnOnce(&[DualTensor<R>], &C) -> Result<DualTensor<R>>,
{
    jvp(f, &[x], &[v], client)
}

/// Compute vector-Jacobian product vᵀ @ J using reverse-mode AD.
///
/// For a function F: ℝⁿ → ℝᵐ, computes vᵀ @ J(x) without forming the full
/// Jacobian matrix. This is useful for:
/// - Adjoint sensitivity analysis (computing ∂J/∂p)
/// - Backpropagation through ODE solvers
/// - Computing gradients of scalar loss functions
///
/// # Mathematical Background
///
/// For adjoint ODE: dλ/dt = -λᵀ @ (∂f/∂y)
///
/// At each time step, we need to compute λᵀ @ J_y where J_y = ∂f/∂y.
/// This function computes exactly that using reverse-mode AD in O(1) passes.
///
/// # Arguments
///
/// * `client` - Runtime client
/// * `f` - Function that takes a Var and returns a Var
/// * `x` - Point at which to evaluate
/// * `v` - Vector to left-multiply with Jacobian (the adjoint λ)
///
/// # Returns
///
/// Tuple of (F(x), vᵀ @ J(x))
///
/// # Example
///
/// ```no_run
/// use numr::runtime::cpu::{CpuClient, CpuDevice, CpuRuntime};
/// use numr::tensor::Tensor;
/// use numr::autograd::Var;
/// let device = CpuDevice::new();
/// let client = CpuClient::new(device.clone());
/// let x = Tensor::from_slice(&[2.0], &[1], &device);
/// let v = Tensor::from_slice(&[1.0], &[1], &device);
/// // F(x) = x², at x=[2], v=[1]
/// let (fx, vjp_result) = solvr::common::jacobian::vjp_autograd(
///     &client,
///     |x_var: &Var<CpuRuntime>, c: &CpuClient| { /* var ops */ unimplemented!() },
///     &x,
///     &v,
/// )?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn vjp_autograd<R, C, F>(
    client: &C,
    f: F,
    x: &Tensor<R>,
    v: &Tensor<R>,
) -> Result<(Tensor<R>, Tensor<R>)>
where
    R: Runtime<DType = DType>,
    C: TensorOps<R> + RuntimeClient<R>,
    R::Client: TensorOps<R>,
    F: Fn(&Var<R>, &C) -> Result<Var<R>>,
{
    // Create input variable with grad tracking
    let x_var = Var::new(x.clone(), true);

    // Forward pass
    let y_var = f(&x_var, client)?;
    let fx = y_var.tensor().clone();

    // Compute vᵀ @ y (element-wise multiplication then sum to get scalar loss)
    let v_var = Var::new(v.clone(), false);
    let prod = var_mul(&y_var, &v_var, client)?;
    // Sum over all dimensions to get scalar
    let all_dims: Vec<usize> = (0..prod.tensor().shape().len()).collect();
    let loss = var_sum(&prod, &all_dims, false, client)?;

    // Backward pass to get vᵀ @ J
    let grads = backward(&loss, client)?;

    // Get gradient w.r.t. x
    let vjp_result = match grads.get(x_var.id()) {
        Some(g) => g.clone(),
        None => {
            // If no gradient, return zeros
            Tensor::<R>::zeros(x.shape(), x.dtype(), x.device())
        }
    };

    Ok((fx, vjp_result))
}

/// Compute the vector-Jacobian product for a function with parameters.
///
/// Computes vᵀ @ (∂f/∂y) and vᵀ @ (∂f/∂p) simultaneously for the adjoint
/// ODE sensitivity method.
///
/// # Arguments
///
/// * `client` - Runtime client
/// * `f` - Function f(t, y, p) returning dy/dt
/// * `t` - Current time (scalar)
/// * `y` - Current state
/// * `p` - Parameters
/// * `v` - Adjoint vector λ
///
/// # Returns
///
/// Tuple of (f(t, y, p), vᵀ @ (∂f/∂y), vᵀ @ (∂f/∂p))
pub fn vjp_with_params<R, C, F>(
    client: &C,
    f: F,
    t: f64,
    y: &Tensor<R>,
    p: &Tensor<R>,
    v: &Tensor<R>,
) -> Result<(Tensor<R>, Tensor<R>, Tensor<R>)>
where
    R: Runtime<DType = DType>,
    C: TensorOps<R> + RuntimeClient<R>,
    R::Client: TensorOps<R>,
    F: Fn(&Var<R>, &Var<R>, &Var<R>, &C) -> Result<Var<R>>,
{
    let device = y.device();

    // Create input variables with grad tracking
    let t_var = Var::new(Tensor::<R>::from_slice(&[t], &[1], device), false);
    let y_var = Var::new(y.clone(), true);
    let p_var = Var::new(p.clone(), true);

    // Forward pass
    let f_var = f(&t_var, &y_var, &p_var, client)?;
    let fx = f_var.tensor().clone();

    // Compute vᵀ @ f (element-wise multiplication then sum to get scalar loss)
    let v_var = Var::new(v.clone(), false);
    let prod = var_mul(&f_var, &v_var, client)?;
    // Sum over all dimensions to get scalar
    let all_dims: Vec<usize> = (0..prod.tensor().shape().len()).collect();
    let loss = var_sum(&prod, &all_dims, false, client)?;

    // Backward pass
    let grads = backward(&loss, client)?;

    // Get gradients w.r.t. y and p
    let vjp_y = match grads.get(y_var.id()) {
        Some(g) => g.clone(),
        None => Tensor::<R>::zeros(y.shape(), y.dtype(), device),
    };

    let vjp_p = match grads.get(p_var.id()) {
        Some(g) => g.clone(),
        None => Tensor::<R>::zeros(p.shape(), p.dtype(), device),
    };

    Ok((fx, vjp_y, vjp_p))
}

#[cfg(test)]
mod tests {
    use super::*;
    use numr::autograd::dual_ops::{dual_mul, dual_mul_scalar};
    use numr::autograd::var_mul_scalar;
    use numr::runtime::cpu::{CpuClient, CpuDevice, CpuRuntime};

    fn setup() -> (CpuDevice, CpuClient) {
        let device = CpuDevice::new();
        let client = CpuRuntime::default_client(&device);
        (device, client)
    }

    #[test]
    fn test_jacobian_autograd_linear() {
        let (device, client) = setup();

        // F(x) = 2x, Jacobian = 2I
        let x = Tensor::<CpuRuntime>::from_slice(&[1.0f64, 2.0, 3.0], &[3], &device);

        let jacobian =
            jacobian_autograd(&client, |dual_x, c| dual_mul_scalar(dual_x, 2.0, c), &x).unwrap();

        assert_eq!(jacobian.shape(), &[3, 3]);
        let j: Vec<f64> = jacobian.to_vec();

        // Diagonal should be 2, off-diagonal 0
        assert!((j[0] - 2.0).abs() < 1e-10);
        assert!((j[4] - 2.0).abs() < 1e-10);
        assert!((j[8] - 2.0).abs() < 1e-10);
        assert!(j[1].abs() < 1e-10);
        assert!(j[2].abs() < 1e-10);
        assert!(j[3].abs() < 1e-10);
    }

    #[test]
    fn test_jacobian_autograd_quadratic() {
        let (device, client) = setup();

        // F(x) = x², Jacobian = diag(2x)
        let x = Tensor::<CpuRuntime>::from_slice(&[1.0f64, 2.0, 3.0], &[3], &device);

        let jacobian =
            jacobian_autograd(&client, |dual_x, c| dual_mul(dual_x, dual_x, c), &x).unwrap();

        let j: Vec<f64> = jacobian.to_vec();

        // Diagonal should be [2, 4, 6]
        assert!((j[0] - 2.0).abs() < 1e-10);
        assert!((j[4] - 4.0).abs() < 1e-10);
        assert!((j[8] - 6.0).abs() < 1e-10);
    }

    #[test]
    fn test_jvp_autograd() {
        let (device, client) = setup();

        // F(x) = x², at x=[2], v=[1]
        // F(x) = 4, J @ v = 2*2*1 = 4
        let x = Tensor::<CpuRuntime>::from_slice(&[2.0f64], &[1], &device);
        let v = Tensor::<CpuRuntime>::from_slice(&[1.0f64], &[1], &device);

        let (fx, jv) = jvp_autograd(
            &client,
            |inputs, c| {
                let x = &inputs[0];
                dual_mul(x, x, c)
            },
            &x,
            &v,
        )
        .unwrap();

        assert!((fx.to_vec::<f64>()[0] - 4.0).abs() < 1e-10);
        assert!((jv.to_vec::<f64>()[0] - 4.0).abs() < 1e-10);
    }

    #[test]
    fn test_vjp_autograd_simple() {
        let (device, client) = setup();

        // F(x) = x², at x=[2], v=[1]
        // vᵀ @ J = 1 * 2*2 = 4
        let x = Tensor::<CpuRuntime>::from_slice(&[2.0f64], &[1], &device);
        let v = Tensor::<CpuRuntime>::from_slice(&[1.0f64], &[1], &device);

        let (fx, vjp_result) =
            vjp_autograd(&client, |x_var, c| var_mul(x_var, x_var, c), &x, &v).unwrap();

        assert!((fx.to_vec::<f64>()[0] - 4.0).abs() < 1e-10);
        assert!((vjp_result.to_vec::<f64>()[0] - 4.0).abs() < 1e-10);
    }

    #[test]
    fn test_vjp_autograd_linear() {
        let (device, client) = setup();

        // F(x) = 2x, Jacobian = 2I
        // vᵀ @ J = [1, 1, 1] @ 2I = [2, 2, 2]
        let x = Tensor::<CpuRuntime>::from_slice(&[1.0f64, 2.0, 3.0], &[3], &device);
        let v = Tensor::<CpuRuntime>::from_slice(&[1.0f64, 1.0, 1.0], &[3], &device);

        let (fx, vjp_result) =
            vjp_autograd(&client, |x_var, c| var_mul_scalar(x_var, 2.0, c), &x, &v).unwrap();

        let fx_vals: Vec<f64> = fx.to_vec();
        assert!((fx_vals[0] - 2.0).abs() < 1e-10);
        assert!((fx_vals[1] - 4.0).abs() < 1e-10);
        assert!((fx_vals[2] - 6.0).abs() < 1e-10);

        let vjp_vals: Vec<f64> = vjp_result.to_vec();
        assert!((vjp_vals[0] - 2.0).abs() < 1e-10);
        assert!((vjp_vals[1] - 2.0).abs() < 1e-10);
        assert!((vjp_vals[2] - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_vjp_with_params() {
        let (device, client) = setup();

        // f(t, y, p) = p * y (scalar multiplication)
        // ∂f/∂y = p, ∂f/∂p = y
        // vᵀ @ (∂f/∂y) = v * p, vᵀ @ (∂f/∂p) = v * y
        let y = Tensor::<CpuRuntime>::from_slice(&[2.0f64], &[1], &device);
        let p = Tensor::<CpuRuntime>::from_slice(&[3.0f64], &[1], &device);
        let v = Tensor::<CpuRuntime>::from_slice(&[1.0f64], &[1], &device);

        let (fx, vjp_y, vjp_p) = vjp_with_params(
            &client,
            |_t, y_var, p_var, c| var_mul(p_var, y_var, c),
            0.0,
            &y,
            &p,
            &v,
        )
        .unwrap();

        // f = p * y = 3 * 2 = 6
        assert!((fx.to_vec::<f64>()[0] - 6.0).abs() < 1e-10);

        // vᵀ @ (∂f/∂y) = v * p = 1 * 3 = 3
        assert!(
            (vjp_y.to_vec::<f64>()[0] - 3.0).abs() < 1e-10,
            "vjp_y = {}",
            vjp_y.to_vec::<f64>()[0]
        );

        // vᵀ @ (∂f/∂p) = v * y = 1 * 2 = 2
        assert!(
            (vjp_p.to_vec::<f64>()[0] - 2.0).abs() < 1e-10,
            "vjp_p = {}",
            vjp_p.to_vec::<f64>()[0]
        );
    }
}