-
Notifications
You must be signed in to change notification settings - Fork 0
/
dual_numbers.c
78 lines (61 loc) · 1.63 KB
/
dual_numbers.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
typedef struct {
double real;
double dual;
} DualNumber;
DualNumber MakeDual(double r, double d) {
DualNumber dual = {r, d};
return dual;
}
DualNumber MakeConstant(double c) {
return MakeDual(c, 0);
}
DualNumber MakeVariable(double v) {
return MakeDual(v, 1);
}
DualNumber Add(DualNumber A, DualNumber B) {
return MakeDual(A.real + B.real, A.dual + B.dual);
}
DualNumber Sub(DualNumber A, DualNumber B) {
return MakeDual(A.real - B.real, A.dual - B.dual);
}
DualNumber Mul(DualNumber A, DualNumber B) {
return MakeDual(A.real * B.real, A.real * B.dual + A.dual * B.real);
}
DualNumber Div(DualNumber A, DualNumber B) {
assert(B.real != 0);
return MakeDual(A.real / B.real, (A.dual - (A.real / B.real) * B.dual) / B.real);
}
DualNumber Sin(DualNumber A) {
return MakeDual(sin(A.real), A.dual * cos(A.real));
}
DualNumber Cos(DualNumber A) {
return MakeDual(cos(A.real), -A.dual * sin(A.real));
}
DualNumber Exp(DualNumber A) {
return MakeDual(exp(A.real), A.dual * exp(A.real));
}
DualNumber Log(DualNumber A) {
assert(A.real > 0);
return MakeDual(log(A.real), A.dual / A.real);
}
DualNumber Abs(DualNumber A) {
assert(A.real != 0);
return MakeDual(abs(A.real), A.dual * ((A.real > 0) ? 1 : -1));
}
DualNumber Power(DualNumber A, double e) {
return MakeDual(pow(A.real, e), A.dual * e * pow(A.real, e - 1));
}
DualNumber Func(double x) {
DualNumber d = MakeVariable(x);
return Div(Mul(Add(d, MakeConstant(1)), Sub(d, MakeConstant(2))),
Add(d, MakeConstant(3)));
}
int main(void) {
DualNumber d = Func(3);
printf("%f %f\n", d.real, d.dual);
return 0;
}