diff options
author | Tom Smeding <tom@tomsmeding.com> | 2025-09-03 23:31:40 +0200 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2025-09-03 23:37:53 +0200 |
commit | 7e60437d3c064bca402d486be967c43bf4326067 (patch) | |
tree | 8b4307b90c942861176d9c705cd760b9fe7b6c6b /eqsystem_solve.h |
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).
Diffstat (limited to 'eqsystem_solve.h')
-rw-r--r-- | eqsystem_solve.h | 62 |
1 files changed, 62 insertions, 0 deletions
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 <optional> +#include <string> +#include <variant> +#include <vector> + + +struct Product { + double cnst; + std::vector<std::string> 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<Product> 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<double> evaluate() const; +}; + +// An equation is not allowed to contain a variable name more than once. +struct Equation { + Sum lhs, rhs; + + std::optional<std::string> 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<std::string> all_vars() const; + bool isolatable(const std::string &target) const; + void isolate_left(const std::string &target); + void simplify(); +}; + +struct System { + std::vector<Equation> eqs; + + std::variant< + std::string, // description of the error + std::vector<std::pair<std::string, double>> // 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: |