Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- struct Dual {
- float real;
- float img;
- Dual(float x, float y=0)
- :real{x}, img{y}
- {}
- };
- Dual operator+(Dual l, Dual r) {
- return {
- l.real+r.real,
- l.img +r.img
- };
- }
- Dual operator*(Dual l, Dual r) {
- return {
- l.real*r.real,
- l.img * r.real + l.real * r.img
- };
- }
- std::ostream& operator<< (std::ostream& o, Dual x) {
- return o << "("<<x.real << "|" <<x.img<<")";
- }
- auto f(auto x) {
- return x*x + 2*x + 3;
- }
- auto df(auto x) {
- return 2*x + 2;
- }
- static Dual d = {0,1};
- int main() {
- std::cout << "manual (f(3)|df(3)) = (" << f(3) << "|" << df(3) <<")"<< std::endl;
- std::cout << "using duals : (f(3)|df(3)) = " << f( 3 + 1*d ) << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement