Ejemplo n.º 1
0
        public static void ConstructionTest()
        {
            TestUtils.AddTestStartingMessage("MathematicaScalar Creation Test Started.");

            var s = MathematicaScalar.CreateRational(Cas, 3, 5);

            TestUtils.AddTest("Try create scalar from rational 3/5 ... ", s);

            s = MathematicaScalar.CreateSymbol(Cas, "x3");
            TestUtils.AddTest("Try create scalar from symbol name ... ", s);

            s = MathematicaScalar.Create(Cas, 5);
            TestUtils.AddTest("Try create scalar from integer 5 ... ", s);

            s = MathematicaScalar.Create(Cas, 5.0f);
            TestUtils.AddTest("Try create scalar from float 5.0f ... ", s);

            s = MathematicaScalar.Create(Cas, 5.0d);
            TestUtils.AddTest("Try create scalar from double 5.0d ... ", s);

            var e = Mfs.Plus["v1".ToSymbolExpr(), Mfs.Power[5.ToExpr(), "n".ToSymbolExpr()]];

            s = MathematicaScalar.Create(Cas, e);
            TestUtils.AddTest("Try create scalar from expression object ... ", s);

            s = MathematicaScalar.Create(Cas, @"Pi + 5 / 3");
            TestUtils.AddTest("Try create scalar from expression text \"Pi + 5 / 3\" ... ", s);

            TestUtils.AddTestCompletionMessage("MathematicaScalar Creation Test Completed.");
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Create a symbolic scalar 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>
 internal MathematicaScalar ToMathematicaScalar(StringSequenceTemplate varNameTemplate)
 {
     return
         (IsConstant
         ? ConstantSymbolicScalar
         : MathematicaScalar.Create(SymbolicUtils.Cas, varNameTemplate.GenerateNextString()));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Convert this binding pattern into a multivector value
        /// </summary>
        /// <param name="basisBladeToVarName"></param>
        /// <returns></returns>
        public AstValueMultivector ToValue(Func <AstFrameBasisBlade, string> basisBladeToVarName)
        {
            var frameInfo = new AstFrame(BaseFrameMultivector.AssociatedFrameMultivector.ParentFrame);

            var mv = GaSymMultivector.CreateZero(
                BaseFrameMultivector.AssociatedFrameMultivector.ParentFrame.GaSpaceDimension
                );

            foreach (var pair in _patternDictionary)
            {
                mv.AddFactor(
                    pair.Key,
                    pair.Value.IsConstant
                        ? pair.Value.ConstantSymbolicScalar
                        : MathematicaScalar.Create(SymbolicUtils.Cas, basisBladeToVarName(frameInfo.BasisBlade(pair.Key)))
                    );
            }

            return
                (new AstValueMultivector(
                     GMacValueMultivector.Create(
                         BaseFrameMultivector.AssociatedFrameMultivector,
                         mv
                         )
                     ));
        }
Ejemplo n.º 4
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)
                     ));
        }
Ejemplo n.º 5
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());
        }
Ejemplo n.º 6
0
        private bool InitSymbolicEngine()
        {
            try
            {
                _log.Append("Initializing Symbolic Engine...");
                labelStatus.Text = _log.ToString();
                Application.DoEvents();

                MathematicaScalar.Create(SymbolicUtils.Cas, "0");

                _log.AppendLine("Done").AppendLine();
                labelStatus.Text = _log.ToString();
                Application.DoEvents();

                return(true);
            }
            catch (Exception e)
            {
                _log.AppendLine("Failed").AppendLine(e.Message).AppendLine();
                labelStatus.Text = _log.ToString();
                Application.DoEvents();

                return(false);
            }
        }
Ejemplo n.º 7
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
                     ));
        }
Ejemplo n.º 8
0
        internal MathematicaConstants(MathematicaInterface parentCas)
        {
            Cas = parentCas;

            True = MathematicaCondition.Create(parentCas, true);
            _constantsDictionary.Add("true", True);

            False = MathematicaCondition.Create(parentCas, false);
            _constantsDictionary.Add("false", False);

            Zero = MathematicaScalar.Create(parentCas, 0);
            _constantsDictionary.Add("zero", Zero);

            One = MathematicaScalar.Create(parentCas, 1);
            _constantsDictionary.Add("one", One);

            MinusOne = MathematicaScalar.Create(parentCas, -1);
            _constantsDictionary.Add("minusone", MinusOne);

            Pi = MathematicaScalar.Create(parentCas, CasConnection.EvaluateToExpr("Pi"));
            _constantsDictionary.Add("pi", Pi);

            TwoPi = MathematicaScalar.Create(parentCas, CasConnection.EvaluateToExpr("Times[2, Pi]"));
            _constantsDictionary.Add("twopi", TwoPi);
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
0
        protected override void ComputeIpm()
        {
            var bvSig = _basisVectorsSignatures.Select(i => MathematicaScalar.Create(CasInterface, i));

            var v = MathematicaVector.CreateFullVector(CasInterface, bvSig);

            InnerProductMatrix = MathematicaMatrix.CreateDiagonal(v);
        }
Ejemplo n.º 11
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())
                  )
              ));
 }
