1.前言
这是作者利用 C++ 编写的实现了多种数学结构的代码。因为作者年龄不大,部分算法如牛顿迭代法,在 DeepSeek 的帮助下完成。代码在最后。
日志
- 2026/08/04 开始编写
- 2026/08/06
Inequality类实现
2.介绍
2.1 Monomial类
用于计算、存储单项式,单项式之间的加减只支持两个单项式为同类项,不支持单项式加减非单项式类型和单项式之间的 。
- 支持利用
get_letters(),get_coe(),get_deg(),get_not()获得字母、系数、次数和符号取反。 set_coe()用于改变系数。sort_out()用于整理单项式。is_like_terms()返回是否同类项。out_Monomial()将单项式转换为字符串。evaluate()用于代入,传入一个vector<pair<char,T>>表示每个字母的数值。rand_Monomial()返回一个随机单项式。make_Monomial()将系数和字母转换成一个单项式。
2.2 Polynomial类
用于计算存储多项式,支持多项式之间、多项式与单项式之间的加减乘除模和 。
split_Polynomial()用于将一个多项式变成vector<Monomial>。find()从多项式中找一个单项式(只匹配字母)。sort_out()用于多项式的同类项合并。out_Polynomial()将多项式转换为字符串。evaluate()用于代入,传入一个vector<pair<char,T>>表示每个字母的数值。get_deg()返回多项式次数。
2.3 Fraction类
用于计算存储分数,支持大小比较、四则运算和转换成 double,float,long double,int。
from_string()传入一个字符串,用于将字符串变成分数。Fraction_to_string()将分数变成字符串。to_double()转换成double。to_int()向下取整转换成int。除了使用成员函数外,也可以隐式转换。normalize()约分、分母保持正。inv()返回倒数。get_num(),get_den()返回分子和分母
2.4 Complex类
用于计算复数,支持复数之间的四则运算和按字典序的大小比较。
from_string()将字符串转化为复数。Complex_to_string将复数转化为字符串。real(),imag(),modulus(),abs(),norm(),conjugate()返回实部、虚部、模(modulus(),abs()都返回模)、模的平方和同轭复数。cmp_string()返回按字典序比较的结果,同<。
2.5 Equation类
用于计算等式(方程)。方程之间的加减乘除模为左式和左式、右式和右式进行运算。
check()传入一个vector<pair<char,T>>表示每个字母的数值,利用代入后两式的和进行比较,检验方程是否成立。rhs_to_zero()将右式移项到左式。Equation_to_string(),from_string()将等式变成字符串或字符串变成等式。solve()求解,目前只支持未知数是 的一元方程,一二次方程利用求根公式,高次方程利用牛顿迭代法(有误差)。simplify()化简左右。
2.6 Inequality类
计算不等式,
2.7 Sequence类(未实现)
2.8 Summation类(未实现)
2.9 Product类(未实现)
2.10 Statistics类(未实现)
2.11 Expression类(未实现)
2.12 Vector类(未实现)
2.13 Angle类(未实现)
2.14 Set类(未实现)
3.代码
Code
#include<bits/stdc++.h>
using namespace std;
namespace super_math{
template<typename T>class Polynomial;
template<typename T>class Monomial;
template<typename T>class Fraction;
template<typename T>class Sequence;//数列
template<typename T>class Summation;//求和
template<typename T>class Product;//求积
template<typename T>class Function;//指数函数,对数函数,三角函数,幂函数
template<typename T>class Complex;//复数
template<typename T>class Statistics;//统计
template<typename T>class Expression;//函数
template<typename T>class Vector;//向量
template<typename T>class Angle;//角
template<typename T>class Inequality;//不等式
template<typename T>class Equation;//等式
template<typename T>class Set;//集合
int __random(int n){//随机数
int random_num=rand()%n;
return random_num;
}
void before_rand(){
srand(time(0));
}
//-----------------------------------------
template<typename T>
Monomial<T> make_Monomial(T c,vector<pair<char,int>> ltr);
template<typename T>
class Monomial{//单项式
public:
Monomial(T c=1,vector<pair<char,int>> l={}):coe(c),letter(l) {}
const vector<pair<char,int>>& get_letters()const{
return letter;
}
T get_coe() const {
return coe;
}
void set_coe(T c){
coe = c;
}
Monomial get_not()const{//取反函数
return make_Monomial(-coe,letter);
}
void sort_out(){
sort(letter.begin(),letter.end());
for (auto it=letter.begin();it!=letter.end();){
auto next=it+1;
if (next!=letter.end()&&it->first==next->first){
it->second+=next->second;
letter.erase(next);
}else{
++it;
}
}
}
bool is_like_terms(const Monomial& b){//是否同类项
if(letter.size()!=b.letter.size()){
return false;
}
for(int i=0;i<letter.size();i++){
if(letter[i]!=b.letter[i]){
return false;
}
}
return true;
}
string out_Monomial()const{
if(letter.empty()) return format_number(coe);
string ltr="";
for(auto i:letter){
string t="1";
t[0]=i.first;
if(i.second==0) continue;
else if(i.second==1) ltr+=t;
else ltr+=t+"^"+to_string(i.second);
}
string s=format_number(coe);
if(s=="1") return ltr;
else if(s=="-1") return "-"+ltr;
else if(s=="0") return "0";
else return s+ltr;
}
T evaluate(const vector<pair<char,T>>& num)const{//代入
T ans=coe;
for(const auto& l:letter){
char ltr=l.first;
int exp=l.second;
auto it=__find(ltr,num);
if(it==num.end()){
return 0;
}
T val=it->second;
ans*=__power(val,exp);
}
return ans;
}
int get_deg()const{
int ans=0;
for(auto i:letter){
ans+=i.second;
}
return ans;
}
void rand_Monomial(const int& maxcoe=1e9+1,const int& maxexp=1e9+1,const int& max_ltr_num=1e5+1){//____________________________________
Monomial<T> ans;
int c=__random(maxcoe);
ans.coe=c;
for(int i=1;i<=max_ltr_num;i++){
char l='a'+__random(26);
int e=__random(maxexp);
ans.letter.push_back({l,e});
}
ans.sort_out();
letter=ans.letter;
coe=ans.coe;
}
string format_number(T val)const{
string s=to_string(val);
if(s.find('.') != string::npos){
while(s.back()=='0') s.pop_back();
if(s.back()=='.') s.pop_back();
}
return s;
}
bool operator==(const Monomial& other) const {
return coe == other.coe && letter == other.letter;
}
bool operator!=(const Monomial& other) const { return !(*this == other); }
Monomial operator-()const{
return Monomial(-coe,letter);
}
Monomial operator+()const{
return (*this);
}
Monomial operator+(const Monomial& other) const{//M+M=M
Monomial ans=make_Monomial(coe+other.coe,letter);
ans.sort_out();
return ans;
}
Monomial operator-(const Monomial& other) const{//M-M=M
Monomial ans=make_Monomial(coe-other.coe,letter);
ans.sort_out();
return ans;
}
Monomial operator*(const Monomial& other) const{//M*M=M
vector<pair<char,int>> newl=letter;
newl.insert(newl.end(),other.letter.begin(),other.letter.end());
Monomial ans=make_Monomial(coe*other.coe,newl);
ans.sort_out();
return ans;
}
Monomial operator*(const T& other) const{//M*int=M
Monomial ans=make_Monomial(coe*other,letter);
ans.sort_out();
return ans;
}
Monomial operator/(const Monomial& other) const{//M/M=M
vector<pair<char,int>> newl=letter,newother;
for(auto i:other.letter){
newother.push_back({i.first,-i.second});
}
newl.insert(newl.end(),newother.begin(),newother.end());
Monomial ans=make_Monomial(coe/other.coe,newl);
ans.sort_out();
return ans;
}
Monomial operator/(const T& other) const{//M/int=M
Monomial ans=make_Monomial(coe/other,letter);
ans.sort_out();
return ans;
}
Monomial operator%(const Monomial& other) const{//M%M=M
if(coe%other.coe!=0){
return *this;
}
auto temp_letters=letter;
for(const auto& p:other.letter){
bool found=false;
for(auto& q:temp_letters){
if(q.first==p.first){
if(q.second<p.second){
return *this;
}
q.second-=p.second;
found=true;
break;
}
}
if(!found){
return *this;
}
}
return make_Monomial(T(0),{});
}
Monomial operator%(const T& other) const{//M%int=M
Monomial ans=make_Monomial(coe%other,letter);
ans.sort_out();
return ans;
}
template<typename U>
friend Monomial<U> operator*(const U& other, const Monomial<U>& m){
return make_Monomial(other*m.get_coe(),m.get_letters());
}
template<typename U>
friend Monomial<U> operator/(const U& other, const Monomial<U>& m){
return make_Monomial(other/m.get_coe(),m.get_letters());
}
template<typename U>
friend Monomial<U> operator%(const U& other, const Monomial<U>& m){
return make_Monomial(other%m.get_coe(),m.get_letters());
}
template<typename U>
friend std::istream& operator>>(std::istream& is, Monomial<U>& m){
std::string token;
is>>token;
if(!is) return is;
m.coe=1;
m.letter.clear();
size_t i=0;
// ---------- 解析系数 ----------
T sign=1;
if(i<token.size()&&token[i]=='-'){
sign=-1;
++i;
}else if(i<token.size()&&token[i]=='+'){
++i;
}
if(i<token.size()&&isdigit(token[i])){
T coe=0;
while(i<token.size()&&isdigit(token[i])){
coe=coe*10+(token[i]-'0');
++i;
}
m.coe=sign*coe;
}else{
m.coe=sign;
}
// ---------- 解析字母部分 ----------
while(i<token.size()){
char c=token[i];
if(!isalpha(c)){
break;
}
++i;
int exp=1;
if(i<token.size()&&token[i]=='^'){
++i;
if(i<token.size()&&isdigit(token[i])){
exp=0;
while(i < token.size() && isdigit(token[i])){
exp=exp*10+(token[i]-'0');
++i;
}
}
}
m.letter.push_back({c,exp});
}
m.sort_out(); // 合并相同字母(如果有)
return is;
}
template<typename U>
friend Monomial<U> gcd(const Monomial<U>&, const Monomial<U>&);
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const Monomial<U>& m) {
os<<m.out_Monomial();
return os;
}
private:
protected:
T coe;//系数 coefficient
vector<pair<char,int>> letter;//字母及其指数
T __power(T a,int exp)const{
T res=1;
while(exp>0){
if(exp&1) res=res*a;
a=a*a;
exp>>=1;
}
return res;
}
typename vector<pair<char,T>>::iterator __find(const char& target,vector<pair<char,T>>& num){//从P中找M 只匹配字母
for(auto i=num.begin();i!=num.end();i++){
if(i->first==target){
return i;
}
}
return num.end();
}
typename vector<pair<char,T>>::const_iterator __find(const char& target,const vector<pair<char,T>>& num)const{//从P中找M 只匹配字母
for(auto i=num.begin();i!=num.end();i++){
if(i->first==target){
return i;
}
}
return num.end();
}
};
template<typename T>
Monomial<T> make_Monomial(T c,vector<pair<char,int>> ltr){//将系数和字母转换成一个单项式
Monomial<T> ans(c,ltr);
ans.sort_out();
return ans;
}
//-------------------------------------------------------
template<typename T>
class Polynomial{//多项式
public:
Polynomial(vector<Monomial<T>> l):mon(l) {}
Polynomial():mon() {}
vector<Monomial<T>> split_Polynomial()const{//将一个P变成vector<M>
return mon;
}
typename vector<Monomial<T>>::iterator find(const Monomial<T>& target){//从P中找M 只匹配字母
for(auto i=mon.begin();i!=mon.end();i++){
if(i->get_letters()==target.get_letters()){
return i;
}
}
return mon.end();
}
typename vector<Monomial<T>>::const_iterator find(const Monomial<T>& target)const{//从P中找M 只匹配字母
for(auto i=mon.begin();i!=mon.end();i++){
if(i->get_letters()==target.get_letters()){
return i;
}
}
return mon.end();
}
void sort_out(){
for(auto i=mon.begin();i!=mon.end();i++){
i->sort_out();
}
sort(mon.begin(),mon.end(),__cmp);
for(auto i=mon.begin();i!=mon.end();){
auto next=i+1;
if(next!=mon.end()&&i->is_like_terms(*next)){
i->set_coe(i->get_coe()+next->get_coe());
mon.erase(next);
}else{
i++;
}
}
for(auto i=mon.begin();i!=mon.end();){
if(i->get_coe()==T(0)){
i=mon.erase(i);
} else {
++i;
}
}
sort(mon.begin(),mon.end(),__cmp);
}
string out_Polynomial()const{
string s="";
bool first=true;
for(const auto& i:mon){
if(!first){
if(i.get_coe()>=0) s+="+";
}
s+=i.out_Monomial();
first=false;
}
return s.empty() ? "0" : s;
}
T evaluate(const vector<pair<char,T>>& num)const{//代入
T ans=0;
for(auto i:mon){
ans+=i.evaluate(num);
}
return ans;
}
int get_deg()const{
int ans=0;
for(auto i:mon){
ans=max(ans,i.get_deg());
}
return ans;
}
Polynomial operator-()const{
Polynomial ans;
for(const auto& m:mon){
ans.mon.push_back(-m);
}
return ans;
}
Polynomial operator+()const{
return (*this);
}
bool operator==(const Polynomial& other)const{
if(mon.size()!=other.mon.size()) return false;
for(size_t i=0;i<mon.size();++i){
if(!(mon[i]==other.mon[i])) return false;
}
return true;
}
bool operator!=(const Polynomial& other)const{
return !(*this==other);
}
//____________________+
Polynomial operator +(const Monomial<T>& b)const{
Polynomial newp=*this;
auto it=newp.find(b);
if(it!=newp.mon.end()){
it->set_coe(it->get_coe()+b.get_coe());
}else{
newp.mon.push_back(b);
}
newp.sort_out();
return newp;
}
Polynomial operator +(const Polynomial& b)const{
vector<Monomial<T>> mons=b.split_Polynomial();
Polynomial newp=*this;
for(auto m:mons){
newp=newp+m;
}
newp.sort_out();
return newp;
}
//_____________________-
Polynomial operator -(const Monomial<T>& b)const{
Polynomial newp=*this;
auto it=newp.find(b);
if(it!=newp.mon.end()){
it->set_coe(it->get_coe()-b.get_coe());
}else{
newp.mon.push_back(b.get_not());
}
newp.sort_out();
return newp;
}
Polynomial operator -(const Polynomial& b)const{
vector<Monomial<T>> mons=b.split_Polynomial();
Polynomial newp=*this;
for(auto m:mons){
newp=newp-m;
}
newp.sort_out();
return newp;
}
//____________________*
Polynomial operator *(const Monomial<T>& b)const{
Polynomial ans;
for(auto i:mon){
ans.mon.push_back(i*b);
}
ans.sort_out();
return ans;
}
Polynomial operator *(const Polynomial& b)const{
Polynomial ans;
for(const auto& i:mon){
for(const auto& j:b.mon){
ans=ans+i*j;
}
}
ans.sort_out();
return ans;
}
//__________________ /
Polynomial operator /(const Monomial<T>& b)const{
Polynomial ans;
for(auto i:mon){
ans.mon.push_back(i/b);
}
ans.sort_out();
return ans;
}
Polynomial operator/(const Polynomial& b)const{
if (b.mon.empty()) return Polynomial();
Polynomial<T> a=*this,d=b;
sort(a.mon.begin(),a.mon.end(),__cmp_high);
sort(d.mon.begin(),d.mon.end(),__cmp_high);
Polynomial<T> Q;
while(!a.mon.empty()){
const auto& a_front=a.mon.front();
const auto& d_front=d.mon.front();
bool can_div=true;
size_t i=0,j=0;
const auto& al=a_front.get_letters();
const auto& dl=d_front.get_letters();
while(i<al.size()&&j<dl.size()){
if(al[i].first<dl[j].first){
++i;
}else if(al[i].first>dl[j].first){
can_div=false;
break;
}else{
if(al[i].second<dl[j].second){
can_div=false;
break;
}
++i;
++j;
}
}
if(j<dl.size()) can_div=false;
if(!can_div) break;
Monomial<T> term=a_front/d_front;
Q.mon.push_back(term);
Polynomial<T> subtrahend=d*term;
a = a - subtrahend;
sort(a.mon.begin(),a.mon.end(),__cmp_high);
for(auto it = a.mon.begin();it!=a.mon.end();){
auto nxt=it+1;
if(nxt!=a.mon.end()&&it->is_like_terms(*nxt)){
it->set_coe(it->get_coe()+nxt->get_coe());
a.mon.erase(nxt);
}else ++it;
}
for(auto it=a.mon.begin();it!=a.mon.end();){
if(it->get_coe()==T(0)) it=a.mon.erase(it);
else ++it;
}
}
sort(Q.mon.begin(),Q.mon.end(),__cmp_high);
return Q;
}
//__________________ %
//__________________ %
Polynomial operator%(const Monomial<T>& b) const {
Polynomial ans;
for (const auto& m : mon) {
Monomial<T> rem = m % b; // 单项式取模
if (rem.get_coe() != T(0)) // 非零余数则保留
ans.mon.push_back(rem);
}
return ans;
}
Polynomial operator%(const Polynomial& b) const {
if(b.mon.empty()) return Polynomial();
Polynomial<T> a=*this,d=b;
sort(a.mon.begin(),a.mon.end(),__cmp_high);
sort(d.mon.begin(),d.mon.end(),__cmp_high);
while(!a.mon.empty()){
const auto& a_front=a.mon.front();
const auto& d_front=d.mon.front();
bool can_div=true;
size_t i=0,j=0;
const auto& al=a_front.get_letters();
const auto& dl=d_front.get_letters();
while(i<al.size()&&j<dl.size()){
if(al[i].first<dl[j].first){
++i;
}else if(al[i].first>dl[j].first){
can_div=false;
break;
}else{
if(al[i].second<dl[j].second){
can_div=false;
break;
}
++i;
++j;
}
}
if(j<dl.size()) can_div=false;
if(!can_div) break;
Monomial<T> term=a_front/d_front;
Polynomial<T> subtrahend=d*term;
a=a-subtrahend;
sort(a.mon.begin(), a.mon.end(),__cmp_high);
for(auto it=a.mon.begin();it!=a.mon.end();){
auto nxt=it + 1;
if (nxt!=a.mon.end()&&it->is_like_terms(*nxt)){
it->set_coe(it->get_coe() + nxt->get_coe());
a.mon.erase(nxt);
} else ++it;
}
for(auto it=a.mon.begin();it!=a.mon.end();){
if(it->get_coe()==T(0)) it=a.mon.erase(it);
else ++it;
}
}
return a;
}
//___________________<< >>
friend std::ostream& operator<<(std::ostream& os, const Polynomial<T>& m){
os<<m.out_Polynomial();
return os;
}
template<typename U>
friend Polynomial<U> operator+(const Monomial<U>& a, const Polynomial<U>& b){
return b+a;
}
template<typename U>
friend Polynomial<U> operator-(const Monomial<U>& a, const Polynomial<U>& b){
Polynomial neg_b=b;
for(auto& m:neg_b.mon){
m.set_coe(-m.get_coe());
}
return neg_b+a;
}
template<typename U>
friend Polynomial<U> operator*(const Monomial<U>& a, const Polynomial<U>& b){
return b*a;
}
template<typename U>
friend Polynomial<U> operator/(const Monomial<U>& a, const Polynomial<U>& b){
return Polynomial<T>({a})/b;
}
template<typename U>
friend Polynomial<U> operator%(const Monomial<U>& a, const Polynomial<U>& b){
return Polynomial<T>({a})%b;
}
template<typename U>
friend Polynomial<U> gcd(const Polynomial<U>&, const Polynomial<U>&);
friend std::istream& operator>>(std::istream& is, Polynomial<T>& m) {
std::string token;
is >> token;
if (!is) return is;
token.erase(std::remove_if(token.begin(), token.end(), ::isspace), token.end());
m.mon.clear();
if (token.empty()) return is;
std::string part;
for (size_t i = 0; i < token.size(); ++i) {
if (token[i] == '+' || token[i] == '-') {
if (!part.empty()) {
std::istringstream iss(part);
Monomial<T> mon;
iss >> mon;
m.mon.push_back(mon);
part.clear();
}
part = token[i];
} else {
part += token[i];
}
}
if (!part.empty()) {
std::istringstream iss(part);
Monomial<T> mon;
iss >> mon;
m.mon.push_back(mon);
}
m.sort_out();
return is;
}
private:
protected:
vector<Monomial<T>> mon;
static bool __cmp(const Monomial<T>& a, const Monomial<T>& b){
const auto& al=a.get_letters();
const auto& bl=b.get_letters();
if(al.size()!=bl.size()){
return al.size()<bl.size();
}
for(size_t i=0;i<al.size();i++){
if(al[i].first!=bl[i].first){
return al[i].first<bl[i].first;
}
}
return false;
}
static bool __cmp_high(const Monomial<T>& a,const Monomial<T>& b){
const auto& al=a.get_letters();
const auto& bl=b.get_letters();
if (al.size()!=bl.size()) return al.size()>bl.size();
for(size_t i=0;i<al.size();++i){
if(al[i].first!=bl[i].first) return al[i].first<bl[i].first;
if(al[i].second!=bl[i].second) return al[i].second>bl[i].second;
}
return false;
}
};
template<typename T>
T gcd_int(T a,T b){
if(a<0) a=-a;
if(b<0) b=-b;
while(b){
T t=a%b;
a=b;
b=t;
}
return a;
}
template<typename T>
Monomial<T> gcd(const Monomial<T>& a, const Monomial<T>& b){
T coeff = gcd_int(a.get_coe(),b.get_coe());
const auto& al=a.get_letters();
const auto& bl=b.get_letters();
vector<pair<char,int>> common;
size_t i=0,j=0;
while(i<al.size()&&j<bl.size()){
if(al[i].first<bl[j].first){
++i;
}else if(al[i].first>bl[j].first){
++j;
}else{
int min_exp=min(al[i].second,bl[j].second);
if(min_exp>0) common.push_back({al[i].first,min_exp});
++i;
++j;
}
}
Monomial<T> result(coeff,common);
result.sort_out();
return result;
}
template<typename T>
Polynomial<T> gcd(const Polynomial<T>& a,const Polynomial<T>& b){
if(b.split_Polynomial().empty()) return a;
Polynomial<T> A=a,B=b;
A.sort_out();
B.sort_out();
while(!B.split_Polynomial().empty()){
Polynomial<T> R=A%B;
if(R.split_Polynomial().empty()) break;
A=B;
B=R;
}
T coeff_gcd=B.split_Polynomial().front().get_coe();
for(const auto& m:B.split_Polynomial()){
coeff_gcd=gcd_int(coeff_gcd,m.get_coe());
}
if(B.split_Polynomial().front().get_coe()<0) coeff_gcd=-coeff_gcd;
Polynomial<T> result;
for(auto m:B.split_Polynomial()){
m.set_coe(m.get_coe()/coeff_gcd);
result.mon.push_back(m);
}
result.sort_out();
return result;
}
//-------------------------------------------------------
template<typename T>
class Fraction{//分数
public:
Fraction(T n=0,T d=1):num(n),den(d){
if(den==0){
num=n;
den=1;
}
if(den<0){
num=-num;
den=-den;
}
normalize();
}
void from_string(const string& str){
size_t slash=str.find('/');
if(slash==string::npos){
num=stoll(str);
den=1;
}else{
num=stoll(str.substr(0,slash));
den=stoll(str.substr(slash+1));
}
normalize();
}
string Fraction_to_string()const{
if(den==1) return to_string(num);
return to_string(num)+"/"+to_string(den);
}
double to_double()const{
return static_cast<double>(num)/den;
}
int to_int()const{
return num/den;
}
void normalize(){//约分 分母保持正
if(den<0){
den=-den;
num=-num;
}
if(den==0) return;
T g=gcd_int(num,den);
g=(g<0?-g:g);
if(g!=0) num/=g,den/=g;
}
Fraction inv()const{
if(num==0) return Fraction(0);
return Fraction(den,num);
}
T get_num()const{
return num;
}
T get_den()const{
return den;
}
explicit operator double() const{
return to_double();
}
explicit operator float() const{
return static_cast<float>(to_double());
}
explicit operator long double() const{
return static_cast<long double>(to_double());
}
explicit operator int() const{
return to_int();
}
bool operator==(const Fraction& other) const{
return (num==other.num)&&(den==other.den);
}
bool operator!=(const Fraction& other) const{
return (num!=other.num)||(den!=other.den);
}
bool operator<(const Fraction& other) const{
return num*other.den<other.num*den;
}
bool operator<=(const Fraction& other) const{
return (*this)<other||(*this)==other;
}
bool operator>(const Fraction& other) const{
return other<*this;
}
bool operator>=(const Fraction& other) const{
return !(*this<other);
}
Fraction operator-() const{
return Fraction(-num,den);
}
Fraction operator+() const{
return (*this);
}
Fraction operator+(const Fraction& other)const{
T common_den=den/gcd_int(den,other.den)*other.den;
T newnum=num*(common_den/den),newnumo=other.num*(common_den/other.den);
return Fraction(newnum+newnumo,common_den);
}
Fraction operator+(const int& other)const{
return Fraction(other)+(*this);
}
Fraction operator-(const Fraction& other)const{
T common_den=den/gcd_int(den,other.den)*other.den;
T newnum=num*(common_den/den),newnumo=other.num*(common_den/other.den);
return Fraction(newnum-newnumo,common_den);
}
Fraction operator-(const int& other)const{
return (*this)-Fraction(other);
}
Fraction operator*(const Fraction& other)const{
return Fraction(num*other.num,den*other.den);
}
Fraction operator*(const int& other)const{
return (*this)*Fraction(other);
}
Fraction operator/(const Fraction& other)const{
if(other.get_num()==0) return Fraction(0);
return (*this)*other.inv();
}
Fraction operator/(const int& other)const{
if(other==0) return Fraction(0);
return (*this)*Fraction(other).inv();
}
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const Fraction<U>& other){
os<<other.Fraction_to_string();
return os;
}
template<typename U>
friend std::istream& operator>>(std::istream& is, Fraction<U>& other){
string str;
is>>str;
other.from_string(str);
return is;
}
template<typename U>
friend Fraction<U> operator+(const int& a, const Fraction<U>& b){
return Fraction<U>(a)+b;
}
template<typename U>
friend Fraction<U> operator-(const int& a, const Fraction<U>& b){
return Fraction<U>(a)-b;
}
template<typename U>
friend Fraction<U> operator*(const int& a, const Fraction<U>& b){
return Fraction<U>(a)*b;
}
template<typename U>
friend Fraction<U> operator/(const int& a, const Fraction<U>& b){
if(b.get_num()==0) return 0;
return Fraction<U>(a)/b;
}
private:
protected:
T num,den;//分子分母numerator,denominator
};
//-------------------------------------------------------
template<typename T>
class Complex{//复数
public:
Complex(T r=0,T i=0):re(r),im(i){}
void from_string(const string& str){
size_t pos=str.find('i');
if(pos==string::npos){
re=stod(str);
im=0;
return;
}
string real_part=str.substr(0,pos);
if(real_part.empty()){
if(str[0]=='-'){
re=0;
im=-1;
}
else{
re=0;
im=1;
}
return;
}
size_t sign_pos=real_part.find_last_of("+-");
if(sign_pos==string::npos) {
re=0;
im=stod(real_part);
}else{
re=stod(real_part.substr(0,sign_pos));
string im_str=real_part.substr(sign_pos);
if(im_str=="+"||im_str=="-") {
im=(im_str=="+"?1:-1);
}else{
im=stod(im_str);
}
}
}
string Complex_to_string()const{
if(im==0) return to_string(re);
if(re==0){
if(im==1) return "i";
if(im==-1) return "-i";
return to_string(im)+"i";
}
string sign=(im > 0)?"+":"";
if(im==1) return to_string(re)+"+i";
if(im==-1) return to_string(re)+"-i";
return to_string(re)+sign+to_string(im)+"i";
}
T real()const{
return re;
}
T imag()const{
return im;
}
T modulus()const{
return sqrt(re*re+im*im);
}
T abs()const{
return sqrt(re*re+im*im);
}
T norm()const{
return re*re+im*im;
}
Complex conjugate()const{
return Complex(re,-im);
}
bool cmp_string(const Complex& other)const{
if(re!=other.re){
return re<other.re;
}
return im<other.im;
}
bool operator==(const Complex& other) const{
return (re==other.re)&&(im==other.im);
}
bool operator!=(const Complex& other) const{
return !(*this==other);
}
bool operator<(const Complex& other) const{
return this->cmp_string(other);
}
bool operator<=(const Complex& other) const{
return (*this)==other||(*this)<other;
}
bool operator>(const Complex& other) const{
return other<*this;
}
bool operator>=(const Complex& other) const{
return (*this)==other||(*this)>other;
}
Complex operator-() const{
return Complex(-re,-im);
}
Complex operator+() const{
return (*this);
}
Complex operator+(const Complex& other)const{
return Complex(re+other.re,im+other.im);
}
Complex operator+(const int& other)const{
return (*this)+Complex(other);
}
Complex operator-(const Complex& other)const{
return Complex(re-other.re,im-other.im);
}
Complex operator-(const int& other)const{
return (*this)-Complex(other);
}
Complex operator*(const Complex& other)const{
return Complex(re*other.re-im*other.im,re*other.im+im*other.re);
}
Complex operator*(const int& other)const{
return (*this)*Complex(other);
}
Complex operator/(const Complex& other)const{
T a=re,b=im,c=other.re,d=other.im;
if((c*c+d*d)==0) return 0;
return Complex((a*c+b*d)/(c*c+d*d),(b*c-a*d)/(c*c+d*d));
}
Complex operator/(const int& other)const{
return (*this)/Complex(other);
}
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const Complex<U>& other){
os<<other.Complex_to_string();
return os;
}
template<typename U>
friend std::istream& operator>>(std::istream& is, Complex<U>& other){
string str;
is>>str;
other.from_string(str);
return is;
}
template<typename U>
friend Complex<U> operator+(const int& a, const Complex<U>& b){
return Complex<U>(a)+b;
}
template<typename U>
friend Complex<U> operator-(const int& a, const Complex<U>& b){
return Complex<U>(a)-b;
}
template<typename U>
friend Complex<U> operator*(const int& a, const Complex<U>& b){
return Complex<U>(a)*b;
}
template<typename U>
friend Complex<U> operator/(const int& a, const Complex<U>& b){
return Complex<U>(a)/b;
}
private:
protected:
T re,im;
};
// //-------------------------------------------------------
// template<typename T>
// class Set{//集合 有限 无限
// public:
//
// private:
//
// protected:
// vector<T> element;
// };
//
// //-------------------------------------------------------
// template<typename T>
// class Function{//指数函数,对数函数,三角函数,幂函数
// public:
// enum class Type{
// Exp, // e^x
// Log, // ln(x)
// Sin,Cos,Tan, // 三角函数
// Power // x^a
// };
// private:
//
// protected:
// Type type;
// Expression<T> argument;
// T exponent;
// };
//
//
// //-------------------------------------------------------
// template<typename T>
// class Expression{//函数
// public:
//
// private:
//
// protected:
//// Set domain,range;//定义与和值域
//
// };
//
// //-------------------------------------------------------
// template<typename T>
// class Summation{//求和
// public:
//
// private:
//
// protected:
// T to,from;
// Expression<T> exp;
// };
//
// //-------------------------------------------------------
// template<typename T>
// class Product{//求积
// public:
//
// private:
//
// protected:
// };
//
// //-------------------------------------------------------
// template<typename T>
// class Angle{//角
// public:
//
// private:
//
// protected:
// };
//
//-------------------------------------------------------
template<typename T>
class Inequality{//不等式
public:
enum class Relation {
Less, LessEqual, Greater, GreaterEqual, Equal, NotEqual
};
static Relation string_to_Relation(string sig){
if(sig=="<") return Relation::Less;
if(sig=="<=") return Relation::LessEqual;
if(sig==">") return Relation::Greater;
if(sig==">=") return Relation::GreaterEqual;
if(sig=="==") return Relation::Equal;
if(sig=="!=") return Relation::NotEqual;
return Relation::Less;
}
static Relation flip(Relation rel){//翻转符号
switch(rel){
case Relation::Less: return Relation::Greater;
case Relation::LessEqual: return Relation::GreaterEqual;
case Relation::Greater: return Relation::Less;
case Relation::GreaterEqual: return Relation::LessEqual;
case Relation::Equal: return Relation::NotEqual;
case Relation::NotEqual: return Relation::Equal;
}
}
static string Relation_to_string(Relation rel){
switch(rel){
case Relation::Less: return "<";
case Relation::LessEqual: return "<=";
case Relation::Greater: return ">";
case Relation::GreaterEqual: return ">=";
case Relation::Equal: return "==";
case Relation::NotEqual: return "!=";
}
}
Inequality(Polynomial<T> l=Polynomial<T>(),Relation rr=Relation::Less,Polynomial<T> r=Polynomial<T>()):lhs(l),rhs(r),sign(rr){}
bool check(const vector<pair<char,T>>& vars) const{
return __sign_f(sign)(lhs.evaluate(vars),rhs.evaluate(vars));
}
void rhs_to_zero(){
lhs=lhs-rhs;
rhs=Polynomial<T>();
}
string Inequality_to_string()const{
return lhs.out_Polynomial()+Relation_to_string(sign)+rhs.out_Polynomial();
}
void from_string(const string& str){
string s = str;
s.erase(remove_if(s.begin(),s.end(),::isspace),s.end());
vector<string> rels={"<=",">=","==","!=","<",">"};
size_t pos=string::npos;
string found_rel;
for(const auto& rel:rels){
pos=s.find(rel);
if(pos!=string::npos){
found_rel=rel;
break;
}
}
if(pos==string::npos){
throw std::invalid_argument("No relation symbol found in inequality string");
}
string left_expr=s.substr(0, pos);
string right_expr=s.substr(pos+found_rel.size());
auto parse=[](const string& expr)->Polynomial<T>{
vector<Monomial<T>> mons;
string token;
for(size_t i=0;i<expr.size();++i){
char c=expr[i];
if(c=='+'||c=='-'){
if(!token.empty()){
std::istringstream iss(token);
Monomial<T> mon;
iss>>mon;
mons.push_back(mon);
token.clear();
}
token=c;
}else{
token+=c;
}
}
if(!token.empty()){
std::istringstream iss(token);
Monomial<T> mon;
iss>>mon;
mons.push_back(mon);
}
return Polynomial<T>(mons);
};
lhs=parse(left_expr);
rhs=parse(right_expr);
sign=string_to_Relation(found_rel);
}
Inequality<T> solve(){
}
void simplify(){
lhs.sort_out();
rhs.sort_out();
}
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const Inequality<U>& other){
os<<other.Inequality_to_string();
return os;
}
template<typename U>
friend std::istream& operator>>(std::istream& is,Inequality<U>& other){
string str;
is>>str;
other.from_string(str);
return is;
}
bool operator==(const Inequality& other) const{
return lhs==other.lhs&&rhs==other.rhs&&sign==other.sign;
}
bool operator!=(const Inequality& other) const{
return !(*this==other);
}
Inequality operator-() const{
return Inequality<T>(-lhs,sign,-rhs);
}
Inequality operator+() const{
return *this;
}
Inequality operator+(const T& other)const{
return Inequality<T>(lhs+other,sign,rhs+other);
}
Inequality operator-(const T& other)const{
return Inequality<T>(lhs-other,sign,rhs-other);
}
Inequality operator*(const T& other)const{
if(other==0) throw std::logic_error("Cannot multiply inequality by 0");
if(other>0) return Inequality<T>(lhs*other,sign,rhs*other);
else return Inequality<T>(lhs*other,flip(sign),rhs*other);
}
Inequality operator/(const T& other)const{
if(other==0) throw std::logic_error("Cannot multiply inequality by 0");
if(other>0) return Inequality<T>(lhs/other,sign,rhs/other);
else return Inequality<T>(lhs/other,flip(sign),rhs/other);
}
template<typename U>
friend Inequality<U> operator+(const U& a, const Inequality<U>& b){
Polynomial<U> pa({ Monomial<U>(a) });
return Inequality<U>(pa + b.lhs, b.sign, pa + b.rhs);
}
template<typename U>
friend Inequality<U> operator-(const U& a, const Inequality<U>& b){
Polynomial<U> pa({ Monomial<U>(a) });
return Inequality<U>(pa - b.lhs, b.sign, pa - b.rhs);
}
template<typename U>
friend Inequality<U> operator*(const U& a, const Inequality<U>& b){
if(a == 0) throw std::logic_error("Cannot multiply inequality by 0");
Polynomial<U> pa({ Monomial<U>(a) });
if(a > 0) return Inequality<U>(pa * b.lhs, b.sign, pa * b.rhs);
else return Inequality<U>(pa * b.lhs, flip(b.sign), pa * b.rhs);
}
template<typename U>
friend Inequality<U> operator/(const U& a, const Inequality<U>& b){
if(a == 0) throw std::logic_error("Cannot divide inequality by 0");
Polynomial<U> pa({ Monomial<U>(a) });
if(a > 0) return Inequality<U>(pa / b.lhs, b.sign, pa / b.rhs);
else return Inequality<U>(pa / b.lhs, flip(b.sign), pa / b.rhs);
}
private:
static std::function<bool(T, T)> __sign_f(Relation r) {
static std::function<bool(T, T)> table[] = {
[](T a, T b) { return a < b; }, // Less
[](T a, T b) { return a <= b; }, // LessEqual
[](T a, T b) { return a > b; }, // Greater
[](T a, T b) { return a >= b; }, // GreaterEqual
[](T a, T b) { return a == b; }, // Equal
[](T a, T b) { return a != b; } // NotEqual
};
return table[static_cast<int>(r)];
}
protected:
Polynomial<T> lhs,rhs;
Relation sign;
};
//-------------------------------------------------------
template<typename T>
class Equation{//等式
public:
Equation(Polynomial<T> l=Polynomial<T>(),Polynomial<T> r=Polynomial<T>()):lhs(l),rhs(r){}
bool check(const vector<pair<char,T>>& vars) const{
return lhs.evaluate(vars)==rhs.evaluate(vars);
}
void rhs_to_zero(){
lhs=lhs-rhs;
rhs=Polynomial<T>();
}
string Equation_to_string()const{
return lhs.out_Polynomial()+"="+rhs.out_Polynomial();
}
void from_string(const string& str){
size_t eq_pos=str.find('=');
if(eq_pos==string::npos){
throw std::invalid_argument("Equation must contain '='");
}
string left=str.substr(0,eq_pos);
string right=str.substr(eq_pos+1);
auto parse = [](const string& s) -> Polynomial<T> {
vector<Monomial<T>> mons;
string token;
for(size_t i=0;i<s.size();++i){
char c=s[i];
if(c=='+'||c=='-'){
if(!token.empty()){
std::istringstream iss(token);
Monomial<T> mon;
iss>>mon;
mons.push_back(mon);
token.clear();
}
token=c;
}else{
token+=c;
}
}
if(!token.empty()){
std::istringstream iss(token);
Monomial<T> mon;
iss>>mon;
mons.push_back(mon);
}
return Polynomial<T>(mons);
};
lhs=parse(left);
rhs=parse(right);
}
vector<Complex<T>> solve(){
Polynomial<T> eq=lhs-rhs;
eq.sort_out();
int d=eq.get_deg();
if(d==0){
return vector<Complex<T>>();
}
vector<T> coeffs(d+1,T(0));
auto mons=eq.split_Polynomial();
for(const auto& m:mons){
const auto& letters=m.get_letters();
if(letters.empty()){
coeffs[0]+=m.get_coe();
}else if(letters.size()==1&&letters[0].first=='x'){
int exp=letters[0].second;
coeffs[exp]+=m.get_coe();
}else{
continue;
}
}
if(d==1){
return __solve_linear(coeffs[1], coeffs[0]);
}else if(d == 2){
return __solve_quadratic(coeffs[2],coeffs[1],coeffs[0]);
}else{
vector<double> vals;
for(int i=d;i>=0;--i){
vals.push_back(static_cast<double>(coeffs[i]));
}
return __solve_numeric_by_newton_method(vals);
}
}
void simplify(){
lhs.sort_out();
rhs.sort_out();
}
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const Equation<U>& other){
os<<other.Equation_to_string();
return os;
}
template<typename U>
friend std::istream& operator>>(std::istream& is,Equation<U>& other){
string str;
is>>str;
other.from_string(str);
return is;
}
bool operator==(const Equation& other) const{
return lhs==other.lhs&&rhs==other.rhs;
}
bool operator!=(const Equation& other) const{
return !(*this==other);
}
Equation operator-() const{
return Equation<T>(lhs*(-1),rhs*(-1));
}
Equation operator+() const{
return *this;
}
Equation operator+(const Equation& other)const{
return Equation<T>(lhs+other.lhs,rhs+other.rhs);
}
Equation operator+(const T& other)const{
return Equation<T>(lhs+other,rhs+other);
}
Equation operator-(const Equation& other)const{
return Equation<T>(lhs-other.lhs,rhs-other.rhs);
}
Equation operator-(const T& other)const{
return Equation<T>(lhs-other,rhs-other);
}
Equation operator*(const Equation& other)const{
return Equation<T>(lhs*other.lhs,rhs*other.rhs);
}
Equation operator*(const T& other)const{
return Equation<T>(lhs*other,rhs*other);
}
Equation operator/(const Equation& other)const{
return Equation<T>(lhs/other.lhs,rhs/other.rhs);
}
Equation operator/(const T& other)const{
return Equation<T>(lhs/other,rhs/other);
}
Equation operator%(const Equation& other)const{
return Equation<T>(lhs%other.lhs,rhs%other.rhs);
}
Equation operator%(const T& other)const{
return Equation<T>(lhs%other,rhs%other);
}
template<typename U>
friend Equation<U> operator+(const T& a, const Equation<U>& b){
return Equation<T>(b.lhs+Polynomial<T>(a),b.rhs+Polynomial<T>(a));
}
template<typename U>
friend Equation<U> operator-(const T& a, const Equation<U>& b){
return Equation<T>(Polynomial<T>(a)-b.lhs,Polynomial<T>(a)-b.rhs);
}
template<typename U>
friend Equation<U> operator*(const T& a, const Equation<U>& b){
return Equation<T>(b.lhs*Polynomial<T>(a),b.rhs*Polynomial<T>(a));
}
template<typename U>
friend Equation<U> operator/(const T& a, const Equation<U>& b){
return Equation<T>(Polynomial<T>(a)/b.lhs,Polynomial<T>(a)/b.rhs);
}
template<typename U>
friend Equation<U> operator%(const T& a, const Equation<U>& b){
return Equation<T>(Polynomial<T>(a)%b.lhs,Polynomial<T>(a)%b.rhs);
}
private:
protected:
Polynomial<T> lhs,rhs;
vector<Complex<T>> __solve_linear(T a,T b){
if(a==0) return vector<Complex<T>>();
return vector<Complex<T>>{Complex<T>(-b/a,0)};
}
vector<Complex<T>> __solve_quadratic(T a,T b,T c){
if(a==0){
return __solve_linear(b,c);
}
T delta=b*b-4*a*c;
vector<Complex<T>> roots;
if(delta>=0){
T sqrt_delta=sqrt(static_cast<double>(delta));
roots.push_back(Complex<T>((-b+sqrt_delta)/(2*a),0));
roots.push_back(Complex<T>((-b-sqrt_delta)/(2*a),0));
}else{
T real_part=-b/(2*a);
T imag_part=sqrt(static_cast<double>(-delta))/(2*a);
roots.push_back(Complex<T>(real_part,imag_part));
roots.push_back(Complex<T>(real_part,-imag_part));
}
return roots;
}
vector<Complex<T>> __solve_numeric_by_newton_method(vector<double> vals){//vals_1x^n+vals_2x^{n-1}+...+vals_n=0
if(vals.empty()||vals[0]==0) return vector<Complex<T>>();
vector<double> coeffs(vals.size());
for(size_t i=0;i<vals.size();++i) coeffs[i]=static_cast<double>(vals[i]);
auto f=[&](double x)->double{
double sum=0;
double pow=1;
int n=coeffs.size()-1;
for(int i=n;i>=0;--i){
sum+=coeffs[i]*pow;
pow*=x;
}
return sum;
};
auto df=[&](double x)->double{
const double h=1e-7;
return (f(x+h)-f(x-h))/(2*h);
};
auto newton=[&](double x0)->double{
double x=x0;
for(int iter=0;iter<100;++iter){
double fx=f(x);
double dfx=df(x);
if(fabs(dfx)<1e-12) break;
double x_new=x-fx/dfx;
if(fabs(x_new-x)< 1e-7) return x_new;
x=x_new;
}
return x;
};
vector<double> roots;
double step=0.05;
for(double x0=-20;x0<=20;x0+=step){
double r=newton(x0);
if(fabs(f(r))<1e-4){
bool dup=false;
for(double root:roots){
if(fabs(root-r)<1e-5){
dup=true;
break;
}
}
if(!dup) roots.push_back(r);
}
}
vector<Complex<T>> result;
for(double r:roots){
result.push_back(Complex<T>(static_cast<T>(r),T(0)));
}
return result;
}
int get_deg(){
int deg=max(lhs.get_deg(),rhs.get_deg());
return deg;
}
};
// //-------------------------------------------------------
// template<typename T>
// class Sequence{//数列
// public:
//
// private:
//
// protected:
//
// };
//
// //-------------------------------------------------------
// template<typename T>
// class Statistics{//统计
// public:
//
// private:
//
// protected:
// };
//
// //-------------------------------------------------------
// template<typename T>
// class Vector{//向量
// public:
//
// private:
//
// protected:
// };
}
using namespace super_math;
int main() {
return 0;
}
/*
2x+1=1
*/