SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Downloaden Sie, um offline zu lesen
C++ Expression Template
: y x
!!
double x = 1.0;
double y = 3.0 * x + 2.0;
double dydx = derivative(y) // ?
4 2 y
4 (Expression Template)
Expression Template
4
4 GOF Composite
4
4
4 2 !!
: <operator, 1, 2>
struct add_op {}; struct sub_op {}; struct mul_op {}; struct div_op {};
template<typename OP, typename E1, typename E2>
class binary_expression {
public:
binary_expression(const E1& e1, const E2& e2)
: _e1(e1), _e2(e2){
}
const E1& e1() const {return _e1;}
const E2& e2() const {return _e2;}
private:
const E1 _e1;
const E2 _e2;
};
class Variable {
public:
Variable(const double x): _x(x) {}
double get() const {return _x;}
private:
const double _x;
};
:
template<typename E1, typename E2>
auto operator +(const E1& x, const E2& y) {
return binary_expression<add_op, E1, E2>(x, y);
};
template<typename E1, typename E2>
auto operator -(const E1& x, const E2& y) {
return binary_expression<sub_op, E1, E2>(x, y);
};
template<typename E1, typename E2>
auto operator *(const E1& x, const E2& y) {
return binary_expression<mul_op, E1, E2>(x, y);
};
template<typename E1, typename E2>
auto operator /(const E1& x, const E2& y) {
return binary_expression<div_op, E1, E2>(x, y);
};
int main() {
auto x = Variable(1.0);
auto y0 = x + 2.0;
auto y1 = 3.0 * y0 + 5.0;
auto y2 = y1 * y0;
std::cout << typeid(y0).name() << std::endl;
std::cout << typeid(y1).name() << std::endl;
std::cout << typeid(y2).name() << std::endl;
}
4
code
auto y0 = x + 2.0;
std::cout << typeid(y0).name() << std::endl;
output
binary_expression<add_op, Variable, double>
code
auto y0 = x + 2.0;
auto y1 = 3.0 * y0 + 5.0;
std::cout << typeid(y1).name() << std::endl;
output
binary_expression<
add_op,
binary_expression<
mul_op, double, binary_expression<add_op, Variable, double>
>,
double
>
code
auto y0 = x + 2.0;
auto y1 = 3.0 * y0 + 5.0;
auto y2 = y1 * y0;
std::cout << typeid(y2).name() << std::endl;
output
binary_expression<
mul_op,
binary_expression<
add_op,
binary_expression<mul_op, double, binary_expression<add_op, Variable, double> >,
double
>,
binary_expression<add_op, Variable, double>
>
: !
auto eval(const Variable& e) {return e.get();}
auto eval(const double e) {return e;}
template<typename E1, typename E2>
auto eval(const binary_expression<add_op, E1, E2>& e) {
return eval(e.e1()) + eval(e.e2());
}
template<typename E1, typename E2>
auto eval(const binary_expression<mul_op, E1, E2>& e) {
return eval(e.e1()) * eval(e.e2());
}
code
auto x = Variable(1.0);
auto y0 = x + 2.0;
auto y1 = 3.0 * y0 + 5.0;
auto y2 = y1 * y0;
std::cout << eval(y2) << std::endl;
output
42
: eval
auto diff(const double e) {return 0;}
auto diff(const Variable& e) {return 1;}
template<typename E1, typename E2>
auto diff(const binary_expression<add_op, E1, E2>& e) {
// x + y -> dx + dy
return diff(e.e1()) + diff(e.e2());
}
template<typename E1, typename E2>
auto diff(const binary_expression<mul_op, E1, E2>& e) {
// x * y -> dx * y + x * dy
return diff(e.e1()) * eval(e.e2()) + eval(e.e1()) * diff(e.e2());
}
code
auto x = Variable(1.0);
auto y0 = x + 2.0;
auto y1 = 3.0 * y0 + 5.0;
auto y2 = y1 * y0;
std::cout << diff(y0) << std::endl;
std::cout << diff(y1) << std::endl;
std::cout << diff(y2) << std::endl;
output
1
3
23
!
!!
auto x = Variable(1.0);
auto y = 3.0 * x * x + 2.0 * x;
auto dydx = derivative(y)
std::cout << typeid(dydx).name() << std::endl;
y
// 6.0 * x + 2.0
binary_expression<
add_op,
binary_expression<mul_op, double, Variable>,
double
>
class one {};
class zero {};
auto derivative(const double e) {return zero();}
auto derivative(const Variable& e) {return one();}
template<typename E1, typename E2>
auto derivative(const binary_expression<add_op, E1, E2>& e) {
return derivative(e.e1()) + derivative(e.e2());
}
template<typename E1, typename E2>
auto derivative(const binary_expression<mul_op, E1, E2>& e) {
return e.e1() * derivative(e.e2()) + derivative(e.e1()) * e.e2();
}
code
auto x = Variable(1.0);
auto y0 = x + 2.0;
std::cout << typeid(y0).name() << std::endl;
output
binary_expression<add_op, one, zero>
code
auto x = Variable(1.0);
auto y0 = x + 2.0;
auto y1 = 3.0 * y0 + 5.0;
std::cout << typeid(y1).name() << std::endl;
output
binary_expression<
add_op,
binary_expression<
add_op,
binary_expression<
mul_op,
double,
binary_expression<add_op, one, zero>
>,
binary_expression<
mul_op,
zero,
binary_expression<add_op, Variable, double>
>
>,
zero
>
!
: !
4 a + 0 = a
template <typename E>
auto operator +(const E& e, const zero&) {return e;}
template <typename E>
auto operator +(const zero&, const E& e) {return e;}
template <typename E>
auto operator *(const E& e, const zero&) {return zero();}
template <typename E>
auto operator *(const zero&, const E& e) {return zero();}
template <typename E>
auto operator *(const E& e, const one&) {return e;}
template <typename E>
auto operator *(const one&, const E& e) {return e;}
code
auto x = Variable(1.0);
auto y0 = x + 2.0;
std::cout << typeid(y0).name() << std::endl;
output! one !
one
code
auto x = Variable(1.0);
auto y0 = x + 2.0;
auto y1 = 3.0 * y0 + 5.0;
std::cout << typeid(y1).name() << std::endl;
output!! double !!
double
4
4 Expression Template ( )
&

