Skip to content

formula

codes.formula

Module for the abstract base class Formula.

Classes:

codes.formula.AggregatedComparisonFormula

AggregatedComparisonFormula(
    aggregation: Callable[[Iterable[bool]], bool],
    comparison_formulas: Sequence[ComparisonFormula],
)

Bases: ComparisonFormula

Base class for aggregating comparison formulas used in the codes. Examples: (angle < angle_max) and (height > height_min).

This class is abstract: it does not implement label, source_document or latex. A concrete formula that aggregates other comparison formulas should subclass this and provide those, the same way concrete subclasses of :class:ComparisonFormula do.

Method for initializing a new instance of the class.

Parameters:

  • aggregation (Callable[[Iterable[bool]], bool]) –

    Type of aggregation function to be used for the comparison formulas. Must be either all or any.

  • comparison_formulas (Sequence[ComparisonFormula]) –

    Sequence of ComparisonFormula instances to be aggregated.

Source code in blueprints/codes/formula.py
257
258
259
260
261
262
263
264
265
266
267
268
269
def __init__(self, aggregation: Callable[[Iterable[bool]], bool], comparison_formulas: Sequence[ComparisonFormula]) -> None:
    """Method for initializing a new instance of the class.

    Parameters
    ----------
    aggregation : Callable[[Iterable[bool]], bool]
        Type of aggregation function to be used for the comparison formulas. Must be either all or any.
    comparison_formulas : Sequence[ComparisonFormula]
        Sequence of ComparisonFormula instances to be aggregated.
    """
    super().__init__()
    self.aggregation = aggregation
    self.comparison_formulas = tuple(comparison_formulas)

codes.formula.AggregatedComparisonFormula.lhs property

lhs: float

Disabled property for getting the left-hand side of the comparison, as it is not relevant for this class.

codes.formula.AggregatedComparisonFormula.rhs property

rhs: float

Disabled property for getting the right-hand side of the comparison, as it is not relevant for this class.

codes.formula.AggregatedComparisonFormula.unity_check property

unity_check: float

Property to present the unity check of the formula.

A unity check is the ratio between the left-hand side (lhs) and right-hand side (rhs) of a comparison formula. For an aggregated comparison formula, the unity check is determined by the aggregation function (all or any) applied to the unity checks of the individual comparison formulas:

  • If aggregation is all, the unity check is the maximum of the unity checks of the individual formulas.
  • If aggregation is any, the unity check is the minimum of the unity checks of the individual formulas.

A unity check <= 1 indicates the condition is satisfied. A unity check > 1 indicates the condition is not satisfied.

Examples:

formula1.unity_check = 0.9 formula2.unity_check = 1.1

aggregated_formula = AggregatedComparisonFormula(all, [formula1, formula2]) aggregated_formula.unity_check # Returns 1.1, as the maximum of the unity checks is taken for 'all' aggregation.

aggregated_formula = AggregatedComparisonFormula(any, [formula1, formula2]) aggregated_formula.unity_check # Returns 0.9, as the minimum of the unity checks is taken for 'any' aggregation.

Returns:

  • float

    The unity check ratio.

codes.formula.AggregatedComparisonFormula.latex

latex(n: int = 3) -> LatexFormula

Return the latex representation of the aggregated comparison formula.

Parameters:

  • n (int, default: 3 ) –

    The number of decimal places to round the result to.

Returns:

  • LatexFormula

    The latex representation of the formula, given in math mode.

Source code in blueprints/codes/formula.py
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
def latex(self, n: int = 3) -> LatexFormula:
    """Return the latex representation of the aggregated comparison formula.

    Parameters
    ----------
    n : int, optional
        The number of decimal places to round the result to.

    Returns
    -------
    LatexFormula
        The latex representation of the formula, given in math mode.
    """
    aggregation = r"\ \&\ " if self.aggregation is all else r"\ \text{or}\ "
    comparison_equations = aggregation.join(formula.latex(n).equation for formula in self.comparison_formulas)
    comparison_numeric_equations = aggregation.join(formula.latex(n).numeric_equation for formula in self.comparison_formulas)
    return LatexFormula(
        return_symbol=r"CHECK",
        result="OK" if self.__bool__() else "\\text{Not OK}",
        equation=comparison_equations,
        numeric_equation=comparison_numeric_equations,
        comparison_operator_label="\\to",
        unit="",
    )

codes.formula.ComparisonFormula

ComparisonFormula(*args, **kwargs)

Bases: Formula, ABC

Base class for comparison formulas used in the codes.

Source code in blueprints/codes/formula.py
21
22
23
24
def __init__(self, *args, **kwargs) -> None:
    """Method for initializing a new instance of the class."""
    super().__init__(*args, **kwargs)
    self._initialized = True

