Ejemplo n.º 1
0
        /// <summary>
        /// Activate record zone
        /// </summary>
        /// <param name="sender">origin</param>
        /// <param name="e">args</param>
        private void Owner_Fetch(object sender, EventArgs e)
        {
            RecordZone <Weight> recordZone = sender as RecordZone <Weight>;
            int h = this[hashCodeName];

            foreach (Weight w in recordZone.Records)
            {
                if (w == this)
                {
                    h = w.GetHashCode();
                    break;
                }
            }
            if (!recordZone.Exists(h))
            {
                recordZone.Add(h, this);
            }
            else
            {
                Weight recorded = recordZone.Ask(h);
                if (recorded != this)
                {
                    throw new InvalidCastException(String.Format("Weight {0} is not equals to Weight {1}", recorded.ToString(), this.ToString()));
                }
                else
                {
                    this.ownerObject   = recorded.ownerObject;
                    this[hashCodeName] = h;
                }
            }
        }
Ejemplo n.º 2
0
 public LoanService(ICustomerRepository repository, IPlan plan,
                    IArithmetic arithmetic)
 {
     _arithmetic = arithmetic;
     _repository = repository;
     _plan       = plan;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Converts all sub-variables into an equation
 /// or into its value
 /// </summary>
 /// <returns>output new equation</returns>
 public override IArithmetic Converting()
 {
     if (this.IsVariableExists(this.Name))
     {
         IArithmetic f = this.GetVariable(this.Name);
         if (f is Function)
         {
             Function fe = f as Function;
             for (int index = 0; index < fe.Parameters.Count; ++index)
             {
                 if (fe.Parameters[index] is Coefficient)
                 {
                     Coefficient coeff = fe.Parameters[index] as Coefficient;
                     this.AddVariable(coeff.Name, this.Parameters[index].Converting());
                 }
                 if (fe.Parameters[index] is UnknownTerm)
                 {
                     UnknownTerm xt = fe.Parameters[index] as UnknownTerm;
                     this.AddVariable(xt.Name, this.Parameters[index].Converting());
                 }
                 if (fe.Parameters[index] is Function)
                 {
                     Function fun = fe.Parameters[index] as Function;
                     this.AddVariable(fun.Name, this.Parameters[index].Converting());
                 }
             }
             return(fe.RightOperand.Converting());
         }
     }
     return(this);
 }
        /// <summary>
        /// Sum
        /// </summary>
        /// <param name="b">b coeff</param>
        /// <param name="xLeft">xLeft</param>
        /// <param name="xRight">xright</param>
        /// <returns>sum</returns>
        public double Sum(double b, double xLeft, double xRight)
        {
            IArithmetic function = ("(2*X0 + b + 1) * (1)").ToArithmetic();

            function.Let("b", b);
            double sum = 0;

            function.Let("Zero", xLeft);
            // si b est pair
            double start;

            if (b % 2 == 0)
            {
                start = ("-(b+1)/2").ToArithmetic().Converting().ToDouble();
            }
            else
            {
                start = ("-b/2").ToArithmetic().Converting().ToDouble();
            }
            for (double x = start; x < xRight; ++x)
            {
                function.Let("X0", x);
                sum = sum + function.Converting().ToDouble();
            }
            return(sum);
        }
 /// <summary>
 /// Create an equation
 /// with a given operator
 /// and one operand
 /// </summary>
 /// <param name="op">operator name</param>
 /// <param name="e">operand</param>
 public MathematicEquation(string op, IEquation e)
 {
     if (op == OperatorPositive)
     {
         IArithmetic eqTemp = Arithmetic.EnsureSign(e.Equation);
         this.eq = new Positive(eqTemp);
     }
     else if (op == OperatorNegative)
     {
         IArithmetic eqTemp = Arithmetic.EnsureSign(e.Equation);
         this.eq = new Negative(eqTemp);
     }
     else if (op == OperatorInverse)
     {
         IArithmetic eqTemp = Arithmetic.EnsureInverse(e.Equation);
         this.eq = new Inverse(eqTemp);
     }
     else if (op == OperatorMultipleAdd)
     {
         this.eq = Arithmetic.EnsureSum(e.Equation, 1);
     }
     else if (op == OperatorMultipleProduct)
     {
         this.eq = Arithmetic.EnsureProduct(e.Equation, 1);
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Ejemplo n.º 6
0
        private static T GreatestCommonDivisor <T>(T one, T two, IArithmetic <T> arithmetic)
        {
            T big       = arithmetic.Max(arithmetic.Abs(one), arithmetic.Abs(two));
            T small     = arithmetic.Min(arithmetic.Abs(one), arithmetic.Abs(two));
            T remainder = arithmetic.Modulo(big, small);

            return(arithmetic.Equals(remainder, arithmetic.Zero) ? small : AdvancedMath.GreatestCommonDivisor(small, remainder, arithmetic));
        }
        /// <summary>
        /// Differential DY
        /// </summary>
        /// <param name="y">y</param>
        /// <param name="y0">y0</param>
        /// <returns>value</returns>
        public double Differential(double y, double y0)
        {
            IArithmetic function = Polynome2.functions["DY"];

            function.Let("Y", y);
            function.Let("Y_0", y0);
            return(function.Converting().ToDouble());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Replace an element to an another
        /// </summary>
        /// <param name="old">old element</param>
        /// <param name="n">new element</param>
        public void Replace(IArithmetic old, IArithmetic n)
        {
            int p = this.List.IndexOf(old);

            if (p != -1)
            {
                this.List[p] = n;
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets Base X
        /// </summary>
        /// <param name="size">size of X</param>
        /// <returns>arithmetic function</returns>
        public IArithmetic GetBaseX(int size)
        {
            Base X = new Base(10, size);
            // creates a polynome for X
            GrobnerPolynome p = new GrobnerPolynome(X.Dimension - 1);
            IArithmetic     a = p.PolynomeAsBase("n", "B");

            return(a);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Construction of elements
 /// with a dynamic element (not working in constructor's function)
 /// </summary>
 /// <param name="type">value type</param>
 /// <param name="op">operator</param>
 /// <param name="value">value</param>
 /// <param name="owner">owner object</param>
 protected void Construct(byte type, char op, dynamic value, IArithmetic owner)
 {
     this[typeValueName]          = type;
     this[arithmeticOperatorName] = op;
     this[valueName]    = value;
     this[hashCodeName] = owner.GetHashCode();
     this.ownerObject   = owner;
     owner.Fetch       += Owner_Fetch;
     owner.Unfetch     += Owner_Unfetch;
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Compute Y0 value
        /// </summary>
        /// <param name="x0">X0 value input</param>
        /// <returns>Y0 value output</returns>
        public double ComputeY0(double x0)
        {
            IArithmetic function = Polynome2.functions["Y_0"];

            this.TermX0 = x0;
            function.Let("b", this.CoefficientB);
            function.Let("c", this.CoefficientC);
            function.Let("X_0", this.TermX0);
            return(function.Converting().Compute().ToDouble());
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Differential DX
        /// </summary>
        /// <param name="dx">dx</param>
        /// <param name="x0">x0</param>
        /// <returns>value</returns>
        public double DifferentialDX(double dx, double x0)
        {
            IArithmetic function = ("DX*[DX+Y']").ToArithmetic();
            IArithmetic yPrime   = ("2*X_0 + b").ToArithmetic();

            function.Let("b", this.CoefficientB);
            function.Let("c", this.CoefficientC);
            function.Let("Y'", yPrime);
            function.Let("X_0", x0);
            return(function.Converting().ToDouble());
        }
 /// <summary>
 /// Select all terms accordingly with model
 /// </summary>
 /// <param name="model">model to search</param>
 /// <returns>list of elements</returns>
 public override IEnumerable <IArithmetic> Select(IArithmetic model)
 {
     if (this.Value.ToString() == model.ToString())
     {
         yield return(model);
     }
     else
     {
         yield break;
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Differential DY
        /// </summary>
        /// <param name="y">y</param>
        /// <param name="y0">y0</param>
        /// <param name="b">coefficient b</param>
        /// <param name="c">coefficient c</param>
        /// <returns>value</returns>
        public double Differential(double y, double y0, double b, double c)
        {
            IArithmetic function = Polynome2.functions["DY"];

            this.CoefficientB = b;
            this.CoefficientC = c;
            function.Let("b", this.CoefficientB);
            function.Let("c", this.CoefficientC);
            function.Let("Y", y);
            function.Let("Y_0", y0);
            return(function.Converting().ToDouble());
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 分别随机符号位作成左边关系式和右边关系式
        /// </summary>
        /// <param name="maximumLimit">最大值</param>
        /// <param name="signFunc">運算符取得用的表達式</param>
        /// <returns>計算式</returns>
        private Formula GetFormulaForRandomNumber(int maximumLimit, Func <SignOfOperation> signFunc)
        {
            // 對四則運算符實例進行cache管理
            IArithmetic strategy = CalculateManager(signFunc());

            return(strategy.CreateFormula(new ArithmeticParameter()
            {
                MaximumLimit = maximumLimit,
                QuestionType = QuestionType.Default,
                MinimumLimit = 0
            }));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Gets Base Y
        /// </summary>
        /// <param name="y">y value</param>
        /// <returns>arithmetic function</returns>
        public IArithmetic GetBaseY(double y)
        {
            Base Y = new Base(10, Base.ConvertToReadable(y, 10));
            // creates a polynome for X
            GrobnerPolynome p = new GrobnerPolynome(Y.Dimension - 1);
            IArithmetic     a = p.PolynomeAsBase("p", "X");

            // gestion variables
            Dictionary <string, IArithmetic> variables = new Dictionary <string, IArithmetic>();

            Arithmetic.EventAddVariable += new EventHandler <KeyValuePair <string, IArithmetic> >((o, e) =>
            {
                if (e.Value != null)
                {
                    if (variables.ContainsKey(e.Key))
                    {
                        variables[e.Key] = e.Value;
                    }
                    else
                    {
                        variables.Add(e.Key, e.Value);
                    }
                }
                else
                {
                    if (variables.ContainsKey(e.Key))
                    {
                        variables.Remove(e.Key);
                    }
                }
            });
            Arithmetic.EventGetVariable = new Func <string, IArithmetic>((s) =>
            {
                if (variables.ContainsKey(s))
                {
                    return(variables[s]);
                }
                else
                {
                    return(null);
                }
            });


            for (int index = Y.Dimension - 1; index >= 0; --index)
            {
                a.Let("p_" + index.ToString(), Y.Vector.ElementAt(index));
            }
            a.Let("X", this.GetBaseX(3));

            return(a.Converting());
        }
        /// <summary>
        /// Calculate the result of this equation
        /// terms that are valued are operated with its numeric value
        /// </summary>
        /// <param name="clean">true if calculate again</param>
        /// <returns>string representation number or algebraic</returns>
        public string Calculate(bool clean)
        {
            IArithmetic output = this.eq.Compute();

            if (output.IsDouble())
            {
                return(output.ToDouble().ToString());
            }
            else
            {
                return(output.ToString());
            }
        }
Ejemplo n.º 18
0
        public static void InitializeArithmetic(IArithmetic <T> implementation)
        {
            if (Implementation != null)
            {
                throw new InvalidOperationException("Arithmetic for type " + typeof(T).FullName + " has already been initialized.");
            }

            if (implementation == null)
            {
                throw new InvalidOperationException("Cannot initalize arithmetic for type " + typeof(T).FullName + " with null.");
            }

            Implementation = implementation;
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 利用算法进行异步计算
        /// </summary>
        /// <param name="arithIden">算法标识</param>
        /// <param name="rawData">原始数据</param>
        /// <returns>带计算结果返回值的异步任务</returns>
        private Task <IList <Data> > CalcViaArithmeticAsyn(string arithIden, IList <Data> rawData)
        {
            Task <IList <Data> > t = new Task <IList <Data> >(() =>
            {
                // 获取算法
                IArithmetic arith = ArithmeticFactory.CreateArithmetic(arithIden);
                // 计算
                IList <Data> calculatedData = null /*arith.Calculate(rawData)*/;

                return(calculatedData);
            });

            return(t);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// 右側計算式集合作成并返回當前新作成的計算式
        /// </summary>
        /// <param name="maximumLimit">計算結果最大值</param>
        /// <param name="leftFormulaAnswer">左側新作成計算式的結果值</param>
        /// <param name="signFunc">運算符取得用的表達式</param>
        /// <returns>新作成的計算式</returns>
        private Formula MakeRightFormulas(int maximumLimit, int leftFormulaAnswer, Func <SignOfOperation> signFunc)
        {
            IArithmetic strategy = CalculateManager(signFunc());

            // 計算式作成(依據左邊算式的答案推算右邊的算式)
            Formula formula = strategy.CreateFormulaWithAnswer(new ArithmeticParameter()
            {
                MaximumLimit = maximumLimit,
                QuestionType = QuestionType.Default,
                MinimumLimit = 0
            }, leftFormulaAnswer);

            return(formula);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 左側計算式集合作成并返回當前新作成的計算式
        /// </summary>
        /// <param name="maximumLimit">計算結果最大值</param>
        /// <param name="signFunc">運算符取得用的表達式</param>
        /// <returns>新作成的計算式</returns>
        private Formula MakeLeftFormulas(int maximumLimit, Func <SignOfOperation> signFunc)
        {
            IArithmetic strategy = CalculateManager(signFunc());

            // 計算式作成
            Formula formula = strategy.CreateFormula(new ArithmeticParameter()
            {
                MaximumLimit = maximumLimit,
                QuestionType = QuestionType.Default,
                MinimumLimit = 0
            });

            return(formula);
        }
Ejemplo n.º 22
0
 /// <summary>
 /// Select all terms accordingly with model
 /// </summary>
 /// <param name="model">model to search</param>
 /// <returns>list of elements</returns>
 public override IEnumerable <IArithmetic> Select(IArithmetic model)
 {
     if (model.ToString() == this.InnerOperand.ToString())
     {
         yield return(this.InnerOperand);
     }
     else
     {
         foreach (IArithmetic s in this.InnerOperand.Select(model))
         {
             yield return(s);
         }
     }
 }
Ejemplo n.º 23
0
        /// <summary>
        /// Compute Y0 value
        /// </summary>
        /// <param name="x0">X0 value input</param>
        /// <param name="b">b coefficient input</param>
        /// <param name="c">c coefficient input</param>
        /// <param name="d">d coefficient input</param>
        /// <returns>Y0 value output</returns>
        public double ComputeY0(double x0, double b, double c, double d)
        {
            IArithmetic function = Polynome3.functions["Y_0"];

            this.TermX0       = x0;
            this.CoefficientB = b;
            this.CoefficientC = c;
            this.CoefficientD = d;
            function.Let("b", this.CoefficientB);
            function.Let("c", this.CoefficientC);
            function.Let("d", this.CoefficientD);
            function.Let("X_0", this.TermX0);
            return(function.Converting().Compute().ToDouble());
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CalculatorBot"/> class.
 /// </summary>
 /// <param name="configuration">The current configuration.</param>
 /// <param name="arithmetic">Arithmetic operations DI.</param>
 /// <param name="calcChatBot">Calculator Chat Bot methods DI.</param>
 /// <param name="telemetryClient">ApplicationInsights DI.</param>
 /// <param name="statistics">Statistic operations DI.</param>
 /// <param name="geometrics">Geometric operations DI.</param>
 public CalculatorBot(
     IConfiguration configuration,
     IArithmetic arithmetic,
     ICalcChatBot calcChatBot,
     TelemetryClient telemetryClient,
     IStatistic statistics,
     IGeometric geometrics)
 {
     this.configuration   = configuration;
     this.arithmetic      = arithmetic;
     this.telemetryClient = telemetryClient;
     this.calcChatBot     = calcChatBot;
     this.statistics      = statistics;
     this.geometrics      = geometrics;
 }
Ejemplo n.º 25
0
        /// <summary>
        /// Compute R1
        /// </summary>
        /// <returns>return R1</returns>
        public double ComputeR1()
        {
            IArithmetic function = Polynome3.functions["R_1"];

            function.Let("b", this.CoefficientB);
            function.Let("c", this.CoefficientC);
            function.Let("d", this.CoefficientD);
            function.Let("X_1", this.TermX1);
            function.Let("X_2", this.TermX2);
            function.Let("V", this.TermV);
            function.Let("Y", this.TermY);
            function.Let("A", Polynome3.functions["A"].Converting());
            function.Let("U", Polynome3.functions["U"].Converting());
            return(function.Converting().Compute().ToDouble());
        }
        /// <summary>
        /// 算式作成
        /// </summary>
        /// <param name="p">題型參數</param>
        /// <param name="signFunc">運算符取得用的表達式</param>
        private void MarkFormulaList(ComputingConnectionParameter p, Func <SignOfOperation> signFunc)
        {
            // 當前反推判定次數(一次推算內次數累加)
            int defeated = 0;

            // 按照指定數量作成相應的數學計算式
            for (int i = 0; i < p.NumberOfQuestions; i++)
            {
                Formula         previousFormula = null;
                IList <Formula> formulas        = new List <Formula>();
                // 随机获取接龙层数
                for (int j = 0; j < p.SectionNumber; j++)
                {
                    // 指定運算符實例作成
                    IArithmetic strategy = CalculateManager(signFunc());
                    Formula     formula  = strategy.CreateFormula(new ArithmeticParameter()
                    {
                        MaximumLimit = p.MaximumLimit,
                        QuestionType = QuestionType.Default,
                        MinimumLimit = 0
                    }, previousFormula);

                    if (CheckIsNeedInverseMethod(p.Formulas, formula, p.MaximumLimit))
                    {
                        defeated++;
                        // 如果大於五次則認為此題無法作成繼續下一題
                        if (defeated == INVERSE_NUMBER)
                        {
                            // 當前反推判定次數復原
                            defeated = 0;
                            continue;
                        }
                        j--;
                        continue;
                    }
                    previousFormula = formula;

                    formulas.Add(formula);
                }
                // 當前反推判定次數復原
                defeated = 0;

                p.Formulas.Add(new ConnectionFormula()
                {
                    ConfixFormulas = formulas, ConfixNumber = formulas.Count
                });
            }
        }
        /// <summary>
        /// 算式作成
        /// </summary>
        /// <param name="p">題型參數</param>
        /// <param name="signFunc">運算符取得用的表達式</param>
        private void MarkFormulaList(ArithmeticOperationsParameter p, Func <SignOfOperation> signFunc)
        {
            // 當前反推判定次數(一次推算內次數累加)
            int defeated = 0;

            // 按照指定數量作成相應的數學計算式
            for (var i = 0; i < p.NumberOfQuestions; i++)
            {
                IArithmetic strategy = CalculateManager(signFunc());
                Formula     formula  = strategy.CreateFormula(new ArithmeticParameter()
                {
                    MaximumLimit = p.MaximumLimit,
                    QuestionType = p.QuestionType,
                    MinimumLimit = 0,
                    LeftScope    = p.LeftScope,
                    RightScope   = p.RightScope
                });

                // 填空項目設定
                formula.Gap = CommonUtil.GetRandomNumber(GapFilling.Left, GapFilling.Right);

                // 判定是否需要反推并重新作成計算式
                if (CheckIsNeedInverseMethod(p, formula))
                {
                    defeated++;
                    // 如果大於五次則認為此題無法作成繼續下一題
                    if (defeated == INVERSE_NUMBER)
                    {
                        // 當前反推判定次數復原
                        defeated = 0;
                        continue;
                    }
                    i--;
                    continue;
                }

                // 計算式作成
                p.Formulas.Add(new ArithmeticOperationsFormula
                {
                    // 四則運算式
                    Arithmetic = formula,
                    // 等式值是不是出現在右邊(如果未指定則隨機產生)
                    AnswerIsRight = p.AnswerIsRight ?? IsRight
                });

                defeated = 0;
            }
        }
Ejemplo n.º 28
0
        /// <summary>
        /// 创建算法对象
        /// </summary>
        /// <param name="arithIden">算法标记</param>
        /// <returns>算法对象</returns>
        public static IArithmetic CreateArithmetic(string arithIden)
        {
            IArithmetic arith = null;

//             switch (arithIden.ToLower())
//             {
//                 case "group":
//                     arith = new CxGroupArithmetic();
//                     break;
//                 case "average":
//                     arith = new AverageArithmetic();
//                     break;
//             }

            return(arith);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Compute A value
        /// </summary>
        /// <param name="y">y search input</param>
        /// <param name="x1">x1 input value</param>
        /// <param name="x2">x2 input value</param>
        /// <param name="v">v input value</param>
        /// <returns>A value</returns>
        public double ComputeA(double y, double x1, double x2, double v)
        {
            IArithmetic function = Polynome3.functions["A"];

            this.TermY  = y;
            this.TermX1 = x1;
            this.TermX2 = x2;
            this.TermV  = v;
            function.Let("b", this.CoefficientB);
            function.Let("c", this.CoefficientC);
            function.Let("d", this.CoefficientD);
            function.Let("X_1", this.TermX1);
            function.Let("X_2", this.TermX2);
            function.Let("V", this.TermV);
            return(function.Converting().Compute().ToDouble());
        }
Ejemplo n.º 30
0
 /// <summary>
 /// Initializes new instance of this type.
 /// </summary>
 /// <param name="implementation"><see cref="MethodCacheBase{TImplementation}.Implementation"/> property value.</param>
 public Arithmetic(IArithmetic <T> implementation)
     : base(implementation)
 {
     Provider   = Implementation.Provider;
     Zero       = Implementation.Zero;
     One        = Implementation.One;
     MaxValue   = Implementation.MaxValue;
     MinValue   = Implementation.MinValue;
     IsSigned   = Implementation.IsSigned;
     Add        = Implementation.Add;
     Subtract   = Implementation.Subtract;
     Multiply   = Implementation.Multiply;
     Divide     = Implementation.Divide;
     Negation   = Implementation.Negation;
     ApplyRules = Implementation.ApplyRules;
 }