// ///////////////////////////////////////////////////////////////// // // 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); }
/** * Create a new UnitExpr that is the inverse of this UnitExpr. * * @return The inverse of this UnitExpr. */ public UnitExpr invert() { UnitExpr retv = new UnitExpr(); Vector myUTerms = new Vector(); for (int i = 0; i < _uTerms.size(); i++) { myUTerms.add(((UnitTerm) (_uTerms.elementAt(i))).invert()); } retv._setUTerms(myUTerms); return retv; }
/** * Create a copy of this UnitExpr. * * @return The new UnitExpr. */ public UnitExpr copy() { UnitExpr retv = new UnitExpr(); Vector newUTerms = new Vector(); for (int i = 0; i < _uTerms.size(); i++) { UnitTerm term = (UnitTerm) (_uTerms.elementAt(i)); newUTerms.add(term.copy()); } retv._setUTerms(newUTerms); return retv; }
/** * 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; }