Skip to content

Handling conditional expressions outside of if statements. #28

Description

@Schlevidon

Problem description

Currently, IFDIFF assumes that conditional expressions (e.g. a >= b) will only appear in if conditions. Additionaly, each if condition may contain only one conditional operator (e.g. so if a >= b + c * (d > e) is not allowed).
A conditional operator that appears outside of an if condition will be ignored silently and could lead to an undetected semantic error.
Multiple conditional operators in a single if condition will make IFDIFF throw an error.

Example

For example, here is the canonical example with if statements replaced by conditional operators. IFDIFF will silently compute the wrong solution without throwing any errors (although attempting to compute sensitivities will result in errors).

function dx = comp_canonicalExampleRHS(t,x,p)

dx = zeros(2,1);
dx(1) = 0.01 * t.^2  +  x(2).^3;

if1 = x(1) < p(1);
if2 = x(1) < p(1) + 0.5;

dx(2) = 5 * ~if1 * if2;

% We need at least one ctrlif, otherwise preprocessing will error
if 1 >= 0, end
end

Proposed solution

Obviously handling all edge cases is unrealistic because there will always be one way or another to trick IFDIFF. However, I think it would be beneficial to at least issue a warning if IFDIFF detects a comparison operator outside of an if condition. At the moment, IFDIFF does not respond to comparison operators outside of if conditions in any way and will silently compute a wrong solution, which would be a disaster for unaware users.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions