public Instance sample ( Random random ) { // Create sample in hypersphere coordinates //get the center through getCenter so subclass have a chance double [ ] center = getCenter ( ) ; final int dimensions = center . length ; final double sin [ ] = new double [ dimensions - 1 ] ; final double cos [ ] = new double [ dimensions - 1 ] ; final double length = random . nextDouble ( ) * getRadius ( ) ; double lastValue = 1.0 ; for ( int i = 0 ; i < dimensions - 1 ; i ++ ) { double angle = random . nextDouble ( ) * 2 * Math . PI ; sin [ i ] = lastValue * Math . sin ( angle ) ; // Store cumulative values cos [ i ] = Math . cos ( angle ) ; lastValue = sin [ i ] ; } // Calculate cartesian coordinates double res [ ] = new double [ dimensions ] ; // First value uses only cosines res [ 0 ] = center [ 0 ] + length * cos [ 0 ] ; // Loop through 'middle' coordinates which use cosines and sines for ( int i = 1 ; i < dimensions - 1 ; i ++ ) { res [ i ] = center [ i ] + length * sin [ i - 1 ] * cos [ i ] ; } // Last value uses only sines res [ dimensions - 1 ] = center [ dimensions - 1 ] + length * sin [ dimensions - 2 ] ; return new DenseInstance ( 1.0 , res ) ; }
Samples this cluster by returning a point from inside it .