patch: valid C code for idx class with print_csrc
Hi! Printing an indexed expression using an instance of the print_csrc_double class produces invalid C code (the same syntax that is used in the tutorial http://www.ginac.de/tutorial/Indexed-objects.html#Indexed-objects) in ginac-1.3.7. I understand that the index class in ginac is more powerful than the indexing facilities of the C language, but still I expected print_csrc_double to produce valid C code simply due to its name. So I added a few lines which handle the case of integer indices (numeric or symbolic) stored in the idx class (patch attached). Kind regards, Markus -- Markus Grabner - Computer Graphics and Vision Graz University of Technology, Inffeldgasse 16a/II, 8010 Graz, Austria Phone: +43/316/873-5041, Fax: +43/316/873-5050 WWW: http://www.icg.tugraz.at/Members/grabner
Hello! On Mon, Jul 02, 2007 at 01:42:14PM +0200, Markus Grabner wrote:
Hi!
Printing an indexed expression using an instance of the print_csrc_double class produces invalid C code (the same syntax that is used in the tutorial http://www.ginac.de/tutorial/Indexed-objects.html#Indexed-objects) in ginac-1.3.7. I understand that the index class in ginac is more powerful than the indexing facilities of the C language, but still I expected print_csrc_double to produce valid C code simply due to its name. So I added a few lines which handle the case of integer indices (numeric or symbolic) stored in the idx class (patch attached).
Thanks for a patch. Unfortunately it breaks API, so it is not suitable for GiNaC 1.3 as is. Here is my variant: [PATCH] print_csrc_double prints indexed expressions as a valid C code. This one is suitable for GiNaC 1.3 --- ginac/idx.cpp | 13 +++++++++++-- 1 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ginac/idx.cpp b/ginac/idx.cpp index 972967d..10cca17 100644 --- a/ginac/idx.cpp +++ b/ginac/idx.cpp @@ -150,8 +150,17 @@ void idx::print_index(const print_context & c, unsigned level) const void idx::do_print(const print_context & c, unsigned level) const { - c.s << "."; - print_index(c, level); + if (is_a<print_csrc_double>(c)) { + c.s << "["; + if (value.info(info_flags::integer)) + c.s << ex_to<numeric>(value).to_int(); + else + value.print(c); + c.s << "]"; + } else { + c.s << "."; + print_index(c, level); + } } void idx::do_print_latex(const print_latex & c, unsigned level) const -- 1.4.4.4 And this one is for development branch: [PATCH] print_csrc_double prints indexed expressions as a valid C code. --- ginac/idx.cpp | 11 +++++++++++ ginac/idx.h | 1 + 2 files changed, 12 insertions(+), 0 deletions(-) diff --git a/ginac/idx.cpp b/ginac/idx.cpp index 80e66bc..8d0497f 100644 --- a/ginac/idx.cpp +++ b/ginac/idx.cpp @@ -37,6 +37,7 @@ namespace GiNaC { GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(idx, basic, print_func<print_context>(&idx::do_print). print_func<print_latex>(&idx::do_print_latex). + print_func<print_csrc>(&idx::do_print_csrc). print_func<print_tree>(&idx::do_print_tree)) GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(varidx, idx, @@ -161,6 +162,16 @@ void idx::do_print_latex(const print_latex & c, unsigned level) const c.s << "}"; } +void idx::do_print_csrc(const print_csrc & c, unsigned level) const +{ + c.s << "["; + if (value.info(info_flags::integer)) + c.s << ex_to<numeric>(value).to_int(); + else + value.print(c); + c.s << "]"; +} + void idx::do_print_tree(const print_tree & c, unsigned level) const { c.s << std::string(level, ' ') << class_name() << " @" << this diff --git a/ginac/idx.h b/ginac/idx.h index 930994d..d859b2e 100644 --- a/ginac/idx.h +++ b/ginac/idx.h @@ -96,6 +96,7 @@ public: protected: void print_index(const print_context & c, unsigned level) const; void do_print(const print_context & c, unsigned level) const; + void do_print_csrc(const print_csrc & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; void do_print_tree(const print_tree & c, unsigned level) const; -- 1.4.4.4 Hopefully I did not break anything :) Best regards, Alexei -- All science is either physics or stamp collecting.
Hi, I applied the print_csrc patch (Alexei's version) to CVS. Thanks! Jens
Am Montag, 9. Juli 2007 17:53 schrieb Jens Vollinga:
Hi,
I applied the print_csrc patch (Alexei's version) to CVS. Thanks! BTW, I noticed a similar issue in fderivative. It outputs code like
D[0](myfunc)(...) regardless of the print context. While it should be possible to declare D such that the above statement is valid C code (should be something like an array of functions taking another function as argument and returing a GiNaC::ex), the resulting code is less clear. I attach a proposed solution, which prints D0_myfunc(...) instead. Alexei probably wants to apply the same improvement as he did with the previous patch I submitted, but for me it works. Kind regards, Markus -- Markus Grabner - Computer Graphics and Vision Graz University of Technology, Inffeldgasse 16a/II, 8010 Graz, Austria Phone: +43/316/873-5041, Fax: +43/316/873-5050 WWW: http://www.icg.tugraz.at/Members/grabner
participants (3)
-
Jens Vollinga
-
Markus Grabner
-
varg@theor.jinr.ru