Esempio n. 1
0
        private static SymbolicVector Add(Vector vector, Diffable scalar)
        {
            Symbolic[] output = new Symbolic[vector.Rank];

            for (int i = 0; i < vector.Rank; i++)
            {
                output[i] = vector._values[i] + scalar;
            }

            return(new SymbolicVector(output));
        }
Esempio n. 2
0
        public static Symbolic Min(Diffable a, Diffable b)
        {
            return(new Symbolic(
                       (Context context) =>
            {
                return System.Math.Min(a.Value(context), b.Value(context));
            },
                       (Context context) =>
            {
                var grads = new Dictionary <Variable, float>();

                bool aLessThanB = (a.Value(context) < b.Value(context));
                foreach (var grad in a.Grads(context))
                {
                    if (aLessThanB)
                    {
                        grads.Add(grad.Key, grad.Value);
                    }
                    else
                    {
                        grads.Add(grad.Key, 0);
                    }
                }
                foreach (var grad in b.Grads(context))
                {
                    if (grads.ContainsKey(grad.Key))
                    {
                        if (!aLessThanB)
                        {
                            grads[grad.Key] = grad.Value;
                        }
                    }
                    else
                    {
                        if (aLessThanB)
                        {
                            grads.Add(grad.Key, 0);
                        }
                        else
                        {
                            grads.Add(grad.Key, grad.Value);
                        }
                    }
                }
                return grads;
            }
                       ));
        }
Esempio n. 3
0
 public static Symbolic Reciprocal(Diffable x)
 {
     return(new Symbolic(
                (Context context) =>
     {
         return 1 / x.Value(context);
     },
                (Context context) =>
     {
         var grads = new Dictionary <Variable, float>();
         var negoneoverx = -1 / (x.Value(context) * x.Value(context));
         foreach (var grad in x.Grads(context))
         {
             grads.Add(grad.Key, grad.Value * negoneoverx);
         }
         return grads;
     }
                ));
 }
Esempio n. 4
0
 public static Symbolic Abs(Diffable x)
 {
     return(new Symbolic(
                (Context context) =>
     {
         return System.Math.Abs(x.Value(context));
     },
                (Context context) =>
     {
         var grads = new Dictionary <Variable, float>();
         var valuesign = System.Math.Sign(x.Value(context));
         foreach (var grad in x.Grads(context))
         {
             grads[grad.Key] = grad.Value * valuesign;
         }
         return grads;
     }
                ));
 }
Esempio n. 5
0
 public static Symbolic Exp(Diffable x)
 {
     return(new Symbolic(
                (Context context) =>
     {
         return (float)System.Math.Exp(x.Value(context));
     },
                (Context context) =>
     {
         var grads = new Dictionary <Variable, float>();
         var expvalue = (float)System.Math.Exp(x.Value(context));
         foreach (var grad in x.Grads(context))
         {
             grads[grad.Key] = grad.Value * expvalue;
         }
         return grads;
     }
                ));
 }
Esempio n. 6
0
 public static Symbolic Sqrt(Diffable x)
 {
     return(new Symbolic(
                (Context context) =>
     {
         return (float)System.Math.Sqrt(x.Value(context));
     },
                (Context context) =>
     {
         var grads = new Dictionary <Variable, float>();
         float twosqrtx = 2 * (float)System.Math.Sqrt(x.Value(context));
         foreach (var grad in x.Grads(context))
         {
             grads[grad.Key] = grad.Value / twosqrtx;
         }
         return grads;
     }
                ));
 }
Esempio n. 7
0
 public bool ContainsGrads(Diffable diffable)
 {
     return(grads.ContainsKey(diffable));
 }
Esempio n. 8
0
 public bool ContainsValue(Diffable diffable)
 {
     return(values.ContainsKey(diffable));
 }
Esempio n. 9
0
 public static Symbolic Pow(Diffable x, Diffable p)
 {
     return(Exp(Log(x) * p));
 }
Esempio n. 10
0
 public static Symbolic Square(Diffable x)
 {
     return(x * x);
 }