-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code generation with if/else statements #74
Comments
Have you tried using a conditional expression ? |
@bradbell Thanks for your help! I have tried, but I got, among others, the following error:
It looks like there is a problem with the type |
All of the arguments to a conditional expression must have the same type. It appears that you are mixing double and AD type arguments. Try converting the double to and AD type first; e.g.
|
Ah, rookie mistake! You are right, now everything works as desired; thanks a lot again! |
@joaoleal I think this issues should be converted to a discussion (github allows for that). |
Hi all, I am reopening this issue because I cannot find a way to have more general if/else statements with CppAD/CppADCodeGen. Indeed, I would like to have a set of operations inside a branch of the conditional operation. For instance:
The AD conditional expressions are limited in this regard since they must return a scalar and not, e.g., Eigen vectors. To cope with this limitation, I also tried to evaluate lambdas containing vector operations and then returning a scalar, but this did not correctly generate the AD code. Thus, the only solution I found so far requires me to write conditional expressions for each and every component of the vectors in the computations; but this is very ugly and error prone. Is there a better way to achieve this objective? |
Please try the following example:
|
@bradbell thank you for your help, but this is not exactly what I am trying to achieve. Basically, you suggest to compute a vector |
@fdevinc In CppAD, you have to record both branches to be able to conditionally execute the desired code. Some execution can be skipped if you optimize your function; see Optimize on I do not know if CppADCodeGen has this type of optimization built in. |
I see, this is unfortunate! Then, I guess that the only way is to create different conditional expressions for each vector components. |
In CppADCodegen, conditionals are converted into if/else statement in the generated source code. Consequently, even if you need to tape both sides, only one of the sides of the if will be evaluated at runtime. |
Thanks! Right now I am having another issue with if/else statements -- probably related to this. Consider this function:
where
As you can see, at the commented line a division by zero happens when the norm of Apart from using |
@fdevinc Can you reproduce this derivative problem with a simple example just using CppAD ? |
I could not reproduce this error by only using CppAD: unless I am wrong, it looks like it is a CppADCodeGen issue. But I noticed something interesting. Consider the following code:
When I print the Jacobian
|
Here is what I get when I try to run your example above with CppAD and NDEBUG not defined:
|
Sorry, I have just updated my code: the vector |
Hello! I am looking to use this package but before I do, I want to know if it is possible at all to write conditionals for if/else statements like this one
From what I read, it is not possible to add conditionals inside conditionals i.e. do something like |
Take a look at the implementation of atan2 at the end of the CondExp documentation on You will note that it implements the following four cases:
Also note that for this case, the result is continuous, exept at (0,0), so we do not have to worry about the boundary cases; i.e., when x=0 or y=0. |
I have modified the simple example in the documentation by changing the line
to
but this results in the following error:
This means that to generate the code for if/else statements I should use CppAD parameters. However, to the best of my knowledge, this feature is currently missing in CppADCodeGen, where parameters can only be emulated by editing the sparsity patterns of the models. Is there a way to generate the derivatives for code containing if/else statements? If yes, how should I modify the above example to make it work?
The text was updated successfully, but these errors were encountered: