-
Notifications
You must be signed in to change notification settings - Fork 6
/
monomial.h
145 lines (130 loc) · 4.02 KB
/
monomial.h
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Ilia Mirkin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <ostream>
#include <unordered_map>
#include <initializer_list>
#pragma once
template <typename T>
class Monomial {
public:
Monomial() {}
Monomial(const Monomial<T>& other) : exponents_(other.exponents_) {}
Monomial(std::initializer_list<T> init) {
for (auto it = init.begin(); it != init.end(); ++it) {
*this << *it;
}
}
void operator=(const Monomial<T>& other) {
exponents_ = other.exponents_;
}
bool operator==(const Monomial<T>& other) const {
if (exponents_.size() != other.exponents_.size()) {
return false;
}
for (auto it = exponents_.begin(); it != exponents_.end(); ++it) {
auto other_it = other.exponents_.find(it->first);
if (other_it == other.exponents_.end())
return false;
if (it->second != other_it->second)
return false;
}
return true;
}
bool operator!=(const Monomial<T>& other) {
return !(*this == other);
}
Monomial& operator*=(const Monomial<T>& other) {
for (auto it = other.exponents_.begin(); it != other.exponents_.end(); ++it) {
*this << *it;
}
return *this;
}
Monomial operator*(const Monomial<T>& other) const {
return Monomial(*this) *= other;
}
Monomial& operator<<(const T& term) {
auto it = exponents_.find(term);
if (it != exponents_.end()) {
it->second++;
} else {
exponents_.insert(std::make_pair(term, 1));
}
return *this;
}
Monomial& operator<<(const std::pair<T, int>& pair) {
auto it = exponents_.find(pair.first);
if (it != exponents_.end()) {
it->second += pair.second;
} else {
exponents_.insert(pair);
}
return *this;
}
std::unordered_map<T, int> exponents_;
};
namespace std {
template <typename T>
struct hash<Monomial<T> > {
size_t operator()(const Monomial<T>& monomial) const {
// TODO: For this to be consistent, need to sort the elements first...
size_t ret = 0;
for (auto it = monomial.exponents_.begin(); it != monomial.exponents_.end(); ++it) {
ret *= 31;
ret += hash<T>()(it->first);
ret *= 31;
ret += hash<int>()(it->second);
}
return ret;
}
};
}
template <typename T>
std::ostream& operator<<(std::ostream& stream, const Monomial<T>& monomial) {
for (auto it = monomial.exponents_.begin(); it != monomial.exponents_.end(); ++it) {
stream << it->first;
if (it->second > 1) {
stream << "^" << it->second;
}
}
return stream;
}
template <typename T>
class MonomialOps {
public:
MonomialOps() {}
static MonomialOps<T> instance;
typedef Monomial<T> element;
typedef SemigroupElt<MonomialOps<T> > semigroup;
typedef MonoidElt<MonomialOps<T> > monoid;
void init(element& a) const {
}
element id() const {
return element();
}
element times(const element& a, const element& b) const {
return a * b;
}
};
template <typename T>
MonomialOps<T> MonomialOps<T>::instance;