Example 7: Jacobian

use f64ad_core::ComplexField;
use f64ad_core::f64ad::{f64ad_jacobian, GlobalComputationGraphs};

fn main() {
    // Create a computation graph.
    let computation_graph = GlobalComputationGraphs::get(None, None);

    // Spawn an f64ad_ variables from computation graph.
    let v0 = computation_graph.spawn_variable(2.0);
    let v1 = computation_graph.spawn_variable(4.0);

    let result1 = v0.powf(v1);
    let result2 = v1.log(v0);
    println!("Result1: {:?}", result1);
    println!("Result2: {:?}", result2);
    println!("////////////////////////////////////////////////////////////////////////////////////");

    // Computes the first order jacobian and prints the summary
    let first_order_jacobian = f64ad_jacobian(&[v0, v1], &[result1, result2], 1);
    first_order_jacobian.print_summary();

    println!("////////////////////////////////////////////////////////////////////////////////////");

    // Computes the fourth order jacobian and prints the summary
    let higher_order_jacobian = f64ad_jacobian(&[v0, v1], &[result1, result2], 4);
    higher_order_jacobian.print_summary();
}

Output

Result1: f64ad_var_f(f64ad_var_f{ value: 16.0, node_idx: 2 })
Result2: f64ad_var_f(f64ad_var_f{ value: 2.0, node_idx: 3 })
////////////////////////////////////////////////////////////////////////////////////
0 --- output: 0, inputs wrt: [0], value: 32.0
1 --- output: 0, inputs wrt: [1], value: 11.090354888959125
2 --- output: 1, inputs wrt: [0], value: -1.4426950408889634
3 --- output: 1, inputs wrt: [1], value: 0.36067376022224085
////////////////////////////////////////////////////////////////////////////////////
0 --- output: 0, inputs wrt: [0, 0, 0, 0], value: 24.0
1 --- output: 0, inputs wrt: [0, 0, 0, 1], value: 85.27106466687738
2 --- output: 0, inputs wrt: [0, 0, 1, 0], value: 85.27106466687738
3 --- output: 0, inputs wrt: [0, 0, 1, 1], value: 69.8779867794306
4 --- output: 0, inputs wrt: [0, 1, 0, 0], value: 85.27106466687738
5 --- output: 0, inputs wrt: [0, 1, 0, 1], value: 69.8779867794306
6 --- output: 0, inputs wrt: [0, 1, 1, 0], value: 69.8779867794306
7 --- output: 0, inputs wrt: [0, 1, 1, 1], value: 22.187661197682576
8 --- output: 0, inputs wrt: [1, 0, 0, 0], value: 85.27106466687738
9 --- output: 0, inputs wrt: [1, 0, 0, 1], value: 69.8779867794306
10 --- output: 0, inputs wrt: [1, 0, 1, 0], value: 69.8779867794306
11 --- output: 0, inputs wrt: [1, 0, 1, 1], value: 22.187661197682576
12 --- output: 0, inputs wrt: [1, 1, 0, 0], value: 69.8779867794306
13 --- output: 0, inputs wrt: [1, 1, 0, 1], value: 22.187661197682576
14 --- output: 0, inputs wrt: [1, 1, 1, 0], value: 22.187661197682576
15 --- output: 0, inputs wrt: [1, 1, 1, 1], value: 3.693361577329335
16 --- output: 1, inputs wrt: [0, 0, 0, 0], value: 33.31458966591519
17 --- output: 1, inputs wrt: [0, 0, 0, 1], value: -1.5053751004845806
18 --- output: 1, inputs wrt: [0, 0, 1, 0], value: -1.5053751004845806
19 --- output: 1, inputs wrt: [0, 0, 1, 1], value: -0.12635828742686595
20 --- output: 1, inputs wrt: [0, 1, 0, 0], value: -1.5053751004845806
21 --- output: 1, inputs wrt: [0, 1, 0, 1], value: -0.12635828742686595
22 --- output: 1, inputs wrt: [0, 1, 1, 0], value: -0.12635828742686595
23 --- output: 1, inputs wrt: [0, 1, 1, 1], value: -0.03252139032821262
24 --- output: 1, inputs wrt: [1, 0, 0, 0], value: -1.505375100484581
25 --- output: 1, inputs wrt: [1, 0, 0, 1], value: -0.1263582874268659
26 --- output: 1, inputs wrt: [1, 0, 1, 0], value: -0.12635828742686595
27 --- output: 1, inputs wrt: [1, 0, 1, 1], value: -0.032521390328212586
28 --- output: 1, inputs wrt: [1, 1, 0, 0], value: -0.12635828742686595
29 --- output: 1, inputs wrt: [1, 1, 0, 1], value: -0.0325213903282126
30 --- output: 1, inputs wrt: [1, 1, 1, 0], value: -0.03252139032821263
31 --- output: 1, inputs wrt: [1, 1, 1, 1], value: -0.033813165020835076