Giter VIP home page Giter VIP logo

Comments (6)

jcarius avatar jcarius commented on June 4, 2024

take a look at things like CppAD::CondExpLt

from cppadcodegen.

bradbell avatar bradbell commented on June 4, 2024

I would suggest looking at
https://coin-or.github.io/CppAD/doc/condexp.htm

from cppadcodegen.

joaoleal avatar joaoleal commented on June 4, 2024

Hi,
You can write conditions which will generate if and else in the generated source code.
Since it is not possible to overload the keywords if/else, you need to write your model using special functions for this purpose.
Information about these functions is available at (as suggested by Brad):
https://coin-or.github.io/CppAD/doc/condexp.htm
You just need to use the active data type AD<CG<scalar>> (scalar can be a double for instance) and register the branches as you would normally with CppAD (using AD<scalar>).

Please also take a look at:
https://github.com/joaoleal/CppADCodeGen/blob/master/test/cppad/cg/operations/cond_exp.hpp

from cppadcodegen.

neodyme60 avatar neodyme60 commented on June 4, 2024

Thank you very much for your help.

I have another similar question, I did not find a way to do "if...elseif....else".
I basically need to do something similar to what this guy is doing here(beginif/BeginElseIf) with his own AD lib =>here and here.

Is there a way to do something equivalent with CppAD ?

from cppadcodegen.

bradbell avatar bradbell commented on June 4, 2024

It is possible to do this sort of thing by making the output of one conditional expression the input to another. For example; see
https://github.com/coin-or/CppAD/blob/master/include/cppad/core/atan2.hpp

from cppadcodegen.

joaoleal avatar joaoleal commented on June 4, 2024

CppAD(Codegen) conditional expressions (if/else) can be implemented using;:

  • out = CondExpLt(a,b,c,d) --> if(a < b) {out = c} else {out = d},
  • out = CondExpLe(a,b,c,d) --> if(a <= b) {out = c} else {out = d},
  • out = CondExpEq(a,b,c,d) --> if(a == b) {out = c} else {out = d},
  • out = CondExpGe(a,b,c,d) --> if(a >= b) {out = c} else {out = d}, and
  • out = CondExpGt(a,b,c,d) --> if(a > b) {out = c} else {out = d}.

See:
https://github.com/joaoleal/CppADCodeGen/blob/6a4f6609365cb25a15e691d7357c84d293491472/include/cppad/cg/cond_exp_op.hpp

from cppadcodegen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.