Ejemplo n.º 12
0
        public static string InitializeGMac()
        {
            var log = new LinearComposer();

            try
            {
                log.Append("Initializing GA Data...");

                GMacLookupTables.Choose(1, 1);

                log.AppendLine("Done").AppendLine();

                log.Append("Initializing GMac Resources...");

                if (Directory.Exists(IconsPath) == false)
                {
                    Directory.CreateDirectory(IconsPath);
                }

                SaveIcon(Resources.BasisVector64, "BasisVector64");
                SaveIcon(Resources.Compile64, "Compile64");
                SaveIcon(Resources.Constant64, "Constant64");
                SaveIcon(Resources.Filter64, "Filter64");
                SaveIcon(Resources.Frame64, "Frame64");
                SaveIcon(Resources.GMacAST64, "GMacAST64");
                SaveIcon(Resources.GMac_Icon64, "GMac_Icon64");
                SaveIcon(Resources.Idea64, "Idea64");
                SaveIcon(Resources.Input64, "Input64");
                SaveIcon(Resources.Macro64, "Macro64");
                SaveIcon(Resources.Namespace64, "Namespace64");
                SaveIcon(Resources.Output64, "Output64");
                SaveIcon(Resources.Scalar64, "Scalar64");
                SaveIcon(Resources.Structure64, "Structure64");
                SaveIcon(Resources.Subspace64, "Subspace64");
                SaveIcon(Resources.Transform64, "Transform64");

                log.AppendLine("Done").AppendLine();

                log.Append("Initializing Symbolic Engine...");

                MathematicaScalar.Create(SymbolicUtils.Cas, "0");

                log.AppendLine("Done").AppendLine();
            }
            catch (Exception e)
            {
                log.AppendLine("Failed").AppendLine(e.Message).AppendLine();
            }

            return(log.ToString());
        }
Ejemplo n.º 13
0
        private ILanguageValue EvaluateBasicUnaryCastToScalar(BasicUnary expr)
        {
            var value1 = expr.Operand.AcceptVisitor(this);

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

            throw new InvalidOperationException("Invalid cast operation");
        }
Ejemplo n.º 14
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();
        }
Ejemplo n.º 15
0
        public static void ConstructionTest()
        {
            TestUtils.AddTestStartingMessage("MathematicaMatrix Creation Test Started.");

            var scalarsArray = new MathematicaScalar[2, 2];

            scalarsArray[0, 0] = MathematicaScalar.Create(Cas, 2);
            scalarsArray[0, 1] = MathematicaScalar.Create(Cas, -2);
            scalarsArray[1, 0] = MathematicaScalar.Create(Cas, 3);
            scalarsArray[1, 1] = MathematicaScalar.Create(Cas, 1);

            var s = MathematicaMatrix.CreateFullMatrix(Cas, scalarsArray);

            TestUtils.AddTest("Try create full matrix from array of scalars ... ", s);

            s = MathematicaMatrix.CreateIdentity(Cas, 3);
            TestUtils.AddTest("Try create full 3x3 identity matrix ... ", s);

            s = MathematicaMatrix.CreateIdentity(Cas, 3, false);
            TestUtils.AddTest("Try create sparse 3x3 identity matrix ... ", s);

            var v = MathematicaVector.CreateFullVector(Cas, Cas.Constants.Zero, Cas.Constants.TwoPi, Cas.Constants.One);

            s = MathematicaMatrix.CreateDiagonal(v);
            TestUtils.AddTest("Try create full 3x3 diagonal matrix from vector {0, 2 Pi, 1} ... ", s);

            s = MathematicaMatrix.CreateDiagonal(v, false);
            TestUtils.AddTest("Try create sparse 3x3 diagonal matrix from vector {0, 2 Pi, 1} ... ", s);

            s = MathematicaMatrix.CreateRowVector(v);
            TestUtils.AddTest("Try create full row vector matrix from vector {0, 2 Pi, 1} ... ", s);

            s = MathematicaMatrix.CreateRowVector(v, false);
            TestUtils.AddTest("Try create sparse row vector matrix from vector {0, 2 Pi, 1} ... ", s);

            s = MathematicaMatrix.CreateColumnVector(v);
            TestUtils.AddTest("Try create full row vector matrix from vector {0, 2 Pi, 1} ... ", s);

            s = MathematicaMatrix.CreateColumnVector(v, false);
            TestUtils.AddTest("Try create sparse row vector matrix from vector {0, 2 Pi, 1} ... ", s);

            s = MathematicaMatrix.CreateConstant(Cas.Constants.Pi, 2, 3);
            TestUtils.AddTest("Try create 2x3 matrix with constant elements of Pi ... ", s);

            s = MathematicaMatrix.Create(Cas, @"{{1, x}, {2, y}, {3, z}}");
            TestUtils.AddTest("Try create matrix from expression text \"{{1, x}, {2, y}, {3, z}}\" ... ", s);

            TestUtils.AddTestCompletionMessage("MathematicaMatrix Creation Test Completed.");
        }
