예제 #1
0
 public ILanguageValue Evaluate(ValuePrimitive <int> value1)
 {
     return(ValuePrimitive <int> .Create(
                value1.ValuePrimitiveType,
                Math.Abs(value1.Value)
                ));
 }
예제 #2
0
 public ILanguageValue Evaluate(ValuePrimitive <MathematicaScalar> value1, ValuePrimitive <MathematicaScalar> value2)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.ValuePrimitiveType,
                SymbolicUtils.Constants.Zero
                ));
 }
예제 #3
0
파일: Norm2Eval.cs 프로젝트: phreed/GMac
 public ILanguageValue Evaluate(GMacValueMultivector value1)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.GMacRootAst.ScalarType,
                value1.SymbolicFrame.Norm2(value1.MultivectorCoefficients)
                ));
 }
예제 #4
0
 public ILanguageValue Evaluate(GMacValueMultivector value1)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.GMacRootAst.ScalarType,
                value1.MultivectorCoefficients.EuclideanMagnitude()
                ));
 }
예제 #5
0
 public ILanguageValue Evaluate(GMacValueMultivector value1)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.GMacRootAst.ScalarType,
                value1.SymbolicMultivector.EMagnitude()
                ));
 }
예제 #6
0
        ///Translate a constant number (Int32 or Double)
        private ILanguageExpression translate_Constant_Number(ParseTreeNode node)
        {
            var numberText = node.FindTokenAndGetText();

            Int32 intNumber;

            if (Int32.TryParse(numberText, out intNumber))
            {
                return(ValuePrimitive <MathematicaScalar> .Create(
                           GMacRootAst.ScalarType,
                           MathematicaScalar.Create(SymbolicUtils.Cas, intNumber)
                           ));
            }

            Double doubleNumber;

            if (Double.TryParse(numberText, out doubleNumber))
            {
                return(ValuePrimitive <MathematicaScalar> .Create(
                           GMacRootAst.ScalarType,
                           MathematicaScalar.Create(SymbolicUtils.Cas, doubleNumber)
                           ));
            }

            return(CompilationLog.RaiseGeneratorError <ILanguageExpression>("Constant number not recognized", node));
        }
예제 #7
0
 public ILanguageValue Evaluate(ValuePrimitive <int> value1, ValuePrimitive <int> value2)
 {
     return(ValuePrimitive <int> .Create(
                value1.ValuePrimitiveType,
                value1.Value *value2.Value
                ));
 }
예제 #8
0
        /// <summary>
        /// Finds the value of a primitive value access if exists; else returns the default primitive value
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <returns></returns>
        public ValuePrimitive <MathematicaScalar> ReadRhsPrimitiveValue(LanguageValueAccess valueAccess)
        {
            //Try to read the value from the existing table low-level entries
            var result = ReadRHSPrimitiveValue_ExistingOnly(valueAccess);

            //If an entry is found just return the value
            if (ReferenceEquals(result, null) == false)
            {
                return(result);
            }

            //If an entry is not found create and return a default value
            if (valueAccess.ExpressionType.IsBoolean())
            {
                ValuePrimitive <MathematicaScalar> .Create(
                    (TypePrimitive)valueAccess.ExpressionType,
                    MathematicaScalar.Create(SymbolicUtils.Cas, "False")
                    );
            }

            return
                (ValuePrimitive <MathematicaScalar> .Create(
                     (TypePrimitive)valueAccess.ExpressionType,
                     SymbolicUtils.Constants.Zero
                     ));
        }
예제 #9
0
파일: OpEval.cs 프로젝트: cloudRoutine/GMac
 public ILanguageValue Evaluate(ValuePrimitive <MathematicaScalar> value1, ValuePrimitive <MathematicaScalar> value2)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.ValuePrimitiveType,
                value1.Value *value2.Value
                ));
 }
