Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion tests/test_utils.py → src/simpy/debug/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional, Tuple, Union

from simpy.debug.utils import debug_repr
from simpy.expr import Expr, Symbol, TrigFunctionNotInverse, cast, log, symbols
from simpy.expr import Expr, Float, Symbol, TrigFunctionNotInverse, cast, log, symbols
from simpy.integration import integrate
from simpy.simplify import expand_logs, trig_simplify

Expand Down Expand Up @@ -83,3 +83,14 @@ def unhashable_set_eq(a: list, b: list) -> bool:
if el not in a:
return False
return True


def eq_float(e1: Expr, e2: Expr, atol=1e-6):
if type(e1) != type(e2):
return False
if isinstance(e1, Float) and isinstance(e2, Float):
return abs(e1.value - e2.value) < atol

if not len(e1.children()) == len(e2.children()):
return False
return all(eq_float(c1, c2) for c1, c2 in zip(e1.children(), e2.children()))
37 changes: 37 additions & 0 deletions src/simpy/equation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from dataclasses import dataclass

from .expr import Expr, Symbol


@dataclass
class Equation:
lhs: Expr
rhs: Expr


def solve(equation: Equation, x: Symbol):
# Ensure the equation is set to 0 (ax + b = 0)
expr = equation.lhs - equation.rhs

# Initialize coefficients
coeff_a = 0
coeff_b = 0

# Iterate over the terms of the expression
for term in expr.as_terms():
if (term / x).symbolless:
coeff_a += term / x
elif not term.contains(x):
coeff_b += term
else:
raise NotImplementedError("can only solve linear eqs")

# Calculate the solution
if coeff_a == 0:
if coeff_b == 0:
return "Infinite solutions (identity equation)."
else:
return "No solution (contradictory equation)."

x_solution = -coeff_b / coeff_a
return x_solution
Loading
Loading