Ejemplo n.º 16
0
        private static ILanguageExpressionAtomic AtomicExpressionToMultivectorAtomicExpression(this CommandBlock commandBlock, GMacFrameMultivector mvType, ILanguageExpressionAtomic oldExpr)
        {
            if (oldExpr.ExpressionType.IsSameType(mvType))
            {
                return(oldExpr);
            }

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

            var valuePrimitive = oldExpr as ValuePrimitive <int>;

            if (valuePrimitive != null)
            {
                return(GMacValueMultivector.CreateScalar(
                           mvType,
                           MathematicaScalar.Create(SymbolicUtils.Cas, valuePrimitive.Value)
                           ));
            }

            var primitive = oldExpr as ValuePrimitive <MathematicaScalar>;

            if (primitive != null)
            {
                return(GMacValueMultivector.CreateScalar(
                           mvType,
                           primitive.Value
                           ));
            }

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

            //Create a cast operation
            var newRhsExpr = BasicUnary.Create(mvType, mvType, 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));
        }
Ejemplo n.º 17
0
        public static GaSymFrameOrthogonal CreateProjective(int vSpaceDim)
        {
            VerifyVSpaceDim(vSpaceDim, 2);

            var sigArray = Enumerable.Repeat(1, vSpaceDim).ToArray();

            sigArray[vSpaceDim - 1] = 0;

            var frameSigList =
                sigArray.Select(
                    i => MathematicaScalar.Create(SymbolicUtils.Cas, i)
                    );

            return(new GaSymFrameOrthogonal(frameSigList));
        }
Ejemplo n.º 18
0
        internal static GaMultivector ToMultivector(this Dictionary <int, ListComposer> accumExprDict, int gaSpaceDim)
        {
            var resultMv = GaMultivector.CreateZero(gaSpaceDim);

            foreach (var pair in accumExprDict)
            {
                var resultCoef = SymbolicUtils.Cas[pair.Value.ToString()];

                if (!(resultCoef.Args.Length == 0 && resultCoef.ToString() == "0"))
                {
                    resultMv.Add(pair.Key, MathematicaScalar.Create(SymbolicUtils.Cas, resultCoef));
                }
            }

            return(resultMv);
        }
Ejemplo n.º 19
0
        private ILanguageValue EvaluateBasicUnaryCastToMultivector(BasicUnary expr)
        {
            var value1 = expr.Operand.AcceptVisitor(this);

            var mvType = (GMacFrameMultivector)expr.Operator;

            if (value1.ExpressionType.IsInteger())
            {
                var intValue = ((ValuePrimitive <int>)value1).Value;

                return(GMacValueMultivector.Create(
                           mvType,
                           GaMultivector.CreateScalar(
                               mvType.ParentFrame.GaSpaceDimension,
                               MathematicaScalar.Create(SymbolicUtils.Cas, intValue)
                               )
                           ));
            }

            if (value1.ExpressionType.IsScalar())
            {
                var scalarValue = ((ValuePrimitive <MathematicaScalar>)value1).Value;

                return(GMacValueMultivector.Create(
                           mvType,
                           GaMultivector.CreateScalar(
                               mvType.ParentFrame.GaSpaceDimension,
                               scalarValue
                               )
                           ));
            }

            if (value1.ExpressionType.IsFrameMultivector() &&
                value1.ExpressionType.GetFrame().VSpaceDimension == mvType.ParentFrame.VSpaceDimension)
            {
                var mvValue = ((GMacValueMultivector)value1);

                return(GMacValueMultivector.Create(
                           mvType,
                           GaMultivector.CreateCopy(mvValue.MultivectorCoefficients)
                           ));
            }

            throw new InvalidOperationException("Invalid cast operation");
        }
Ejemplo n.º 20
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)
                     ));
        }
Ejemplo n.º 21
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));
        }
Ejemplo n.º 22
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));
        }
Ejemplo n.º 23
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
                     ));
        }
