Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4.  
  5. struct Dual {
  6. float real;
  7. float img;
  8.  
  9. Dual(float x, float y=0)
  10. :real{x}, img{y}
  11. {}
  12. };
  13.  
  14. Dual operator+(Dual l, Dual r) {
  15. return {
  16. l.real+r.real,
  17. l.img +r.img
  18. };
  19. }
  20.  
  21. Dual operator*(Dual l, Dual r) {
  22. return {
  23. l.real*r.real,
  24. l.img * r.real + l.real * r.img
  25. };
  26. }
  27.  
  28. std::ostream& operator<< (std::ostream& o, Dual x) {
  29. return o << "("<<x.real << "|" <<x.img<<")";
  30. }
  31.  
  32.  
  33. auto f(auto x) {
  34. return x*x + 2*x + 3;
  35. }
  36.  
  37. auto df(auto x) {
  38. return 2*x + 2;
  39. }
  40.  
  41. static Dual d = {0,1};
  42.  
  43. int main() {
  44. std::cout << "manual (f(3)|df(3)) = (" << f(3) << "|" << df(3) <<")"<< std::endl;
  45. std::cout << "using duals : (f(3)|df(3)) = " << f( 3 + 1*d ) << std::endl;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement