SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
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 ( )
&

Contenu connexe

Tendances

C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 
Kotlin killed Java stars
Kotlin killed Java starsKotlin killed Java stars
Kotlin killed Java starsMatteo Bonifazi
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nmmohamed sikander
 
C tech questions
C tech questionsC tech questions
C tech questionsvijay00791
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programmingPrabhu Govind
 
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 ArrayMohammad Imam Hossain
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacionJeff Tu Pechito
 
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 Overloadingmohamed sikander
 
Assignement of c++
Assignement of c++Assignement of c++
Assignement of c++Syed Umair
 
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
 
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 2Rumman Ansari
 
Inteligencia artificial 4
Inteligencia artificial 4Inteligencia artificial 4
Inteligencia artificial 4Nauber Gois
 

Tendances (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
 

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

Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたAkira Maruoka
 
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 #4Ismar Silveira
 
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...Mario Fusco
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
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.docxsngyun4t79
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Lin Yo-An
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methodsphil_nash
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)hhliu
 
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...Codemotion
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogrammingMårten Rånge
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFTimur Safin
 

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

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++
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 

Dernier

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Dernier (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

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