示例#1
0
文件: UnitTerm.java 项目: ptII/ptII
  /**
   * Invert this UnitTerm.
   *
   * @return The inverse of this UnitTerm.
   */
  public UnitTerm invert() {
    UnitTerm retv = copy();

    switch (_type) {
      case _VARIABLE:
      case _UNIT:
        {
          retv.setExponent(-getExponent());
          break;
        }

      case _UNITEXPR:
        {
          retv.setUnitExpr(_unitExpr.invert());
          break;
        }
    }

    return retv;
  }
示例#2
0
  /**
   * Transform to the canonical form of the equation. The canonical form of an equation is <b> Ex1,
   * Ex2, ..., Exn = A <b> where each Exi is a Unit term containing only one variable, and A is a
   * Unit term containing one Unit and no variables.
   *
   * @return unitEquation The UnitEquation in canonical form.
   */
  public UnitEquation canonicalize() {
    UnitExpr lhsUExpr = getLhs();
    UnitExpr rhsUExpr = getRhs();
    UnitExpr newLeftUExpr = new UnitExpr();
    UnitExpr newRightUExpr = new UnitExpr();
    Vector leftUTerms = lhsUExpr.getUTerms();

    for (int i = 0; i < leftUTerms.size(); i++) {
      UnitTerm uTerm = ((UnitTerm) (leftUTerms.elementAt(i)));

      if (uTerm.isUnit()) {
        newRightUExpr.addUnitTerm(uTerm.invert());
      } else if (uTerm.isVariable()) {
        newLeftUExpr.addUnitTerm(uTerm);
      }
    }

    Vector rightUTerms = rhsUExpr.getUTerms();

    for (int i = 0; i < rightUTerms.size(); i++) {
      UnitTerm uTerm = ((UnitTerm) (rightUTerms.elementAt(i)));

      if (uTerm.isUnit()) {
        newRightUExpr.addUnitTerm(uTerm);
      } else if (uTerm.isVariable()) {
        newLeftUExpr.addUnitTerm(uTerm.invert());
      }
    }

    if (newRightUExpr.getUTerms().isEmpty()) {
      UnitTerm x = new UnitTerm();
      x.setUnit(UnitLibrary.Identity);
      newRightUExpr.addUnitTerm(x);
    }

    newRightUExpr = newRightUExpr.reduce();
    return new UnitEquation(newLeftUExpr, newRightUExpr);
  }
示例#3
0
文件: UnitTerm.java 项目: ptII/ptII
 /**
  * Make a shallow copy of this UnitTerm. That is, the underlying Unit or UnitExpr is not copied.
  *
  * @return The copy of this UnitTerm
  */
 public UnitTerm copy() {
   UnitTerm retv = new UnitTerm();
   retv.setVariable(_variable);
   retv.setExponent(getExponent());
   retv.setUnit(_unit);
   retv.setUnitExpr(_unitExpr);
   retv._setType(_type);
   return retv;
 }