finitediff/array/
jacobian.rs

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
// Copyright 2018-2024 argmin developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use std::ops::AddAssign;

use anyhow::Error;
use num::{Float, FromPrimitive};

use crate::pert::PerturbationVectors;
use crate::utils::{mod_and_calc, mod_and_calc_const};

use super::OpFn;

pub fn forward_jacobian_const<const N: usize, const M: usize, F>(
    x: &[F; N],
    fs: OpFn<'_, N, M, F>,
) -> Result<[[F; N]; M], Error>
where
    F: Float + FromPrimitive,
{
    let fx = (fs)(x)?;
    let mut xt = *x;
    let eps_sqrt = F::epsilon().sqrt();
    let mut out = [[F::from_f64(0.0).unwrap(); N]; M];

    for i in 0..N {
        let fx1 = mod_and_calc_const(&mut xt, fs, i, eps_sqrt)?;

        for j in 0..M {
            out[j][i] = (fx1[j] - fx[j]) / eps_sqrt;
        }
    }
    Ok(out)
}

pub fn central_jacobian_const<const N: usize, const M: usize, F>(
    x: &[F; N],
    fs: OpFn<'_, N, M, F>,
) -> Result<[[F; N]; M], Error>
where
    F: Float + FromPrimitive,
{
    let mut xt = *x;
    let eps_cbrt = F::epsilon().cbrt();
    let mut out = [[F::from_f64(0.0).unwrap(); N]; M];
    for i in 0..M {
        let fx1 = mod_and_calc(&mut xt, fs, i, eps_cbrt)?;
        let fx2 = mod_and_calc(&mut xt, fs, i, -eps_cbrt)?;

        for j in 0..M {
            out[j][i] = (fx1[j] - fx2[j]) / (F::from_f64(2.0).unwrap() * eps_cbrt);
        }
    }
    Ok(out)
}

pub fn forward_jacobian_vec_prod_const<const N: usize, const M: usize, F>(
    x: &[F; N],
    fs: OpFn<'_, N, M, F>,
    p: &[F; N],
) -> Result<[F; M], Error>
where
    F: Float + FromPrimitive,
{
    let fx = (fs)(x)?;
    let eps_sqrt = F::epsilon().sqrt();
    let mut x1 = [F::from_f64(0.0).unwrap(); N];
    x1.iter_mut()
        .enumerate()
        .map(|(i, o)| *o = x[i] + eps_sqrt * p[i])
        .count();

    let fx1 = (fs)(&x1)?;
    let mut out = [F::from_f64(0.0).unwrap(); M];
    out.iter_mut()
        .enumerate()
        .map(|(i, o)| {
            *o = (fx1[i] - fx[i]) / eps_sqrt;
        })
        .count();
    Ok(out)
}

pub fn central_jacobian_vec_prod_const<const N: usize, const M: usize, F>(
    x: &[F; N],
    fs: OpFn<'_, N, M, F>,
    p: &[F; N],
) -> Result<[F; M], Error>
where
    F: Float + FromPrimitive,
{
    let eps_cbrt = F::epsilon().cbrt();
    let mut x1 = [F::from_f64(0.0).unwrap(); N];
    let mut x2 = [F::from_f64(0.0).unwrap(); N];
    x1.iter_mut()
        .zip(x2.iter_mut())
        .enumerate()
        .map(|(i, (x1, x2))| {
            let tmp = eps_cbrt * p[i];
            *x1 = x[i] + tmp;
            *x2 = x[i] - tmp;
        })
        .count();
    let fx1 = (fs)(&x1)?;
    let fx2 = (fs)(&x2)?;
    let mut out = [F::from_f64(0.0).unwrap(); M];
    out.iter_mut()
        .enumerate()
        .map(|(i, o)| {
            *o = (fx1[i] - fx2[i]) / (F::from_f64(2.0).unwrap() * eps_cbrt);
        })
        .count();
    Ok(out)
}

pub fn forward_jacobian_pert_const<const N: usize, const M: usize, F>(
    x: &[F; N],
    fs: OpFn<'_, N, M, F>,
    pert: &PerturbationVectors,
) -> Result<[[F; N]; M], Error>
where
    F: Float + FromPrimitive + AddAssign,
{
    let fx = (fs)(x)?;
    let eps_sqrt = F::epsilon().sqrt();
    let mut xt = *x;
    let mut out = [[F::from_f64(0.0).unwrap(); N]; M];
    for pert_item in pert.iter() {
        for j in pert_item.x_idx.iter() {
            xt[*j] += eps_sqrt;
        }

        let fx1 = (fs)(&xt)?;

        for j in pert_item.x_idx.iter() {
            xt[*j] = x[*j];
        }

        for (k, x_idx) in pert_item.x_idx.iter().enumerate() {
            for j in pert_item.r_idx[k].iter() {
                out[*j][*x_idx] = (fx1[*j] - fx[*j]) / eps_sqrt;
            }
        }
    }
    Ok(out)
}

