示例#1
0
        public CNumber(CRatio thatRatio)
        {
            double dblValue = thatRatio.Numerator;

            dblValue /= thatRatio.Denominator;
            InitFromNumber(dblValue, true);
        }
示例#2
0
        // Divide like quantities by subtracting powers.
        // This is usually applied to matching numerator and denominator factors in an Expression when normalizing.
        public bool DivideLike(CFactor thatFactor)
        {
            bool bSuccess = false;

            if (this.Label == thatFactor.Label)
            {
                if (this.Root == 1 && thatFactor.Root == 1)
                {
                    _ratioPower.NumeratorSubtract(thatFactor.Power);
                }
                else
                {
                    if (this.Root == thatFactor.Root)
                    {
                        _ratioPower.NumeratorSubtract(thatFactor.Power);
                    }
                    else
                    {
                        // Cross multiply
                        int intRootM  = _ratioPower.Denominator * thatFactor.Root;
                        int intPowerM = _ratioPower.Numerator * thatFactor.Root - thatFactor.Power * _ratioPower.Denominator;
                        _ratioPower = new CRatio(intRootM, intPowerM);
                    }
                    _ratioPower.Normalize();
                }
                bSuccess = true;
            }
            return(bSuccess);
        }
示例#3
0
 // deep copy
 public CExpression(CExpression thatExpression)
 {
     Init();
     _numerator   = new CFactorList(thatExpression.Numerator);
     _denominator = new CFactorList(thatExpression.Denominator);
     _power       = new CRatio(thatExpression.RatioPower);
     _log         = thatExpression.Log;
     _symbolCount = thatExpression.SymbolCount;
 }
示例#4
0
 private void Init()
 {
     _power       = new CRatio(1);
     _numerator   = new CFactorList();
     _denominator = new CFactorList();
     _log         = 0;
     _isLogValid  = false;
     _isValid     = true;
     _symbolCount = 0;
 }
示例#5
0
 // Simplify integer to an integer power.
 // Resultant power is either 1 or 1/n.
 public void ApplyPowerToInteger()
 {
     if (Qty.IsInteger)
     {
         int           intValue        = (int)Qty.Number.Value;
         CRatio        thisRatio       = new CRatio(intValue);
         bool          bSuccess        = thisRatio.ApplyPower(ref _ratioPower);
         CNumber       numberToPower   = new CNumber(thisRatio);
         CQuantityList theQuantityList = this.Qty.QuantityList;
         // Because the calculated quantity value has changed, the factor qty must be replaced.
         this.Qty = theQuantityList.ReplaceIntegerQuantity(numberToPower);
     }
 }
示例#6
0
        // Return true and apply root to value if the result works out to integer
        // else return false.
        public bool ApplyRoot(int intRoot)
        {
            bool bSuccess = false;

            if (intRoot == 0)
            {
                // panic, zero root
                bSuccess = false;
            }
            else if (intRoot == 1)
            {
                bSuccess = true;
            }
            else if (intRoot < 0)
            {
                // panic, negative root
                bSuccess = false;
            }
            else if (_remainder == 1)
            {
                // general case
                bSuccess = true;    // innocent until proven guilty
                for (int i = 0; i < Count; ++i)
                {
                    CPrimeFactor thisFactor = this[i];
                    int          intTrial   = thisFactor.Count;
                    bSuccess = CRatio.DivideInt(ref intTrial, intRoot);
                    if (bSuccess)
                    {
                        thisFactor.SetCount(intTrial);
                    }
                    else
                    {
                        break;  // guilty
                    }
                }
                if (bSuccess)
                {
                    Recalc();
                }
            }
            return(bSuccess);
        }
