Hi, I want to compare (or test the equality of) two ex objects which contain symbols. a = a0 + x*a1; b = a0 + a1*x; If the ex objects are evaluated (symbolically), such that a-b becomes 0, then the test would be finished. However, how do I evaluate such a expression as a-b symbolically? Or are there other ways of comparing two such ex objects? Any suggestions on how to do this? Thanks, Vera Louise Hauge
Hi, #include <ginac/ginac.h> #include <iostream> using namespace std; using namespace GiNaC; int main() { symbol a0("a0"), x("x"), a1("a1"); ex a = a0 + x*a1; ex b = a0 + a1*x; ex diff = a - b; ex diff_expd = diff.expand(); cout << "a-b = " << diff_expd << endl; // should give: a-b = 0 return 0; }
I want to compare (or test the equality of) two ex objects which contain symbols. a = a0 + x*a1; b = a0 + a1*x;
If the ex objects are evaluated (symbolically), such that a-b becomes 0, then the test would be finished.
However, how do I evaluate such a expression as a-b symbolically?
is this what you wanted? Cheers Markus
Hi, Thank you for your suggestion! It is almost what I want to do. However, in my case also the symbols a0 and a1 are different objects with the same text: int main(){ ex A = symbolic_matrix(1,2, "a"); ex B = symbolic_matrix(1,2, "a"); symbol x("x"); ex a, b; int n=0; for (const_iterator i = A.begin(); i != A.end(); ++i) { a += (*i)*pow(x,n); n++; } n=0; for (const_iterator i = B.begin(); i != B.end(); ++i) { b += (*i)*pow(x,n); n++; } cout << "a-b=" << (a-b).expand() << endl; return 0; } Any suggestions when the expressions "a0+a1*x" are produced this way? Maybe string comparison of the texts in the coefficient objects is a way to do it? -- Vera Louise Hauge On Thu, 18 Aug 2005, Markus Knodel wrote:
Hi,
#include <ginac/ginac.h> #include <iostream>
using namespace std; using namespace GiNaC;
int main() {
symbol a0("a0"), x("x"), a1("a1");
ex a = a0 + x*a1; ex b = a0 + a1*x;
ex diff = a - b;
ex diff_expd = diff.expand();
cout << "a-b = " << diff_expd << endl;
// should give: a-b = 0
return 0; }
I want to compare (or test the equality of) two ex objects which contain symbols. a = a0 + x*a1; b = a0 + a1*x;
If the ex objects are evaluated (symbolically), such that a-b becomes 0, then the test would be finished.
However, how do I evaluate such a expression as a-b symbolically?
is this what you wanted?
Cheers Markus
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de http://thep.physik.uni-mainz.de/mailman/listinfo/ginac-list
Hi Vera Just noticing your lines:
ex A = symbolic_matrix(1,2, "a"); ex B = symbolic_matrix(1,2, "a");
Are delibarately creating two different sets of symbols here with the same name? I don't know why you'd want to do that. This means that A and B will *look* the same but will not be treated by GiNaC as the same thing - different symbols, just happening to have the same labels. Is that any help? JP Vera Louise Hauge wrote:
Hi,
Thank you for your suggestion!
It is almost what I want to do. However, in my case also the symbols a0 and a1 are different objects with the same text:
int main(){
ex A = symbolic_matrix(1,2, "a"); ex B = symbolic_matrix(1,2, "a"); symbol x("x");
ex a, b; int n=0; for (const_iterator i = A.begin(); i != A.end(); ++i) { a += (*i)*pow(x,n); n++; } n=0; for (const_iterator i = B.begin(); i != B.end(); ++i) { b += (*i)*pow(x,n); n++; } cout << "a-b=" << (a-b).expand() << endl; return 0; }
Any suggestions when the expressions "a0+a1*x" are produced this way?
Maybe string comparison of the texts in the coefficient objects is a way to do it?
-- Vera Louise Hauge
On Thu, 18 Aug 2005, Markus Knodel wrote:
Hi,
#include <ginac/ginac.h> #include <iostream>
using namespace std; using namespace GiNaC;
int main() {
symbol a0("a0"), x("x"), a1("a1");
ex a = a0 + x*a1; ex b = a0 + a1*x;
ex diff = a - b;
ex diff_expd = diff.expand();
cout << "a-b = " << diff_expd << endl;
// should give: a-b = 0
return 0; }
I want to compare (or test the equality of) two ex objects which contain symbols. a = a0 + x*a1; b = a0 + a1*x;
If the ex objects are evaluated (symbolically), such that a-b becomes 0, then the test would be finished.
However, how do I evaluate such a expression as a-b symbolically?
is this what you wanted?
Cheers Markus
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de http://thep.physik.uni-mainz.de/mailman/listinfo/ginac-list
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de http://thep.physik.uni-mainz.de/mailman/listinfo/ginac-list
Hi, The example I listed was just a constructed example to illustrate what kind of comparison I want to do. The point is exactly that there are two different sets of symbols, which share the same text/names. In the actual case where I want to do this comparison, it is not really possible to set them equal (i.e ex B = A;). Hopefully I have made my question clearer now. -- Vera Louise Hauge On Tue, 23 Aug 2005, John Pye wrote:
Hi Vera
Just noticing your lines:
ex A = symbolic_matrix(1,2, "a"); ex B = symbolic_matrix(1,2, "a");
Are delibarately creating two different sets of symbols here with the same name? I don't know why you'd want to do that. This means that A and B will *look* the same but will not be treated by GiNaC as the same thing - different symbols, just happening to have the same labels.
Is that any help?
JP
Vera Louise Hauge wrote:
Hi,
Thank you for your suggestion!
It is almost what I want to do. However, in my case also the symbols a0 and a1 are different objects with the same text:
int main(){
ex A = symbolic_matrix(1,2, "a"); ex B = symbolic_matrix(1,2, "a"); symbol x("x");
ex a, b; int n=0; for (const_iterator i = A.begin(); i != A.end(); ++i) { a += (*i)*pow(x,n); n++; } n=0; for (const_iterator i = B.begin(); i != B.end(); ++i) { b += (*i)*pow(x,n); n++; } cout << "a-b=" << (a-b).expand() << endl; return 0; }
Any suggestions when the expressions "a0+a1*x" are produced this way?
Maybe string comparison of the texts in the coefficient objects is a way to do it?
-- Vera Louise Hauge
On Thu, 18 Aug 2005, Markus Knodel wrote:
Hi,
#include <ginac/ginac.h> #include <iostream>
using namespace std; using namespace GiNaC;
int main() {
symbol a0("a0"), x("x"), a1("a1");
ex a = a0 + x*a1; ex b = a0 + a1*x;
ex diff = a - b;
ex diff_expd = diff.expand();
cout << "a-b = " << diff_expd << endl;
// should give: a-b = 0
return 0; }
I want to compare (or test the equality of) two ex objects which contain symbols. a = a0 + x*a1; b = a0 + a1*x;
If the ex objects are evaluated (symbolically), such that a-b becomes 0, then the test would be finished.
However, how do I evaluate such a expression as a-b symbolically?
is this what you wanted?
Cheers Markus
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de http://thep.physik.uni-mainz.de/mailman/listinfo/ginac-list
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de http://thep.physik.uni-mainz.de/mailman/listinfo/ginac-list
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de http://thep.physik.uni-mainz.de/mailman/listinfo/ginac-list
On Mon, 22 Aug 2005, Vera Louise Hauge wrote:
It is almost what I want to do. However, in my case also the symbols a0 and a1 are different objects with the same text:
int main(){
ex A = symbolic_matrix(1,2, "a"); ex B = symbolic_matrix(1,2, "a"); symbol x("x");
ex a, b; int n=0; for (const_iterator i = A.begin(); i != A.end(); ++i) { a += (*i)*pow(x,n); n++; } n=0; for (const_iterator i = B.begin(); i != B.end(); ++i) { b += (*i)*pow(x,n); n++; } cout << "a-b=" << (a-b).expand() << endl; return 0; }
Any suggestions when the expressions "a0+a1*x" are produced this way?
Maybe string comparison of the texts in the coefficient objects is a way to do it?
Of course, there are two symbols a0 and a1 each in your example above. They are different objects that just happen to share the same print-name. Why don't you assign the elements of B that are supposed to be equal to some previously used symbols to these symbols? Try initializing all of B as a copy of A ex B = A; and you'll see. If for some reason there is no way you can do this, a global symbol directory might be of help. See this FAQ: <http://www.ginac.de/FAQ.html#flyweightfactory>. Regards -richy. -- Richard B. Kreckel <http://www.ginac.de/~kreckel/>
I typed the attached into ginsh, maybe it helps,
lsolve({a0+x*a1 == y, y - (b0+b1*x) ==0}, {x,y}); {x==(a1-b1)^(-1)*(-a0+b0),y==(a1-b1)^(-1)*(b0*a1-a0*b1)}
You can also the 'collect' expression on a-b to help simplify. Can you give some more information on what you need to do? JP Vera Louise Hauge wrote:
Hi,
I want to compare (or test the equality of) two ex objects which contain symbols. a = a0 + x*a1; b = a0 + a1*x;
If the ex objects are evaluated (symbolically), such that a-b becomes 0, then the test would be finished.
However, how do I evaluate such a expression as a-b symbolically?
Or are there other ways of comparing two such ex objects?
Any suggestions on how to do this?
Thanks,
Vera Louise Hauge
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de http://thep.physik.uni-mainz.de/mailman/listinfo/ginac-list
On Thu, 18 Aug 2005, Vera Louise Hauge wrote:
I want to compare (or test the equality of) two ex objects which contain symbols. a = a0 + x*a1; b = a0 + a1*x;
If the ex objects are evaluated (symbolically), such that a-b becomes 0, then the test would be finished.
However, how do I evaluate such a expression as a-b symbolically?
Or are there other ways of comparing two such ex objects?
Any suggestions on how to do this?
I fail to understand: Expressions are canonicalized by eval() if that can be done within "reasonable" time [0]. Normally, reasonable time means in order < O(N^2), where N would be the size of the expression (number of monomials in a polynomial in your example). In your case, that means that in GiNaC the two ex objects cannot be expressed the way you wrote them because they are canonicalized immediately. You should really try. More generally, regarding test for zero, I recommend you read about Richardson's Theorem in [1] or [2]. Regards -richy. [0] <http://www.ginac.de/FAQ.html#evaluation> [1] <http://mathworld.wolfram.com/RichardsonsTheorem.html> [2] <http://www.cis.upenn.edu/~wilf/AeqB.html>, page 5. -- Richard B. Kreckel <http://www.ginac.de/~kreckel/>
Hi! On Thu, Aug 18, 2005 at 03:01:07PM +0200, Vera Louise Hauge wrote:
a = a0 + x*a1; b = a0 + a1*x;
If the ex objects are evaluated (symbolically), such that a-b becomes 0, then the test would be finished.
However, how do I evaluate such a expression as a-b symbolically?
ex c = a - b;
Or are there other ways of comparing two such ex objects?
if (a == b) { ... } Bye, Christian -- / Physics is an algorithm \/ http://www.uni-mainz.de/~bauec002/
participants (5)
-
Christian Bauer
-
John Pye
-
Markus Knodel
-
Richard B. Kreckel
-
Vera Louise Hauge