/// <summary> /// Interpolating a line in 2D /// </summary> /// <param name="startX">X coordinate of starting point</param> /// <param name="startY">Y coordinate of starting point</param> /// <param name="endX">X coordinate of ending point</param> /// <param name="endY">X coordinate of ending point</param> /// <param name="count">Count of points</param> /// <returns></returns> public static double[,] InterpolateLine2D(double startX, double startY, double endX, double endY, int count) { double[,] ret = new double[count, 2]; try { double[] xCoordinates = DistributionHelper.Subdivide(new double[2] { startX, endX }, count + 1); double[] yCoordinates = DistributionHelper.Subdivide(new double[2] { startY, endY }, count + 1); for (int i = 0; i < count; i++) { ret[i, 0] = xCoordinates[i]; ret[i, 1] = yCoordinates[i]; } } catch { } return(ret); }
/// <summary> /// Reproduces an initial distribution of a variable according to the empirical histogram /// </summary> /// <param name="mesh">Mesh</param> /// <param name="array">Value distribution</param> /// <param name="countBins">Count of bins</param> /// <returns></returns> public static void ReproduceDistribution(this Mesh mesh, double[] array, int countBins = 10) { try { //Subdividing into bins double[] bins = DistributionHelper.Subdivide(array, countBins); //Getting value counts for each bin int[] counts = DistributionHelper.Counts(array, bins); List <double> frequencies = new List <double>(); for (int i = 0; i < counts.Length; i++) { frequencies.Add((double)counts[i] / (double)array.Length); counts[i] = Convert.ToInt32(Math.Round(frequencies[i] * mesh.Vertices.Count())); } //Creating a list of vertex indices List <int> indices = new List <int>(); for (int i = 0; i < mesh.Vertices.Count(); i++) { indices.Add(i); } //Shuffling the list for random access indices.Shuffle(); //Assigning a random value in the range of the selected bin to each vertex for (int i = 0; i < bins.Length - 1; i++) { List <int> verticesIndices = indices.Take(counts[i]).ToList(); indices.RemoveRange(0, counts[i]); for (int j = 0; j < counts[i]; j++) { double min = bins[i]; double max = bins[i + 1]; double r1 = Convert.ToDouble(rnd.NextDouble() * (max - min) + min); mesh.Vertices[verticesIndices[j]].Value[0] = r1; } } double mean = array.Average(); //Assigning not-assigned values foreach (int index in indices) { mesh.Vertices[index].Value[0] = mean; } } catch { } }
/// <summary> /// Calculates the DykstraParsonCoefficient /// </summary> /// <param name="distribution"></param> /// <returns></returns> public static double?CalculateDykstraParsonCoefficient(double[] distribution) { try { if (distribution == null || distribution.Count() == 0) { double perc84 = (double)DistributionHelper.CalculateNthPercentile(distribution, 84); double med = (double)DistributionHelper.CalculateMedian(distribution); return((med - perc84) / med); } else { return(0); } } catch { return(0); } }
/// <summary> /// Calculates the coefficient of variation /// </summary> /// <param name="distribution"></param> /// <returns></returns> public static double?CalculateCoefficientOfVariation(double[] distribution) { try { if (distribution == null || distribution.Count() == 0) { double sta = (double)DistributionHelper.CalculateStandardDeviation(distribution); double av = distribution.Average(); return(sta / av); } else { return(0); } } catch { return(0); } }
public static List <XY> ExperimentalVariogram(List <LocationValue <double> > dataSet, int steps = 10) { //Initialising the list of variogram values List <XY> variogramValues = new List <XY>(); for (int a = 0; a < steps; a++) { variogramValues.Add(new XY()); } if (dataSet != null) { if (dataSet.Count < 1) { return(variogramValues); } } else { return(variogramValues); } List <XY> valuesDistance = new List <XY>(); dataSet = dataSet.OrderBy(x => x.X).OrderBy(x => x.Y).OrderBy(x => x.Z).ToList(); //Subdividing into bins //Converting coordinates of points to distances foreach (var pt in dataSet) { var begin = dataSet.First(); var value = pt.Value; XY valDist = new XY(); if (pt.Geographic) { //Calculating the distance between the first point and every other point var distValue = EuclideanDistance(0, 0, 0, true, begin.Latitude, begin.Longitude, pt.Latitude, pt.Longitude); valDist.Y = distValue; } else { //Calculating the distance between the first point and every other point var distValue = EuclideanDistance(pt.X - begin.X, pt.Y - begin.Y, pt.Z - begin.Z); valDist.Y = distValue; } valDist.X = pt.Value; valuesDistance.Add(valDist); } double[] bins = DistributionHelper.Subdivide(valuesDistance.Select(x => x.Y).ToArray(), steps); //Getting all values of a bin for (int i = 0; i < bins.Count(); i++) { variogramValues[i].X = bins[i]; List <XY> valuesInRange = new List <XY>((from valueDistance in valuesDistance where valueDistance.Y <= bins[i] select valueDistance).ToList()); for (int j = 0; j < valuesInRange.Count(); j++) { if (j == 0) { continue; } int n = valuesInRange.Count(); double val = 0; for (int z = 0; z <= n - j; z++) { val += Math.Pow(valuesInRange[n - (z + 1)].X - valuesInRange[j].X, 2); } variogramValues[i].Y = val / n; } } return(variogramValues); }