예제 #10
0
        private ValuePrimitive <MathematicaScalar> GetRandomValue(ILanguageType valueType)
        {
            Expr valueExpr;

            if (valueType.IsInteger())
            {
                valueExpr = new Expr(_randomSource.Next(1, 10));
            }

            else if (valueType.IsBoolean())
            {
                valueExpr = new Expr(_randomSource.Next(0, 1) != 0);
            }

            else if (valueType.IsScalar())
            {
                valueExpr = new Expr(_randomSource.NextDouble() * 10.0 - 5.0);
            }
            //value_expr = new Expr(new Expr(ExpressionType.Symbol, "Rational"), _RandomSource.Next(1, 10), _RandomSource.Next(1, 10));

            else
            {
                throw new InvalidOperationException();
            }

            return
                (ValuePrimitive <MathematicaScalar> .Create(
                     (TypePrimitive)valueType,
                     MathematicaScalar.Create(SymbolicUtils.Cas, valueExpr)
                     ));
        }
예제 #11
0
 public ILanguageValue Evaluate(ValuePrimitive <MathematicaScalar> value1)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.ValuePrimitiveType,
                value1.Value.Abs()
                ));
 }
예제 #12
0
파일: SpEval.cs 프로젝트: phreed/GMac
        public ILanguageValue Evaluate(GMacValueMultivector value1, GMacValueMultivector value2)
        {
            var scalar = value1.SymbolicFrame.Sp(
                value1.MultivectorCoefficients, value2.MultivectorCoefficients
                );

            return(ValuePrimitive <MathematicaScalar> .Create(value1.CoefficientType, scalar[0]));
        }
예제 #13
0
        public ILanguageValue Evaluate(GMacValueMultivector value1, GMacValueMultivector value2)
        {
            var scalar = value1.SymbolicMultivector.ESp(value2.SymbolicMultivector);

            return(ValuePrimitive <MathematicaScalar> .Create(
                       value1.CoefficientType,
                       scalar[0].ToMathematicaScalar()
                       ));
        }
예제 #14
0
 internal ValuePrimitive <MathematicaScalar> this[int id]
 {
     get
     {
         return(ValuePrimitive <MathematicaScalar> .Create(CoefficientType, MultivectorCoefficients[id]));
     }
     set
     {
         MultivectorCoefficients[id] = value.Value;
     }
 }
예제 #15
0
 /// <summary>
 /// Convert this basis blade into a multivector term value with unity coefficient
 /// </summary>
 public AstValueMultivectorTerm ToMultivectorTermValue()
 {
     return(new AstValueMultivectorTerm(
                AssociatedFrame.MultivectorType,
                BasisBladeId,
                ValuePrimitive <MathematicaScalar> .Create(
                    AssociatedFrame.GMacRootAst.ScalarType,
                    SymbolicUtils.Constants.One
                    )
                ));
 }
예제 #16
0
 /// <summary>
 /// Create a symbolic value from this pattern. If the pattern is a constant its internal
 /// expression is returned, else a symbolic expression with a single variable is created and returned
 /// </summary>
 /// <param name="varNameTemplate"></param>
 /// <returns></returns>
 public AstValueScalar ToValue(StringSequenceTemplate varNameTemplate)
 {
     return
         (IsConstant
         ? ConstantValue
         : new AstValueScalar(
              ValuePrimitive <MathematicaScalar> .Create(
                  GMacType.AssociatedPrimitiveType,
                  MathematicaScalar.Create(SymbolicUtils.Cas, varNameTemplate.GenerateNextString())
                  )
              ));
 }
예제 #17
0
파일: GMacAst.cs 프로젝트: phreed/GMac
        public override ILanguageValue CreateDefaultValue(ILanguageType langType)
        {
            if (ReferenceEquals(langType, null))
            {
                throw new ArgumentNullException();
            }

            if (langType.IsSameType(BooleanType))
            {
                return(ValuePrimitive <bool> .Create((TypePrimitive)langType, false));
            }

            if (langType.IsSameType(IntegerType))
            {
                return(ValuePrimitive <int> .Create((TypePrimitive)langType, 0));
            }

            if (langType.IsSameType(ScalarType))
            {
                return(ValuePrimitive <MathematicaScalar> .Create((TypePrimitive)langType, SymbolicUtils.Constants.Zero));
            }

            var typeStructure = langType as GMacStructure;

            if (typeStructure != null)
            {
                var structure = typeStructure;

                var valueSparse = ValueStructureSparse.Create(structure);

                //This code is not required for a sparse structure value
                //foreach (var data_member in structure.DataMembers)
                //    value_sparse[data_member.ObjectName] = this.CreateDefaultValue(data_member.SymbolType);

                return(valueSparse);
            }

            if (!(langType is GMacFrameMultivector))
            {
                throw new InvalidOperationException("GMac type not recognized!");
            }

            var mvType = (GMacFrameMultivector)langType;

            var value = GMacValueMultivector.CreateZero(mvType);

            //This code is not required for a sparse multivector value
            //for (int id = 0; id < mv_type.ParentFrame.GASpaceDimension; id++)
            //    value[id] = SymbolicUtils.Constants.Zero;

            return(value);
        }
