Operation#
- class impacthpc.src.core.Operation(name: str | None, value: Quantity | str, min: Quantity | str | None, max: Quantity | str | None, operator: str, operands: list[ReplicableValue | int | float], isInfix: bool = True, standard_deviation: Quantity | str | None = None, warnings: List[str] = [], explaination: str | None = None, ontology: Ontology | None = None, important: bool = False)#
Bases:
ReplicableValue
Subclass of
ReplicableValue
representing the composed nodes of a tree, as opposed toSourcedValue
, which represents the leaf of the tree.In addition to the attributes of ReplicableValue, Operations have an operator and operands and can represent infix operations (like a + b, where the
+
operator is between the two operands) or prefix operations (like ln(a), where the operator is a function name before the operands).- operands#
The list of operands. Operands can be ReplicableValues or numbers (int or float). Numbers are treated as if min = max = value, and standard_deviation = 0.
- Type:
List[ReplicableValue | int | float]
- isInfix#
True
if the operation is infix (like a + b, where the+
operator is between the two operands),False
if it is prefix (like ln(a), where the operator is a function name before the operands).- Type:
Methods Summary
Methods Documentation
- _as_formula() str #
Returns a string representing the formula that this ReplicableValue represents.
For each operand, it is converted to a string (see
ReplicableValue.__repr__()
). If the operand is an intermediate result (i.e., it has a name, seeReplicableValue.make_intermediate_result()
), it will return its name; otherwise, it will recursively call itsReplicableValue._as_formula()
method.The decision to parenthesize an operand is based on the following rule: If an operand is not an intermediate result and it is an Operation (i.e., its string representation will be a formula), then we examine its operator. If this operator has a higher priority than
self.operator
according tooperator_priorities
, we parenthesize the operand. Otherwise, we do not.- Returns:
A string representing the formula that this ReplicableValue represents.
- Return type:
- _explain_rec(formatter: ~impacthpc.src.core.formatters.Formatter[~typing.Any, T] = <impacthpc.src.core.formatters.TextFormatter object>, recursion_level: int = 0, already_seen_values: set[int] = {}) T | list[T] #
Provides an explanation of the current object using the specified formatter. Uses a Depth-first search algorithm, and respect the order of the
operands
list.First, it calls the
ReplicableValue._explain_rec()
method on each of its operands. Then, it flattens the list of operands’ operands to obtain the list of ReplicableValues used to compute this result.If the current object is an intermediate result (i.e., it has a name, see
ReplicableValue.make_intermediate_result()
), it callsFormatter.format_operation()
with the created list.Otherwise, if it is not an intermediate result, it simply returns this list as is.
For example, suppose that
self.operator
is “+” andself.operands
is[A, B]
, where A and B are two ReplicableValues. Ifself.name
is not None, then this Operation is an intermediate result. We callA.explain()
andB.explain()
.Let’s say A is a SourcedValue, so
A.explain()
returns something of the formatter’s type, whileB.explain()
is an operation that isn’t an intermediate result. Thus,B.explain()
returns a list of ReplicableValues. Suppose it returns a list of two SourcedValues. We create a 3-element list with the return value of A and the two operands of B, and we pass it toFormatter.format_operation()
.If
self.name
had been None, then this Operation would not be an intermediate result, and thus we would have just returned the 3-element list as is.- Parameters:
formatter (Formatter, optional) – The formatter used to format the explanation. Defaults to
TextFormatter
.recursion_level (int, optional) – The current recursion depth, used to control nested explanations. Defaults to 0.
- Returns:
The formatted explanation of the object or a list of formatted explanations.
- Return type:
Union[T, List[T]]
- extract_important_values() list[SourcedValue] #
Returns a list of the SourcedValues (leaves) used in the computation.
Recursively calls
extract_sourced_values()
on each operand.