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
- class \AbstractUnitsMathematicalOperator implements UnitsMathematicalOperatorInterface
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;
  }
} 
      