Ejemplo n.º 24
0
        public static void BasicOpsTest()
        {
            //Expr e;
            var s1 = MathematicaScalar.CreateRational(Cas, -3, 5);
            var s2 = MathematicaScalar.CreateRational(Cas, 9, 5);
            var s3 = MathematicaScalar.Create(Cas, "Pi");

            TestUtils.AddTestStartingMessage("MathematicaScalar Basic Operations Test Started.");

            TestUtils.AddTest("Try negate rational -3/5 ... ", -s1);
            TestUtils.AddTest("Try add rationals -3/5 and 9/5 ... ", s1 + s2);
            TestUtils.AddTest("Try subtract rationals -3/5 and 9/5 ... ", s1 - s2);
            TestUtils.AddTest("Try multiply rationals -3/5 and 9/5 ... ", s1 * s2);
            TestUtils.AddTest("Try divide rationals -3/5 and 9/5 ... ", s1 / s2);
            TestUtils.AddTest("Try rise rational 9/5 to the power of Pi ... ", s2 ^ s3);

            TestUtils.AddTest("Try apply Abs to rational -3/5 ... ", s1.Abs());
            TestUtils.AddTest("Try apply Sqrt to rational -3/5 ... ", s1.Sqrt());
            TestUtils.AddTest("Try apply Sin to rational 9/5 ... ", s2.Sin());
            TestUtils.AddTest("Try apply Cos to rational 9/5 ... ", s2.Cos());
            TestUtils.AddTest("Try apply Tan to rational 9/5 ... ", s2.Tan());
            TestUtils.AddTest("Try apply Sinh to rational 9/5 ... ", s2.Sinh());
            TestUtils.AddTest("Try apply Cosh to rational 9/5 ... ", s2.Cosh());
            TestUtils.AddTest("Try apply Tanh to rational 9/5 ... ", s2.Tanh());
            TestUtils.AddTest("Try apply Log to rational 9/5 ... ", s2.Log());
            TestUtils.AddTest("Try apply Log10 to rational 9/5 ... ", s2.Log10());
            TestUtils.AddTest("Try apply Log2 to rational 9/5 ... ", s2.Log2());
            TestUtils.AddTest("Try apply Exp to rational 9/5 ... ", s2.Exp());

            var s = MathematicaScalar.Create(Cas, @"3 x ^ 2 + 2 Pi ^ x - Sin[x]");
            var d = MathematicaScalar.CreateSymbol(Cas, "x");

            TestUtils.AddTest("Try differentiate 3 x ^ 2 + 2 Pi ^ x - Sin[x] w.r.t. x ... ", s.DiffBy(d));

            TestUtils.AddTestCompletionMessage("MathematicaScalar Basic Operations Test Completed.");
        }
Ejemplo n.º 25
0
        /// <summary>
        /// frameSigList must have all items equal to 1 (false) or -1 (true) with at least one negative item
        /// </summary>
        /// <param name="frameSigList"></param>
        internal GaSymFrameOrthonormal(IEnumerable <bool> frameSigList)
        {
            BasisVectorsSignatures = frameSigList.Select(b => b ? -1 : 1).ToArray();

            OrthonormalMetric = GaSymMetricOrthonormal.Create(BasisVectorsSignatures);

            Op  = ComputedOp = new GaSymOp(VSpaceDimension);
            Gp  = ComputedGp = new GaSymOrthonormalGp(OrthonormalMetric);
            Sp  = ComputedSp = new GaSymOrthonormalSp(OrthonormalMetric);
            Lcp = ComputedLcp = new GaSymOrthonormalLcp(OrthonormalMetric);
            Rcp = ComputedRcp = new GaSymOrthonormalRcp(OrthonormalMetric);
            Fdp = ComputedFdp = new GaSymOrthonormalFdp(OrthonormalMetric);
            Hip = ComputedHip = new GaSymOrthonormalHip(OrthonormalMetric);
            Acp = ComputedAcp = new GaSymOrthonormalAcp(OrthonormalMetric);
            Cp  = ComputedCp = new GaSymOrthonormalCp(OrthonormalMetric);

            UnitPseudoScalarCoef =
                MathematicaScalar.Create(
                    SymbolicUtils.Cas,
                    MaxBasisBladeId.BasisBladeIdHasNegativeReverse()
                        ? -1.0d / OrthonormalMetric[MaxBasisBladeId]
                        : 1.0d / OrthonormalMetric[MaxBasisBladeId]
                    );
        }
Ejemplo n.º 26
0
 public MathematicaScalar GetSymbolicInteger(int minLimit, int maxLimit)
 {
     return(MathematicaScalar.Create(SymbolicUtils.Cas, GetInteger(minLimit, maxLimit)));
 }
