Any form of simplify method, or at least expanding the expressions, available?

69 views
Skip to first unread message

Panagiotis Stavropoulos

unread,
Oct 3, 2019, 10:55:38 AM10/3/19
to symengine
Hello,

I am using this package to do symbolic calculations with c++.

First of thank you to the developers for this very useful package!

Now my actual issue. I am using SymEngine::Expression to store symbolic expressions and in the actual calculation their are a lot of operations of the form x*y+z*w, where x,y,z,w are expressions. I am running into an issue where basic simplifications are not happening. Following is a very simple program showing this behaviour:

Program in file test_basic_prob.cpp:

#include <symengine/cwrapper.h>
#include <symengine/expression.h>

int main(){

// Declerations
SymEngine::Expression b("b");
SymEngine::Expression a("a");

SymEngine::Expression x, y, w, z;
SymEngine::Expression res;

// Set up expressions
x = SymEngine::Expression(-3);
y = (SymEngine::Expression(3)-a);
z = (-SymEngine::Expression(9)+b);
w = SymEngine::Expression(-1);
res = x*y + z*w;

// Print results;
std::cout << " x = " << x << std::endl;
std::cout << " y = " << y << std::endl;
std::cout << " z = " << z << std::endl;
std::cout << " w = " << w << std::endl;
std::cout << " x*y + z*w = " << res << std::endl;

return 0;
}

Compile and output:
icpc -std=c++14 -c -I/usr/local/include/symengine test_basic_prob.cpp -o test_basic_prob.o
icpc -std=c++14 -I/usr/local/include/symengine -o test.out test_basic_prob.o -L/usr/local/lib -lsymengine -lgmp
./test.out
  x = -3
  y = 3 - a
 z = -9 + b
 w = -1
 x*y + z*w = -(-9 + b) - 3*(3 - a)

As you can see their is no problem compiling and running. The final result in res variable should have clearly been a - b or -b + a. However nothing is simplifying.
This issue is worst when a and b are the same variable, and the expression will not simplify to zero.

I tried going through the test programs to see if their is any instruction in the package to do this but I cannot seam to figure it out. Is their a way around this issue?

Isuru Fernando

unread,
Oct 3, 2019, 11:02:32 AM10/3/19
to symengine
In SymEngine, distribution of multiplication is not considered simple and is not done automatically. To do this you have to explicitly call expand.

Isuru

--
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/3f01a7d5-a827-4beb-8c4b-def84cbcf67d%40googlegroups.com.

Panagiotis Stavropoulos

unread,
Oct 3, 2019, 11:21:56 AM10/3/19
to symengine
Ok works great. That was simple! Thank you.
Reply all
Reply to author
Forward
0 new messages