示例#7
0
        public void Normalize()
        {
            int intNumIndex, intDenomIndex = 0;

            _numerator.Normalize();
            _denominator.Normalize();
            if (_numerator.Count > 0 && _denominator.Count > 0)
            {
                for (intNumIndex = _numerator.Count - 1; intNumIndex > -1; --intNumIndex)
                {
                    // if dupe is found in denominator then adjust both numerator and denominator
                    CFactor numeratorFactor = _numerator[intNumIndex];
                    intDenomIndex = _denominator.Find(numeratorFactor.Label);
                    if (intDenomIndex > -1)
                    {
                        CFactor denominatorFactor = _denominator[intDenomIndex];
                        numeratorFactor.DivideLike(denominatorFactor);
                        _denominator.RemoveAt(intDenomIndex);
                        _isLogValid = false;
                    }
                    // if inverse is found in denominator then adjust both numerator and denominator
                    intDenomIndex = _denominator.Find(numeratorFactor.Qty.InverseLabel);
                    if (intDenomIndex > -1)
                    {
                        CFactor   denominatorFactor = _denominator[intDenomIndex];
                        CQuantity inverseQty        = denominatorFactor.Qty.InverseQty;
                        denominatorFactor.Qty = inverseQty;
                        numeratorFactor.MultiplyLike(denominatorFactor);
                        _denominator.RemoveAt(intDenomIndex);
                        _isLogValid = false;
                    }
                }
            }
            // if numerator factor has negative power swap it to denominator
            for (intNumIndex = _numerator.Count - 1; intNumIndex > -1; --intNumIndex)
            {
                CFactor numeratorFactor = _numerator[intNumIndex];
                if (numeratorFactor.Power == 0)
                {
                    _numerator.RemoveAt(intNumIndex);
                }
                else if (numeratorFactor.Power < 0)
                {
                    _numerator.RemoveAt(intNumIndex);
                    numeratorFactor.FlipPower();
                    _denominator.Add(numeratorFactor);
                    _isLogValid = false;
                }
            }
            // if denominator factor has negative power swap it to numerator
            for (intDenomIndex = _denominator.Count - 1; intDenomIndex > -1; --intDenomIndex)
            {
                CFactor denominatorFactor = _denominator[intDenomIndex];
                if (denominatorFactor.Power == 0)
                {
                    _denominator.RemoveAt(intDenomIndex);
                }
                else if (denominatorFactor.Power < 0)
                {
                    _denominator.RemoveAt(intDenomIndex);
                    denominatorFactor.FlipPower();
                    _numerator.Add(denominatorFactor);
                    _isLogValid = false;
                }
            }
            // net out integers between numerator and denominator
            int intNumeratorIndex   = _numerator.FindInteger();
            int intDenominatorIndex = _denominator.FindInteger();

            if (intNumeratorIndex > -1 && intDenominatorIndex > -1)
            {
                CFactor numeratorFactor   = _numerator[intNumeratorIndex];
                CFactor denominatorFactor = _denominator[intDenominatorIndex];
                if (numeratorFactor.RatioPower.Equals(1) && denominatorFactor.RatioPower.Equals(1))
                {
                    int intNumeratorValue   = (int)numeratorFactor.Qty.Value;
                    int intDenominatorValue = (int)denominatorFactor.Qty.Value;

                    // Putting both integers into a CRatio has the effect of reducing them
                    CRatio        ratioThis         = new CRatio(intNumeratorValue, intDenominatorValue);
                    double        dblNumerator      = (double)ratioThis.Numerator;
                    double        dblDenominator    = (double)ratioThis.Denominator;
                    CNumber       numberNumerator   = new CNumber(dblNumerator);
                    CNumber       numberDenominator = new CNumber(dblDenominator);
                    CQuantityList theQuantityList   = numeratorFactor.Qty.QuantityList;

                    numeratorFactor.Qty   = theQuantityList.ReplaceIntegerQuantity(numberNumerator);
                    denominatorFactor.Qty = theQuantityList.ReplaceIntegerQuantity(numberDenominator);
                }
            }
            _numerator.Normalize();
            _denominator.Normalize();
            SetSymbolCount();
        }
示例#8
0
 private void Init()
 {
     _ratioPower = new CRatio(1);
 }
示例#9
0
 // Raise this factor to p/r
 public void ApplyPower(CRatio thatPower)
 {
     _ratioPower.ApplyMultiplication(thatPower);
 }