Ejemplo n.º 27
0
        public static void IsOpsTest()
        {
            TestUtils.AddTestStartingMessage("MathematicaScalar 'Is' Operations Test Started.");

            var s1 = MathematicaScalar.Create(Cas, 0.0f);

            TestUtils.AddTest("Try apply IsPossibleZero() to 0.0f ... ", s1.IsPossibleZero());

            s1 = MathematicaScalar.CreateSymbol(Cas, "Pi");
            TestUtils.AddTest("Try apply IsPossibleZero() to Pi ... ", s1.IsPossibleZero());

            s1 = MathematicaScalar.Create(Cas, @"x - x");
            TestUtils.AddTest("Try apply IsPossibleZero() to (x - x) ... ", s1.IsPossibleZero());

            s1 = MathematicaScalar.Create(Cas, @"3 x ^ 2 + 2 Pi ^ x - Sin[x]");
            TestUtils.AddTest("Try apply IsPossibleZero() to 3 x ^ 2 + 2 Pi ^ x - Sin[x] ... ", s1.IsPossibleZero());


            s1 = MathematicaScalar.Create(Cas, 0.0f);
            TestUtils.AddTest("Try apply IsEqualZero() to 0.0f ... ", s1.IsEqualZero());

            s1 = MathematicaScalar.CreateSymbol(Cas, "Pi");
            TestUtils.AddTest("Try apply IsEqualZero() to Pi ... ", s1.IsEqualZero());

            s1 = MathematicaScalar.Create(Cas, @"x - x");
            TestUtils.AddTest("Try apply IsEqualZero() to (x - x) ... ", s1.IsEqualZero());

            s1 = MathematicaScalar.Create(Cas, @"3 x ^ 2 + 2 Pi ^ x - Sin[x]");
            TestUtils.AddTest("Try apply IsEqualZero() to 3 x ^ 2 + 2 Pi ^ x - Sin[x] ... ", s1.IsEqualZero());


            s1 = MathematicaScalar.Create(Cas, 5.0f);
            var s2 = MathematicaScalar.Create(Cas, 5);

            TestUtils.AddTest("Try apply IsPossibleScalar() to 5.0f with 5 ... ", s1.IsPossibleScalar(s2));

            s1 = MathematicaScalar.Create(Cas, 5.1f);
            s2 = MathematicaScalar.Create(Cas, 5);
            TestUtils.AddTest("Try apply IsPossibleScalar() to 5.1f with 5 ... ", s1.IsPossibleScalar(s2));

            s1 = MathematicaScalar.Create(Cas, 1.0f);
            s2 = MathematicaScalar.Create(Cas, "Sin[Pi]");
            TestUtils.AddTest("Try apply IsPossibleScalar() to 1.0f with Sin[Pi] ... ", s1.IsPossibleScalar(s2));

            s1 = MathematicaScalar.Create(Cas, -1.0f);
            s2 = MathematicaScalar.Create(Cas, "Cos[Pi]");
            TestUtils.AddTest("Try apply IsPossibleScalar() to -1.0f with Cos[Pi] ... ", s1.IsPossibleScalar(s2));


            s1 = MathematicaScalar.Create(Cas, 5.0f);
            s2 = MathematicaScalar.Create(Cas, 5);
            TestUtils.AddTest("Try apply IsEqualScalar() to 5.0f with 5 ... ", s1.IsEqualScalar(s2));

            s1 = MathematicaScalar.Create(Cas, 5.1f);
            s2 = MathematicaScalar.Create(Cas, 5);
            TestUtils.AddTest("Try apply IsEqualScalar() to 5.1f with 5 ... ", s1.IsEqualScalar(s2));

            s1 = MathematicaScalar.Create(Cas, 1.0f);
            s2 = MathematicaScalar.Create(Cas, "Sin[Pi]");
            TestUtils.AddTest("Try apply IsEqualScalar() to 1.0f with Sin[Pi] ... ", s1.IsEqualScalar(s2));

            s1 = MathematicaScalar.Create(Cas, -1.0f);
            s2 = MathematicaScalar.Create(Cas, "Cos[Pi]");
            TestUtils.AddTest("Try apply IsEqualScalar() to -1.0f with Cos[Pi] ... ", s1.IsEqualScalar(s2));


            s1 = MathematicaScalar.Create(Cas, -1.0f);
            TestUtils.AddTest("Try apply IsConstant() to -1.0f ... ", s1.IsConstant());

            s1 = MathematicaScalar.Create(Cas, "Sin[Pi]");
            TestUtils.AddTest("Try apply IsConstant() to Sin[Pi] ... ", s1.IsConstant());

            s1 = MathematicaScalar.Create(Cas, "Sin[x]");
            TestUtils.AddTest("Try apply IsConstant() to Sin[x] ... ", s1.IsConstant());

            s1 = MathematicaScalar.Create(Cas, "Sin[x - x]");
            TestUtils.AddTest("Try apply IsConstant() to Sin[x - x] ... ", s1.IsConstant());


            s1 = MathematicaScalar.Create(Cas, 0.0f);
            TestUtils.AddTest("Try apply IsNonZeroRealConstant() to 0.0f ... ", s1.IsNonZeroRealConstant());

            s1 = MathematicaScalar.Create(Cas, -1.0f);
            TestUtils.AddTest("Try apply IsNonZeroRealConstant() to -1.0f ... ", s1.IsNonZeroRealConstant());

            s1 = MathematicaScalar.Create(Cas, "Sin[Pi]");
            TestUtils.AddTest("Try apply IsNonZeroRealConstant() to Sin[Pi] ... ", s1.IsNonZeroRealConstant());

            s1 = MathematicaScalar.Create(Cas, "Sin[x]");
            TestUtils.AddTest("Try apply IsNonZeroRealConstant() to Sin[x] ... ", s1.IsNonZeroRealConstant());

            s1 = MathematicaScalar.Create(Cas, "Sin[x - x]");
            TestUtils.AddTest("Try apply IsNonZeroRealConstant() to Sin[x - x] ... ", s1.IsNonZeroRealConstant());

            s1 = MathematicaScalar.Create(Cas, "3 + i * Sin[Pi / 2]");
            TestUtils.AddTest("Try apply IsNonZeroRealConstant() to 3 + i * Sin[Pi / 2] ... ", s1.IsNonZeroRealConstant());


            TestUtils.AddTestCompletionMessage("MathematicaScalar 'Is' Operations Test Completed.");
        }
