]> www.ginac.de Git - ginac.git/blob - check/exam_structure.cpp
Synced to HEAD.
[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-2004 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 void sprod::print(const print_context & c, unsigned level) const
51 {
52         const sprod_s & sp = get_struct();
53         c.s << "<" << sp.left << "|" << sp.right << ">";
54 }
55
56 ex sprod::eval(int level) const
57 {
58         // symmetric scalar product
59         const sprod_s & sp = get_struct();
60         if (sp.left.compare(sp.right) <= 0)
61                 return hold();
62         else
63                 return make_sprod(sp.right, sp.left);
64 }
65
66 unsigned exam_structure()
67 {
68         unsigned result = 0;
69
70         cout << "examining structure template" << flush;
71         clog << "----------structure template:" << endl;
72
73         symbol x("x"), y("y");
74         ex e;
75
76         e = make_sprod(x, y) - make_sprod(y, x);
77         if (!e.is_zero()) {
78                 clog << "<x|y>-<y|x> erroneously returned " << e << " instead of 0" << endl;
79                 ++result;
80         }
81
82         cout << '.' << flush;
83
84         e = make_sprod(x, x) - make_sprod(y, y);
85         if (e.is_zero()) {
86                 clog << "<x|x>-<y|y> erroneously returned 0" << endl;
87                 ++result;
88         }
89
90         cout << '.' << flush;
91
92         if (!result) {
93                 cout << " passed " << endl;
94                 clog << "(no output)" << endl;
95         } else {
96                 cout << " failed " << endl;
97         }
98
99         return result;
100 }