[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
6.4.2 Structure output
While the sprod
type is useable it still leaves something to be
desired, most notably proper output:
... cout << e << endl; // -> [structure object] ... |
By default, any structure types you define will be printed as
‘[structure object]’. To override this you can either specialize the
template's print()
member function, or specify print methods with
set_print_func<>()
, as described in GiNaC's expression output system. Unfortunately,
it's not possible to supply class options like print_func<>()
to
structures, so for a self-contained structure type you need to resort to
overriding the print()
function, which is also what we will do here.
The member functions of GiNaC classes are described in more detail in the next section, but it shouldn't be hard to figure out what's going on here:
void sprod::print(const print_context & c, unsigned level) const { // tree debug output handled by superclass if (is_a<print_tree>(c)) inherited::print(c, level); // get the contained sprod_s object const sprod_s & sp = get_struct(); // print_context::s is a reference to an ostream c.s << "<" << sp.left << "|" << sp.right << ">"; } |
Now we can print expressions containing scalar products:
... cout << e << endl; // -> <a|b> cout << swap_sprod(e) << endl; // -> <b|a> ... |