Beispiel #1
0
        public ILanguageType VerifyType_SymbolicExpression(MathematicaScalar sc, OperandsByName operands)
        {
            var s = new StringBuilder();

            var varTypes = new Dictionary <string, MathematicaAtomicType>();

            foreach (var pair in operands.OperandsDictionary)
            {
                //var lhs_type = this.GMacSymbolTable.Scalar
                var rhsType = pair.Value.ExpressionType;

                if (rhsType.IsBoolean())
                {
                    varTypes.Add(pair.Key, MathematicaAtomicType.Boolean);
                }

                if (rhsType.IsInteger())
                {
                    varTypes.Add(pair.Key, MathematicaAtomicType.Integer);
                }

                if (rhsType.IsScalar())
                {
                    varTypes.Add(pair.Key, MathematicaAtomicType.Real);
                }

                else
                {
                    s.Append("cannot assign RHS expression of type ");
                    s.Append(rhsType.TypeSignature);
                    s.Append(" to symbolic expression parameter ");
                    s.Append(pair.Key);
                }
            }

            if (s.Length > 0)
            {
                return(Context.CreateTypeMismatch(s.ToString()));
            }

            var assumeExpr = Cas.CreateAssumeExpr(varTypes);

            if (sc.IsBooleanScalar(assumeExpr))
            {
                return(GMacRootAst.BooleanType);
            }

            //if (sc.IsIntegerScalar(assumeExpr))
            //    return GMacRootAst.IntegerType;

            if (sc.IsRealScalar(assumeExpr))
            {
                return(GMacRootAst.ScalarType);
            }

            return
                (sc.IsComplexScalar(assumeExpr)
                ? GMacRootAst.ScalarType
                : Context.CreateTypeMismatch("Symbolic expression type cannot be determined"));
        }
Beispiel #2
0
        private void translate_Dependency_List(string expressionText, out MathematicaScalar scalar, out OperandsByName operands)
        {
            var finalScalarText = new StringBuilder(expressionText);

            operands = OperandsByName.Create();

            var varIdx = 1;

            var allMatches = GenUtils.ExtractDistinctInternalExpressions(expressionText);

            foreach (var rgexMatch in allMatches)
            {
                var rhsExprText = rgexMatch.Value.Substring(1, rgexMatch.Value.Length - 2);

                var rhsExpr =
                    BasicExpressionGenerator.Generate_PolyadicOperand(
                        GMacRootAst.ScalarType,
                        translate_LanguageExpression(rhsExprText)
                        );

                var lhsVarName = "var" + (varIdx++).ToString("0000");

                finalScalarText = finalScalarText.Replace(rgexMatch.Value, lhsVarName);

                operands.AddOperand(lhsVarName, rhsExpr);
            }

            scalar = MathematicaScalar.Create(Cas, finalScalarText.ToString());
        }
        public IEnumerable <SymbolLValue> GetLValues(OperandsByName operands)
        {
            var lvalues = Enumerable.Empty <SymbolLValue>();

            return
                (operands
                 .OperandsDictionary
                 .Where(pair => pair.Value is LanguageValueAccess)
                 .Aggregate(
                     lvalues,
                     (current, pair) => current.Concat(((LanguageValueAccess)pair.Value).AccessLValues)
                     ));
        }
Beispiel #4
0
        private ILanguageExpression CompileSymbolicExpressionCall(GMacParametricSymbolicExpression expr, OperandsByName operands)
        {
            var compiledOperands = OperandsByName.Create();

            foreach (var pair in operands.OperandsDictionary)
            {
                var compiledRhsExpr =
                    (ILanguageExpressionAtomic)CompileExpression(pair.Value);

                compiledOperands.AddOperand(pair.Key, compiledRhsExpr);
            }

            var compiledExpr = BasicPolyadic.Create(GMacRootAst.ScalarType, expr);

            compiledExpr.Operands = compiledOperands;

            return(compiledExpr);
        }
        public OperandsByName ReplaceLValueByExpression(OperandsByName oldOperands, SymbolLValue oldLvalue, ILanguageExpressionAtomic newExpr)
        {
            var newOperands = new Dictionary <string, ILanguageExpressionAtomic>();

            foreach (var pair in oldOperands.OperandsDictionary)
            {
                var newOpExpr = ReplaceLValueByExpression(pair.Value, oldLvalue, newExpr);

                newOperands.Add(pair.Key, newOpExpr);
            }

            oldOperands.OperandsDictionary.Clear();

            foreach (var pair in newOperands)
            {
                oldOperands.AddOperand(pair.Key, pair.Value);
            }

            return(oldOperands);
        }
Beispiel #6
0
        private ValuePrimitive <MathematicaScalar> EvaluateBasicPolyadicSymbolicExpressionCall(GMacParametricSymbolicExpression symbolicExpr, OperandsByName operands)
        {
            var exprText = symbolicExpr.AssociatedMathematicaScalar.ExpressionText;

            foreach (var pair in operands.OperandsDictionary)
            {
                var rhsValue = pair.Value.AcceptVisitor(this);

                exprText = exprText.Replace(pair.Key, ((ValuePrimitive <MathematicaScalar>)rhsValue).Value.ExpressionText);
            }

            var scalar =
                MathematicaScalar.Create(symbolicExpr.AssociatedMathematicaScalar.CasInterface, exprText);

            //May be required
            //scalar = MathematicaScalar.Create(scalar.CAS, scalar.CASEvaluator.FullySimplify(scalar.MathExpr));

            return
                (ValuePrimitive <MathematicaScalar> .Create(
                     GMacRootAst.ScalarType,
                     scalar
                     ));
        }
Beispiel #7
0
        public ILanguageExpression Generate_SymbolicExpression(MathematicaScalar casExpr, OperandsByName operands)
        {
            var exprType = ScalarType;

            //var expr_type = this.VerifyType_SymbolicExpression(cas_expr, operands) as TypePrimitive;

            if (operands.OperandsDictionary.Count == 0)
            {
                return(ValuePrimitive <MathematicaScalar> .Create(exprType, casExpr));
            }

            var basicExpr = BasicPolyadic.Create(exprType, GMacParametricSymbolicExpression.Create(casExpr));

            basicExpr.Operands = operands;

            return(basicExpr);
        }
Beispiel #8
0
        internal void Generate_BasicPolyadic_CASExpression(GMacParametricSymbolicExpression casExpr, OperandsByName operands)
        {
            Log.Append("(");

            var flag = false;

            foreach (var pair in operands.OperandsDictionary)
            {
                if (flag)
                {
                    Log.Append(", ");
                }
                else
                {
                    flag = true;
                }

                Log.Append(pair.Key);

                Log.Append(" = ");

                pair.Value.AcceptVisitor(this);
            }

            Log.Append(")");
        }