pub fn central_jacobian_pert_const<const N: usize, const M: usize, F>(
    x: &[F; N],
    fs: OpFn<'_, N, M, F>,
    pert: &PerturbationVectors,
) -> Result<[[F; N]; M], Error>
where
    F: Float + FromPrimitive + AddAssign,
{
    let eps_cbrt = F::epsilon().cbrt();
    let mut xt = *x;
    let mut out = [[F::from_f64(0.0).unwrap(); N]; M];
    for pert_item in pert.iter() {
        for j in pert_item.x_idx.iter() {
            xt[*j] += eps_cbrt;
        }

        let fx1 = (fs)(&xt)?;

        for j in pert_item.x_idx.iter() {
            xt[*j] = x[*j] - eps_cbrt;
        }

        let fx2 = (fs)(&xt)?;

        for j in pert_item.x_idx.iter() {
            xt[*j] = x[*j];
        }

        for (k, x_idx) in pert_item.x_idx.iter().enumerate() {
            for j in pert_item.r_idx[k].iter() {
                out[*j][*x_idx] = (fx1[*j] - fx2[*j]) / (F::from_f64(2.0).unwrap() * eps_cbrt);
            }
        }
    }
    Ok(out)
}

#[cfg(test)]
mod tests {
    use crate::PerturbationVector;

    use super::*;

    const COMP_ACC: f64 = 1e-6;

    fn f(x: &[f64; 6]) -> Result<[f64; 6], Error> {
        Ok([
            2.0 * (x[1].powi(3) - x[0].powi(2)),
            3.0 * (x[1].powi(3) - x[0].powi(2)) + 2.0 * (x[2].powi(3) - x[1].powi(2)),
            3.0 * (x[2].powi(3) - x[1].powi(2)) + 2.0 * (x[3].powi(3) - x[2].powi(2)),
            3.0 * (x[3].powi(3) - x[2].powi(2)) + 2.0 * (x[4].powi(3) - x[3].powi(2)),
            3.0 * (x[4].powi(3) - x[3].powi(2)) + 2.0 * (x[5].powi(3) - x[4].powi(2)),
            3.0 * (x[5].powi(3) - x[4].powi(2)),
        ])
    }

    fn res1() -> [[f64; 6]; 6] {
        [
            [-4.0, 6.0, 0.0, 0.0, 0.0, 0.0],
            [-6.0, 5.0, 6.0, 0.0, 0.0, 0.0],
            [0.0, -6.0, 5.0, 6.0, 0.0, 0.0],
            [0.0, 0.0, -6.0, 5.0, 6.0, 0.0],
            [0.0, 0.0, 0.0, -6.0, 5.0, 6.0],
            [0.0, 0.0, 0.0, 0.0, -6.0, 9.0],
        ]
    }

    fn res2() -> [f64; 6] {
        [8.0, 22.0, 27.0, 32.0, 37.0, 24.0]
    }

    fn x() -> [f64; 6] {
        [1.0f64, 1.0, 1.0, 1.0, 1.0, 1.0]
    }

    fn p() -> [f64; 6] {
        [1.0f64, 2.0, 3.0, 4.0, 5.0, 6.0]
    }

    fn pert() -> PerturbationVectors {
        vec![
            PerturbationVector::new()
                .add(0, vec![0, 1])
                .add(3, vec![2, 3, 4]),
            PerturbationVector::new()
                .add(1, vec![0, 1, 2])
                .add(4, vec![3, 4, 5]),
            PerturbationVector::new()
                .add(2, vec![1, 2, 3])
                .add(5, vec![4, 5]),
        ]
    }

    #[test]
    fn test_forward_jacobian_const_f64() {
        let jacobian = forward_jacobian_const(&x(), &f).unwrap();
        let res = res1();
        // println!("{:?}", jacobian);
        for i in 0..6 {
            for j in 0..6 {
                assert!((res[i][j] - jacobian[i][j]).abs() < COMP_ACC)
            }
        }
    }

    #[test]
    fn test_central_jacobian_const_f64() {
        let jacobian = central_jacobian_const(&x(), &f).unwrap();
        let res = res1();
        println!("{:?}", jacobian);
        for i in 0..6 {
            for j in 0..6 {
                assert!((res[i][j] - jacobian[i][j]).abs() < COMP_ACC);
            }
        }
    }

    #[test]
    fn test_forward_jacobian_vec_prod_const_f64() {
        let jacobian = forward_jacobian_vec_prod_const(&x(), &f, &p()).unwrap();
        let res = res2();
        // println!("{:?}", jacobian);
        // the accuracy for this is pretty bad!!
        for i in 0..6 {
            assert!((res[i] - jacobian[i]).abs() < 11.0 * COMP_ACC)
        }
    }

    #[test]
    fn test_central_jacobian_vec_prod_const_f64() {
        let jacobian = central_jacobian_vec_prod_const(&x(), &f, &p()).unwrap();
        let res = res2();
        // println!("{:?}", jacobian);
        for i in 0..6 {
            assert!((res[i] - jacobian[i]).abs() < COMP_ACC)
        }
    }

    #[test]
    fn test_forward_jacobian_pert_const_f64() {
        let jacobian = forward_jacobian_pert_const(&x(), &f, &pert()).unwrap();
        let res = res1();
        // println!("jacobian:\n{:?}", jacobian);
        // println!("res:\n{:?}", res);
        for i in 0..6 {
            for j in 0..6 {
                assert!((res[i][j] - jacobian[i][j]).abs() < COMP_ACC)
            }
        }
    }

    #[test]
    fn test_central_jacobian_pert_const_f64() {
        let jacobian = central_jacobian_pert_const(&x(), &f, &pert()).unwrap();
        let res = res1();
        // println!("jacobian:\n{:?}", jacobian);
        // println!("res:\n{:?}", res);
        for i in 0..6 {
            for j in 0..6 {
                assert!((res[i][j] - jacobian[i][j]).abs() < COMP_ACC)
            }
        }
    }
}