Beispiel #1
0
  // /////////////////////////////////////////////////////////////////
  // // private methods ////
  private void _flatten() {
    if (_isFlat) {
      return;
    }

    Vector newUTerms = new Vector();

    for (int i = 0; i < _uTerms.size(); i++) {
      UnitTerm unitTerm = (UnitTerm) (_uTerms.elementAt(i));

      if (unitTerm.isUnitExpr()) {
        UnitExpr uExpr = unitTerm.getUnitExpr();

        if (uExpr != null) {
          uExpr._flatten();
          newUTerms.addAll(uExpr.getUTerms());
        }
      } else {
        newUTerms.add(unitTerm);
      }
    }

    _isFlat = true;
    _setUTerms(newUTerms);
  }
Beispiel #2
0
  /**
   * Reduce a UnitExpr to produce a UnitExpr that has at most one Unit. Any embedded UnitExpr is
   * first transformed so that all embedded UnitExprs are replaced with Units. This intermediate
   * result is a mixture of Units and variables. The Units are then replaced with their product. The
   * result is a single Unit and all of the original variables.
   *
   * @return The reduced UnitExpr.
   */
  public UnitExpr reduce() {
    _flatten();

    Unit reductionUnit = UnitLibrary.Identity.copy();
    Vector newUTerms = new Vector();

    for (int i = 0; i < _uTerms.size(); i++) {
      UnitTerm unitTerm = (UnitTerm) (_uTerms.elementAt(i));

      if (unitTerm.isUnit()) {
        reductionUnit = reductionUnit.multiplyBy(unitTerm.getUnit().pow(unitTerm.getExponent()));
      } else {
        newUTerms.add(unitTerm);
      }
    }

    newUTerms.add(new UnitTerm(reductionUnit));

    UnitExpr retv = new UnitExpr();
    retv._setUTerms(newUTerms);
    return retv;
  }