Weitere ähnliche Inhalte

Was ist angesagt?

Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
Jeff Tu Pechito
 
Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)
Syed Umair
 

Was ist angesagt? (20)

C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Kotlin killed Java stars
Kotlin killed Java starsKotlin killed Java stars
Kotlin killed Java stars
 
Functional DDD
Functional DDDFunctional DDD
Functional DDD
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
Pointers
PointersPointers
Pointers
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Array notes
Array notesArray notes
Array notes
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
 
SPL 10.1 | Manual Tracing on Basic Loop & 1D Array
SPL 10.1 | Manual Tracing on Basic Loop & 1D ArraySPL 10.1 | Manual Tracing on Basic Loop & 1D Array
SPL 10.1 | Manual Tracing on Basic Loop & 1D Array
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
 
Qno 1 (e)
Qno 1 (e)Qno 1 (e)
Qno 1 (e)
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
Assignement of c++
Assignement of c++Assignement of c++
Assignement of c++
 
Program presentation
Program presentationProgram presentation
Program presentation
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
pointers 1
pointers 1pointers 1
pointers 1
 
MFC Polygon
MFC PolygonMFC Polygon
MFC Polygon
 
Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2
 
Inteligencia artificial 4
Inteligencia artificial 4Inteligencia artificial 4
Inteligencia artificial 4
 

Ähnlich wie C++ Expression Templateを使って式をコンパイル時に微分

C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 

Ähnlich wie C++ Expression Templateを使って式をコンパイル時に微分 (20)

C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Make two classes called Points and Lines- Class points should have the.docx
Make two classes called Points and Lines- Class points should have the.docxMake two classes called Points and Lines- Class points should have the.docx
Make two classes called Points and Lines- Class points should have the.docx
 
Qno 1 (c)
Qno 1 (c)Qno 1 (c)
Qno 1 (c)
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
Php 5.6
Php 5.6Php 5.6
Php 5.6
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methods
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Hack tutorial
Hack tutorialHack tutorial
Hack tutorial
 
L10
L10L10
L10
 
Clean code
Clean codeClean code
Clean code
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogramming
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

