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)); }
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; } )); }
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; } )); }
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; } )); }
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; } )); }
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; } )); }
public bool ContainsGrads(Diffable diffable) { return(grads.ContainsKey(diffable)); }
public bool ContainsValue(Diffable diffable) { return(values.ContainsKey(diffable)); }
public static Symbolic Pow(Diffable x, Diffable p) { return(Exp(Log(x) * p)); }
public static Symbolic Square(Diffable x) { return(x * x); }