How can I get a GiNaC::ex's name
Hello, Say I construct an ex by: GiNaC::ex E(GiNaC::symbol("abc")); How can I get back "abc" from E as a C++ string or char*? Now I do this by print the ex to a std::ostringstream, and get the string from the ostringstream. Thanks. -- Zhongxing Xu
This works for me: string s = ex_to<symbol>(E).get_name(); For example, --------------------------------------- #include <iostream> #include <ginac/ginac.h> using namespace std; using namespace GiNaC; int main() { ex e(symbol("abc")); string s = ex_to<symbol>(e).get_name(); cout << s << endl; return 0; } --------------------------------------- On Thu, 2007-05-24 at 18:46 +0800, Zhongxing Xu wrote:
Hello,
Say I construct an ex by: GiNaC::ex E(GiNaC::symbol("abc"));
How can I get back "abc" from E as a C++ string or char*?
Now I do this by print the ex to a std::ostringstream, and get the string from the ostringstream.
Thanks.
-- Zhongxing Xu
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de https://www.cebix.net/mailman/listinfo/ginac-list
On Thu, 2007-05-24 at 14:50 -0400, Warren Weckesser wrote:
This works for me:
string s = ex_to<symbol>(E).get_name();
I have a similar problem, if I define the clifford units this way, varidx nu(symbol("nu", "\\nu"), 3); ex basis1 = clifford_unit(mu, diag_matrix(lst(1, 1, 1)),0); ex e00 = basis1.subs(mu == 0), e01 = basis1.subs(mu == 1), e02 = basis1.subs(mu == 2); Then they are ploted cout << e00 << endl; Like this e~0 I want to rename using symbol.set_name(string), but wwhen I do cout << ex_to<symbol>(e00).get_name() << endl; Anything gets plotted. So I suspect that e00 is not a symbol an then, that I can not change its name.??? It woul be interesting to have this posibility, tough elements of diferent basis are right now plotted in the same way. Basis1 e00, plots as e~0 Basis2 e10, plots as e~0 Nevertheless if plotting in latex style, units are plotted in a different way. How can I deal with this?. Thanks in advance Javier
Dear Javier,
"JRG" == Javier Ros Ganuza <jros@unavarra.es> writes: JRG> It woul be interesting to have this posibility, tough elements JRG> of diferent basis are right now plotted in the same way.
Indeed it may be worth to see a difference in the output for Clifford units with different representation labels. I include a patch which give this in the way which is shown by such an example: #include <ginac/ginac.h> using namespace std; using namespace GiNaC; int main() { try { varidx mu(symbol("nu", "\\nu"), 3); ex basis1 = clifford_unit(mu, diag_matrix(lst(1, 1, 1)),0), basis2 = clifford_unit(mu, diag_matrix(lst(1, 1, 1)),1); ex e00 = basis1.subs(mu == 0), e10 = basis2.subs(mu == 0); cout << e00 << endl; // <-- e~0 cout << e10 << endl; // <-- e[1]~0 } catch (exception &p) { cerr << "Got problem: " << p.what() << endl; } } Best wishes, Vladimir -- Vladimir V. Kisil email: kisilv@maths.leeds.ac.uk -- www: http://maths.leeds.ac.uk/~kisilv/
Hello! On Thu, 2007-05-24 at 14:50 -0400, Warren Weckesser wrote:
This works for me:
string s = ex_to<symbol>(E).get_name();
Firstly, I'd like to note that GiNaC expressions do not have any "names" at all. Only symbols have "names" which serve merely as a printing representation. Two different symbols may have the same name, as in symbol x("x"), y("x"); // valid code or namespace A { symbol x("x"); } namespace B { symbol x("x"); } So your code will work if (and only if) E is a symbol. And it is good idea to check if E is indeed symbol, since ex_to<T> functions are unsafe (as documented in the manual), e.g. string s = "dunno"; if (is_a<symbol>(E)) s = ex_to<symbol>(E).get_name(); On Fri, May 25, 2007 at 10:48:44AM +0200, Javier Ros Ganuza wrote:
I have a similar problem, if I define the clifford units this way,
varidx nu(symbol("nu", "\\nu"), 3); ex basis1 = clifford_unit(mu, diag_matrix(lst(1, 1, 1)),0);
ex e00 = basis1.subs(mu == 0), e01 = basis1.subs(mu == 1), e02 = basis1.subs(mu == 2);
Then they are ploted
cout << e00 << endl;
Like this
e~0 I want to rename using symbol.set_name(string), but when I do
cout << ex_to<symbol>(e00).get_name() << endl;
This should segfault. e00 is not a symbol, it is element of Clifford algebra. So ex_to<symbol> is meaningless here...
Anything gets plotted.
Well, some sunny day it may even print something. Probably you are lucky :)
So I suspect that e00 is not a symbol an then,
It is definitely not a symbol. It is an element of Clifford algebra.
It woul be interesting to have this posibility, tough elements of diferent basis are right now plotted in the same way.
I don't think renaming symbols will help here.
Basis1 e00, plots as e~0
Basis2 e10, plots as e~0
Nevertheless if plotting in latex style, units are plotted in a different way.
Indeed. And this is $ cat test.cpp #include <iostream> #include <ginac/ginac.h> using namespace std; using namespace GiNaC; int main(int argc, char** argv) { varidx nu(symbol("nu", "\\nu"), 3); ex basis1 = clifford_unit(nu, diag_matrix(lst(1, 1, 1)), 0); ex basis2 = clifford_unit(nu, diag_matrix(lst(3, 1, 2)), 1); ex e10 = basis1.subs(nu == 0), e20 = basis2.subs(nu == 1); cout << latex << "e10 = " << e10 << endl << "e20 = " << e20 << endl << dflt << "e10 = " << e10 << endl << "e20 = " << e20 << endl; return 0; } $ g++ test.cpp; ./a.out e10 = \clifford[0]{e}^{{0} } e20 = \clifford[0]{e}^{{1} } e10 = e~0 e20 = e~1
How can I deal with this?.
May be make both formats print the same (well, almost) thing, like this patch does: diff --git a/ginac/clifford.cpp b/ginac/clifford.cpp index c7ce8d3..56ae54f 100644 --- a/ginac/clifford.cpp +++ b/ginac/clifford.cpp @@ -203,6 +203,7 @@ void clifford::do_print_dflt(const print_dflt & c, unsigned level) const seq[0].print(c, precedence()); c.s << "\\"; } else + c.s << "e[" << int(representation_label) << "]"; this->print_dispatch<inherited>(c, level); } Best regards, Alexei -- All science is either physics or stamp collecting.
Dear Alexei,
"SA" == Sheplyakov Alexei <varg@theor.jinr.ru> writes: SA> + c.s << "e[" << int(representation_label) << "]"; this-> print_dispatch<inherited>(c, level);
I think this would output something like e[0]e~0 instead of e[0]~0 as you probably intended. Best wishes, Vladimir -- Vladimir V. Kisil email: kisilv@maths.leeds.ac.uk -- www: http://maths.leeds.ac.uk/~kisilv/
It works!, thanks for your quick answers. Javier On Fri, 2007-05-25 at 14:32 +0100, Vladimir Kisil wrote:
Dear Alexei,
"SA" == Sheplyakov Alexei <varg@theor.jinr.ru> writes: SA> + c.s << "e[" << int(representation_label) << "]"; this-> print_dispatch<inherited>(c, level);
I think this would output something like e[0]e~0 instead of e[0]~0 as you probably intended.
Best wishes, Vladimir
participants (5)
-
Javier Ros Ganuza
-
varg@theor.jinr.ru
-
Vladimir Kisil
-
Warren Weckesser
-
Zhongxing Xu