Skip to content

Commit

Permalink
🚧 WIP: Use the Rust implementation on the Variable (to connect with t…
Browse files Browse the repository at this point in the history
…he different rust methods easily)
  • Loading branch information
ricardoleal20 committed Aug 11, 2024
1 parent 5ad5bde commit 3bade43
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
20 changes: 20 additions & 0 deletions pymath_compute/engine/model.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Rust models. Needed for all the important mathematical calculations
and mathematical operations
"""
from typing import TypeVar, Optional

Bound = TypeVar("Bound", int, float)


class EngineVar:
"""Rust Variable for the engine operations."""

def __init__(
self,
name: str,
lb: Bound = float("-inf"),
ub: Bound = float("inf"),
v0: Optional[Bound] = None,
only_integer: bool = False
) -> None: ...
37 changes: 19 additions & 18 deletions pymath_compute/model/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from pymath_compute.model.types import Operators, Bound
from pymath_compute.model.expression import MathExpression
from pymath_compute.model.function import MathFunction
# From the engine, import the Variable
from pymath_compute.engine.model import EngineVar


class Variable:
Expand All @@ -28,13 +30,16 @@ class Variable:
_name: str
lower_bound: float
upper_bound: float
_value: Optional[float]
# Get the variable for the Rust Engine
_eng_var: EngineVar
_is_integer: bool
# Define the slots to save memory space
__slots__ = [
"_name", "lower_bound",
"upper_bound", "_value",
"_is_integer"
"_name",
"lower_bound",
"upper_bound",
"_is_integer",
"_eng_var"
]

def __init__(
Expand Down Expand Up @@ -70,8 +75,11 @@ def __init__(
self._name = name
self.lower_bound = lb
self.upper_bound = ub
self._value = value
self._is_integer = only_integer
# Create the engine var
self._eng_var = EngineVar(
name, lb, ub, value, only_integer
)

@property
def name(self) -> str:
Expand All @@ -90,8 +98,8 @@ def value(self) -> float:
Returns:
float: The current value of the variable.
"""
value = self._value if self._value else 0.0

# Get the var from the EngineVar
value = self._eng_var.value
return int(value) if self._is_integer else value

@value.setter
Expand All @@ -106,15 +114,8 @@ def value(self, new_value: float) -> int | float:
[lower_bound, upper_bound] range, it would
take the closes bound as the value.
"""
# Evaluate if the value is inside the range
if self.lower_bound >= new_value:
self._value = self.lower_bound
elif self.upper_bound <= new_value:
self._value = self.upper_bound
else:
# Set the value
self._value = new_value
return self._value
self._eng_var.set_value(new_value)
return self.value

def to_expression(self) -> 'MathExpression':
"""Convert this Variable into a MathExpression"""
Expand All @@ -125,8 +126,8 @@ def plot(self) -> None:
self.to_expression().plot()

def __repr__(self) -> str:
if self._value is not None:
return f"{self.name}: {self._value}"
if self.value is not None:
return f"{self.name}: {self.value}"
return self.name

# ============================================= #
Expand Down

0 comments on commit 3bade43

Please sign in to comment.