All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.venky.clustering.euclidean.EuclideanCenterFinder Maven / Gradle / Ivy

There is a newer version: 1.18
Show newest version
package com.venky.clustering.euclidean;

import java.util.ArrayList;
import java.util.List;

import com.venky.clustering.CenterFinder;
import com.venky.clustering.Cluster;
import com.venky.core.util.Bucket;

public class EuclideanCenterFinder implements CenterFinder{
	Cluster cluster;
	public EuclideanCenterFinder(Cluster cluster) {
		this.cluster = cluster;
	}
	EuclideanPoint center;
	@Override
	public EuclideanPoint center() {
		
		List coordinates = new ArrayList();
		double numPoints = cluster.getPoints().size();
		
		for (EuclideanPoint p : cluster.getPoints()){
			int dim = p.coordinates.length;
			while (coordinates.size() < dim){
				coordinates.add(new Bucket());
			}
			for (int i = 0 ; i < dim ; i ++ ){
				coordinates.get(i).increment(p.coordinates[i]/numPoints);
			}
		}
		
		center = new EuclideanPoint(coordinates.toArray(new Bucket[]{}));
		return center;
	}

	@Override
	public EuclideanPoint center(EuclideanPoint newPoint) {
		List coordinates = new ArrayList();
		
		int dim = Math.max(center.coordinates.length,newPoint.coordinates.length);

		while (coordinates.size() < dim){
			coordinates.add(new Bucket());
		}
		
		double numOldPoints = cluster.getPoints().size() - 1;
		for (int i = 0 ; i < dim ; i ++ ){
			double c = ( center.coordinates[i] * numOldPoints + newPoint.coordinates[i] )/ ( numOldPoints + 1 ) ;
			coordinates.get(i).increment(c);
		}
		
		center= new EuclideanPoint(coordinates.toArray(new Bucket[]{}));
		return center;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy