/** * Returns the index of the sample with specified value. If this * sampling has a sample value that equals (to within the sampling * tolerance) the specified value, then this method returns the * index of that sample. Otherwise, this method returns -1. * @param x the value. * @return the index of the matching sample; -1, if none. */ public int indexOf(double x) { int i = -1; if (isUniform()) { int j = (int)Math.round((x-_f)/_d); if (0<=j && j<_n && almostEqual(x,_f+j*_d,_td)) i = j; } else { int j = binarySearch(_v,x); if (0<=j) { i = j; } else { j = -(j+1); if (j>0 && almostEqual(x,_v[j-1],_td)) { i = j-1; } else if (j<_n && almostEqual(x,_v[j],_td)) { i = j; } } } return i; }
/** * Constructs a sampling from the specified array of values and tolerance. * The values must be strictly increasing. * <p> * The constructed sampling may or may not be uniform, depending on the * specified values and tolerance. If uniform (to within the specified * tolerance), then the array of values is discarded, and the sampling is * represented by the count, sampling interval, and first sample value. * @param v the array of sampling values; must have non-zero length. * @param t the sampling tolerance, expressed as fraction of delta. */ public Sampling(double[] v, double t) { Check.argument(v.length>0,"v.length>0"); Check.argument(isIncreasing(v),"v is increasing"); _n = v.length; _d = (_n<2)?1.0:(v[_n-1]-v[0])/(_n-1); _f = v[0]; _t = t; _td = _t*_d; boolean uniform = true; for (int i=0; i<_n && uniform; ++i) { double vi = _f+i*_d; if (!almostEqual(v[i],vi,_td)) uniform = false; } _v = (uniform)?null: copy(v); }
/** * Determines whether this sampling is equivalent to the specified sampling. * Two samplings are equivalent if each of their sample values differs by * no more than the sampling tolerance. * @param s the sampling to compare to this sampling. * @return true, if equivalent; false, otherwise. */ public boolean isEquivalentTo(Sampling s) { Sampling t = this; if (t.isUniform()!=s.isUniform()) return false; if (t.isUniform()) { if (t.getCount()!=s.getCount()) return false; double tiny = tinyWith(s); double tf = t.getFirst(); double tl = t.getLast(); double sf = s.getFirst(); double sl = s.getLast(); return almostEqual(tf,sf,tiny) && almostEqual(tl,sl,tiny); } else { double tiny = tinyWith(s); for (int i=0; i<_n; ++i) { if (!almostEqual(_v[i],s.value(i),tiny)) return false; } return true; } }
double tiny = tinyWith(s); for (jt=it,js=is; jt!=mt; ++jt,++js) { if (!almostEqual(t.value(jt),s.value(js),tiny)) return false;
double tiny = tinyWith(s); for (jt=it,js=is; jt!=mt; ++jt,++js) { if (!almostEqual(t.value(jt),s.value(js),tiny)) return null;