[GiNaC-devel] patch: valid C code for idx class with print_csrc

Sheplyakov Alexei varg at theor.jinr.ru
Mon Jul 2 22:27:49 CEST 2007


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.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 827 bytes
Desc: Digital signature
URL: <http://www.ginac.de/pipermail/ginac-devel/attachments/20070703/c42871e8/attachment.sig>


More information about the GiNaC-devel mailing list