public Tuple <EdgeValueType[], EdgeValueType> CreateAlphaPartitionTreeElementArray <ElementValueType, EdgeValueType>( IAlgebraReal <EdgeValueType> algebra, IFunctionDissimilarity <ElementValueType, EdgeValueType> edge_function, ElementValueType[] element_values) { int element_count = this.ElementCount; EdgeValueType [] new_image = new EdgeValueType [element_count]; EdgeValueType max_value = algebra.MinValue; // do x edges for (int element_index = x_edge_offset; element_index < element_count; element_index++) { new_image[element_index] = edge_function.Compute( element_values[element_index - x_edge_offset], element_values[element_index - x_edge_offset + 1]); max_value = algebra.Max(max_value, new_image[element_index]); } // set node values for (int element_index = 0; element_index < x_edge_offset; element_index++) { new_image[element_index] = max_value; } // flip x edges for (int element_index = x_edge_offset; element_index < element_count; element_index++) { new_image[element_index] = algebra.Subtract(max_value, new_image[element_index]); } return(new Tuple <EdgeValueType[], EdgeValueType>(new_image, max_value)); }
public static RealType QuantileSorted <RealType>( IAlgebraReal <RealType> algebra, IList <RealType> array_sorted, double quantile) { if ((quantile < 0.0f) || (1.0f < quantile)) { throw new Exception("Out of range"); } double index_real = array_sorted.Count * quantile; int index_low = (int)Math.Floor(index_real); if (index_low == array_sorted.Count) { return(array_sorted[array_sorted.Count - 1]); } else { RealType index_low_weight = algebra.ToDomain(index_real - (double)index_low); RealType index_high_weight = algebra.Subtract(algebra.ToDomain(index_low + 1), algebra.ToDomain(index_real)); return(algebra.Add( algebra.Multiply(array_sorted[index_low], index_low_weight), algebra.Multiply(array_sorted[index_low + 1], index_high_weight))); } }
public Tuple <EdgeValueType[], EdgeValueType> CreateAlphaPartitionTreeElementArray <ElementValueType, EdgeValueType>( IAlgebraReal <EdgeValueType> algebra, IFunctionDissimilarity <ElementValueType, EdgeValueType> edge_function, ElementValueType [] element_values) { int element_count = this.ElementCount; EdgeValueType[] element_and_edge_values = new EdgeValueType[element_count]; EdgeValueType max_value = algebra.MinValue; // do x edges for (int element_index = x_edge_offset; element_index < y_edge_offset; element_index++) { if (element_index % raster_size[0] != raster_size[0] - 1) { element_and_edge_values[element_index] = edge_function.Compute( element_values[element_index - x_edge_offset], element_values[element_index - x_edge_offset + 1]); max_value = algebra.Max(max_value, element_and_edge_values[element_index]); } } // do y edges for (int element_index = y_edge_offset; element_index < z_edge_offset; element_index++) { if ((element_index % size_xy) / raster_size[0] != raster_size[1] - 1) { element_and_edge_values[element_index] = edge_function.Compute( element_values[element_index - y_edge_offset], element_values[element_index - y_edge_offset + this.size_x]); max_value = algebra.Max(max_value, element_and_edge_values[element_index]); } } // do z edges for (int element_index = z_edge_offset; element_index < (element_and_edge_values.Length - size_xy); element_index++) { element_and_edge_values[element_index] = edge_function.Compute( element_values[element_index - z_edge_offset], element_values[element_index - z_edge_offset + this.size_xy]); max_value = algebra.Max(max_value, element_and_edge_values[element_index]); } // set real node values to max value for (int element_index = 0; element_index < x_edge_offset; element_index++) { element_and_edge_values[element_index] = max_value; } // flip all edges for (int element_index = x_edge_offset; element_index < element_count; element_index++) { element_and_edge_values[element_index] = algebra.Subtract(max_value, element_and_edge_values[element_index]); } return(new Tuple <EdgeValueType[], EdgeValueType>(element_and_edge_values, max_value)); }
public static RealType[] Subtract <RealType>(IAlgebraReal <RealType> algebra, RealType value, IList <RealType> to_subtract) { RealType[] result = new RealType[to_subtract.Count]; for (int index = 0; index < to_subtract.Count; index++) { result[index] = algebra.Subtract(value, to_subtract[index]); } return(result); }
public static RealType VariancePooled <RealType>( IAlgebraReal <RealType> algebra, IList <RealType> sample_0, IList <RealType> sample_1) { RealType mean_0 = Mean(algebra, sample_0); RealType mean_1 = Mean(algebra, sample_1); RealType variance = algebra.AddIdentity; for (int index = 0; index < sample_0.Count; index++) { variance = algebra.Add(variance, algebra.Sqr(algebra.Subtract(sample_0[index], mean_0))); } for (int index = 0; index < sample_1.Count; index++) { variance = algebra.Add(variance, algebra.Sqr(algebra.Subtract(sample_1[index], mean_1))); } return(algebra.Divide(variance, algebra.ToDomain((float)(sample_0.Count + sample_1.Count - 2)))); }
/// <summary> /// Work out if this leaf node should split. If it should, a new split value and dimension is calculated /// based on the dimension with the largest range. /// </summary> /// <returns>True if the node split, false if not.</returns> private bool CalculateSplit() { // Don't split if we are just one point. if (bSinglePoint) { return(false); } // Find the dimension with the largest range. This will be our split dimension. DomainType fWidth = d_algebra.AddIdentity; for (int i = 0; i < d_dimension_count; i++) { DomainType fDelta = d_algebra.Subtract(tMaxBound[i], tMinBound[i]); if (d_algebra.IsNaN(fDelta)) { fDelta = d_algebra.AddIdentity; } if (fDelta.CompareTo(fWidth) == 1) { d_split_dimension = i; fWidth = fDelta; } } // If we are not wide (i.e. all the points are in one place), don't split. if (fWidth.Equals(d_algebra.AddIdentity)) { return(false); } // Split in the middle of the node along the widest dimension. d_split_value = d_algebra.Mean(tMinBound[d_split_dimension], tMaxBound[d_split_dimension]); // Never split on infinity or NaN. if (d_split_value.Equals(d_algebra.PositiveInfinity)) { d_split_value = d_algebra.MaxValue; } else if (d_split_value.Equals(d_algebra.NegativeInfinity)) { d_split_value = d_algebra.MinValue; } // Don't let the split value be the same as the upper value as // can happen due to rounding errors! if (d_split_value.Equals(tMaxBound[d_split_dimension])) { d_split_value = tMinBound[d_split_dimension]; } // Success return(true); }
public ElementType[] GetDisplayValues() { ElementType[] display_values = new ElementType[this.max_tree.RealElementCount]; ElementType[] display_values_max = this.max_tree.GetDisplayValues(); ElementType[] display_values_min = this.min_tree.GetDisplayValues(); for (int index = 0; index < display_values.Length; index++) { display_values[index] = algebra.Subtract(algebra.Add(display_values_max[index], display_values_min[index]), element_values[index]); } return(display_values); }
public static RealType[] AbsoluteDifference <RealType>(IAlgebraReal <RealType> algebra, IList <RealType> list_0, IList <RealType> list_1) { if (list_0.Count != list_1.Count) { throw new Exception("Sizes do not match"); } RealType[] result = new RealType[list_0.Count]; Parallel.For(0, result.Length, index => { result[index] = algebra.Abs(algebra.Subtract(list_0[index], list_1[index])); }); return(result); }
public static RealType[] SubtractElements <RealType>(IAlgebraReal <RealType> algebra, IList <RealType> list_0, IList <RealType> list_1) { if (list_0.Count != list_1.Count) { throw new Exception("Size mismatch: " + list_0.Count + " does not match with " + list_1.Count); } RealType[] result = new RealType[list_0.Count]; Parallel.For(0, result.Length, index => { result[index] = algebra.Subtract(list_0[index], list_1[index]); }); return(result); }
public static RealType VariancePooled <RealType>( IAlgebraReal <RealType> algebra, IList <IList <RealType> > samples) { RealType variance = algebra.AddIdentity; RealType degrees_of_freedom = algebra.AddIdentity; foreach (IList <RealType> sample in samples) { RealType mean_0 = Mean(algebra, sample); for (int index = 0; index < sample.Count; index++) { variance = algebra.Add(variance, algebra.Sqr(algebra.Subtract(sample[index], mean_0))); } degrees_of_freedom = algebra.Add(degrees_of_freedom, algebra.ToDomain((float)(sample.Count - 1))); } return(algebra.Divide(variance, degrees_of_freedom)); }
public static RealType Interpolation1DLinear <RealType>(IAlgebraReal <RealType> algebra, RealType domain_0, RealType value_0, RealType domain_1, RealType value_1, RealType domain_new) { return(Interpolation1DLinear(algebra, algebra.Divide(algebra.Subtract(domain_new, domain_0), algebra.Subtract(domain_1, domain_0)), value_0, value_1)); }
public static RealType Interpolation1DLinear <RealType>(IAlgebraReal <RealType> algebra, RealType fraction_0, RealType value_0, RealType value_1) { return(algebra.Add(algebra.Multiply(value_0, fraction_0), algebra.Multiply(value_1, algebra.Subtract(algebra.MultiplyIdentity, fraction_0)))); }
//Richardson Extrapolation http://en.wikipedia.org/wiki/Richardson_extrapolation // // @param value * original value // @param refined_value * with base times smaller intervals // @param base * refinement factor of value // @param power * order of scale //@return public static RealType RichardsonExtrapolation <RealType>(IAlgebraReal <RealType> algebra, RealType value, RealType refined_value, RealType base_value, RealType power) { RealType scale = algebra.Pow(base_value, power); return(algebra.Divide(algebra.Subtract(algebra.Multiply(scale, refined_value), value), algebra.Subtract(scale, algebra.MultiplyIdentity))); }