From 7e60437d3c064bca402d486be967c43bf4326067 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Wed, 3 Sep 2025 23:31:40 +0200 Subject: Initial (with old code) This includes old code too, perhaps from 2021-05-05, judging from the birth timestamp on the directory. The old code included option parsing and equation solving for fancy mutually-dependent options, but no actual rendering (imagine that). --- eqsystem_solve.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 eqsystem_solve.h (limited to 'eqsystem_solve.h') diff --git a/eqsystem_solve.h b/eqsystem_solve.h new file mode 100644 index 0000000..34cd4cc --- /dev/null +++ b/eqsystem_solve.h @@ -0,0 +1,62 @@ +#pragma once + +#include +#include +#include +#include + + +struct Product { + double cnst; + std::vector vars; + + const Product times(double x) const; + void times_inplace(double x); + const Product times(const Product &p) const; + bool contains(const std::string &var) const; + const Product without(const std::string &var) const; + + bool operator==(const Product &other) const; +}; + +struct Sum { + std::vector terms; + + const Sum times(double x) const; + const Sum substitute(const std::string &var, Sum sum) const; + void simplify(); + // Returns std::nullopt if there are variables in the terms + std::optional evaluate() const; +}; + +// An equation is not allowed to contain a variable name more than once. +struct Equation { + Sum lhs, rhs; + + std::optional validate() const; + const Equation substitute(const std::string &var, Sum sum) const; + void substitute_inplace(const std::string &var, Sum sum); + int num_vars() const; + std::vector all_vars() const; + bool isolatable(const std::string &target) const; + void isolate_left(const std::string &target); + void simplify(); +}; + +struct System { + std::vector eqs; + + std::variant< + std::string, // description of the error + std::vector> // result assignment + > solve_inplace(bool debug); + + void substitute_inplace(const std::string &var, Sum sum); +}; + +std::ostream& operator<<(std::ostream &os, const Product &p); +std::ostream& operator<<(std::ostream &os, const Sum &s); +std::ostream& operator<<(std::ostream &os, const Equation &eq); +std::ostream& operator<<(std::ostream &os, const System &sys); + +// vim: set sw=4 ts=4 noet: -- cgit v1.2.3-70-g09d2