Ejemplo n.º 28
0
        public static void BasicOpsTest()
        {
            var s   = MathematicaScalar.Create(Cas, -2);
            var v   = MathematicaVector.CreateFullVector(Cas, Cas.Constants.One, Cas.Constants.Pi, Cas.Constants.MinusOne);
            var m1  = MathematicaMatrix.Create(Cas, "RandomInteger[{-5, 5}, {3, 4}]");
            var m2  = MathematicaMatrix.CreateFullDiagonalMatrix(Cas, Cas.Constants.One, Cas.Constants.Pi, Cas.Constants.MinusOne);
            var sm3 = MathematicaMatrix.Create(Cas, "SparseArray[{{1, 0, 0, 0}, {0, 2, 0, 4}, {0, 0, 0, 3}}]");
            var sm4 = m2.ToMathematicaSparseMatrix();

            TestUtils.AddTestStartingMessage("MathematicaMatrix Basic Operations Test Started.");

            TestUtils.AddTest("Try get rows of full 3x4 matrix ... ", m1.RowCount);
            TestUtils.AddTest("Try get columns of full 3x4 matrix ... ", m1.ColumnCount);

            TestUtils.AddTest("Try get rows of sparse 3x4 matrix ... ", sm3.RowCount);
            TestUtils.AddTest("Try get columns of sparse 3x4 matrix ... ", sm3.ColumnCount);

            for (var i = 0; i < m1.RowCount; i++)
            {
                for (var j = 0; j < m1.ColumnCount; j++)
                {
                    TestUtils.AddTest("Try get component (" + i + ", " + j + ") of full 3x4 matrix " + m1.ExpressionText + " ... ", m1[i, j]);
                }
            }

            for (var i = 0; i < sm3.RowCount; i++)
            {
                for (var j = 0; j < sm3.ColumnCount; j++)
                {
                    TestUtils.AddTest("Try get component (" + i + ", " + j + ") of sparse 3x4 matrix " + sm3.ToMathematicaFullMatrix().ExpressionText + " ... ", sm3[i, j]);
                }
            }

            for (var i = 0; i < m1.RowCount; i++)
            {
                TestUtils.AddTest("Try get row " + i + " of full 3x4 matrix " + m1.ExpressionText + " ... ", m1.GetRow(i));
            }

            for (var i = 0; i < m1.ColumnCount; i++)
            {
                TestUtils.AddTest("Try get column " + i + " of full 3x4 matrix " + m1.ExpressionText + " ... ", m1.GetColumn(i));
            }

            for (var i = 0; i < sm3.RowCount; i++)
            {
                TestUtils.AddTest("Try get row " + i + " of sparse 3x4 matrix " + sm3.ToMathematicaFullMatrix().ExpressionText + " ... ", sm3.GetRow(i));
            }

            for (var i = 0; i < sm3.ColumnCount; i++)
            {
                TestUtils.AddTest("Try get column " + i + " of sparse 3x4 matrix " + sm3.ToMathematicaFullMatrix().ExpressionText + " ... ", sm3.GetColumn(i));
            }

            TestUtils.AddTest("Try get diagonal of full matrix " + m2.ExpressionText + " ... ", m2.GetDiagonal());
            TestUtils.AddTest("Try get diagonal of sparse matrix " + sm4.ToMathematicaFullMatrix().ExpressionText + " ... ", sm4.GetDiagonal());
            TestUtils.AddTest("Try negate matrix " + m2.ExpressionText + " ... ", -m2);
            TestUtils.AddTest("Try add matrix " + m1.ExpressionText + " and " + sm3.ToMathematicaFullMatrix().ExpressionText + " ... ", m1 + sm3);
            TestUtils.AddTest("Try subtract matrix " + m1.ExpressionText + " and " + sm3.ToMathematicaFullMatrix().ExpressionText + " ... ", m1 - sm3);
            TestUtils.AddTest("Try multiply matrix " + m2.ExpressionText + " and " + m1.ToMathematicaFullMatrix().ExpressionText + " ... ", m2 * m1);
            TestUtils.AddTest("Try multiply matrix " + m2.ExpressionText + " and scalar -2 ... ", m2 * s);
            TestUtils.AddTest("Try multiply scalar -2 and matrix " + m2.ExpressionText + " ... ", s * m2);
            TestUtils.AddTest("Try divide matrix " + m2.ExpressionText + " by scalar -2 ... ", m2 / s);
            TestUtils.AddTest("Try multiply matrix " + m2.ExpressionText + " and vector " + v.ExpressionText + " ... ", m2.Times(v));
            TestUtils.AddTest("Try multiply vector " + v.ExpressionText + " and matrix " + m2.ExpressionText + " ... ", v.Times(m2));
            TestUtils.AddTest("Try transpose matrix " + m2.ExpressionText + " ... ", m2.Transpose());
            TestUtils.AddTest("Try inverse matrix " + m2.ExpressionText + " ... ", m2.Inverse());
            TestUtils.AddTest("Try inverse transpose matrix " + m2.ExpressionText + " ... ", m2.InverseTranspose());

            m2 = MathematicaMatrix.Create(Cas, "{{1, -1, 0}, {-1, 2, -1}, {0, -1, 1}}");
            TestUtils.AddTest("Try get eigen values (in a vector) of matrix " + m2.ExpressionText + " ... ", m2.EigenValues_InVector());
            TestUtils.AddTest("Try get eigen values (in a matrix) of matrix " + m2.ExpressionText + " ... ", m2.EigenValues_InDiagonalMatrix());
            TestUtils.AddTest("Try get eigen vectors (as rows of a matrix) of matrix " + m2.ExpressionText + " ... ", m2.EigenVectors(MathematicaMatrix.EigenVectorsSpecs.InMatrixRows));
            TestUtils.AddTest("Try get eigen vectors (as columns of a matrix) of matrix " + m2.ExpressionText + " ... ", m2.EigenVectors(MathematicaMatrix.EigenVectorsSpecs.InMatrixColumns));
            TestUtils.AddTest("Try get eigen vectors (as orthonormal rows of a matrix) of matrix " + m2.ExpressionText + " ... ", m2.EigenVectors(MathematicaMatrix.EigenVectorsSpecs.OrthogonalInMatrixRows));
            TestUtils.AddTest("Try get eigen vectors (as orthonormal columns of a matrix) of matrix " + m2.ExpressionText + " ... ", m2.EigenVectors(MathematicaMatrix.EigenVectorsSpecs.OrthogonalInMatrixColumns));

            MathematicaVector eval1;
            MathematicaMatrix eval2;
            MathematicaMatrix evec;

            m2.EigenSystem(MathematicaMatrix.EigenVectorsSpecs.InMatrixRows, out eval1, out evec);
            TestUtils.AddTest("Try get eigen system values (in a vector) of matrix " + m2.ExpressionText + " ... ", eval1);

            m2.EigenSystem(MathematicaMatrix.EigenVectorsSpecs.InMatrixRows, out eval2, out evec);
            TestUtils.AddTest("Try get eigen system values (in a matrix) of matrix " + m2.ExpressionText + " ... ", eval2);

            m2.EigenSystem(MathematicaMatrix.EigenVectorsSpecs.InMatrixRows, out eval1, out evec);
            TestUtils.AddTest("Try get eigen system vectors (as rows of a matrix) of matrix " + m2.ExpressionText + " ... ", evec);

            m2.EigenSystem(MathematicaMatrix.EigenVectorsSpecs.InMatrixColumns, out eval1, out evec);
            TestUtils.AddTest("Try get eigen system vectors (as columns of a matrix) of matrix " + m2.ExpressionText + " ... ", evec);

            m2.EigenSystem(MathematicaMatrix.EigenVectorsSpecs.OrthogonalInMatrixRows, out eval1, out evec);
            TestUtils.AddTest("Try get eigen system vectors (as orthonormal rows of a matrix) of matrix " + m2.ExpressionText + " ... ", evec);

            m2.EigenSystem(MathematicaMatrix.EigenVectorsSpecs.OrthogonalInMatrixColumns, out eval1, out evec);
            TestUtils.AddTest("Try get eigen system vectors (as orthonormal columns of a matrix) of matrix " + m2.ExpressionText + " ... ", evec);

            TestUtils.AddTestCompletionMessage("MathematicaMatrix Basic Operations Test Completed.");
        }
