/// <summary> /// /// </summary> private static IEnumerable <Vector2d> IntegrateFromRK2(IField2d <Vector2d> field, Vector2d point, double stepSize) { while (true) { var v0 = field.ValueAt(point); var v1 = field.ValueAt(point + v0 * stepSize); point += (v0 + v1) * 0.5 * stepSize; yield return(point); } }
/// <summary> /// /// </summary> private static IEnumerable <Vector2d> IntegrateFromRK4(IField2d <Vector2d> field, Vector2d point, double stepSize) { double dt2 = stepSize * 0.5; double dt6 = stepSize / 6.0; while (true) { var v0 = field.ValueAt(point); var v1 = field.ValueAt(point + v0 * dt2); var v2 = field.ValueAt(point + v1 * dt2); var v3 = field.ValueAt(point + v2 * stepSize); point += (v0 + 2.0 * v1 + 2.0 * v2 + v3) * dt6; yield return(point); } }
/// <summary> /// /// </summary> private static IEnumerable <Vector2d> IntegrateFromEuler(IField2d <Vector2d> field, Vector2d point, double stepSize) { while (true) { point += field.ValueAt(point) * stepSize; yield return(point); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="U"></typeparam> /// <param name="field"></param> /// <param name="other"></param> /// <param name="converter"></param> /// <param name="parallel"></param> public static void Sample <T, U>(this IDiscreteField2d <T> field, IField2d <U> other, Func <U, T> converter, bool parallel = false) { if (parallel) { Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2)); } else { Body(0, field.Count); } void Body(int from, int to) { var vals = field.Values; for (int i = from; i < to; i++) { vals[i] = converter(other.ValueAt(field.CoordinateAt(i))); } } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="U"></typeparam> /// <param name="field"></param> /// <param name="other"></param> /// <param name="converter"></param> /// <param name="parallel"></param> public static void Sample <T, U>(this IDiscreteField2d <T> field, IField2d <U> other, Func <U, T> converter, bool parallel = false) { var vals = field.Values; Action <Tuple <int, int> > body = range => { for (int i = range.Item1; i < range.Item2; i++) { vals[i] = converter(other.ValueAt(field.CoordinateAt(i))); } }; if (parallel) { Parallel.ForEach(Partitioner.Create(0, field.Count), body); } else { body(Tuple.Create(0, field.Count)); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="field"></param> /// <param name="other"></param> /// <param name="parallel"></param> public static void Sample <T>(this ISampledField2d <T> field, IField2d <T> other, bool parallel = false) { if (parallel) { Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2)); } else { Body(0, field.Count); } void Body(int from, int to) { var vals = field.Values; for (int i = from; i < to; i++) { vals[i] = other.ValueAt(field.PointAt(i)); } } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="transform"></param> /// <param name="other"></param> /// <returns></returns> public static IField2d <T> CreateTransformed <T>(IField2d <T> other, Transform2d transform) { transform.Invert(); return(Create(p => other.ValueAt(transform.Apply(p)))); }