/** * 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 bool 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); } }
/** * Determines the overlap between this sampling and the specified sampling. * Both the specified sampling and this sampling represent a first-to-last * range of sample values. The overlap is a contiguous set of samples that * have values that are equal, to within the minimum sampling tolerance of * the two samplings. This set is represented by an array of three ints, * {n,it,is}, where n is the number of overlapping samples, and it and is * denote the indices of the first samples in the overlapping parts of this * sampling (t) and the specified sampling (s). There exist three cases. * <ul><li> * The ranges of sample values overlap, and all values in the overlapping * parts are equivalent. In this case, the two samplings are compatible; * and this method returns the array {n,it,is}, as described * above. * </li><li> * The ranges of sample values in the two samplings do not overlap. * In this case, the two samplings are compatible; and this method * returns either {0,nt,0} or {0,0,ns}, depending on whether all * sample values in this sampling are less than or greater than * those in the specified sampling, respectively. * </li><li> * The ranges of sample values overlap, but not all values in the * overlapping parts are equivalent. In this case, the two samplings * are incompatible; and this method returns null. * </li></ul> * @param s the sampling to compare with this sampling. * @return the array {n,it,is} that describes the overlap; null, if * the specified sampling is incompatible with this sampling. */ public int[] overlapWith(Sampling s) { Sampling t = this; int nt = t.getCount(); int ns = s.getCount(); double tf = t.getFirst(); double sf = s.getFirst(); double tl = t.getLast(); double sl = s.getLast(); int it = 0; int IS = 0; int jt = nt - 1; int js = ns - 1; if (tl < sf) { return(new int[] { 0, nt, 0 }); } else if (sl < tf) { return(new int[] { 0, 0, ns }); } else { if (tf < sf) { it = t.indexOf(sf); } else { IS = s.indexOf(tf); } if (it < 0 || IS < 0) { return(null); } if (tl < sl) { js = s.indexOf(tl); } else { jt = t.indexOf(sl); } if (jt < 0 || js < 0) { return(null); } int mt = 1 + jt - it; int ms = 1 + js - IS; if (mt != ms) { return(null); } if (!t.isUniform() || !s.isUniform()) { double tiny = tinyWith(s); for (jt = it, js = IS; jt != mt; ++jt, ++js) { if (!almostEqual(t.value(jt), s.value(js), tiny)) { return(null); } } } return(new int[] { mt, it, IS }); } }