예제 #18
0
        /// <summary>
        /// Low level processing requires the use of MathematicaScalar primitive values only. This method preforms
        /// the required conversion of primitive values of other forms.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static ValuePrimitive <MathematicaScalar> ToScalarValue(this ILanguageValuePrimitive value)
        {
            var primitive1 = value as ValuePrimitive <MathematicaScalar>;

            if (primitive1 != null)
            {
                return(primitive1);
            }

            //Convert into equivalent Mathematica scalar value
            var primitive2 = value as ValuePrimitive <int>;

            if (primitive2 != null)
            {
                return(ValuePrimitive <MathematicaScalar> .Create(
                           (TypePrimitive)value.ExpressionType,
                           MathematicaScalar.Create(SymbolicUtils.Cas, primitive2.Value)
                           ));
            }

            //Convert into equivalent Mathematica scalar value
            var primitive3 = value as ValuePrimitive <bool>;

            if (primitive3 != null)
            {
                return(ValuePrimitive <MathematicaScalar> .Create(
                           (TypePrimitive)value.ExpressionType,
                           MathematicaScalar.Create(SymbolicUtils.Cas, primitive3.Value ? "True" : "False")
                           ));
            }

            //if (value is ValuePrimitive<double>)
            //{
            //return ValuePrimitive<MathematicaScalar>.Create(
            //    (TypePrimitive)value.ExpressionType,
            //    MathematicaScalar.Create(SymbolicUtils.CAS, ((ValuePrimitive<double>)value).Value)
            //    );
            //}

            //if (value is ValuePrimitive<float>)
            //{
            //return ValuePrimitive<MathematicaScalar>.Create(
            //    (TypePrimitive)value.ExpressionType,
            //    MathematicaScalar.Create(SymbolicUtils.CAS, ((ValuePrimitive<float>)value).Value)
            //    );
            //}

            //This should never happen
            throw new InvalidOperationException();
        }
예제 #19
0
        //Methods for processing high-level RHS expressions into values with suitable composition for low-level code generation
        #region High-Level Expression Evaluation and Processing Methods

        /// <summary>
        /// Evaluate a cast to a scalar value operation
        /// </summary>
        /// <param name="expr"></param>
        /// <returns></returns>
        private ILanguageValue EvaluateBasicUnaryCastToScalar(BasicUnary expr)
        {
            var value1 = expr.Operand.AcceptVisitor(this);

            if (value1.ExpressionType.IsInteger())
            {
                return(ValuePrimitive <MathematicaScalar> .Create(
                           (TypePrimitive)expr.ExpressionType,
                           ((ValuePrimitive <MathematicaScalar>)value1).Value
                           ));
            }

            throw new InvalidOperationException("Invalid cast operation");
        }
예제 #20
0
 internal ValuePrimitive <MathematicaScalar> this[int id]
 {
     get
     {
         return(ValuePrimitive <MathematicaScalar> .Create(
                    CoefficientType,
                    SymbolicMultivector[id].ToMathematicaScalar()
                    ));
     }
     set
     {
         SymbolicMultivector.SetTermCoef(id, value.Value);
     }
 }
