File: exam_structure.cpp

package info (click to toggle)
ginac 1.8.9-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,640 kB
  • sloc: cpp: 49,195; sh: 5,402; makefile: 448; python: 193
file content (102 lines) | stat: -rw-r--r-- 2,477 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/** @file exam_structure.cpp
 *
 *  Small test for the structure<> template. */

/*
 *  GiNaC Copyright (C) 1999-2025 Johannes Gutenberg University Mainz, Germany
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "ginac.h"
using namespace GiNaC;

#include <iostream>
using namespace std;

struct sprod_s {
	ex left, right;

	sprod_s() {}
	sprod_s(const ex & l, const ex & r) : left(l), right(r) {}
};

inline bool operator==(const sprod_s & lhs, const sprod_s & rhs)
{
	return lhs.left.is_equal(rhs.left) && lhs.right.is_equal(rhs.right);
}
     
inline bool operator<(const sprod_s & lhs, const sprod_s & rhs)
{
	return lhs.left.compare(rhs.left) < 0 ? true : lhs.right.compare(rhs.right) < 0;
}

typedef structure<sprod_s, compare_std_less> sprod;

inline ex make_sprod(const ex & l, const ex & r)
{
	return sprod(sprod_s(l, r));
}

namespace GiNaC {

template <> void sprod::print(const print_context & c, unsigned level) const
{
	const sprod_s & sp = get_struct();
	c.s << "<" << sp.left << "|" << sp.right << ">";
}

template <> ex sprod::eval() const
{
	// symmetric scalar product
	const sprod_s & sp = get_struct();
	if (sp.left.compare(sp.right) <= 0)
		return hold();
	else
		return make_sprod(sp.right, sp.left);
}

}  // namespace GiNaC

unsigned exam_structure()
{
	unsigned result = 0;

	cout << "examining structure template" << flush;

	symbol x("x"), y("y");
	ex e;

	e = make_sprod(x, y) - make_sprod(y, x);
	if (!e.is_zero()) {
		clog << "<x|y>-<y|x> erroneously returned " << e << " instead of 0" << endl;
		++result;
	}

	cout << '.' << flush;

	e = make_sprod(x, x) - make_sprod(y, y);
	if (e.is_zero()) {
		clog << "<x|x>-<y|y> erroneously returned 0" << endl;
		++result;
	}

	return result;
}

int main(int argc, char** argv)
{
	return exam_structure();
}