Пример #1
0
        protected void initialize(string initialValue, IBase power, bool isNegative)
        {
            if (string.IsNullOrWhiteSpace(initialValue))
            {
                return;
            }

            initialValue = preParse(initialValue, ref power, ref isNegative);
            IBase initialValueObject = parseInitialValue(initialValue);

            if (((this is ProductQuotientSet &&
                  initialValueObject is ProductQuotientSet) ||
                 (this is SumDifferenceSet &&
                  initialValueObject is SumDifferenceSet)) &&
                (power == null && !isNegative))
            { // Merge identical set types
                BaseSet newInitialValueObject = initialValueObject as BaseSet;
                for (int i = 0; i < newInitialValueObject.Count; i++)
                {
                    addUnitOperatorPair(newInitialValueObject._unitOperatorPair[i].Unit,
                                        newInitialValueObject._unitOperatorPair[i].Operator);
                }
                addPower(newInitialValueObject._power);
                if (newInitialValueObject._sign.IsNegative())
                {
                    _sign = new Sign(-1);
                }
            }
            else
            {   // Add set or unit type
                initialize(initialValueObject, power, isNegative);
            }
        }
Пример #2
0
        protected void finalizeUnits()
        {
            if (Count != 1)
            {
                return;
            }
            IBase initialValueObject = _unitOperatorPair[0].Unit;

            if (((!(this is ProductQuotientSet) || !(initialValueObject is ProductQuotientSet)) &&
                 (!(this is SumDifferenceSet) || !(initialValueObject is SumDifferenceSet))) ||
                HasPower() && initialValueObject.HasPower())
            {
                return;
            }

            // Merge identical set types
            BaseSet newInitialValueObject = initialValueObject as BaseSet;

            _unitOperatorPair.Clear();
            for (int i = 0; i < newInitialValueObject.Count; i++)
            {
                addUnitOperatorPair(newInitialValueObject._unitOperatorPair[i].Unit,
                                    newInitialValueObject._unitOperatorPair[i].Operator);
            }
            _sign = _sign * newInitialValueObject._sign;

            if (!HasPower())
            {
                addPower(newInitialValueObject._power);
            }
        }
Пример #3
0
        protected static ProductQuotientSet unitSet(BaseSet newSet, int i)
        {
            if (i > newSet.Count - 1)
            {
                return(new ProductQuotientSet(1));
            }

            IBase unit = newSet._unitOperatorPair[i].Unit;

            if (unit is PrimitiveUnit newSubUnit &&
                newSubUnit.IsFraction())
            {
                return(new ProductQuotientSet(newSubUnit.Label()));
            }

            if (unit is ProductQuotientSet productQuotientSet &&
                (0 < productQuotientSet.Count && productQuotientSet.Count <= 2))
            {
                return(productQuotientSet);
            }

            if (unit is SumDifferenceSet sumDifferenceSet &&
                (0 < sumDifferenceSet.Count && sumDifferenceSet.Count <= 2))
            {
                return(new ProductQuotientSet(sumDifferenceSet.Label()));
            }

            return(new ProductQuotientSet(unit.Label()));
        }
Пример #4
0
        public bool Equals(BaseSet other)
        {   // Equality is ignoring possible simplifications
            if (other == null)
            {
                return(false);
            }
            if (Count != other.Count)
            {
                return(false);
            }
            if (!base.Equals(other))
            {
                return(false);
            }
            switch (Count)
            {
            case 0 when other.Count == 0:
                return(true);

            case 1 when other.Count == 1:
                return(_unitOperatorPair[0].Unit.Equals(other._unitOperatorPair[0].Unit));

            default:
                return(baseUnitsAreEqual(other));
            }
        }
Пример #5
0
        protected bool isUnitMatching(IBase unit, string targetOperator, BaseSet other)
        {
            foreach (UnitOperatorPair otherUnitOperator in other._unitOperatorPair)
            {
                if (targetOperator == otherUnitOperator.Operator &&
                    unit.Equals(otherUnitOperator.Unit))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #6
0
        /// <summary>
        /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</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 BaseSet))
            {
                return(false);
            }
            BaseSet other = (BaseSet)obj;

            if (IsEmpty() && other.IsEmpty())
            {
                return(true);
            }

            return(Equals(other));
        }
Пример #7
0
        protected bool baseUnitsAreEqual(BaseSet other)
        {
            // Check that operators contain equal numbers of +, -, or *, /
            if ((operatorsCount(Query.ADD) != other.operatorsCount(Query.ADD)) ||
                (operatorsCount(Query.SUBTRACT) != other.operatorsCount(Query.SUBTRACT)) ||
                (operatorsCount(Query.MULTIPLY) != other.operatorsCount(Query.MULTIPLY)) ||
                (operatorsCount(Query.DIVIDE) != other.operatorsCount(Query.DIVIDE)))
            {
                return(false);
            }

            // Check each unit+operator pair
            foreach (UnitOperatorPair unitOperator in _unitOperatorPair)
            {
                if (!isUnitMatching(unitOperator.Unit, unitOperator.Operator, other))
                {
                    return(false);
                }
            }
            return(true);
        }