You are here

class UnitsMathematicalOperatorNonLinear in Units of Measurement 7.2

Implementation of "mathematical operator" interface for non-linear operation.

Implementation of "mathematical operator" interface for a non-linear mathematical operation.

Hierarchy

Expanded class hierarchy of UnitsMathematicalOperatorNonLinear

1 string reference to 'UnitsMathematicalOperatorNonLinear'
non_linear.inc in plugins/operator/non_linear.inc

File

./units.module, line 1598
Provide API for managing and converting units of measurement.

View source
class UnitsMathematicalOperatorNonLinear extends AbstractUnitsMathematicalOperator {

  /**
   * Constructor.
   *
   * @param array $operator
   *   Definition of a cTools 'operator' plugin that represents the specific
   *   operator
   * @param \UnitsConstantMathematicalExpression $operand1
   *   Operand #1 for this mathematical expression
   * @param \UnitsEntity $operand2
   *   Operand #2 for this mathematical expression
   *
   * @throws UnitsMathematicalExpressionMalformedException
   *   Exception is thrown if the linearity is not consistent (none or both
   *   non-linear operands)
   */
  public function __construct(array $operator, \UnitsConstantMathematicalExpression $operand1, \UnitsEntity $operand2) {
    parent::__construct($operator, $operand1, $operand2);
    if ($this
      ->nonLinearMember()
      ->isLinear()) {
      throw new UnitsMathematicalExpressionMalformedException(t('Expression %expression operates on 2 linear members while the operator is non-linear.', array(
        '%expression' => $this
          ->toInfix(),
      )));
    }
  }

  /**
   * Determine physical dimension of this mathematical expression.
   *
   * @return array
   *   Dimension array of this mathematical expression
   */
  public function dimension() {
    return $this
      ->nonLinearMember()
      ->dimension();
  }

  /**
   * Format a certain amount of quantity within this mathematical expression.
   *
   * @param float $quantity
   *   Quantity to be formatted
   *
   * @return UnitsMathematicalExpression
   *   Formatted quantity into this mathematical expression. Sometimes the
   *   mathematical expression itself must mutate in order to format the
   *   quantity. So the returned mathematical expression may not necessarily be
   *   the mathematical expression on which this method was invoked. For
   *   example, the expression "unit" would mutate into "1 * unit" in order to
   *   have a dimensionless member and therefore be able to format the $quantity
   */
  public function formatQuantity($quantity) {
    $unit = $this
      ->nonLinearMember();
    $decomposition = $unit
      ->inverseDecompose();

    // Swap the quantity placeholder with the real value.
    $decomposition = units_mathematical_expression_create_from_postfix(str_replace(UNITS_QUANTITY, $quantity, $decomposition
      ->toPostfix()))
      ->getExpression();
    $this
      ->linearMember()
      ->formatQuantity($decomposition
      ->evaluate());
    return $this;
  }

  /**
   * Numerically evaluate this mathematical expression.
   *
   * @return float
   *   Numerical value of this mathematical expression
   */
  public function evaluate() {
    return $this
      ->decompose()
      ->evaluate();
  }

  /**
   * Decompose (simplify) this mathematical expression.
   *
   * @return UnitsMathematicalExpression
   *   Decomposed (simplified) version of this mathematical expression
   */
  public function decompose() {
    $unit = $this
      ->nonLinearMember();

    // This is somewhat hacky, but it's the best I could come up with. We will
    // temporarily swap decomposition expression in the unit with the specific
    // quantity and then will invoke the decomposition on the unit.
    $temporary_decomposition = $unit->decomposition;
    $unit->decomposition = units_mathematical_expression_create_from_postfix(str_replace(UNITS_QUANTITY, $this
      ->linearMember()
      ->toPostfix(), $unit->decomposition
      ->getExpression()
      ->toPostfix()));
    $decomposition = $unit
      ->decompose();
    $unit->decomposition = $temporary_decomposition;
    return $decomposition;
  }

  /**
   * Whether this expression is linearly decomposable.
   *
   * @return bool
   *   Whether this expression is linearly decomposable, i.e. its decomposition
   *   can be plugged in into another mathematical expression via multiplication
   *   without losing sense
   */
  public function isLinear() {
    return FALSE;
  }

  /**
   * Fetch the linear member of the non-linear operator.
   *
   * @return \UnitsConstantMathematicalExpression
   *   The linear member of the non-linear operator, which is always a constant
   */
  public function linearMember() {
    return $this->operand1;
  }

  /**
   * Fetch the non-linear member of the non-linear operator.
   *
   * @return \UnitsEntity
   *   The non-linear member of the non-linear operator, which is always a
   *   non-linear unit entity
   */
  public function nonLinearMember() {
    return $this->operand2;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractUnitsMathematicalOperator::$operand1 protected property Operand #1 of this mathematical expression.
AbstractUnitsMathematicalOperator::$operand2 protected property Operand #2 of this mathematical expression.
AbstractUnitsMathematicalOperator::$operator protected property Definition of the underlying cTools 'operator' plugin.
AbstractUnitsMathematicalOperator::containsDimensionlessMember public function Test whether this mathematical expression includes a dimensionless member. Overrides UnitsMathematicalExpression::containsDimensionlessMember
AbstractUnitsMathematicalOperator::evaluateOperands protected function Numerically evaluate both operands and return them as an array.
AbstractUnitsMathematicalOperator::isolateOperand public function Numerically isolate a certain operand of this mathematical expression. Overrides UnitsMathematicalOperatorInterface::isolateOperand
AbstractUnitsMathematicalOperator::operand1 public function Retrieve operand #1 from this mathematical operator. Overrides UnitsMathematicalOperatorInterface::operand1
AbstractUnitsMathematicalOperator::operand2 public function Retrieve operand #2 from this mathematical operator. Overrides UnitsMathematicalOperatorInterface::operand2
AbstractUnitsMathematicalOperator::toInfix public function Represent this mathematical expression in human-friendly infix notation. Overrides UnitsMathematicalExpression::toInfix
AbstractUnitsMathematicalOperator::toPostfix public function Represent this mathematical expression in postfix notation. Overrides UnitsMathematicalExpression::toPostfix
AbstractUnitsMathematicalOperator::unitsMathematicalExpressionSave public function Save the mathematical expression into database. Overrides UnitsMathematicalExpression::unitsMathematicalExpressionSave
UnitsMathematicalOperatorNonLinear::decompose public function Decompose (simplify) this mathematical expression. Overrides AbstractUnitsMathematicalOperator::decompose
UnitsMathematicalOperatorNonLinear::dimension public function Determine physical dimension of this mathematical expression. Overrides AbstractUnitsMathematicalOperator::dimension
UnitsMathematicalOperatorNonLinear::evaluate public function Numerically evaluate this mathematical expression. Overrides AbstractUnitsMathematicalOperator::evaluate
UnitsMathematicalOperatorNonLinear::formatQuantity public function Format a certain amount of quantity within this mathematical expression. Overrides AbstractUnitsMathematicalOperator::formatQuantity
UnitsMathematicalOperatorNonLinear::isLinear public function Whether this expression is linearly decomposable. Overrides AbstractUnitsMathematicalOperator::isLinear
UnitsMathematicalOperatorNonLinear::linearMember public function Fetch the linear member of the non-linear operator.
UnitsMathematicalOperatorNonLinear::nonLinearMember public function Fetch the non-linear member of the non-linear operator.
UnitsMathematicalOperatorNonLinear::__construct public function Constructor. Overrides AbstractUnitsMathematicalOperator::__construct