]> www.ginac.de Git - ginac.git/blob - check/exam_structure.cpp
Fix example of symmetrization in tutorial.
[ginac.git] / check / exam_structure.cpp
1 /** @file exam_structure.cpp
2  *
3  *  Small test for the structure<> template. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2011 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "ginac.h"
24 using namespace GiNaC;
25
26 #include <iostream>
27 using namespace std;
28
29 struct sprod_s {
30         ex left, right;
31
32         sprod_s() {}
33         sprod_s(const ex & l, const ex & r) : left(l), right(r) {}
34 };
35
36 inline bool operator==(const sprod_s & lhs, const sprod_s & rhs)
37 {
38         return lhs.left.is_equal(rhs.left) && lhs.right.is_equal(rhs.right);
39 }
40      
41 inline bool operator<(const sprod_s & lhs, const sprod_s & rhs)
42 {
43         return lhs.left.compare(rhs.left) < 0 ? true : lhs.right.compare(rhs.right) < 0;
44 }
45
46 typedef structure<sprod_s, compare_std_less> sprod;
47
48 inline ex make_sprod(const ex & l, const ex & r)
49 {
50         return sprod(sprod_s(l, r));
51 }
52
53 namespace GiNaC {
54
55 template <> void sprod::print(const print_context & c, unsigned level) const
56 {
57         const sprod_s & sp = get_struct();
58         c.s << "<" << sp.left << "|" << sp.right << ">";
59 }
60
61 template <> ex sprod::eval(int level) const
62 {
63         // symmetric scalar product
64         const sprod_s & sp = get_struct();
65         if (sp.left.compare(sp.right) <= 0)
66                 return hold();
67         else
68                 return make_sprod(sp.right, sp.left);
69 }
70
71 }  // namespace GiNaC
72
73 unsigned exam_structure()
74 {
75         unsigned result = 0;
76
77         cout << "examining structure template" << flush;
78
79         symbol x("x"), y("y");
80         ex e;
81
82         e = make_sprod(x, y) - make_sprod(y, x);
83         if (!e.is_zero()) {
84                 clog << "<x|y>-<y|x> erroneously returned " << e << " instead of 0" << endl;
85                 ++result;
86         }
87
88         cout << '.' << flush;
89
90         e = make_sprod(x, x) - make_sprod(y, y);
91         if (e.is_zero()) {
92                 clog << "<x|x>-<y|y> erroneously returned 0" << endl;
93                 ++result;
94         }
95
96         return result;
97 }
98
99 int main(int argc, char** argv)
100 {
101         return exam_structure();
102 }