-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjacobian.sac
More file actions
49 lines (35 loc) · 832 Bytes
/
jacobian.sac
File metadata and controls
49 lines (35 loc) · 832 Bytes
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
import Array: all;
import FloatDual: all;
inline
float_dual[.] foo (float_dual[.] x)
{
return { iv -> x[iv] + x[iv] | iv < shape( x) ;
iv -> make_dual (0f,0f) | iv < shape( x)};
}
inline
float_dual[.,.] jacobian (float[.] x)
{
n = shape(x)[0];
default = with {
} : genarray( [n], make_dual (0f,0f));
res = with {
([0,0] <= [i,j] < [n,n]) : make_dual (x[i], tof( i==j)) ;
} : genarray( [n,n], inject (0f));
return res;
}
inline
float_dual[.,.] jacobian_foo (float[.] x)
{
res = with {
([0] <= [i] < shape(x)) : foo (jacobian (x)[[i]]);
} : modarray( jacobian (x));
return res;
}
int main()
{
x = tof (iota (10));
resd = jacobian_foo (x);
res = { [i] -> get_deriv( sum( resd[[i]]))};
ArrayIO::print(res);
return 0;
}