Exemple #1
0
        public override Operand EvaluationStep(Context context)
        {
            Operand operand = base.EvaluationStep(context);

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

            // To avoid infinite evaluation looping, we must apply...
            //   1) vB = v.B + v^B, and
            //   2) v^B = vB - v.B,
            // ...according to rules that dictate when and where they're appropriate.
            // Also to avoid infinite looping, the distributive property must take
            // precedence over anything we do here.

            // All reduction cases must be eliminated before it is safe to handle the expansion cases.
            for (int i = 0; i < operandList.Count - 1; i++)
            {
                Blade bladeA = operandList[i] as Blade;
                Blade bladeB = operandList[i + 1] as Blade;

                if (bladeA != null && bladeB != null && bladeA.Grade > 1 && bladeB.Grade > 1)
                {
                    GeometricProduct geometricProduct;

                    if (context.useOperandCache)
                    {
                        geometricProduct = new GeometricProduct(new List <Operand>()
                        {
                            new Blade(new NumericScalar(1.0), bladeA.vectorList.ToList()), new Blade(new NumericScalar(1.0), bladeB.vectorList.ToList())
                        });
                        string  key          = geometricProduct.Print(Format.PARSEABLE, context);
                        Operand cachedResult = null;
                        if (!context.operandCache.GetStorage(key, ref cachedResult))
                        {
                            context.useOperandCache = false;
                            cachedResult            = Operand.ExhaustEvaluation(geometricProduct, context);
                            context.useOperandCache = true;
                            context.operandCache.SetStorage(key, cachedResult);
                        }

                        return(new GeometricProduct(new List <Operand>()
                        {
                            bladeA.scalar, bladeB.scalar, cachedResult
                        }));
                    }

                    // Here our choice of which blade to reduce is arbitrary from a stand-point of correctness.
                    // However, we might converge faster by choosing the blade with smaller grade.
                    // Note there is also something arbitrary about how we're reducing the blades.
                    int   j        = bladeA.Grade <= bladeB.Grade ? i : i + 1;
                    Blade blade    = operandList[j] as Blade;
                    Blade subBlade = blade.MakeSubBlade(0);
                    Blade vector   = new Blade(blade.vectorList[0]);
                    geometricProduct = new GeometricProduct(new List <Operand>()
                    {
                        vector, subBlade
                    });
                    InnerProduct innerProduct = new InnerProduct(new List <Operand>()
                    {
                        vector.Copy(), subBlade.Copy()
                    });
                    operandList[j] = new Sum(new List <Operand>()
                    {
                        geometricProduct, new GeometricProduct(new List <Operand>()
                        {
                            new NumericScalar(-1.0), innerProduct
                        })
                    });
                    return(this);
                }
            }

            // All reduction cases eliminated, it is now safe to handle some expansion cases.
            for (int i = 0; i < operandList.Count - 1; i++)
            {
                Blade bladeA = operandList[i] as Blade;
                Blade bladeB = operandList[i + 1] as Blade;

                if (bladeA == null || bladeB == null)
                {
                    continue;
                }

                if ((bladeA.Grade == 1 && bladeB.Grade > 1) || (bladeA.Grade > 1 && bladeB.Grade == 1))
                {
                    InnerProduct innerProduct = new InnerProduct(new List <Operand>()
                    {
                        bladeA, bladeB
                    });
                    OuterProduct outerProduct = new OuterProduct(new List <Operand>()
                    {
                        bladeA.Copy(), bladeB.Copy()
                    });
                    operandList[i] = new Sum(new List <Operand>()
                    {
                        innerProduct, outerProduct
                    });
                    operandList.RemoveAt(i + 1);
                    return(this);
                }
            }

            // It is now safe to handle the remaining expansion cases.
            for (int i = 0; i < operandList.Count - 1; i++)
            {
                Blade bladeA = operandList[i] as Blade;
                Blade bladeB = operandList[i + 1] as Blade;

                if (bladeA == null || bladeB == null)
                {
                    continue;
                }

                if (bladeA.Grade == 1 && bladeB.Grade == 1)
                {
                    operandList.RemoveAt(i + 1);
                    GeometricProduct innerProduct = new GeometricProduct(new List <Operand>()
                    {
                        bladeA.scalar, bladeB.scalar, context.BilinearForm(bladeA.vectorList[0], bladeB.vectorList[0])
                    });
                    Blade outerProduct = new Blade(new GeometricProduct(new List <Operand>()
                    {
                        bladeA.scalar.Copy(), bladeB.scalar.Copy()
                    }));
                    outerProduct.vectorList.Add(bladeA.vectorList[0]);
                    outerProduct.vectorList.Add(bladeB.vectorList[0]);
                    operandList[i] = new Sum(new List <Operand>()
                    {
                        innerProduct, outerProduct
                    });
                    return(this);
                }
            }

            return(null);
        }
        public override Operand EvaluationStep(Context context)
        {
            Operand operand = base.EvaluationStep(context);

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

            if (operandList.Count == 2)
            {
                Blade bladeA = operandList[0] as Blade;
                Blade bladeB = operandList[1] as Blade;

                if (bladeA != null && bladeB != null)
                {
                    if (bladeA.Grade == 1 && bladeB.Grade == 1)
                    {
                        return(new GeometricProduct(new List <Operand>()
                        {
                            bladeA.scalar, bladeB.scalar, context.BilinearForm(bladeA.vectorList[0], bladeB.vectorList[0])
                        }));
                    }
                    else if (bladeA.Grade == 1 && bladeB.Grade > 1)
                    {
                        Sum sum = new Sum();

                        for (int i = 0; i < bladeB.vectorList.Count; i++)
                        {
                            Blade subBlade = bladeB.MakeSubBlade(i);
                            subBlade.scalar = new GeometricProduct(new List <Operand>()
                            {
                                new NumericScalar(i % 2 == 1 ? -1.0 : 1.0), bladeA.scalar, bladeB.scalar, context.BilinearForm(bladeA.vectorList[0], bladeB.vectorList[i])
                            });
                            sum.operandList.Add(subBlade);
                        }

                        return(sum);
                    }
                    else if (bladeA.Grade > 1 && bladeB.Grade == 1)
                    {
                        operandList[0] = bladeB;
                        operandList[1] = bladeA;

                        if (bladeA.Grade % 2 == 0)
                        {
                            operandList.Add(new NumericScalar(-1.0));
                        }

                        return(this);
                    }
                    else if (bladeA.Grade > 1 && bladeB.Grade > 1)
                    {
                        if (context.useOperandCache)
                        {
                            InnerProduct innerProduct = new InnerProduct(new List <Operand>()
                            {
                                new Blade(new NumericScalar(1.0), bladeA.vectorList.ToList()), new Blade(new NumericScalar(1.0), bladeB.vectorList.ToList())
                            });
                            string  key          = innerProduct.Print(Format.PARSEABLE, context);
                            Operand cachedResult = null;
                            if (!context.operandCache.GetStorage(key, ref cachedResult))
                            {
                                context.useOperandCache = false;
                                cachedResult            = Operand.ExhaustEvaluation(innerProduct, context);
                                context.useOperandCache = true;
                                context.operandCache.SetStorage(key, cachedResult);
                            }

                            return(new GeometricProduct(new List <Operand>()
                            {
                                bladeA.scalar, bladeB.scalar, cachedResult
                            }));
                        }

                        if (bladeA.Grade <= bladeB.Grade)
                        {
                            return(new InnerProduct(new List <Operand>()
                            {
                                bladeA.MakeSubBlade(bladeA.Grade - 1), new InnerProduct(new List <Operand>()
                                {
                                    new Blade(bladeA.vectorList[bladeA.Grade - 1]), bladeB
                                })
                            }));
                        }
                        else
                        {
                            return(new InnerProduct(new List <Operand>()
                            {
                                new InnerProduct(new List <Operand>()
                                {
                                    bladeA, new Blade(bladeB.vectorList[0])
                                }), bladeB.MakeSubBlade(0)
                            }));
                        }
                    }
                }
            }

            return(null);
        }