C++ Expression Templateを使って式をコンパイル時に微分

  • 2. : y x !! double x = 1.0; double y = 3.0 * x + 2.0; double dydx = derivative(y) // ? 4 2 y 4 (Expression Template)
  • 3. Expression Template 4 4 GOF Composite 4 4 4 2 !!
  • 4. : <operator, 1, 2> struct add_op {}; struct sub_op {}; struct mul_op {}; struct div_op {}; template<typename OP, typename E1, typename E2> class binary_expression { public: binary_expression(const E1& e1, const E2& e2) : _e1(e1), _e2(e2){ } const E1& e1() const {return _e1;} const E2& e2() const {return _e2;} private: const E1 _e1; const E2 _e2; }; class Variable { public: Variable(const double x): _x(x) {} double get() const {return _x;} private: const double _x; };
  • 5. : template<typename E1, typename E2> auto operator +(const E1& x, const E2& y) { return binary_expression<add_op, E1, E2>(x, y); }; template<typename E1, typename E2> auto operator -(const E1& x, const E2& y) { return binary_expression<sub_op, E1, E2>(x, y); }; template<typename E1, typename E2> auto operator *(const E1& x, const E2& y) { return binary_expression<mul_op, E1, E2>(x, y); }; template<typename E1, typename E2> auto operator /(const E1& x, const E2& y) { return binary_expression<div_op, E1, E2>(x, y); };
  • 6. int main() { auto x = Variable(1.0); auto y0 = x + 2.0; auto y1 = 3.0 * y0 + 5.0; auto y2 = y1 * y0; std::cout << typeid(y0).name() << std::endl; std::cout << typeid(y1).name() << std::endl; std::cout << typeid(y2).name() << std::endl; } 4
  • 7. code auto y0 = x + 2.0; std::cout << typeid(y0).name() << std::endl; output binary_expression<add_op, Variable, double>
  • 8. code auto y0 = x + 2.0; auto y1 = 3.0 * y0 + 5.0; std::cout << typeid(y1).name() << std::endl; output binary_expression< add_op, binary_expression< mul_op, double, binary_expression<add_op, Variable, double> >, double >
  • 9. code auto y0 = x + 2.0; auto y1 = 3.0 * y0 + 5.0; auto y2 = y1 * y0; std::cout << typeid(y2).name() << std::endl; output binary_expression< mul_op, binary_expression< add_op, binary_expression<mul_op, double, binary_expression<add_op, Variable, double> >, double >, binary_expression<add_op, Variable, double> >
  • 10. : ! auto eval(const Variable& e) {return e.get();} auto eval(const double e) {return e;} template<typename E1, typename E2> auto eval(const binary_expression<add_op, E1, E2>& e) { return eval(e.e1()) + eval(e.e2()); } template<typename E1, typename E2> auto eval(const binary_expression<mul_op, E1, E2>& e) { return eval(e.e1()) * eval(e.e2()); }
  • 11. code auto x = Variable(1.0); auto y0 = x + 2.0; auto y1 = 3.0 * y0 + 5.0; auto y2 = y1 * y0; std::cout << eval(y2) << std::endl; output 42
  • 12. : eval auto diff(const double e) {return 0;} auto diff(const Variable& e) {return 1;} template<typename E1, typename E2> auto diff(const binary_expression<add_op, E1, E2>& e) { // x + y -> dx + dy return diff(e.e1()) + diff(e.e2()); } template<typename E1, typename E2> auto diff(const binary_expression<mul_op, E1, E2>& e) { // x * y -> dx * y + x * dy return diff(e.e1()) * eval(e.e2()) + eval(e.e1()) * diff(e.e2()); }
  • 13. code auto x = Variable(1.0); auto y0 = x + 2.0; auto y1 = 3.0 * y0 + 5.0; auto y2 = y1 * y0; std::cout << diff(y0) << std::endl; std::cout << diff(y1) << std::endl; std::cout << diff(y2) << std::endl; output 1 3 23
  • 14. ! !!
  • 15. auto x = Variable(1.0); auto y = 3.0 * x * x + 2.0 * x; auto dydx = derivative(y) std::cout << typeid(dydx).name() << std::endl; y // 6.0 * x + 2.0 binary_expression< add_op, binary_expression<mul_op, double, Variable>, double >
  • 16. class one {}; class zero {}; auto derivative(const double e) {return zero();} auto derivative(const Variable& e) {return one();} template<typename E1, typename E2> auto derivative(const binary_expression<add_op, E1, E2>& e) { return derivative(e.e1()) + derivative(e.e2()); } template<typename E1, typename E2> auto derivative(const binary_expression<mul_op, E1, E2>& e) { return e.e1() * derivative(e.e2()) + derivative(e.e1()) * e.e2(); }
  • 17. code auto x = Variable(1.0); auto y0 = x + 2.0; std::cout << typeid(y0).name() << std::endl; output binary_expression<add_op, one, zero>
  • 18. code auto x = Variable(1.0); auto y0 = x + 2.0; auto y1 = 3.0 * y0 + 5.0; std::cout << typeid(y1).name() << std::endl; output binary_expression< add_op, binary_expression< add_op, binary_expression< mul_op, double, binary_expression<add_op, one, zero> >, binary_expression< mul_op, zero, binary_expression<add_op, Variable, double> > >, zero >
  • 19. !
  • 20. : ! 4 a + 0 = a template <typename E> auto operator +(const E& e, const zero&) {return e;} template <typename E> auto operator +(const zero&, const E& e) {return e;} template <typename E> auto operator *(const E& e, const zero&) {return zero();} template <typename E> auto operator *(const zero&, const E& e) {return zero();} template <typename E> auto operator *(const E& e, const one&) {return e;} template <typename E> auto operator *(const one&, const E& e) {return e;}
  • 21. code auto x = Variable(1.0); auto y0 = x + 2.0; std::cout << typeid(y0).name() << std::endl; output! one ! one
  • 22. code auto x = Variable(1.0); auto y0 = x + 2.0; auto y1 = 3.0 * y0 + 5.0; std::cout << typeid(y1).name() << std::endl; output!! double !! double