codes.formula.ComparisonFormula.lhs property

lhs: float

Property for getting the left-hand side of the comparison.

Returns:

  • float

    The left-hand side value of the comparison.

codes.formula.ComparisonFormula.rhs property

rhs: float

Property for getting the right-hand side of the comparison.

Returns:

  • float

    The right-hand side value of the comparison.

codes.formula.ComparisonFormula.unity_check property

unity_check: float

Property to present the unity check of the formula.

A unity check is the ratio between the left-hand side (lhs) and right-hand side (rhs) of a comparison formula. The calculation is operator-dependent to ensure a unity check less than 1 always indicates the condition is satisfied:

  • For le (<=) and lt (<): unity_check = lhs / rhs
  • For ge (>=) and gt (>): unity_check = rhs / lhs
  • For eq (==) and other operators: unity_check = lhs / rhs

A unity check < 1 indicates the condition is satisfied. A unity check >= 1 indicates the condition is not satisfied.

Examples:

lhs = 0.11, rhs = 0.1, Formula: lhs <= rhs, unity_check = 1.1 # NOT satisfied lhs = 0.09, rhs = 0.1, Formula: lhs <= rhs, unity_check = 0.9 # satisfied lhs = 0.2, rhs = 0.1, Formula: lhs >= rhs, unity_check = 0.5 # satisfied lhs = 0.05, rhs = 0.1, Formula: lhs >= rhs, unity_check = 2.0 # NOT satisfied

Returns:

  • float

    The unity check ratio.

codes.formula.DoubleComparisonFormula

DoubleComparisonFormula(*args, **kwargs)

Bases: Formula

Base class for double comparison formulas used in the codes. Examples: angle_min < angle < angle_max or angle_min > angle > angle_max.

Note that the comparison operators must point in the same direction for both sides: - Ascending: operator.lt (<) or operator.le (<=) - Descending: operator.gt (>) or operator.ge (>=) Mixed directions (e.g., < and >) are not allowed.

Source code in blueprints/codes/formula.py
21
22
23
24
def __init__(self, *args, **kwargs) -> None:
    """Method for initializing a new instance of the class."""
    super().__init__(*args, **kwargs)
    self._initialized = True

codes.formula.DoubleComparisonFormula.lhs property

lhs: float

Property for getting the left-hand side of the double comparison.

Returns:

  • float

    The left-hand side value of the comparison.

codes.formula.DoubleComparisonFormula.rhs property

rhs: float

Property for getting the right-hand side of the double comparison.

Returns:

  • float

    The right-hand side value of the comparison.

codes.formula.DoubleComparisonFormula.val property

val: float

Property for getting the middle value of the double comparison to be checked against the bounds.

Returns:

  • float

    The left-hand side value of the comparison.

codes.formula.Formula

Formula(*args, **kwargs)

Bases: float, ABC

Abstract base class for formulas used in the codes.

Method for initializing a new instance of the class.

Source code in blueprints/codes/formula.py
21
22
23
24
def __init__(self, *args, **kwargs) -> None:
    """Method for initializing a new instance of the class."""
    super().__init__(*args, **kwargs)
    self._initialized = True

codes.formula.Formula.detailed_result property

detailed_result: dict

Property for providing the detailed result of the formula.

Returns:

  • dict

    The detailed result of the formula. Keys are strings representing the name of the partial or intermediate result. Values types will depend on the specific implementation, but must be a serializable type.

codes.formula.Formula.label abstractmethod property

label: str

Property for the formula label.

For example, "5.2" for formula 5.2.

Returns:

  • str

    The label/number associated with the formula. This is an abstract method and must be implemented in all subclasses.

codes.formula.Formula.source_document abstractmethod property

source_document: str

Property for the source document.

For example, "EN 1992-1-1:2004" Try to use the official and complete name of the document including publishing year, if possible.

Returns:

  • str

    The reference to the document where the formula originates. This is an abstract method and must be implemented in all subclasses.

codes.formula.Formula.latex abstractmethod

latex(n: int = 3) -> LatexFormula

Abstract method for the latex representation of the formula, given in math mode.

Parameters:

  • n (int, default: 3 ) –

    The number of decimal places to round the result to.

Returns:

  • LatexFormula

    The latex representation of the formula, given in math mode. This is an abstract method and must be implemented in all subclasses.

Source code in blueprints/codes/formula.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
@abstractmethod
def latex(self, n: int = 3) -> LatexFormula:
    """Abstract method for the latex representation of the formula, given in math mode.

    Parameters
    ----------
    n : int, optional
        The number of decimal places to round the result to.

    Returns
    -------
    LatexFormula
        The latex representation of the formula, given in math mode.
        This is an abstract method and must be implemented in all subclasses.
    """