Exemplo n.º 1
0
 public ILanguageValue Evaluate(GMacValueMultivector value1)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.GMacRootAst.ScalarType,
                value1.SymbolicMultivector.EMagnitude()
                ));
 }
Exemplo n.º 2
0
 public ILanguageValue Evaluate(ValuePrimitive <MathematicaScalar> value1, ValuePrimitive <MathematicaScalar> value2)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.ValuePrimitiveType,
                SymbolicUtils.Constants.Zero
                ));
 }
Exemplo n.º 3
0
 public ILanguageValue Evaluate(GMacValueMultivector value1, ValuePrimitive <MathematicaScalar> value2)
 {
     return(GMacValueMultivector.Create(
                value1.ValueMultivectorType,
                value1.MultivectorCoefficients * value2.Value
                ));
 }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
0
 public ILanguageValue Evaluate(ValuePrimitive <MathematicaScalar> value1, ValuePrimitive <MathematicaScalar> value2)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.ValuePrimitiveType,
                value1.Value *value2.Value
                ));
 }
Exemplo n.º 6
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)
                     ));
        }
Exemplo n.º 7
0
 public ILanguageValue Evaluate(ValuePrimitive <MathematicaScalar> value1, GMacValueMultivector value2)
 {
     return(GMacValueMultivector.Create(
                value2.ValueMultivectorType,
                value2.SymbolicMultivector * value1.Value
                ));
 }
Exemplo n.º 8
0
 public ILanguageValue Evaluate(GMacValueMultivector value1)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.GMacRootAst.ScalarType,
                value1.SymbolicFrame.Norm2(value1.MultivectorCoefficients)
                ));
 }
Exemplo n.º 9
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
                     ));
        }
Exemplo n.º 10
0
 public ILanguageValue Evaluate(ValuePrimitive <MathematicaScalar> value1)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.ValuePrimitiveType,
                value1.Value.Abs()
                ));
 }
Exemplo n.º 11
0
 public ILanguageValue Evaluate(ValuePrimitive <int> value1, ValuePrimitive <int> value2)
 {
     return(ValuePrimitive <int> .Create(
                value1.ValuePrimitiveType,
                value1.Value *value2.Value
                ));
 }
Exemplo n.º 12
0
 public ILanguageValue Evaluate(ValuePrimitive <int> value1)
 {
     return(ValuePrimitive <int> .Create(
                value1.ValuePrimitiveType,
                Math.Abs(value1.Value)
                ));
 }
Exemplo n.º 13
0
 public ILanguageValue Evaluate(GMacValueMultivector value1)
 {
     return(ValuePrimitive <MathematicaScalar> .Create(
                value1.GMacRootAst.ScalarType,
                value1.MultivectorCoefficients.EuclideanMagnitude()
                ));
 }
Exemplo n.º 14
0
        /// <summary>
        /// Set the value associated with a primitive local variable or output parameter's low-level item
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="value"></param>
        public void WriteLhsPrimitiveValue(LanguageValueAccess valueAccess, ValuePrimitive <MathematicaScalar> value)
        {
            //For a local variable define a new low-level temp with the given value (perhaps after sub-expression refactoring)
            //Note: Before low-level code generation the macro code must be in SSA form
            if (valueAccess.IsLocalVariable)
            {
                DefineTemp(valueAccess, ProcessRhsValue(value));

                return;
            }

            //For output parameters
            if (!valueAccess.IsOutputParameter)
            {
                throw new InvalidOperationException(
                          "Cannot assign a value to a macro input parameter after its definition");
            }

            var        hlName = valueAccess.GetName();
            LlDataItem llDataItem;

            //If the low-level output parameter is not defined it's value is ignored
            if (_hlDictionary.TryGetValue(hlName, out llDataItem))
            {
                llDataItem.SetAssignedRhsValue(ProcessRhsValue(value), _llVarEvaluationOrder++);
            }
        }
Exemplo n.º 15
0
        public ILanguageValue Evaluate(GMacValueMultivector value1, GMacValueMultivector value2)
        {
            var scalar = value1.SymbolicFrame.Sp(
                value1.MultivectorCoefficients, value2.MultivectorCoefficients
                );

            return(ValuePrimitive <MathematicaScalar> .Create(value1.CoefficientType, scalar[0]));
        }
Exemplo n.º 16
0
        public TreeNode Visit(ValuePrimitive <int> value)
        {
            var node = new TreeNode("<INT_VALUE> " + value)
            {
                Tag = value
            };

            return(node);
        }
Exemplo n.º 17
0
        public TreeNode Visit(ValuePrimitive <double> value)
        {
            var node = new TreeNode("<DOUBLE_VALUE> " + value)
            {
                Tag = value
            };

            return(node);
        }
Exemplo n.º 18
0
        public ILanguageValue Evaluate(GMacValueMultivector value1, GMacValueMultivector value2)
        {
            var scalar = value1.SymbolicMultivector.ESp(value2.SymbolicMultivector);

            return(ValuePrimitive <MathematicaScalar> .Create(
                       value1.CoefficientType,
                       scalar[0].ToMathematicaScalar()
                       ));
        }
Exemplo n.º 19
0
        public TreeNode Visit(ValuePrimitive <bool> value)
        {
            var node = new TreeNode("<BOOL_VALUE> " + value)
            {
                Tag = value
            };

            return(node);
        }
Exemplo n.º 20
0
        public TreeNode Visit(ValuePrimitive <MathematicaScalar> value)
        {
            var node = new TreeNode("<SCALAR_VALUE> " + value)
            {
                Tag = value
            };

            return(node);
        }
Exemplo n.º 21
0
 internal ValuePrimitive <MathematicaScalar> this[int id]
 {
     get
     {
         return(ValuePrimitive <MathematicaScalar> .Create(CoefficientType, MultivectorCoefficients[id]));
     }
     set
     {
         MultivectorCoefficients[id] = value.Value;
     }
 }
Exemplo n.º 22
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
                    )
                ));
 }
Exemplo n.º 23
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())
                  )
              ));
 }
Exemplo n.º 24
0
 /// <summary>
 /// Change the assigned value and evaluation order for an output item only
 /// </summary>
 /// <param name="assignedValue"></param>
 /// <param name="evaluationOrder"></param>
 public void SetAssignedRhsValue(ValuePrimitive <MathematicaScalar> assignedValue, int evaluationOrder)
 {
     if (IsOutput)
     {
         AssignedRhsValue = assignedValue;
         EvaluationOrder  = evaluationOrder;
     }
     else
     {
         throw new InvalidOperationException("Only output macro parameters can be assigned value after their creation");
     }
 }
Exemplo n.º 25
0
        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);
        }
Exemplo n.º 26
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();
        }
Exemplo n.º 27
0
 internal ValuePrimitive <MathematicaScalar> this[int id]
 {
     get
     {
         return(ValuePrimitive <MathematicaScalar> .Create(
                    CoefficientType,
                    SymbolicMultivector[id].ToMathematicaScalar()
                    ));
     }
     set
     {
         SymbolicMultivector.SetTermCoef(id, value.Value);
     }
 }
Exemplo n.º 28
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");
        }
Exemplo n.º 29
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);
        }
Exemplo n.º 30
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);
        }