/// <summary> /// Distributes the sign from power. /// </summary> /// <returns>IBase.</returns> public override IBase DistributeSignFromPower() { if (IsEmpty()) { return(new PrimitiveUnit(string.Empty)); } PrimitiveUnit newSet = CloneUnit(); //for (int i = 0; i < _units.Count; i++) //{ // if (_operators[i] == Query.EMPTY.ToString()) // { // newSet._units[i].FlipSign(); // } // else if (_operators[i] == Query.ADD.ToString()) // { // newSet._operators[i] = Query.SUBTRACT.ToString(); // } // else if (_operators[i] == Query.SUBTRACT.ToString()) // { // newSet._operators[i] = Query.ADD.ToString(); // } //} return(newSet); }
private static IBase parseToObject(string value) { int indexOfAddSubtract = Query.IndexOfNextOperator(value, Query.AddSubtractTypes.ToArray()); int indexOfProductQuotient = Query.IndexOfNextOperator(value, Query.MultiplyDivideTypes.ToArray()); if (((indexOfAddSubtract == -1 && indexOfProductQuotient == -1)) && PrimitiveUnit.HasValidPowers(value)) { // No operators remain return(new PrimitiveUnit(value)); } if (0 < indexOfAddSubtract && (indexOfAddSubtract < indexOfProductQuotient || indexOfProductQuotient == -1)) { // Add/Subtract occur first, outside of groups return(parseAsSumDifference(value)); } if ((0 < indexOfProductQuotient && (indexOfProductQuotient < indexOfAddSubtract || indexOfAddSubtract == -1)) || !PrimitiveUnit.HasValidPowers(value)) { // Multiply/divide occur first, outside of groups, or the powers are not valid for direct parsing in a primitive unit return(parseAsProductQuotient(value)); } return(new PrimitiveUnit(value)); }
protected SumDifferenceSet combinePowers(SumDifferenceSet newSet) { for (int i = 0; i < newSet.Count - 1; i++) { bool baseAIsNegative = newSet._unitOperatorPair[i].Unit.GetSign().IsNegative(); IBase baseA = newSet._unitOperatorPair[i].Unit.GetBase(); IBase powerA = newSet._unitOperatorPair[i].Unit.GetPower() ?? new PrimitiveUnit(1); IBase baseB = newSet._unitOperatorPair[i + 1].Unit.GetBase(); IBase powerB = newSet._unitOperatorPair[i + 1].Unit.GetPower() ?? new PrimitiveUnit(1); bool baseSignIsSame = newSet._unitOperatorPair[i].Unit.GetSign().Equals(newSet._unitOperatorPair[i + 1].Unit.GetSign()); bool isSum = (newSet._unitOperatorPair[i + 1].OperatorEquals(Query.ADD)); bool baseIsSame = baseA.GetBase().Equals(baseB.GetBase()); bool exponentIsSame = powerA.GetBase().Equals(powerB.GetBase()); bool powerSignIsSame = powerA.GetSign().Equals(powerB.GetSign()); // (1a) Same Base, Same exponent, Same exponent sign if (baseIsSame && exponentIsSame && powerSignIsSame) { if ((isSum && baseSignIsSame) || (!isSum && !baseSignIsSame)) { // Add Exponent Same Sign ProductQuotientSet newBasePower = new ProductQuotientSet(2); newBasePower.MultiplyItem(new ProductQuotientSet(baseA, powerA)); newSet = new SumDifferenceSet(newBasePower, null, baseAIsNegative); } else { // Subtract Exponent Same Sign newSet = new SumDifferenceSet(0); } } // (1b) Same Base, Same exponent, Different sign // (2) Same Base, Different exponent (ignore sign) // (3) Different Base, Same exponent (ignore sign) // (4) Different Base, Different exponent (ignore sign) if (!baseIsSame && !exponentIsSame) { // Do Nothing } } // Calculate if numeric IBase power = newSet.GetPower(); if (power == null || !power.IsNumber()) { return(newSet); } power = new PrimitiveUnit(power.Calculate()); newSet = new SumDifferenceSet( new ProductQuotientSet(newSet.GetBase(), power, newSet.SignIsNegative()) ); return(newSet); }
/// <summary> /// Extracts the sign from power. /// </summary> /// <returns>IBase.</returns> public override IBase ExtractSignFromPower() { if (IsEmpty()) { return(new PrimitiveUnit(string.Empty)); } PrimitiveUnit newSet = CloneUnit(); IBase power = newSet.GetPower()?.ExtractSign(); newSet.addPower(power); return(newSet); }
/// <summary> /// Equalses the specified other. /// </summary> /// <param name="other">The other.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool Equals(PrimitiveUnit other) { // Equality is ignoring possible simplifications if (other == null) { return(false); } return(base.Equals(other) && _baseIsNumber == other._baseIsNumber && _baseIsInteger == other._baseIsInteger && _baseIsFloat == other._baseIsFloat && _baseIsFraction == other._baseIsFraction && string.Equals(_base, other._base)); }
protected static T simplifyNumeric <T>(IBaseSet set) where T : IBaseSet, new() { if (!set.IsNumber()) { return((T)set); } IBase newValuePrimitive = new PrimitiveUnit(set.Calculate()); set = new T(); set = set.Factory(newValuePrimitive); return((T)set); }
/// <summary> /// Determines whether the specified <see cref="System.Object" /> is equal to this instance. /// </summary> /// <param name="obj">The object to compare with the current object.</param> /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns> public override bool Equals(object obj) { if (!(obj is PrimitiveUnit)) { return(false); } PrimitiveUnit other = (PrimitiveUnit)obj; if (IsEmpty() && other.IsEmpty()) { return(true); } return(Equals(other)); }
/// <summary> /// Initializes the power object. /// </summary> /// <param name="value">The value.</param> /// <returns>PrimitiveUnit.</returns> private static PrimitiveUnit initializePowerObject(string value) { string powerValue = parseExponentValue(value); if (powerValue.Length > 1 && powerValue[0] == Sign.NEGATIVE) { powerValue = powerValue.Substring(1); if (Group.HasOuterParentheses(powerValue)) { powerValue = Group.RemoveOuterBrackets(powerValue); } powerValue = Sign.NEGATIVE + powerValue; } else if (Group.HasOuterParentheses(powerValue)) { powerValue = Group.RemoveOuterBrackets(powerValue); } PrimitiveUnit power = new PrimitiveUnit(powerValue); return(power); }