public TopologyElementRaster4D8Connectivity(IRaster4DInteger raster) : base(raster, 8) { this.raster_size = ToolsCollection.Copy(raster.SizeArray); this.size_01 = raster_size[0] * raster_size[1]; this.size_012 = size_01 * raster_size[2]; }
public DomainType[] Minimize(DomainType[] state, IFunction <DomainType[], EvaluationType> evaluation_function) { DomainType[] new_state = ToolsCollection.Copy(state); EvaluationType evaluation = evaluation_function.Compute(state); for (int iteration_index = 0; iteration_index < IterationCount; iteration_index++) { int mutated_element = d_mutator.MutatePoint(d_random, new_state); EvaluationType new_evaluation = evaluation_function.Compute(new_state); // if new state is higher if (new_evaluation.CompareTo(evaluation) == 1) { //accept it with a chance if (d_random.Toss(d_accept_chance.Compute(iteration_index))) { state[mutated_element] = new_state[mutated_element]; evaluation = new_evaluation; } else { //reject it new_state[mutated_element] = state[mutated_element]; } } else // if new state is lower or equal { //accept it state[mutated_element] = new_state[mutated_element]; evaluation = new_evaluation; } } return(state); }
public ReportDiscrete(IModelDiscrete <DomainType, LabelType> model, int[,] confusion_matrix_instances) { this.Model = model; this.confusion_matrix_instances = ToolsCollection.Copy(confusion_matrix_instances); this.confusion_matrix_rates = new double[confusion_matrix_instances.GetLength(0), confusion_matrix_instances.GetLength(1)]; int instance_count = 0; this.CorrectLabelCount = 0; for (int index_0 = 0; index_0 < confusion_matrix_instances.GetLength(0); index_0++) { for (int index_1 = 0; index_1 < confusion_matrix_instances.GetLength(1); index_1++) { instance_count += confusion_matrix_instances[index_0, index_1]; if (index_0 == index_1) { CorrectLabelCount += confusion_matrix_instances[index_0, index_1]; } } } CorrectLabelRate = CorrectLabelCount / (double)instance_count; for (int index_0 = 0; index_0 < confusion_matrix_instances.GetLength(0); index_0++) { for (int index_1 = 0; index_1 < confusion_matrix_instances.GetLength(1); index_1++) { confusion_matrix_rates[index_0, index_1] = (double)confusion_matrix_instances[index_0, index_1] / (double)instance_count; } } }
public Bitmap Render(IImageSpace3D <float, float> source_image) { Bitmap destination_image = new Bitmap(resolution_x, resolution_y); float[] coordinates_y = ToolsCollection.Copy(render_origen); for (int index_y = 0; index_y < resolution_x; index_y++) { ToolsMathCollection.AddRBA <float>(algebra, coordinates_y, render_stride_y, coordinates_y); float[] coordinates_x = ToolsCollection.Copy(coordinates_y); for (int index_x = 0; index_x < resolution_y; index_x++) { float[] coordinates_z = ToolsCollection.Copy(coordinates_x); float max_value = source_image.GetLocationValue(coordinates_z); ToolsMathCollection.AddRBA(algebra, coordinates_z, render_stride_z, coordinates_z); for (int index_z = 1; index_z < resolution_z; index_z++) { float value = source_image.GetLocationValue(coordinates_z); if (this.comparer.Compare(max_value, value) == -1) { max_value = value; } ToolsMathCollection.AddRBA(algebra, coordinates_z, render_stride_z, coordinates_z); } destination_image.SetPixel(index_x, index_y, this.converter.Compute(max_value)); ToolsMathCollection.AddRBA(algebra, coordinates_x, render_stride_x, coordinates_x); } } return(destination_image); }
public MaxTreeAutoDual(IAlgebraReal <ElementType> algebra, IMaxTreeBuilder <ElementType> builder, ITopologyElement topology, ElementType[] element_values) { this.algebra = algebra; this.max_tree = builder.BuildMaxTree(element_values, new ComparerNatural <ElementType>(), topology, element_values.Length); this.min_tree = builder.BuildMaxTree(element_values, new ComparerNatural <ElementType>(true), topology, element_values.Length); this.element_values = ToolsCollection.Copy(element_values); }
public TopologyElementRaster3D6Connectivity(IRaster3DInteger raster, float[] voxel_spacing) : base(raster, 6) { this.raster_size = ToolsCollection.Copy(raster.SizeArray); this.size_xy = raster_size[0] * raster_size[1]; this.neigbour_distance = new float[] { voxel_spacing[0], voxel_spacing[0], voxel_spacing[1], voxel_spacing[1], voxel_spacing[2], voxel_spacing[2] }; }
public static ArrayType[,,] Select3D <ArrayType>(this ArrayType[,,,,] source, int dim_0, int dim_1, int dim_2, int[] offsets) { if ((dim_0 == dim_1) || (dim_1 == dim_2) || (dim_0 == dim_1)) { throw new Exception("Dimension must be different"); } int size_0 = source.GetLength(dim_0); int size_1 = source.GetLength(dim_1); int size_2 = source.GetLength(dim_2); ArrayType[,,] target = new ArrayType[size_0, size_1, size_2]; Parallel.For(0, size_0, index_0 => { int[] source_index = ToolsCollection.Copy(offsets); source_index[dim_0] = index_0; for (int index_1 = 0; index_1 < size_1; index_1++) { source_index[dim_1] = index_1; for (int index_2 = 0; index_2 < size_2; index_2++) { source_index[dim_2] = index_2; target[index_0, index_1, index_2] = source[source_index[0], source_index[1], source_index[2], source_index[3], source_index[4]]; } } }); return(target); }
public List <ParameterSet> GetNeighborsArea(ParameterSet parameter_set) { int[] source_location = GetLocation(parameter_set); List <int[]> locations = new List <int[]>(); for (int axes_index = 0; axes_index < AxesList.Count; axes_index++) { int[] new_location_0 = ToolsCollection.Copy(source_location); int[] new_location_1 = ToolsCollection.Copy(source_location); new_location_0[axes_index]++; new_location_1[axes_index]--; if (Contains(new_location_0)) { locations.Add(new_location_0); } if (Contains(new_location_1)) { locations.Add(new_location_1); } } List <ParameterSet> parameter_sets = new List <ParameterSet>(); foreach (int[] location in locations) { parameter_sets.Add(GetParameterSet(location)); } return(parameter_sets); }
public TransformRescale( float [] lower_bounds, float [] upper_bounds) { this.lower_bounds = ToolsCollection.Copy(lower_bounds); this.upper_bounds = ToolsCollection.Copy(upper_bounds); window_sizes = ToolsMathCollectionFloat.subtract(upper_bounds, lower_bounds); }
public FileTypeDescriptor(string tag, string description, int signature_offset, byte[] signature, string default_extension) { this.Tag = tag; this.Description = description; this.SignatureOffset = signature_offset; this.signature = ToolsCollection.Copy(signature); this.ascii_signature = ToolsBinary.ByteArrayToRegularString(signature); this.IsHeaderType = true; }
public static RealType Quantile <RealType>( IAlgebraReal <RealType> algebra, IList <RealType> array, float quantile) { RealType[] copy = ToolsCollection.Copy(array); Array.Sort(copy, algebra); return(QuantileSorted(algebra, array, quantile)); }
public RangeType[] GetElementValues(bool copy_values) { if (copy_values) { return(ToolsCollection.Copy(this.image)); } else { return(this.image); } }
public override MinimizerResult Minimize( IFunction <double[], double> minimization_function, IFunction <double[], bool> validation_function, IMinimizerHaltingCriterion <Simplex> halting_criterion, double[] parameters_initial, double[] initial_vextex_size) { Simplex simplex = new Simplex(parameters_initial, initial_vextex_size, minimization_function, validation_function); /* Holds vertices of simplex */ MinimizerResult result = new MinimizerResult(simplex); if (TrackResuls) { for (int vertex_index = 0; vertex_index < simplex.VertexCount; vertex_index++) { result.EvaluationList.Add(ToolsCollection.Copy(simplex.vertexes[vertex_index])); result.ValueList.Add(simplex.vertex_values[vertex_index]); result.IterationList.Add(0); } } double[] centroid_vertex = new double[simplex.ParameterCount]; for (int iteration = 1; iteration <= halting_criterion.MaximumIterationCount; iteration++) { simplex.ComputeCentroidVertex(centroid_vertex); // Check if the reflected point is valid; if so, accept or try expansion, if not then try an iside or outside contraction. bool reflection_or_expansion_successfull = TryReflectionAndExpansion(minimization_function, validation_function, simplex, centroid_vertex, result, iteration); // Check if reflextion and expansion where succesfull if (!reflection_or_expansion_successfull) { bool contraction_successful = TryContraction(minimization_function, validation_function, simplex, centroid_vertex, result, iteration); // Check if contraction was succesfull if (!contraction_successful) { bool shrink_successful = TryShrink(minimization_function, validation_function, simplex, centroid_vertex, result, iteration); // Check if shrink was succesfull if (!shrink_successful) { return(result); } } } if (halting_criterion.CheckHalt(simplex)) { result.IsHalted = false; break; } } result.IsSuccesFull = true; return(result); }
// The polynomial p(x)= 3x^2 + 2x + 1 is evaluated at x = 5 with polyval([3,2,1], 5) private static double Polyval(double[] coefficeients, double domain_value) { double [] coefficeients_reverse = ToolsCollection.Copy(coefficeients); Array.Reverse(coefficeients_reverse); double result = 0; for (int index = 0; index < coefficeients.Length; index++) { result += Math.Pow(domain_value, index) * coefficeients[index]; } return(result); }
public ModelNaiveBayesNominal( IDataContext data_context, double[] class_priors, IFunction <int, double>[,] marginals) : base(data_context, "ModelNaiveBayesNominal") { this.class_priors = ToolsMathCollectionDouble.LogE(class_priors); //These need to be log likelyhoods this.marginals = ToolsCollection.Copy(marginals); //TODO gutted object this.feature_count = this.marginals.GetLength(0); this.class_count = this.marginals.GetLength(1); Debug.Assert(class_priors.Length == class_count); }
public ImageRaster2DWrapperImageSpace4D(IImageSpace <float, RangeType> image, float[] offset, float[] spaceing_0, float[] spaceing_1) { if (wrapped_image.DimensionCount != 4) { throw new Exception(); } this.wrapped_image = image; this.offset = ToolsCollection.Copy(offset); this.spaceing_0 = ToolsCollection.Copy(spaceing_0); this.spaceing_1 = ToolsCollection.Copy(spaceing_1); }
public FunctionColorToFloat32Gray( float [] factors) { if (factors.Length != 4) { throw new Exception("Factors must be of length 4"); } else { argb_weigths = ToolsCollection.Copy(factors); sum = ToolsMathCollectionFloat.sum(argb_weigths); } }
protected override float Projection(float[] origen, float[] stride_size, int stride_count, IImageSpace3D <float, float> source_image) { float[] coordinates = ToolsCollection.Copy(origen); for (int index_z = 0; index_z < stride_count; index_z++) { float value = source_image.GetLocationValue(coordinates); if (this.comparer.Compare(value, min_value) == 1) { return(value); } ToolsMathCollection.AddRBA(algebra, coordinates, stride_size, coordinates); } return(min_value); }
public ToneSystem(double base_frequency, int tone_count, string [] all_tone_names, int[] selected_tone_indexes) { this.BaseFrequency = base_frequency; this.ToneCount = tone_count; this.ScaleToneCount = selected_tone_indexes.Length; this.frequency_multiplyers = new double[tone_count]; this.notes_to_tones = ToolsMathSeries.RangeInt32(tone_count).Select(selected_tone_indexes); this.tone_names = ToolsCollection.Copy(all_tone_names); for (int tone_index = 0; tone_index < tone_count; tone_index++) { frequency_multiplyers[tone_index] = Math.Pow(2, tone_index / (double)tone_count); } }
public FunctionInterpolationLinear(double [] domain, double[] range) { if (domain.Length == 0) { throw new Exception("Array length cannot be 0"); } if (domain.Length != range.Length) { throw new Exception("Array lengths do not match"); } this.domain = ToolsCollection.Copy(domain); this.values = ToolsCollection.Copy(range); }
public override double[] GetLikelihoods(int[] instance_features) { Debug.Assert(instance_features.Length == feature_count); double[] class_likelyhoods = ToolsCollection.Copy(class_priors); //These need to be log likelyhoods for (int index_feature = 0; index_feature < feature_count; index_feature++) { for (int index_class = 0; index_class < class_count; index_class++) { double likelyhood = marginals[index_feature, index_class].Compute(instance_features[index_feature]); class_likelyhoods[index_class] = class_likelyhoods[index_class] + Math.Log(likelyhood); } } return(ToolsMathCollectionDouble.Exp(class_likelyhoods)); //TODO use exp? }
protected ImageRaster( RasterType raster, RangeType [] image, bool copy_array) { this.Raster = raster; if (copy_array) { this.image = ToolsCollection.Copy(image); } else { this.image = image; } }
public VariableDescriptor( string feature_name, DataLevel feature_level, IList <string> value_strings) { Name = feature_name; DataLevel = feature_level; ValueStrings = ToolsCollection.Copy(value_strings); value_string_to_value_index = new Dictionary <string, int>(); for (int value_level_index = 0; value_level_index < value_strings.Count; value_level_index++) { value_string_to_value_index[value_strings[value_level_index]] = value_level_index; } }
public override double[] GetLikelihoods(int [] instance_features) { //Use join occurrence table of B to A to compute P(A|B) if occurance of B = 0 then revert to priors of A if (!this.occurences.ContainsKey(instance_features)) { return(ToolsCollection.Copy(priors)); } else { int[] occurence = this.occurences[instance_features]; double[] probabilities = new double[this.DataContext.LabelDescriptors[0].ValueCount]; for (int label_index = 0; label_index < this.DataContext.LabelDescriptors[0].ValueCount; label_index++) { probabilities[label_index] = ((double)occurence[label_index + 1]) / ((double)occurence[0]); } return(probabilities); } }
public bool TryContraction( IFunction <double[], double> minimization_function, IFunction <double[], bool> validation_function, Simplex simplex, double[] centroid_vertex, MinimizerResult result, int iteration) { double[] contraction_vertex = new double[simplex.ParameterCount]; ComputeContractionVertex(simplex, centroid_vertex, contraction_vertex); if (validation_function.Compute(contraction_vertex)) { double contraction_value = minimization_function.Compute(contraction_vertex); if (TrackResuls) { result.EvaluationList.Add(ToolsCollection.Copy(contraction_vertex)); result.ValueList.Add(contraction_value); result.IterationList.Add(iteration); } if (contraction_value < simplex.LargestVertexValue) { simplex.SetVertexLargest(contraction_vertex, contraction_value); //Contraction was successful return(true); } else { //Contraction was not successful (not lower) return(false); } } else { //Contraction was not successful (not valid) return(false); } }
public ModelHMRFNeighborhoodDisjuntion( IFunction <int[], float>[] label_likelyhood_models, IFunction <FeatureType[], float>[] feature_likelyhood_models, int background_label, FeatureType background_feature, int feature_image_count, int[] neighborhood_size) { Debug.Assert(label_likelyhood_models.Length == feature_likelyhood_models.Length); this.label_likelyhood_models = label_likelyhood_models; this.feature_likelyhood_models = feature_likelyhood_models; this.background_label = background_label; this.background_feature = background_feature; this.label_count = label_likelyhood_models.Length; this.feature_image_count = feature_image_count; this.neighborhood_size = ToolsCollection.Copy(neighborhood_size); this.neighborhood_element_count = 1; for (int dimension_index = 0; dimension_index < neighborhood_size.Length; dimension_index++) { this.neighborhood_element_count *= (neighborhood_size[dimension_index] * 2) + 1; } }
public static void SetValues2D <ArrayType>(this ArrayType[,,] target, ArrayType[,] source, int dim_0, int dim_1, int[] offsets) { if (dim_0 == dim_1) { throw new Exception("Target dimensions must be different"); } int size_0 = source.GetLength(0); int size_1 = source.GetLength(1); Parallel.For(0, size_0, index_0 => { int[] target_index = ToolsCollection.Copy(offsets); target_index[dim_0] = index_0; for (int index_1 = 0; index_1 < size_1; index_1++) { target_index[dim_1] = index_1; target[target_index[0], target_index[1], target_index[2]] = source[index_0, index_1]; } }); }
public bool TryShrink( IFunction <double[], double> minimization_function, IFunction <double[], bool> validation_function, Simplex simplex, double[] centroid_vertex, MinimizerResult result, int iteration) { for (int vertex_index = 0; vertex_index < simplex.VertexCount; vertex_index++) { if (vertex_index != simplex.SmallestVertexIndex) { for (int parameter_index = 0; parameter_index < simplex.ParameterCount; parameter_index++) { simplex.vertexes[vertex_index][parameter_index] = simplex.vertexes[simplex.SmallestVertexIndex][parameter_index] + (shrink_factor * (simplex.vertexes[vertex_index][parameter_index] - simplex.vertexes[simplex.SmallestVertexIndex][parameter_index])); } if (validation_function.Compute(simplex.vertexes[vertex_index])) { double shrink_value = minimization_function.Compute(simplex.vertexes[vertex_index]); if (TrackResuls) { result.EvaluationList.Add(ToolsCollection.Copy(simplex.vertexes[vertex_index])); result.ValueList.Add(shrink_value); result.IterationList.Add(iteration); } simplex.SetVertexValue(vertex_index, shrink_value); } else { //Shrink was not successful (not valid) return(false); } } } //Shrink was successful noone cares about the vertex values return(true); }
public Polynomial <DomainType> Add(Polynomial <DomainType> other) { Debug.Assert(algebra.Equals(other.algebra)); if (other.coeffecients.Length < coeffecients.Length) { DomainType[] coeffecients = ToolsCollection.Copy(this.coeffecients); for (int index = 0; index < other.coeffecients.Length; index++) { coeffecients[index] = algebra.Add(this.coeffecients[index], other.coeffecients[index]); } return(new Polynomial <DomainType>(algebra, coeffecients)); } else { DomainType[] coeffecients = ToolsCollection.Copy(other.coeffecients); for (int index = 0; index < this.coeffecients.Length; index++) { coeffecients[index] = algebra.Add(this.coeffecients[index], other.coeffecients[index]); } return(new Polynomial <DomainType>(algebra, coeffecients)); } }
public Tuple <Polynomial <DomainType>, Polynomial <DomainType> > DivideRemainder(Polynomial <DomainType> divisor_poly) { Debug.Assert(algebra.Equals(divisor_poly.algebra)); // if divisor is too big if (coeffecients.Length < divisor_poly.coeffecients.Length) { return(new Tuple <Polynomial <DomainType>, Polynomial <DomainType> >(new Polynomial <DomainType>(algebra), new Polynomial <DomainType>(algebra, coeffecients))); } // if divisor is too big if ((coeffecients.Length == divisor_poly.coeffecients.Length) && (algebra.Compare(coeffecients[coeffecients.Length - 1], coeffecients[coeffecients.Length - 1]) == -1)) { return(new Tuple <Polynomial <DomainType>, Polynomial <DomainType> >(new Polynomial <DomainType>(algebra), new Polynomial <DomainType>(algebra, coeffecients))); } //Else do long division DomainType[] remainder = ToolsCollection.Copy(coeffecients); DomainType[] results = new DomainType[coeffecients.Length]; DomainType[] divisor = divisor_poly.coeffecients; for (int index = 0; index < results.Length; index++) { results[index] = algebra.AddIdentity; } int max_shift = remainder.Length - divisor.Length; for (int shift = max_shift; 0 <= shift; shift--) { DomainType multiplier = algebra.Divide(remainder[shift], divisor[divisor.Length - 1]); for (int index = shift; index < coeffecients.Length; index++) { remainder[index] = algebra.Subtract(remainder[index], algebra.Multiply(divisor[index + shift], multiplier)); } results[shift] = multiplier; } return(new Tuple <Polynomial <DomainType>, Polynomial <DomainType> >(new Polynomial <DomainType>(algebra, results), new Polynomial <DomainType>(algebra, remainder))); }