本文整理汇总了Java中net.jafama.FastMath.sqrt方法的典型用法代码示例。如果您正苦于以下问题:Java FastMath.sqrt方法的具体用法?Java FastMath.sqrt怎么用?Java FastMath.sqrt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.jafama.FastMath
的用法示例。
在下文中一共展示了FastMath.sqrt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: prepare
import net.jafama.FastMath; //导入方法依赖的package包/类
@Override
public <A> void prepare(A array, NumberArrayAdapter<?, A> adapter) {
if(pmin == null || pmax == null) {
DoubleMinMax mm = new DoubleMinMax();
final int size = adapter.size(array);
for(int i = 0; i < size; i++) {
double val = adapter.getDouble(array, i);
if(!Double.isInfinite(val)) {
mm.put(val);
}
}
min = (pmin == null) ? mm.getMin() : pmin;
max = (pmax == null) ? mm.getMax() : pmax;
}
factor = FastMath.sqrt(max - min);
}
示例2: uncenteredCorrelation
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Compute the uncentered correlation of two vectors.
*
* @param x first NumberVector
* @param y second NumberVector
* @return the uncentered correlation coefficient for x and y
*/
public static double uncenteredCorrelation(NumberVector x, NumberVector y) {
final int xdim = x.getDimensionality(), ydim = y.getDimensionality();
if(xdim != ydim) {
throw new IllegalArgumentException("Invalid arguments: number vectors differ in dimensionality.");
}
double sumXX = 0., sumYY = 0., sumXY = 0.;
for(int i = 0; i < xdim; i++) {
final double xv = x.doubleValue(i), yv = y.doubleValue(i);
sumXX += xv * xv;
sumYY += yv * yv;
sumXY += xv * yv;
}
// One or both series were constant:
if(!(sumXX > 0. && sumYY > 0.)) {
return (sumXX == sumYY) ? 1. : 0.;
}
return sumXY / FastMath.sqrt(sumXX * sumYY);
}
示例3: distance
import net.jafama.FastMath; //导入方法依赖的package包/类
@Override
public double distance(NumberVector v1, NumberVector v2) {
final int dim = dimensionality(v1, v2);
double agg = 0.;
for(int d = 0; d < dim; d++) {
final double xd = v1.doubleValue(d), yd = v2.doubleValue(d);
if(xd == yd) {
continue;
}
final double md = .5 * (xd + yd);
if(!(md > 0. || md < 0.)) {
continue;
}
if(xd > 0.) {
agg += xd * FastMath.log(xd / md);
}
if(yd > 0.) {
agg += yd * FastMath.log(yd / md);
}
}
return FastMath.sqrt(agg);
}
示例4: recomputeSeperation
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Recompute the separation of cluster means.
*
* @param means Means
* @param sep Output array of separation
* @param cdist Center-to-Center distances
*/
private void recomputeSeperation(double[][] means, double[] sep, double[][] cdist) {
final int k = means.length;
assert (sep.length == k);
boolean issquared = distanceFunction.isSquared();
Arrays.fill(sep, Double.POSITIVE_INFINITY);
for(int i = 1; i < k; i++) {
DoubleVector mi = DoubleVector.wrap(means[i]);
for(int j = 0; j < i; j++) {
double d = distanceFunction.distance(mi, DoubleVector.wrap(means[j]));
d = issquared ? FastMath.sqrt(d) : d;
d *= .5;
cdist[i][j] = d;
cdist[j][i] = d;
sep[i] = (d < sep[i]) ? d : sep[i];
sep[j] = (d < sep[j]) ? d : sep[j];
}
}
}
示例5: minDist
import net.jafama.FastMath; //导入方法依赖的package包/类
@Override
public double minDist(SpatialComparable mbr1, SpatialComparable mbr2) {
final int dim1 = mbr1.getDimensionality(), dim2 = mbr2.getDimensionality();
final int mindim = (dim1 < dim2) ? dim1 : dim2;
final NumberVector v1 = (mbr1 instanceof NumberVector) ? (NumberVector) mbr1 : null;
final NumberVector v2 = (mbr2 instanceof NumberVector) ? (NumberVector) mbr2 : null;
double agg = (v1 != null) //
? (v2 != null) ? preDistance(v1, v2, 0, mindim) : preDistanceVM(v1, mbr2, 0, mindim) //
: (v2 != null) ? preDistanceVM(v2, mbr1, 0, mindim) : preDistanceMBR(mbr1, mbr2, 0, mindim);
// first object has more dimensions.
if(dim1 > mindim) {
agg += (v1 != null) ? preNorm(v1, mindim, dim1) : preNormMBR(mbr1, mindim, dim1);
}
// second object has more dimensions.
if(dim2 > mindim) {
agg += (v2 != null) ? preNorm(v2, mindim, dim2) : preNormMBR(mbr2, mindim, dim2);
}
return FastMath.sqrt(agg);
}
示例6: naiveQueryDense
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Query the most similar objects, dense version.
*
* @param obj Query object
* @param scores Score storage
* @param cands Non-zero objects set
* @return Result
*/
private double naiveQueryDense(NumberVector obj, WritableDoubleDataStore scores, HashSetModifiableDBIDs cands) {
double len = 0.; // Length of query object, for final normalization
for(int dim = 0, max = obj.getDimensionality(); dim < max; dim++) {
final double val = obj.doubleValue(dim);
if(val == 0. || val != val) {
continue;
}
len += val * val;
// No matching documents in index:
if(dim >= index.size()) {
continue;
}
ModifiableDoubleDBIDList column = index.get(dim);
for(DoubleDBIDListIter n = column.iter(); n.valid(); n.advance()) {
scores.increment(n, n.doubleValue() * val);
cands.add(n);
}
}
return FastMath.sqrt(len);
}
示例7: minDist
import net.jafama.FastMath; //导入方法依赖的package包/类
@Override
public double minDist(SpatialComparable mbr1, SpatialComparable mbr2) {
if(mbr1.getDimensionality() != mbr2.getDimensionality()) {
throw new IllegalArgumentException("Different dimensionality of objects\n " + "first argument: " + mbr1.toString() + "\n " + "second argument: " + mbr2.toString());
}
double sqrDist = 0;
for(int d = BitsUtil.nextSetBit(dimensions, 0); d >= 0; d = BitsUtil.nextSetBit(dimensions, d + 1)) {
final double delta;
final double max1 = mbr1.getMax(d);
final double min2 = mbr2.getMin(d);
if(max1 < min2) {
delta = min2 - max1;
}
else {
final double min1 = mbr1.getMin(d);
final double max2 = mbr2.getMax(d);
if(min1 > max2) {
delta = min1 - max2;
}
else { // The mbrs intersect!
continue;
}
}
sqrDist += delta * delta;
}
return FastMath.sqrt(sqrDist);
}
示例8: entropyNMISqrt
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Get the sqrt-normalized mutual information (normalized, 0 = unequal)
*
* @return Sqrt Normalized Mutual information
*/
public double entropyNMISqrt() {
if(entropyFirst() * entropySecond() <= 0) {
return entropyMutualInformation();
}
return (entropyMutualInformation() / FastMath.sqrt(entropyFirst() * entropySecond()));
}
示例9: calculate_MDEF_norm
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Method for the MDEF calculation
*
* @param sn Sampling Neighborhood
* @param cg Counting Neighborhood
*
* @return MDEF norm
*/
private static double calculate_MDEF_norm(Node sn, Node cg) {
// get the square sum of the counting neighborhoods box counts
long sq = sn.getSquareSum(cg.getLevel() - sn.getLevel());
/*
* if the square sum is equal to box count of the sampling Neighborhood then
* n_hat is equal one, and as cg needs to have at least one Element mdef
* would get zero or lower than zero. This is the case when all of the
* counting Neighborhoods contain one or zero Objects. Additionally, the
* cubic sum, square sum and sampling Neighborhood box count are all equal,
* which leads to sig_n_hat being zero and thus mdef_norm is either negative
* infinite or undefined. As the distribution of the Objects seem quite
* uniform, a mdef_norm value of zero ( = no outlier) is appropriate and
* circumvents the problem of undefined values.
*/
if(sq == sn.getCount()) {
return 0.0;
}
// calculation of mdef according to the paper and standardization as done in
// LOCI
long cb = sn.getCubicSum(cg.getLevel() - sn.getLevel());
double n_hat = (double) sq / sn.getCount();
double sig_n_hat = FastMath.sqrt(cb * sn.getCount() - (sq * sq)) / sn.getCount();
// Avoid NaN - correct result 0.0?
if(sig_n_hat < Double.MIN_NORMAL) {
return 0.0;
}
double mdef = n_hat - cg.getCount();
return mdef / sig_n_hat;
}
示例10: coefficient
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Compute the Pearson product-moment correlation coefficient for two
* NumberVectors.
*
* @param x first NumberVector
* @param y second NumberVector
* @return the Pearson product-moment correlation coefficient for x and y
*/
public static double coefficient(NumberVector x, NumberVector y) {
final int xdim = x.getDimensionality();
final int ydim = y.getDimensionality();
if(xdim != ydim) {
throw new IllegalArgumentException("Invalid arguments: number vectors differ in dimensionality.");
}
if(xdim == 0) {
throw new IllegalArgumentException("Empty vector.");
}
// Inlined computation of Pearson correlation, to avoid allocating objects!
// This is a numerically stabilized version, avoiding sum-of-squares.
double sumXX = 0., sumYY = 0., sumXY = 0.;
double sumX = x.doubleValue(0), sumY = y.doubleValue(0);
int i = 1;
while(i < xdim) {
final double xv = x.doubleValue(i), yv = y.doubleValue(i);
// Delta to previous mean
final double deltaX = xv * i - sumX;
final double deltaY = yv * i - sumY;
// Increment count first
final double oldi = i; // Convert to double!
++i;
final double f = 1. / (i * oldi);
// Update
sumXX += f * deltaX * deltaX;
sumYY += f * deltaY * deltaY;
// should equal deltaY * neltaX!
sumXY += f * deltaX * deltaY;
// Update sums
sumX += xv;
sumY += yv;
}
// One or both series were constant:
if(!(sumXX > 0. && sumYY > 0.)) {
return (sumXX == sumYY) ? 1. : 0.;
}
return sumXY / FastMath.sqrt(sumXX * sumYY);
}
示例11: relevantSubspace
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Subspace relevance test.
*
* @param subspace Subspace to test
* @param neigh Neighbor list
* @param kernel Kernel density estimator
* @return relevance test result
*/
protected boolean relevantSubspace(long[] subspace, DoubleDBIDList neigh, KernelDensityEstimator kernel) {
Relation<V> relation = kernel.relation;
final double crit = K_S_CRITICAL001 / FastMath.sqrt(neigh.size());
for(int dim = BitsUtil.nextSetBit(subspace, 0); dim > 0; dim = BitsUtil.nextSetBit(subspace, dim + 1)) {
// TODO: can we save this copy somehow?
double[] data = new double[neigh.size()];
{
int count = 0;
for(DBIDIter neighbor = neigh.iter(); neighbor.valid(); neighbor.advance()) {
V vector = relation.get(neighbor);
data[count] = vector.doubleValue(dim);
count++;
}
assert (count == neigh.size());
}
Arrays.sort(data);
final double norm = data[data.length - 1] - data[0];
final double min = data[0];
// Kolmogorow-Smirnow-Test against uniform distribution:
for(int j = 1; j < data.length - 2; j++) {
double delta = (j / (data.length - 1.)) - ((data[j] - min) / norm);
if(Math.abs(delta) > crit) {
return false;
}
}
}
return true;
}
示例12: maxMoved
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Maximum distance moved.
*
* @param means Old means
* @param newmeans New means
* @param dists Distances moved
* @return Maximum distance moved
*/
private double maxMoved(double[][] means, double[][] newmeans, double[] dists) {
assert (means.length == k);
assert (newmeans.length == k);
assert (dists.length == k);
boolean issquared = distanceFunction.isSquared();
double max = 0.;
for(int i = 0; i < k; i++) {
double d = distanceFunction.distance(DoubleVector.wrap(means[i]), DoubleVector.wrap(newmeans[i]));
d = issquared ? FastMath.sqrt(d) : d;
dists[i] = d;
max = (d > max) ? d : max;
}
return max;
}
示例13: getCorrelation
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Get the Pearson correlation value.
*
* @return Correlation value
*/
public double getCorrelation() {
if(!(sumXX > 0. && sumYY > 0.)) {
return (sumXX == sumYY) ? 1. : 0.;
}
return sumXY / FastMath.sqrt(sumXX * sumYY);
}
示例14: ecefToLatRad
import net.jafama.FastMath; //导入方法依赖的package包/类
@Override
public double ecefToLatRad(double x, double y, double z) {
final double p = FastMath.sqrt(x * x + y * y);
return FastMath.atan2(z, p);
}
示例15: minCosAngle
import net.jafama.FastMath; //导入方法依赖的package包/类
/**
* Compute the minimum angle between two rectangles.
*
* @param v1 first rectangle
* @param v2 second rectangle
* @return Angle
*/
public static double minCosAngle(SpatialComparable v1, SpatialComparable v2) {
if(v1 instanceof NumberVector && v2 instanceof NumberVector) {
return cosAngle((NumberVector) v1, (NumberVector) v2);
}
final int dim1 = v1.getDimensionality(), dim2 = v2.getDimensionality();
final int mindim = (dim1 <= dim2) ? dim1 : dim2;
// Essentially, we want to compute this:
// absmax(v1.transposeTimes(v2))/(min(v1.euclideanLength())*min(v2.euclideanLength()));
// We can just compute all three in parallel.
double s1 = 0, s2 = 0, l1 = 0, l2 = 0;
for(int k = 0; k < mindim; k++) {
final double min1 = v1.getMin(k), max1 = v1.getMax(k);
final double min2 = v2.getMin(k), max2 = v2.getMax(k);
final double p1 = min1 * min2, p2 = min1 * max2;
final double p3 = max1 * min2, p4 = max1 * max2;
s1 += Math.max(Math.max(p1, p2), Math.max(p3, p4));
s2 += Math.min(Math.min(p1, p2), Math.min(p3, p4));
if(max1 < 0) {
l1 += max1 * max1;
}
else if(min1 > 0) {
l1 += min1 * min1;
} // else: 0
if(max2 < 0) {
l2 += max2 * max2;
}
else if(min2 > 0) {
l2 += min2 * min2;
} // else: 0
}
for(int k = mindim; k < dim1; k++) {
final double min1 = v1.getMin(k), max1 = v1.getMax(k);
if(max1 < 0.) {
l1 += max1 * max1;
}
else if(min1 > 0.) {
l1 += min1 * min1;
} // else: 0
}
for(int k = mindim; k < dim2; k++) {
final double min2 = v2.getMin(k), max2 = v2.getMax(k);
if(max2 < 0.) {
l2 += max2 * max2;
}
else if(min2 > 0.) {
l2 += min2 * min2;
} // else: 0
}
final double cross = Math.max(s1, Math.abs(s2));
final double a = (cross == 0.) ? 0. : //
(l1 == 0. || l2 == 0.) ? 1. : //
FastMath.sqrt((cross / l1) * (cross / l2));
return (a < 1.) ? a : 1.;
}