Ejemplo n.º 29
0
 public MathematicaScalar ToSymbolicScalar(GaSymMultivectorTempTree mv1, GaSymMultivectorTempTree mv2)
 {
     return(MathematicaScalar.Create(SymbolicUtils.Cas, this[mv1, mv2]));
 }
Ejemplo n.º 30
0
        public static void BasicOpsTest()
        {
            var v1  = MathematicaVector.CreateFullVector(Cas, Cas.Constants.Zero, Cas.Constants.One, Cas.Constants.TwoPi);
            var v2  = MathematicaVector.Create(Cas, @"{-1, x, Sin[t]}");
            var sv3 = MathematicaVector.Create(Cas, "SparseArray[{Rule[1, Pi], Rule[5, -1]}]");
            var s   = MathematicaScalar.Create(Cas, "x");

            TestUtils.AddTestStartingMessage("MathematicaVector Basic Operations Test Started.");

            TestUtils.AddTest("Try get size of full vector {-1, x, Sin[t]} ... ", v2.Size);
            TestUtils.AddTest("Try get 1st component of full vector {-1, x, Sin[t]} ... ", v2[0]);
            TestUtils.AddTest("Try get 2nd component of full vector {-1, x, Sin[t]} ... ", v2[1]);
            TestUtils.AddTest("Try get 3rd component of full vector {-1, x, Sin[t]} ... ", v2[2]);

            TestUtils.AddTest("Try get size of sparse vector {Pi, 0, 0, 0, -1} ... ", sv3.Size);
            TestUtils.AddTest("Try get 1st component of sparse vector {Pi, 0, 0, 0, -1} ... ", sv3[0]);
            TestUtils.AddTest("Try get 3rd component of sparse vector {Pi, 0, 0, 0, -1} ... ", sv3[2]);
            TestUtils.AddTest("Try get 5th component of sparse vector {Pi, 0, 0, 0, -1} ... ", sv3[4]);

            TestUtils.AddTest("Try list components of full vector {-1, x, Sin[t]}", "");
            var i = 0;

            foreach (var scalar in v2)
            {
                TestUtils.AddTest("   Component " + i + " ... ", scalar);
                i++;
            }

            TestUtils.AddTest("Try list components of sparse vector {Pi, 0, 0, 0, -1}", "");
            i = 0;
            foreach (var scalar in sv3)
            {
                TestUtils.AddTest("   Component " + i + " ... ", scalar);
                i++;
            }

            TestUtils.AddTest("Try negate vector {-1, x, Sin[t]} ... ", -v2);
            TestUtils.AddTest("Try add vectors {0, 1, 2 Pi} and {-1, x, Sin[t]} ... ", v1 + v2);
            TestUtils.AddTest("Try subtract vectors {0, 1, 2 Pi} and {-1, x, Sin[t]} ... ", v1 - v2);
            TestUtils.AddTest("Try find dot product of {0, 1, 2 Pi} and {-1, x, Sin[t]} ... ", v1 * v2);
            TestUtils.AddTest("Try find product of vector {-1, x, Sin[t]} with scalar x ... ", v2 * s);
            TestUtils.AddTest("Try find product of scalar x with vector {-1, x, Sin[t]} ... ", s * v2);
            TestUtils.AddTest("Try divide vector {-1, x, Sin[t]} by scalar x ... ", v2 / s);

            TestUtils.AddTest("Try apply Norm to vector {0, 1, 2 Pi} ... ", v1.Norm());
            TestUtils.AddTest("Try apply Norm2 to vector {0, 1, 2 Pi} ... ", v1.Norm2());

            var m = MathematicaMatrix.CreateFullDiagonalMatrix(Cas, Cas.Constants.One, Cas.Constants.Pi, Cas.Constants.MinusOne);

            TestUtils.AddTest("Try find product of vector {-1, x, Sin[t]} with matrix DiagonalMatrix[{1, Pi, -1}] ... ", v2.Times(m));

            TestUtils.AddTest("Try apply ToMathematicaVector() to full vector {0, 1, 2 Pi} ... ", v1.ToMathematicaVector());
            TestUtils.AddTest("Try apply ToMathematicaFullVector() to full vector {0, 1, 2 Pi} ... ", v1.ToMathematicaFullVector());
            TestUtils.AddTest("Try apply ToMathematicaSparseVector() to full vector {0, 1, 2 Pi} ... ", v1.ToMathematicaSparseVector());

            TestUtils.AddTest("Try apply ToMathematicaVector() to sparse vector {Pi, 0, 0, 0, -1} ... ", sv3.ToMathematicaVector());
            TestUtils.AddTest("Try apply ToMathematicaFullVector() to sparse vector {Pi, 0, 0, 0, -1} ... ", sv3.ToMathematicaFullVector());
            TestUtils.AddTest("Try apply ToMathematicaSparseVector() to sparse vector {Pi, 0, 0, 0, -1} ... ", sv3.ToMathematicaSparseVector());

            TestUtils.AddTestCompletionMessage("MathematicaVector Basic Operations Test Completed.");
        }