// Substitute an expression for a factor in the denominator. public void SubstituteIntoDenominator(int intIndex, CExpression thatExpression) { CFactor oldFactor = _denominator[intIndex]; CExpression insertExpression = new CExpression(thatExpression); insertExpression.DistributePower(); insertExpression.RatioPower.Set(oldFactor.RatioPower); insertExpression.DistributePower(); _denominator.RemoveAt(intIndex); _denominator.Append(insertExpression.Numerator); _numerator.Append(insertExpression.Denominator); _isLogValid = false; Normalize(); }
// Construct the cell as the product or quotient of quantities x and y public CMatrixCell(CQuantity quantityX, CQuantity quantityY, bool bIsProduct) { _isProduct = bIsProduct; _ptrX = new CFactor(quantityX); _ptrY = new CFactor(quantityY); if (_isProduct) { _number = quantityX.Number.Product(quantityY.Number); } else { _number = quantityX.Number.Quotient(quantityY.Number); } _isExact = (quantityX.IsExact && quantityY.IsExact); }
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(); }
// Substitute every term of each expression to form potential new expressions public void CalculateSubstitution(int intMaxFactors, bool bFirstPass) { int intLimit = _expressionList.Count; double dblLogThis = this.Log; CExpressionList thisExpressionList; // Integers are never substituted. if (!this._isInteger) { // On first pass initialize from the established expression list. // On subsequent passes build on previous candidate list. if (bFirstPass) { thisExpressionList = new CExpressionList(_expressionList); _candidateExpressionList = new CExpressionList(_expressionList); } else { thisExpressionList = new CExpressionList(_candidateExpressionList); } // Limit this to only the expressions that are already in the list. intLimit = thisExpressionList.Count; // for each expression in this quantity's expression list for (int i = 0; i < intLimit; ++i) { CExpression baseExpression = thisExpressionList[i]; CNumber baseNumber = new CNumber(baseExpression.Log, false); int intNumeratorLimit = baseExpression.Numerator.Count; int intDenominatorLimit = baseExpression.Denominator.Count; // for each factor in the numerator for (int intFactorIndex = 0; intFactorIndex < intNumeratorLimit; ++intFactorIndex) { CFactor baseFactor = baseExpression.Numerator[intFactorIndex]; CQuantity baseQuantity = new CQuantity(baseFactor.Qty); // Integers are never substituted. // Computational intermediates are never substituted. They are expanded on printout. if (!baseQuantity._isInteger /* && !baseQuantity._isComputational*/) { // for each expression associated with the numerator factor for (int k = 0; k < baseQuantity.ExpressionList.Count; ++k) { CExpression insertExpression = new CExpression(baseQuantity.ExpressionList[k]); if (!insertExpression.ContainsSymbol(this.Symbol)) { CExpression newExpression = new CExpression(baseExpression); newExpression.SubstituteIntoNumerator(intFactorIndex, insertExpression); CNumber newNumber = new CNumber(newExpression.Log, false); if (!baseNumber.IsEquivalent(newNumber)) { // !!! error double dblLogTest = newExpression.Log; // testing only } newExpression.Normalize(); if (newExpression.SymbolCount <= intMaxFactors) { _candidateExpressionList.Add(newExpression); } } } } } // for each factor in the denominator for (int intFactorIndex = 0; intFactorIndex < intDenominatorLimit; ++intFactorIndex) { CFactor baseFactor = baseExpression.Denominator[intFactorIndex]; CQuantity baseQuantity = new CQuantity(baseFactor.Qty); // Integers are never substituted. // Computational intermediates are never substituted. They are expanded on printout. if (!baseQuantity._isInteger /* && !baseQuantity._isComputational*/) { for (int k = 0; k < baseQuantity.ExpressionList.Count; ++k) { // for each expression associated with the denominator factor CExpression insertExpression = new CExpression(baseQuantity.ExpressionList[k]); if (!insertExpression.ContainsSymbol(this.Symbol)) { CExpression newExpression = new CExpression(baseExpression); newExpression.SubstituteIntoDenominator(intFactorIndex, insertExpression); CNumber newNumber = new CNumber(newExpression.Log, false); if (!baseNumber.IsEquivalent(newNumber)) { // !!! error double dblLogTest = newExpression.Log; // testing only } newExpression.Normalize(); if (newExpression.SymbolCount <= intMaxFactors) { _candidateExpressionList.Add(newExpression); } } } } } _candidateExpressionList.SuppressDupes(); _candidateExpressionList.SuppressTautology(this); } GC.Collect(); } }