blob: 34cd4ccd5e28b2c99443e821cdb93790dd12ccbe (
plain)
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
|
#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:
|