public IFunction <RealType, RealType> ConvolveFunction(IFunction <RealType, RealType> base_function, IFunction <RealType, RealType> kernel_function, RealType base_domain_min, RealType base_domain_max, RealType kernel_domain_min, RealType kernel_domain_max) { int base_sample_count = this.algebra.FloorInt(this.algebra.Divide(this.algebra.Subtract(base_domain_max, base_domain_min), sample_time)) + 1; RealType[] base_sample_times = new RealType[base_sample_count]; RealType[] list_base = new RealType[base_sample_count]; base_sample_times[0] = base_domain_min; list_base[0] = base_function.Compute(base_sample_times[0]); for (int sample_index = 1; sample_index < base_sample_count; sample_index++) { base_sample_times[sample_index] = this.algebra.Add(base_sample_times[sample_index - 1], this.sample_time); list_base[sample_index] = base_function.Compute(base_sample_times[sample_index]); } int kernel_sample_count = this.algebra.FloorInt(this.algebra.Divide(this.algebra.Subtract(kernel_domain_max, kernel_domain_min), sample_time)) + 1; RealType[] list_kernel = new RealType[kernel_sample_count]; RealType kernel_sample_time = kernel_domain_min; for (int sample_index = 0; sample_index < kernel_sample_count; sample_index++) { list_kernel[sample_index] = kernel_function.Compute(kernel_sample_time); kernel_sample_time = this.algebra.Add(kernel_sample_time, sample_time); } RealType[] result = ConvolveUniform(list_base, list_kernel); for (int sample_index = 0; sample_index < base_sample_count; sample_index++) { result[sample_index] = this.algebra.Multiply(result[sample_index], this.sample_time); } return(new FunctionMultiStep <RealType, RealType>(base_sample_times, result)); }
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 Simplex(double[] initial_parameters, double[] initial_step, IFunction <double[], double> minimization_function, IFunction <double[], bool> validation_function) { if (initial_parameters.Length != initial_step.Length) { throw new Exception("unequal lenght"); } ParameterCount = initial_parameters.Length; VertexCount = 1; for (int paramter_index = 0; paramter_index < ParameterCount; paramter_index++) { if (initial_step[paramter_index] != 0) { VertexCount++; } } if (VertexCount < 3) { throw new Exception("Simplex to small"); } this.vertexes = ToolsCollection.CreateArrayArray <double>(VertexCount, ParameterCount); this.vertex_values = new double[VertexCount]; //initialize the vertexes //TODO check for fixed paramters int vertex_index = 0; for (int step_index = 0; step_index < ParameterCount; step_index++) { if (initial_step[step_index] != 0) { for (int parameter_index = 0; parameter_index < ParameterCount; parameter_index++) { if (parameter_index == step_index) { this.vertexes[vertex_index][parameter_index] = initial_parameters[parameter_index] + initial_step[step_index]; } else { this.vertexes[vertex_index][parameter_index] = initial_parameters[parameter_index]; } } vertex_values[vertex_index] = minimization_function.Compute(this.vertexes[vertex_index]); vertex_index++; } } // The last vertex get all steps negative for (int parameter_index = 0; parameter_index < ParameterCount; parameter_index++) { this.vertexes[vertex_index][parameter_index] += initial_parameters[parameter_index] - initial_step[parameter_index]; } SetVertexValue(vertex_index, minimization_function.Compute(this.vertexes[vertex_index])); }
public override AMatrix <MatrixType> Integrate(IFunction <AMatrix <MatrixType>, AMatrix <MatrixType> > differntial, AMatrix <MatrixType> inital_value, double step_size) { AMatrix <MatrixType> k1 = differntial.Compute(inital_value) * (step_size / 6.0); AMatrix <MatrixType> k2 = differntial.Compute(inital_value + (k1 * 0.5)) * (step_size / 3.0); AMatrix <MatrixType> k3 = differntial.Compute(inital_value + (k2 * 0.5)) * (step_size / 3.0); AMatrix <MatrixType> k4 = differntial.Compute(inital_value + k3) * (step_size / 6.0); AMatrix <MatrixType> increment = k1 + k2 + k3 + k4; return(inital_value + increment); }
public IClusteringCentroid <DomainType> Cluster(IDataSet <DomainType> data_set) { IFunction <IList <DomainType[]>, ICentroidDistance <DomainType> > centroid_calculator = centroid_calculator_template.Generate(data_set.DataContext); IList <DomainType[]> instance_features_list = data_set.FeatureData; IList <IList <DomainType[]> > cluster_members = new List <IList <DomainType[]> >(); IList <ICentroidDistance <DomainType> > centroids = new List <ICentroidDistance <DomainType> >(); // add first cluster cluster_members.Add(new List <DomainType[]>()); cluster_members[0].Add(instance_features_list[0]); centroids.Add(centroid_calculator.Compute(cluster_members[0])); // assign to clusters for (int index_instance = 1; index_instance < instance_features_list.Count; index_instance++) { DomainType[] instance_features = instance_features_list[index_instance]; int best_cluster_index = 0; double best_distance = centroids[0].ComputeDistance(instance_features); for (int cluster_index = 1; cluster_index < centroids.Count; cluster_index++) { double distance = centroids[cluster_index].ComputeDistance(instance_features); if (distance.CompareTo(best_distance) == 1) { best_distance = distance; best_cluster_index = cluster_index; } } if (best_distance.CompareTo(critical_distance) == 1) { // create new cluster IList <DomainType[]> new_cluster = new List <DomainType[]>(); new_cluster.Add(instance_features); cluster_members.Add(new_cluster); centroids.Add(centroid_calculator.Compute(new_cluster)); } else { // add to existing cluster and recompute centroid cluster_members[best_cluster_index].Add(instance_features); centroids[best_cluster_index] = centroid_calculator.Compute(cluster_members[best_cluster_index]); } } return(new ClusteringCentroid <DomainType>(data_set.DataContext, centroids)); }
/// <summary>Computes a Logistic Kernel matrix from the given input matrix.</summary> /// <param name="m">Input Matrix.</param> /// <returns>Logistic Kernel Matrix.</returns> public Matrix Compute(Matrix m) { var K = Matrix.Zeros(m.Rows); for (var i = 0; i < m.Rows; i++) { for (var j = i; j < m.Rows; j++) { var xy = m[i].Dot(m[j]); K[i, j] = K[j, i] = LogisticFunction.Compute(Lambda * xy); } } return(K); }
public BigInteger Unlock(IList <Tuple <BigInteger, BigInteger> > Keys) { // Check key count if (Keys.Count < RequiredKeyCount) { throw new Exception("Insufficient Keys"); } // Build langrange polynomial out of required keys IFunction <BigInteger, BigInteger> polynomial = new FunctionPolynomialLagrange <BigInteger>(new AlgebraSymbolBigInteger(), Keys); // Check that excess key also fall on polynomial for (int key_index = RequiredKeyCount; key_index < Keys.Count; key_index++) { if (polynomial.Compute(Keys[key_index].Item1) != Keys[key_index].Item2) { throw new Exception("Key check failed, on or more keys where not consistent"); } } // Compute secret BigInteger secret_result = polynomial.Compute(secret_point); // Check secret hash if (hashing_function.Compute(secret_result) != secret_result_hash) { throw new Exception("Hash check failed, on or more keys where incorrect"); } return(secret_result); }
public void Filter(IList <MaxTreeNode <ElementType> > list_bottom_to_top) { list_bottom_to_top.Reverse(); foreach (IMaxTreeNode <ElementType> current_node in list_bottom_to_top) { ElementType mean = this.algebra.AddIdentity; foreach (IMaxTreeNode <ElementType> child_node in current_node.GetNodeChildren()) { mean = this.algebra.Multiply(this.algebra.Add(mean, child_node.DisplayValue), algebra.ToDomain((float)child_node.CulmativeRealSize)); } foreach (int element_index in current_node.GetElementIndexArrayNodeReal()) { mean = this.algebra.Add(mean, this.source_values[element_index]); } current_node.DisplayValue = this.algebra.Divide(mean, this.algebra.ToDomain((float)current_node.CulmativeRealSize)); } list_bottom_to_top.Reverse(); foreach (IMaxTreeNode <ElementType> current_node in list_bottom_to_top) { if (filter_function.Compute(current_node) && (current_node.Parent != null)) { current_node.DisplayValue = current_node.Parent.DisplayValue; } else { current_node.DisplayValue = current_node.Value; } } }
public Tuple <PublicKeyLamportDiffie, PrivateKeyLamportDiffie> GenerateKeys() { BigInteger[,] private_key_array = new BigInteger[256, 2]; BigInteger[,] public_key_array = new BigInteger[256, 2]; for (int index_key = 0; index_key < private_key_array.GetLength(0); index_key++) { private_key_array[index_key, 0] = d_random.RandomPositiveBigIntegerOfSizeInBits(256); private_key_array[index_key, 1] = d_random.RandomPositiveBigIntegerOfSizeInBits(256); public_key_array[index_key, 0] = d_hash_key.Compute(private_key_array[index_key, 0]); public_key_array[index_key, 1] = d_hash_key.Compute(private_key_array[index_key, 1]); } PublicKeyLamportDiffie public_key = new PublicKeyLamportDiffie(private_key_array); PrivateKeyLamportDiffie private_key = new PrivateKeyLamportDiffie(public_key_array); return(new Tuple <PublicKeyLamportDiffie, PrivateKeyLamportDiffie>(public_key, private_key)); }
//------------- public functions -----------// /* * processing functions */ public float[] process(float[] input) { //apply function y = f(Wx) //store local copy of the input and create space for bias ToolsCollection.CopyRBA(input, d_input); d_input[d_input_size - 1] = 1.0f; //put in bias //calculate and store activation ToolsCollection.SetValue(d_activation, 0.0f); for (int o = 0; o < d_output_size; o++) { for (int i = 0; i < d_input_size; i++) { d_activation[o] += d_weights[o, i] * d_input[i]; } } //calculate and store output for (int o = 0; o < d_output_size; o++) { d_output[o] = d_output_function.Compute((float)d_activation[o]); //TODO double? } //and return the output to be processed by later layers return(d_output); }
public static double[] IntegrateStatic(IFunction <double[], double[], double[]> differntial, double[] history, double[] inital_value, double step_size) { double[] k1 = ToolsMathCollectionDouble.Multply(differntial.Compute(inital_value, history), step_size / 6.0); double[] temp = ToolsMathCollectionDouble.Add(inital_value, ToolsMathCollectionDouble.Multply(k1, 0.5)); double[] k2 = ToolsMathCollectionDouble.Multply(differntial.Compute(temp, history), step_size / 3.0f); temp = ToolsMathCollectionDouble.Add(inital_value, ToolsMathCollectionDouble.Multply(k2, 0.5)); double[] k3 = ToolsMathCollectionDouble.Multply(differntial.Compute(temp, history), step_size / 3.0f); temp = ToolsMathCollectionDouble.Add(inital_value, k2); double[] k4 = ToolsMathCollectionDouble.Multply(differntial.Compute(temp, history), step_size / 6.0f); ToolsCollection.CopyRBA(inital_value, temp); for (int index = 0; index < inital_value.Length; index++) { temp[index] += k1[index] + k2[index] + k3[index] + k4[index]; } return(temp); }
private static Tuple <ISet <int>, double, bool> PickMinimal(ISet <int> current, List <ISet <int> > options, IFunction <ISet <int>, double> to_minimize) { ISet <int> minimal_option = current; double minimial_value = to_minimize.Compute(current); bool has_new = false; foreach (ISet <int> option in options) { double new_value = to_minimize.Compute(option); if (new_value < minimial_value) { minimal_option = option; minimial_value = new_value; has_new = true; } } return(new Tuple <ISet <int>, double, bool>(minimal_option, minimial_value, has_new)); }
public static DestinationRangeType[] Convert <SourceRangeType, DestinationRangeType>(SourceRangeType[] source, IFunction <SourceRangeType, DestinationRangeType> converter) { DestinationRangeType [] destination = new DestinationRangeType[source.Length]; for (int index = 0; index < source.Length; index++) { destination[index] = converter.Compute(source[index]); } return(destination); }
private double CalculateFunctionValue(List <BinaryChromosome> chromosomes, Range range, double bitsInChromosome) { if (chromosomes.Count != 2) { throw new ArgumentException("Incorrect chromosomes amount."); } return(function.Compute( BinaryUtils.BinaryToDecimalRepresentation(chromosomes[0].BinaryRepresentation, range, bitsInChromosome), BinaryUtils.BinaryToDecimalRepresentation(chromosomes[1].BinaryRepresentation, range, bitsInChromosome))); }
public BigInteger [] Sign(BigInteger message, PrivateKeyLamportDiffie private_key) { BigInteger message_hash = d_hash_message.Compute(message); BigInteger [] signature = new BigInteger[256]; BigInteger[,] key_array = private_key.KeyArray(); for (int index_key = 0; index_key < signature.Length; index_key++) { if (message_hash.IsBitSet(index_key))// check bit is set) { signature[index_key] = key_array[index_key, 0]; } else { signature[index_key] = key_array[index_key, 1]; } } return(signature); }
public ImageRaster3D <TargetRangeType> ConvertTyped <TargetRangeType>(IFunction <RangeType, TargetRangeType> converter) { RangeType [] element_values = GetElementValues(false); TargetRangeType[] data = new TargetRangeType[element_values.Length]; for (int index = 0; index < element_values.Length; index++) { data[index] = converter.Compute(element_values[index]); } return(new ImageRaster3D <TargetRangeType>(Raster, data, false)); }
public IImageRaster <IRaster2DInteger, TargetRangeType> Convert <TargetRangeType>(IFunction <RangeType, TargetRangeType> converter) { RangeType[] source_values = GetElementValues(false); TargetRangeType [] target_values = new TargetRangeType[Raster.ElementCount]; Parallel.For(0, Raster.ElementCount, index => { target_values[index] = converter.Compute(source_values[index]); }); return(new ImageRaster2D <TargetRangeType>(this.Raster, target_values, false)); }
public void ComputeCentroids( IImageRaster <IRasterType, int> image_labeling, IImageRaster <IRasterType, DomainType> image_data, IList <int[]> cluster_spatial_centroids, IList <DomainType> cluster_feature_centroids) { //Use the cluster raster for spacing the clusters in the image IList <IList <DomainType> > clusters = new List <IList <DomainType> >(); int dimension_count = image_labeling.Raster.DimensionCount; int element_count = image_labeling.Raster.ElementCount; int [] element_coordinates = new int[dimension_count]; //clear clusters // TODO make paralel for (int cluster_index = 0; cluster_index < cluster_spatial_centroids.Count; cluster_index++) { clusters.Add(new List <DomainType>()); for (int dimension_index = 0; dimension_index < image_labeling.Raster.DimensionCount; dimension_index++) { cluster_spatial_centroids[cluster_index][dimension_index] = 0; } } // Aggregate cluster. // TODO make paralel for (int element_index = 0; element_index < element_count; element_index++) { int cluster_index = image_labeling.GetElementValue(element_index); image_data.Raster.GetElementCoordinatesRBA(element_index, element_coordinates); clusters[cluster_index].Add(image_data.GetElementValue(element_index)); ToolsMathCollectionInteger.AddFill(cluster_spatial_centroids[cluster_index], element_coordinates, cluster_spatial_centroids[cluster_index]); } // Compute new cluster centers. // TODO make paralel for (int cluster_index = 0; cluster_index < clusters.Count; cluster_index++) { if (clusters[cluster_index].Count != 0) { for (int dimension_index = 0; dimension_index < image_labeling.Raster.DimensionCount; dimension_index++) { cluster_spatial_centroids[cluster_index][dimension_index] /= clusters[cluster_index].Count; } cluster_feature_centroids[cluster_index] = d_centroid_feature_calculator.Compute(clusters[cluster_index]); } else { // reduce cluster count cluster_feature_centroids.RemoveAt(cluster_index); cluster_spatial_centroids.RemoveAt(cluster_index); clusters.RemoveAt(cluster_index); cluster_index--; } } }
public static (D[], R[]) Plot <D, R>(this IFunction <D, R> func, params D[] plotPoints) { R[] functionPlot = new R[plotPoints.Length]; for (int i = 0; i < functionPlot.Length; i++) { functionPlot[i] = func.Compute(plotPoints[i]); } return(plotPoints, functionPlot); }
public static Matrix <T> Plot <T>(this IFunction <T, T> func, params T[] plotPoints) where T : Field <T>, new() { Matrix <T> functionPlot = new Matrix <T>(2, plotPoints.Length); for (int i = 0; i < functionPlot.Columns; i++) { functionPlot[0, i] = plotPoints[i]; functionPlot[1, i] = func.Compute(plotPoints[i]); } return(functionPlot); }
public double Compute(params int[] values) { var result = _cache[values]; if (result != null) { return((double)result); } result = _function.Compute(values); _cache[values] = result; return((double)result); }
public void Filter(IList <MaxTreeNode <ElementType> > list_bottom_to_top) { foreach (IMaxTreeNode <ElementType> current_node in list_bottom_to_top) { if (filter_function.Compute(current_node) && (current_node.Parent != null)) { current_node.DisplayValue = current_node.Parent.DisplayValue; } else { current_node.DisplayValue = current_node.Value; } } }
public Bitmap Render(IImageRaster <IRaster2DInteger, ElementType> source_image) { IRaster2DInteger raster = source_image.Raster; Bitmap destination_image = new Bitmap(raster.Size0, raster.Size1); for (int index_y = 0; index_y < raster.Size1; index_y++) { for (int index_x = 0; index_x < raster.Size0; index_x++) { destination_image.SetPixel(index_x, index_y, converter.Compute(source_image.GetElementValue(raster.GetElementIndex(index_x, index_y)))); } } return(destination_image); }
public static void FillArrayRBA <TypeDomain0, TypeDomain1, TypeRange>( IFunction <TypeDomain0, TypeDomain1, TypeRange> function, TypeDomain0[] domain_0_values, TypeDomain1[] domain_1_values, TypeRange[,] range_values) { for (int index_0 = 0; index_0 < domain_0_values.Length; index_0++) { for (int index_1 = 0; index_1 < domain_1_values.Length; index_1++) { range_values[index_0, index_1] = function.Compute(domain_0_values[index_0], domain_1_values[index_1]); } } }
public static ImageRaster <CommonRasterType, ToElementType> Convert <CommonRasterType, FromElementType, ToElementType>( IImageRaster <CommonRasterType, FromElementType> source, IFunction <FromElementType, ToElementType> converter) where CommonRasterType : IRasterInteger { CommonRasterType raster = source.Raster; int element_count = raster.ElementCount; ToElementType [] image = new ToElementType[element_count]; Parallel.For(0, element_count, element_index => { image[element_index] = converter.Compute(source.GetElementValue(element_index)); }); return(new ImageRaster <CommonRasterType, ToElementType>(raster, image, false)); }
public IFunction <double, double> ConvolveFunction(IFunction <double, double> base_function, IFunction <double, double> kernel_function, double base_domain_min, double base_domain_max, double kernel_domain_min, double kernel_domain_max) { int base_sample_count = ((int)((base_domain_max - base_domain_min) / sample_time)) + 1; double[] base_sample_times = new double[base_sample_count]; double[] list_base = new double[base_sample_count]; base_sample_times[0] = base_domain_min; list_base[0] = base_function.Compute(base_sample_times[0]); for (int sample_index = 1; sample_index < base_sample_count; sample_index++) { base_sample_times[sample_index] = base_sample_times[sample_index - 1] + this.sample_time; list_base[sample_index] = base_function.Compute(base_sample_times[sample_index]); } int kernel_sample_count = ((int)((kernel_domain_max - kernel_domain_min) / sample_time)) + 1; double[] list_kernel = new double[kernel_sample_count]; double kernel_sample_time = kernel_domain_min; for (int sample_index = 0; sample_index < kernel_sample_count; sample_index++) { list_kernel[sample_index] = kernel_function.Compute(kernel_sample_time); kernel_sample_time += sample_time; } double[] result = ConvolveUniform(list_base, list_kernel); if (sample_time != 1.0) { for (int sample_index = 0; sample_index < base_sample_count; sample_index++) { result[sample_index] *= this.sample_time; } } return(new FunctionMultiStep <double, double>(base_sample_times, result)); }
private Dictionary <List <BinaryChromosome>, double> GetFunctionValueByChromosome(List <BinaryChromosome> chromosomes, Range range, double bitsInChromosome) { var valuesByChromosomes = new Dictionary <List <BinaryChromosome>, double>(chromosomes.Count); for (int i = 0; i < chromosomes.Count; i += 2) { valuesByChromosomes.Add(new List <BinaryChromosome> { chromosomes[i], chromosomes[i + 1] }, function.Compute( BinaryUtils.BinaryToDecimalRepresentation(chromosomes[i].BinaryRepresentation, range, bitsInChromosome), BinaryUtils.BinaryToDecimalRepresentation(chromosomes[i + 1].BinaryRepresentation, range, bitsInChromosome))); } return(valuesByChromosomes); }
private static void GenerateOptionsAdd(ISet <int> all, ISet <int> current, List <ISet <int> > options, IFunction <ISet <int>, bool> constraints) { foreach (int to_add in all) { if (!current.Contains(to_add)) { ISet <int> option = new HashSet <int>(current); option.Add(to_add); if (constraints.Compute(option)) { options.Add(option); } } } }
// static vertex constructor public Simplex(double[] initial_parameters, IFunction <double[], double> minimization_function, IFunction <double[], bool> validation_function) { ParameterCount = initial_parameters.Length; VertexCount = 1; this.vertexes = ToolsCollection.CreateArrayArray <double>(VertexCount, ParameterCount); this.vertex_values = new double[1]; for (int parameter_index = 0; parameter_index < ParameterCount; parameter_index++) { this.vertexes[0][parameter_index] = initial_parameters[parameter_index]; } if (validation_function.Compute(initial_parameters)) { SetVertexValue(0, minimization_function.Compute(initial_parameters)); } LargestVertexIndex = 0; }
public BitmapSource Render(RenderSourceType render_source) { IImageRaster <IRaster2DInteger, ElementValueType> rendered_image = inner_renderer.Render(render_source); BitmapFast bitmap_fast = new BitmapFast(rendered_image.Raster.Size0, rendered_image.Raster.Size1); bitmap_fast.Lock(); for (int y_index = 0; y_index < rendered_image.Raster.Size1; y_index++) { for (int x_index = 0; x_index < rendered_image.Raster.Size0; x_index++) { bitmap_fast.SetPixel(x_index, y_index, converter.Compute(rendered_image.GetElementValue(rendered_image.Raster.GetElementIndex(x_index, y_index)))); } } bitmap_fast.Unlock(); return(ToolsRendering.CreateBitmapSourceFromBitmap(bitmap_fast.Bitmap)); }