예제 #21
0
        /// <summary>
        /// Finds the value of a primitive value access if exists; else returns null
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <returns></returns>
        private ValuePrimitive <MathematicaScalar> ReadRHSPrimitiveValue_ExistingOnly(LanguageValueAccess valueAccess)
        {
            var        hlName = valueAccess.GetName();
            LlDataItem dataInfo;

            if (_hlDictionary.TryGetValue(hlName, out dataInfo))
            {
                return(ValuePrimitive <MathematicaScalar> .Create(
                           (TypePrimitive)valueAccess.ExpressionType,
                           dataInfo.RhsUsableSymbolicScalar
                           ));
            }

            return(null);
        }
예제 #22
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);
        }
예제 #23
0
        /// <summary>
        /// Replace the given value by a processed version depending on some GMac compiler options
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static ValuePrimitive <MathematicaScalar> ProcessRhsValue(ValuePrimitive <MathematicaScalar> value)
        {
            //No simplification or sub-expression refactoring required; just return the value without any processing
            if (GMacCompilerOptions.SimplifyLowLevelRhsValues == false)
            {
                return(value);
            }

            //Symbolically simplify the value if indicated. This takes a lot of Mathematica processing and slows
            //compilation but produces target code with better processing performance in some cases
            var finalExpr = SymbolicUtils.Evaluator.Simplify(value.Value.Expression);

            return
                (ValuePrimitive <MathematicaScalar> .Create(
                     (TypePrimitive)value.ExpressionType,
                     MathematicaScalar.Create(SymbolicUtils.Cas, finalExpr)
                     ));
        }
예제 #24
0
        //private static readonly Regex LlVarOnlyRegex = new Regex(@"^\b" + LlVarNamePrefix + @"[0-9A-F][0-9A-F][0-9A-F][0-9A-F]\b$", RegexOptions.ECMAScript);


        /// <summary>
        /// Low level processing requires the use of MathematicaScalar primitive values only. This method extracts
        /// the a primitive value of the given type as a MathematicaScalar primitive value.
        /// </summary>
        /// <param name="langType"></param>
        /// <returns></returns>
        public static ValuePrimitive <MathematicaScalar> GetDefaultScalarValue(this TypePrimitive langType)
        {
            MathematicaScalar scalar;

            if (langType.IsNumber())
            {
                scalar = MathematicaScalar.Create(SymbolicUtils.Cas, 0);
            }

            else if (langType.IsBoolean())
            {
                scalar = MathematicaScalar.Create(SymbolicUtils.Cas, "False");
            }

            else
            {
                throw new InvalidOperationException();
            }

            return(ValuePrimitive <MathematicaScalar> .Create(langType, scalar));
        }
예제 #25
0
        private static ILanguageExpressionAtomic AtomicExpressionToScalarAtomicExpression(this CommandBlock commandBlock, ILanguageExpressionAtomic oldExpr)
        {
            var scalarType = ((GMacAst)commandBlock.RootAst).ScalarType;

            if (oldExpr.ExpressionType.IsSameType(scalarType))
            {
                return(oldExpr);
            }

            if (!oldExpr.ExpressionType.IsInteger())
            {
                throw new InvalidCastException("Cannot convert atomic expression of type " + oldExpr.ExpressionType.TypeSignature + " to a scalar atomic expression");
            }

            var valuePrimitive = oldExpr as ValuePrimitive <int>;

            if (valuePrimitive != null)
            {
                return(ValuePrimitive <MathematicaScalar> .Create(
                           scalarType,
                           MathematicaScalar.Create(SymbolicUtils.Cas, valuePrimitive.Value)
                           ));
            }

            //This should be a value access of type integer
            if (!(oldExpr is LanguageValueAccess))
            {
                throw new InvalidCastException("Cannot convert atomic expression " + oldExpr + " of type " +
                                               oldExpr.ExpressionType.TypeSignature + " to a scalar atomic expression");
            }

            //Create a cast operation
            var newRhsExpr = BasicUnary.Create(scalarType, scalarType, oldExpr);

            //The new expresssion is not atomic. Create a local variable to hold value and return the local variable
            //as a direct value access object
            return(NonAtomicExpressionToValueAccess(commandBlock, newRhsExpr));
        }
예제 #26
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
                     ));
        }