how to correctly use relational operators

41 views
Skip to first unread message

Joe Wezorek

unread,
Mar 2, 2020, 4:00:42 PM3/2/20
to syme...@googlegroups.com
Hi,

I'm using symengine in a C++ project and am confused how I can get C++ boolean values from the Gt, Lt, Ge, Le, etc. Because they seem to only actually return a boolean result in case which the expressions one is comparing fully reduce into a "number" node rather than as an expression tree that defines a number but not as a single node. 

Consider the following

void test_sym_engine()
{
    namespace se = SymEngine;
    se::Expression three("1+1+1");
    se::Expression eight("2 ** 3");
    se::Expression one_hundred("50+50");
    se::Expression phi("(1 + sqrt(5)) / 2");

    std::cout << eight << " > " << three << " => " <<
        ((se::Gt(eight, three) == se::boolTrue) ? "true" : "false") << "\n";  // A
    std::cout << one_hundred << " > " << phi << " => " <<
        ((se::Gt(one_hundred, phi) == se::boolTrue) ? "true" : "false") << "\n"; // B
}
  
A will return true but B will not because B returns some object, I believe, representing the relation since lhs and rhs are not both simple numbers. Is there some way to do a comparison like this and get back a C++ bool as expected.

Also looking at the code for se::Gt(...) I see this

RCP<const Boolean> Gt(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs)
{
    return Lt(rhs, lhs);
}

but this is incorrect math, (a > b) is the same thing as (b <= a) not (b < a). This mistake seems to be all over the file symengine\logic.cpp

Thanks,
joe

Isuru Fernando

unread,
Mar 2, 2020, 4:09:01 PM3/2/20
to symengine
> Is there some way to do a comparison like this and get back a C++ bool as expected.

You can do, `eval_double(se::Gt(one_hundred, phi)) == 1.0` to evaluate the expression at 53 bits and get the result.  (If you need more precision, that's possible using eval_mpfr)


> (a > b) is the same thing as (b <= a) not (b < a)

I'm sorry, I don't understand. How is it that, (a > b) and (b <= a) are equal?  The first inequality means that (a != b),  but the second one implies that it is possible for a and b to be equal.

Idutu

--
You received this message because you are subscribed to the Google Groups "symengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to symengine+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/symengine/CAKpXZPfD1ppu4fKcM-4H9P0-_owM8%3DDrWmP51XhaRPyPBdWQ8Q%40mail.gmail.com.

Joe Wezorek

unread,
Mar 2, 2020, 4:20:07 PM3/2/20
to syme...@googlegroups.com
oh yeah you're right regarding the identities.

se::eval_double(se::Gt(one_hundred, phi)) == 1.0 

is not compiling under visual studio because se::Gt(...) does not return an expression, I think. I see Gt returning an RCP<const Boolean> unless im reading wrong



Isuru Fernando

unread,
Mar 2, 2020, 4:24:19 PM3/2/20
to symengine
Try, se::eval_double(*se::Gt(one_hundred, phi)) == 1.0

Isuru

Joe Wezorek

unread,
Mar 2, 2020, 5:27:13 PM3/2/20
to symengine
This works. thanks.
Joe
To unsubscribe from this group and stop receiving emails from it, send an email to syme...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "symengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to syme...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "symengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to syme...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages