Exemplo n.º 1
0
        public static Aad Tan(Aad x)
        {
            var res = new Aad(() => Math.Tan(x.Value));

            if (x.IsVariable)
            {
                AadCalculationStack.Instance.Add(x.Index, res.Index, () => 1.0 + Math.Tan(x.Value) * Math.Tan(x.Value));
            }
            return(res);
        }
Exemplo n.º 2
0
        public static Aad operator *(Aad left, Aad right)
        {
            var res = new Aad(() => left.Value * right.Value);

            if (left.IsVariable)
            {
                AadCalculationStack.Instance.Add(left.Index, res.Index, () => right.Value);
            }
            if (right.IsVariable)
            {
                AadCalculationStack.Instance.Add(right.Index, res.Index, () => left.Value);
            }
            return(res);
        }
Exemplo n.º 3
0
        public static Aad Max(Aad lhs, Aad rhs)
        {
            var res = new Aad(() => Math.Max(lhs.Value, rhs.Value));

            if (lhs.IsVariable)
            {
                AadCalculationStack.Instance.Add(lhs.Index, res.Index, () => lhs.Value > rhs.Value ? 1.0 : 0.0);
            }
            if (rhs.IsVariable)
            {
                AadCalculationStack.Instance.Add(rhs.Index, res.Index, () => rhs.Value > lhs.Value ? 1.0 : 0.0);
            }
            return(res);
        }
Exemplo n.º 4
0
        public double Derivative(Aad variable)
        {
            if (!variable.IsVariable)
            {
                return(0.0);
            }
            var stack = AadCalculationStack.Instance;
            var data  = stack.Data.ToArray();
            var vals  = new double[Index + 1];

            vals[Index] = 1.0;
            for (var i = 1; i <= data.Length; ++i)
            {
                var derivData = data[data.Length - i];
                if (derivData.DerivTarget <= Index)
                {
                    vals[derivData.DerivBy] += derivData.Value * vals[derivData.DerivTarget];
                }
            }
            return(vals[variable.Index]);
        }