Ejemplo n.º 1
0
        public bool WriteExpressions(string strPath, string strLogPath)
        {
            bool         bSuccess      = true;
            StreamWriter swExpressions = null;
            string       strLine;

            try
            {
                swExpressions = new StreamWriter(strPath);
                for (int i = 0; i < this.Count; ++i)
                {
                    CQuantity thisQuantity = this[i].Qty;
                    strLine = thisQuantity.SimpleLabel + "\t= ";
                    CExpressionList thisExpressionList = thisQuantity.ExpressionList;
                    thisExpressionList.SuppressDupes();
                    if (thisExpressionList.Count > 0)
                    {
                        strLine += thisExpressionList[0].ToString();
                    }
                    swExpressions.WriteLine(strLine);
                    for (int j = 1; j < thisExpressionList.Count; ++j)
                    {
                        strLine = "\t= " + thisExpressionList[j].ToString();
                        swExpressions.WriteLine(strLine);
                    }
                }
            }
            catch (Exception e)
            {
                bSuccess = false;
                string strLogEntry = "\r\n Error writing expression file:" + e.Message;
                File.AppendAllText(strLogPath, strLogEntry);
            }
            finally
            {
                if (swExpressions != null)
                {
                    swExpressions.Close();
                }
            }

            return(bSuccess);
        }
Ejemplo n.º 2
0
        // 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();
            }
        }