lp_types/equations/
mod.rs1use crate::{LinearConstraint, LpResult};
2use serde_derive::{Deserialize, Serialize};
3use std::{
4 collections::BTreeMap,
5 fmt::{Display, Formatter},
6 ops::AddAssign,
7};
8
9mod display;
10
11#[derive(Debug)]
12pub struct LinearEquation<T> {
13 coefficients: BTreeMap<String, LinearCoefficient<T>>,
14 constraint: LinearConstraint<T>,
15}
16
17#[derive(Debug, Serialize, Deserialize)]
18pub struct LinearCoefficient<T> {
19 symbol: String,
20 coefficients: T,
21}
22
23impl<T> LinearCoefficient<T> {}
24
25impl<T> LinearEquation<T> {
26 pub fn new(constraint: LinearConstraint<T>) -> LpResult<Self> {
27 Ok(Self { coefficients: BTreeMap::new(), constraint })
28 }
29 pub fn get_coefficients(&self) -> impl Iterator<Item = (&str, &T)> {
30 self.coefficients.iter().map(|(s, c)| (s.as_str(), &c.coefficients))
31 }
32 pub fn add_coefficient(&mut self, coefficient: T, symbol: &str)
33 where
34 T: AddAssign,
35 {
36 match self.coefficients.get_mut(symbol) {
37 Some(s) => {
38 s.coefficients += coefficient;
39 }
40 None => {
41 self.coefficients
42 .insert(symbol.to_string(), LinearCoefficient { symbol: symbol.to_string(), coefficients: coefficient });
43 }
44 }
45 }
46 pub fn variables(&self) -> impl Iterator<Item = &str> {
47 self.coefficients.keys().map(|s| s.as_str())
48 }
49}