调整目录结构

This commit is contained in:
2025-05-19 14:02:28 +08:00
parent 0ae1785e6c
commit 26ac3ddebd
7 changed files with 494 additions and 616 deletions

225
src/common.cpp Normal file
View File

@@ -0,0 +1,225 @@
#include "common.hpp"
using namespace sv;
double Var::get(DoubleAttr attr)
{
switch (attr) {
case DoubleAttr::Coeff:
return coeffs;
case DoubleAttr::X:
return val;
}
return -1;
}
int Var::get(IntAttr attr)
{
return col;
}
Expr sv::operator+(const Expr& x, const Expr& y)
{
Expr exp;
exp.coeffs.resize(std::max(x.coeffs.size(), y.coeffs.size()), 0);
for (int c = 0; c < exp.coeffs.size(); c++) {
if (c < x.coeffs.size() && c < y.coeffs.size()) {
exp.coeffs[c] = x.coeffs[c] + y.coeffs[c];
}
else {
exp.coeffs[c] = c < x.coeffs.size() ? x.coeffs[c] : y.coeffs[c];
}
}
exp.constant = x.constant + y.constant;
return exp;
}
Expr sv::operator+(const Expr& x)
{
return x;
}
Expr sv::operator+(Var x, Var y)
{
Expr exp;
exp.coeffs.resize(std::max(x.get(IntAttr::NumCol) + 1, y.get(IntAttr::NumCol) + 1), 0);
exp.coeffs[x.get(IntAttr::NumCol)] = x.get(DoubleAttr::Coeff);
exp.coeffs[y.get(IntAttr::NumCol)] = y.get(DoubleAttr::Coeff);
return exp;
}
Expr sv::operator+(Var x, double a)
{
Expr exp;
exp.coeffs.resize(x.get(IntAttr::NumCol) + 1);
exp.constant = a;
return exp;
}
Expr sv::operator+(double a, Var x)
{
return x + a;
}
Expr sv::operator-(const Expr& x, const Expr& y)
{
Expr exp;
exp.coeffs.resize(std::max(x.coeffs.size(), y.coeffs.size()), 0);
for (int c = 0; c < exp.coeffs.size(); c++) {
if (c < x.coeffs.size() && c < y.coeffs.size()) {
exp.coeffs[c] = x.coeffs[c] - y.coeffs[c];
}
else {
exp.coeffs[c] = c < x.coeffs.size() ? x.coeffs[c] : y.coeffs[c];
}
}
exp.constant = x.constant + y.constant;
return exp;
}
Expr sv::operator-(const Expr& x)
{
Expr expr(x);
for (int c = 0; c < expr.coeffs.size(); c++) {
expr.coeffs[c] = -expr.coeffs[c];
}
expr.constant = -expr.constant;
return expr;
}
Expr sv::operator-(Var x)
{
return -Expr(x);
}
Expr sv::operator-(Var x, Var y)
{
Expr exp;
exp.coeffs.resize(std::max(x.get(IntAttr::NumCol) + 1, y.get(IntAttr::NumCol) + 1), 0);
exp.coeffs[x.get(IntAttr::NumCol)] = x.get(DoubleAttr::Coeff);
exp.coeffs[y.get(IntAttr::NumCol)] = -y.get(DoubleAttr::Coeff);
return exp;
}
Expr sv::operator-(Var x, double a)
{
return x - Var(a);
}
Expr sv::operator-(double a, Var x)
{
return x - a;
}
Expr sv::operator*(double a, Var x)
{
Expr exp;
exp.coeffs.resize(x.get(IntAttr::NumCol) + 1, 0);
exp.coeffs[x.get(IntAttr::NumCol)] = a * x.get(DoubleAttr::Coeff);
return exp;
}
Expr sv::operator*(Var x, double a)
{
return a * x;
}
Expr sv::operator*(const Expr& x, double a)
{
Expr exp = x;
for (int c = 0; c < exp.coeffs.size(); c++) {
exp.coeffs[c] *= a;
}
exp.constant *= a;
return exp;
}
Expr sv::operator*(double a, const Expr& x)
{
return x * a;
}
Expr sv::operator/(Var x, double a)
{
return Expr(x) / a;
}
Expr sv::operator/(const Expr& x, double a)
{
Expr exp = x;
for (int c = 0; c < exp.coeffs.size(); c++) {
exp.coeffs[c] /= a;
}
exp.constant /= a;
return exp;
}
Expr::Expr(double constant)
:constant(constant)
{
}
Expr::Expr(Var var, double coeff)
{
this->coeffs.resize(var.col + 1);
this->coeffs[var.col] = coeff;
this->constant = 0;
}
Expr Expr::operator=(const Expr& rhs)
{
return *this;
}
void Expr::operator+=(const Expr& expr)
{
coeffs.resize(std::max(coeffs.size(), expr.coeffs.size()));
for (int c = 0; c < expr.coeffs.size(); c++) {
coeffs[c] += expr.coeffs[c];
}
constant += expr.constant;
}
void Expr::operator-=(const Expr& expr)
{
coeffs.resize(std::max(coeffs.size(), expr.coeffs.size()));
for (int c = 0; c < expr.coeffs.size(); c++) {
coeffs[c] -= expr.coeffs[c];
}
constant -= expr.constant;
}
void Expr::operator*=(double mult)
{
for (int c = 0; c < coeffs.size(); c++) {
coeffs[c] *= mult;
}
constant *= mult;
}
void Expr::operator/=(double a)
{
for (int c = 0; c < coeffs.size(); c++) {
coeffs[c] /= a;
}
constant /= a;
}
Expr Expr::operator+(const Expr& rhs)
{
coeffs.resize(std::max(coeffs.size(), rhs.coeffs.size()));
for (int c = 0; c < rhs.coeffs.size(); c++) {
coeffs[c] += rhs.coeffs[c];
}
constant += rhs.constant;
return *this;
}
Expr Expr::operator-(const Expr& rhs)
{
coeffs.resize(std::max(coeffs.size(), rhs.coeffs.size()));
for (int c = 0; c < rhs.coeffs.size(); c++) {
coeffs[c] -= rhs.coeffs[c];
}
constant -= rhs.constant;
return *this;
}