public static double angle ( NumberVector v1 , NumberVector v2 , NumberVector o ) { final int dim1 = v1 . getDimensionality ( ) , dim2 = v2 . getDimensionality ( ) , dimo = o . getDimensionality ( ) ; final int mindim = ( dim1 <= dim2 ) ? dim1 : dim2 ; // Essentially, we want to compute this: // v1' = v1 - o, v2' = v2 - o // v1'.transposeTimes(v2') / (v1'.euclideanLength()*v2'.euclideanLength()); // We can just compute all three in parallel. double cross = 0 , l1 = 0 , l2 = 0 ; for ( int k = 0 ; k < mindim ; k ++ ) { final double ok = k < dimo ? o . doubleValue ( k ) : 0. ; final double r1 = v1 . doubleValue ( k ) - ok ; final double r2 = v2 . doubleValue ( k ) - ok ; cross += r1 * r2 ; l1 += r1 * r1 ; l2 += r2 * r2 ; } for ( int k = mindim ; k < dim1 ; k ++ ) { final double ok = k < dimo ? o . doubleValue ( k ) : 0. ; final double r1 = v1 . doubleValue ( k ) - ok ; l1 += r1 * r1 ; } for ( int k = mindim ; k < dim2 ; k ++ ) { final double ok = k < dimo ? o . doubleValue ( k ) : 0. ; final double r2 = v2 . doubleValue ( k ) - ok ; l2 += r2 * r2 ; } final double a = ( cross == 0. ) ? 0. : // ( l1 == 0. || l2 == 0. ) ? 1. : // FastMath . sqrt ( ( cross / l1 ) * ( cross / l2 ) ) ; return ( a < 1. ) ? a : 1. ; }
Compute the angle between two vectors with respect to a reference point .