Skip to content
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

Question about conditionals. #11

Open
dnmnsmith opened this issue Jan 23, 2024 · 1 comment
Open

Question about conditionals. #11

dnmnsmith opened this issue Jan 23, 2024 · 1 comment

Comments

@dnmnsmith
Copy link

Hi Emanuel-

Looking at how to handle conditionals, as not everything maps to linear functions. I'd really like a fn (a,b,c) which returns b if a else c, or functionality equivalent to a ? b : c

I don't see anything supporting that in the framework. Lunchtime hacking turned up the below.

If this approach is good, it may be worth including in examples. If not, what is the recommended approach, and is a ternary operator a reasonable feature request?

Thanks!

Duncan

        context.RegisterLogical("<=", (a, b) => a <= b, Precedence.Addition);
        context.RegisterLogical(">", (a, b) => a > b, Precedence.Addition);

        context.RegisterBinary("if",(a,b) => (b != 0) ? a : 0, Precedence.Addition);

        MathExpr expr = new MathExpr("({x} if {x} <= 0.5) + (({x} - 0.5) if ({x} > 0.5))", context);

        var fn = expr.Compile("x");

        Debug.Assert(IsNear(fn(0.0), 0));
        Debug.Assert(IsNear(fn(0.75), 0.25));
@miroiu
Copy link
Owner

miroiu commented Jan 23, 2024

Hi Duncan,

Conditionals are not part of the library and I don't believe they would work well with the existing features. The best and fastest thing I could do is to add support for user defined functions. (see PR: #12)
This is a breaking change because new functionality was added to IMathContext which is a public interface.

While I'm not comfortable with adding this feature into the library right now, I may consider it for a future major release.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants