]> www.ginac.de Git - ginac.git/blob - check/exam_structure.cpp
lst_to_clifford() and clifford_to_lst() can handle pseudo-vector representation
[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-2007 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 "exams.h"
24
25
26 struct sprod_s {
27         ex left, right;
28
29         sprod_s() {}
30         sprod_s(const ex & l, const ex & r) : left(l), right(r) {}
31 };
32
33 inline bool operator==(const sprod_s & lhs, const sprod_s & rhs)
34 {
35         return lhs.left.is_equal(rhs.left) && lhs.right.is_equal(rhs.right);
36 }
37      
38 inline bool operator<(const sprod_s & lhs, const sprod_s & rhs)
39 {
40         return lhs.left.compare(rhs.left) < 0 ? true : lhs.right.compare(rhs.right) < 0;
41 }
42
43 typedef structure<sprod_s, compare_std_less> sprod;
44
45 inline ex make_sprod(const ex & l, const ex & r)
46 {
47         return sprod(sprod_s(l, r));
48 }
49
50 namespace GiNaC {
51
52 template <> void sprod::print(const print_context & c, unsigned level) const
53 {
54         const sprod_s & sp = get_struct();
55         c.s << "<" << sp.left << "|" << sp.right << ">";
56 }
57
58 template <> ex sprod::eval(int level) const
59 {
60         // symmetric scalar product
61         const sprod_s & sp = get_struct();
62         if (sp.left.compare(sp.right) <= 0)
63                 return hold();
64         else
65                 return make_sprod(sp.right, sp.left);
66 }
67
68 }  // namespace GiNaC
69
70 unsigned exam_structure()
71 {
72         unsigned result = 0;
73
74         cout << "examining structure template" << flush;
75         clog << "----------structure template:" << endl;
76
77         symbol x("x"), y("y");
78         ex e;
79
80         e = make_sprod(x, y) - make_sprod(y, x);
81         if (!e.is_zero()) {
82                 clog << "<x|y>-<y|x> erroneously returned " << e << " instead of 0" << endl;
83                 ++result;
84         }
85
86         cout << '.' << flush;
87
88         e = make_sprod(x, x) - make_sprod(y, y);
89         if (e.is_zero()) {
90                 clog << "<x|x>-<y|y> erroneously returned 0" << endl;
91                 ++result;
92         }
93
94         cout << '.' << flush;
95
96         if (!result) {
97                 cout << " passed " << endl;
98                 clog << "(no output)" << endl;
99         } else {
100                 cout << " failed " << endl;
101         }
102
103         return result;
104 }