-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbol.hpp
More file actions
191 lines (167 loc) · 5.69 KB
/
symbol.hpp
File metadata and controls
191 lines (167 loc) · 5.69 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef SYMBOL_HPP
#define SYMBOL_HPP
#include <map>
#include <string>
#include <utility>
#include "except.hpp"
#include "fraction.hpp"
struct Symbol {
std::map<char, Fraction> variables;
Symbol() = default;
Symbol(const Fraction &value) {
if (!value.is_zero())
variables[0] = value; // Use 0 as a special key for constant terms
}
Symbol(const char name) { variables[name] = Fraction(1); }
Symbol(const char name, const Fraction &coefficient) {
if (!coefficient.is_zero())
variables[name] = coefficient;
}
void add_variable(const char name, const Fraction &coefficient) {
if (coefficient.is_zero())
return; // Ignore zero coefficients
if (variables.find(name) != variables.end())
variables[name] += coefficient;
else
variables[name] = coefficient;
if (variables[name].is_zero())
variables.erase(name); // Remove zero coefficients
}
bool is_number() const {
if (variables.size() == 0)
return true;
if (variables.size() == 1 && variables.begin()->first == 0)
return true;
return false;
}
Fraction get_coefficient(const char name) const {
auto it = variables.find(name);
if (it != variables.end())
return it->second;
return Fraction(0); // Return zero if variable not found
}
Fraction get_value() const {
if (variables.size() == 0)
return Fraction(0);
if (variables.size() == 1 && variables.begin()->first == 0)
return variables.begin()->second;
throw std::runtime_error("Symbol is not a number.");
}
bool is_zero() const { return variables.size() == 0; }
const std::map<char, Fraction> &get_variables() const { return variables; }
Symbol operator+() const {
return *this; // Unary plus does not change the symbol
}
Symbol operator-() const {
Symbol result;
for (const auto &pair : variables)
result.add_variable(pair.first, -pair.second);
return result;
}
Symbol operator+(const Symbol &other) const {
Symbol result = *this;
for (const auto &pair : other.variables)
result.add_variable(pair.first, pair.second);
return result;
}
Symbol operator-(const Symbol &other) const {
Symbol result = *this;
for (const auto &pair : other.variables)
result.add_variable(pair.first, -pair.second);
return result;
}
Symbol operator*(const Fraction &other) const {
Symbol result;
for (const auto &pair : variables)
result.add_variable(pair.first, pair.second * other);
return result;
}
Symbol operator*(const Symbol &other) const {
if (other.is_number())
return *this * other.get_value();
if (this->is_number())
return other * this->get_value();
throw not_implemented("Multiplication of two symbols");
}
Symbol operator/(const Fraction &other) const {
if (other.is_zero())
throw std::domain_error("Division by zero.");
Symbol result;
for (const auto &pair : variables)
result.add_variable(pair.first, pair.second / other);
return result;
}
Symbol operator/(const Symbol &other) const {
if (other.is_number())
return *this / other.get_value();
throw not_implemented("Division by a symbol");
}
Symbol &operator+=(const Symbol &other) {
for (const auto &pair : other.variables)
add_variable(pair.first, pair.second);
return *this;
}
Symbol &operator-=(const Symbol &other) {
for (const auto &pair : other.variables)
add_variable(pair.first, -pair.second);
return *this;
}
Symbol &operator*=(const Fraction &other) {
if (other.is_zero())
return variables.clear(), *this; // Clear all variables if multiplied by zero
if (other.is_one())
return *this; // No change if multiplied by one
for (auto &pair : variables)
pair.second *= other;
return *this;
}
Symbol &operator*=(const Symbol &other) {
if (other.is_number())
return *this *= other.get_value();
if (this->is_number())
return *this = other * this->get_value();
throw not_implemented("Multiplication of two symbols");
}
Symbol &operator/=(const Fraction &other) {
if (other.is_zero())
throw std::domain_error("Division by zero.");
if (other.is_one())
return *this; // No change if divided by one
for (auto &pair : variables)
pair.second /= other;
return *this;
}
Symbol &operator/=(const Symbol &other) {
if (other.is_number())
return *this /= other.get_value();
throw not_implemented("Division by a symbol");
}
static std::string to_string(const std::pair<char, Fraction> &pair) {
if (pair.first == 0) // Special case for constant term
return pair.second.to_string();
if (pair.second.is_one())
return std::string(1, pair.first); // Just the variable name if coefficient is 1
if (pair.second.is_minus_one())
return "-" + std::string(1, pair.first); // Just the negative variable name if coefficient is -1
return pair.second.to_string() + " * " + pair.first; // Coefficient and variable name
}
std::string to_string() const {
if (variables.empty())
return "0"; // No variables means it's zero
std::string result;
for (const auto &pair : variables) {
if (!result.empty())
result += " + "; // Add separator for multiple terms
result += to_string(pair);
}
return result;
}
explicit operator bool() const { return !is_zero(); }
};
Symbol operator""_sym(char c) { return Symbol(c); }
Symbol operator""_sym(const char *s, size_t) { return Symbol(BigInt(s)); }
Symbol operator""_sym(unsigned long long v) { return v? Symbol(BigInt((uintmax_t)v)): Symbol(); }
namespace std {
std::string to_string(const Symbol &symbol) { return symbol.to_string(); }
} // namespace std
#endif // SYMBOL_HPP