code
stringlengths
73
34.1k
label
stringclasses
1 value
@Override public ImageSource apply(ImageSource input) { final int[][] pixelMatrix = new int[3][3]; int w = input.getWidth(); int h = input.getHeight(); int[][] output = new int[h][w]; for (int j = 1; j < h - 1; j++) { for (int i = 1; i < w - 1; i++) { pixelMatrix[0][0] = input.getR(i - 1, j - 1); pixelMatrix[0][1] = input.getRGB(i - 1, j); pixelMatrix[0][2] = input.getRGB(i - 1, j + 1); pixelMatrix[1][0] = input.getRGB(i, j - 1); pixelMatrix[1][2] = input.getRGB(i, j + 1); pixelMatrix[2][0] = input.getRGB(i + 1, j - 1); pixelMatrix[2][1] = input.getRGB(i + 1, j); pixelMatrix[2][2] = input.getRGB(i + 1, j + 1); int edge = (int) convolution(pixelMatrix); int rgb = (edge << 16 | edge << 8 | edge); output[j][i] = rgb; } } MatrixSource source = new MatrixSource(output); return source; }
java
public <L extends Listener> void popEvent(Event<?, L> expected) { synchronized (this.stack) { final Event<?, ?> actual = this.stack.pop(); if (actual != expected) { throw new IllegalStateException(String.format( "Unbalanced pop: expected '%s' but encountered '%s'", expected.getListenerClass(), actual)); } } }
java
protected void modify(Transaction t) { try { this.lock.writeLock().lock(); t.perform(); } finally { this.lock.writeLock().unlock(); } }
java
protected <E> E read(Supplier<E> sup) { try { this.lock.readLock().lock(); return sup.get(); } finally { this.lock.readLock().unlock(); } }
java
protected void setOffsetAndLength(long offset, int length) throws IOException { this.offset = offset; this.length = length; this.position = 0; if (subStream.position() != offset) { subStream.seek(offset); } }
java
public static double J0(double x) { double ax; if ((ax = Math.abs(x)) < 8.0) { double y = x * x; double ans1 = 57568490574.0 + y * (-13362590354.0 + y * (651619640.7 + y * (-11214424.18 + y * (77392.33017 + y * (-184.9052456))))); double ans2 = 57568490411.0 + y * (1029532985.0 + y * (9494680.718 + y * (59272.64853 + y * (267.8532712 + y * 1.0)))); return ans1 / ans2; } else { double z = 8.0 / ax; double y = z * z; double xx = ax - 0.785398164; double ans1 = 1.0 + y * (-0.1098628627e-2 + y * (0.2734510407e-4 + y * (-0.2073370639e-5 + y * 0.2093887211e-6))); double ans2 = -0.1562499995e-1 + y * (0.1430488765e-3 + y * (-0.6911147651e-5 + y * (0.7621095161e-6 - y * 0.934935152e-7))); return Math.sqrt(0.636619772 / ax) * (Math.cos(xx) * ans1 - z * Math.sin(xx) * ans2); } }
java
public static double J(int n, double x) { int j, m; double ax, bj, bjm, bjp, sum, tox, ans; boolean jsum; double ACC = 40.0; double BIGNO = 1.0e+10; double BIGNI = 1.0e-10; if (n == 0) return J0(x); if (n == 1) return J(x); ax = Math.abs(x); if (ax == 0.0) return 0.0; else if (ax > (double) n) { tox = 2.0 / ax; bjm = J0(ax); bj = J(ax); for (j = 1; j < n; j++) { bjp = j * tox * bj - bjm; bjm = bj; bj = bjp; } ans = bj; } else { tox = 2.0 / ax; m = 2 * ((n + (int) Math.sqrt(ACC * n)) / 2); jsum = false; bjp = ans = sum = 0.0; bj = 1.0; for (j = m; j > 0; j--) { bjm = j * tox * bj - bjp; bjp = bj; bj = bjm; if (Math.abs(bj) > BIGNO) { bj *= BIGNI; bjp *= BIGNI; ans *= BIGNI; sum *= BIGNI; } if (jsum) sum += bj; jsum = !jsum; if (j == n) ans = bjp; } sum = 2.0 * sum - bj; ans /= sum; } return x < 0.0 && n % 2 == 1 ? -ans : ans; }
java
public static double Y0(double x) { if (x < 8.0) { double y = x * x; double ans1 = -2957821389.0 + y * (7062834065.0 + y * (-512359803.6 + y * (10879881.29 + y * (-86327.92757 + y * 228.4622733)))); double ans2 = 40076544269.0 + y * (745249964.8 + y * (7189466.438 + y * (47447.26470 + y * (226.1030244 + y * 1.0)))); return (ans1 / ans2) + 0.636619772 * J0(x) * Math.log(x); } else { double z = 8.0 / x; double y = z * z; double xx = x - 0.785398164; double ans1 = 1.0 + y * (-0.1098628627e-2 + y * (0.2734510407e-4 + y * (-0.2073370639e-5 + y * 0.2093887211e-6))); double ans2 = -0.1562499995e-1 + y * (0.1430488765e-3 + y * (-0.6911147651e-5 + y * (0.7621095161e-6 + y * (-0.934945152e-7)))); return Math.sqrt(0.636619772 / x) * (Math.sin(xx) * ans1 + z * Math.cos(xx) * ans2); } }
java
public static double Y(double x) { if (x < 8.0) { double y = x * x; double ans1 = x * (-0.4900604943e13 + y * (0.1275274390e13 + y * (-0.5153438139e11 + y * (0.7349264551e9 + y * (-0.4237922726e7 + y * 0.8511937935e4))))); double ans2 = 0.2499580570e14 + y * (0.4244419664e12 + y * (0.3733650367e10 + y * (0.2245904002e8 + y * (0.1020426050e6 + y * (0.3549632885e3 + y))))); return (ans1 / ans2) + 0.636619772 * (J(x) * Math.log(x) - 1.0 / x); } else { double z = 8.0 / x; double y = z * z; double xx = x - 2.356194491; double ans1 = 1.0 + y * (0.183105e-2 + y * (-0.3516396496e-4 + y * (0.2457520174e-5 + y * (-0.240337019e-6)))); double ans2 = 0.04687499995 + y * (-0.2002690873e-3 + y * (0.8449199096e-5 + y * (-0.88228987e-6 + y * 0.105787412e-6))); return Math.sqrt(0.636619772 / x) * (Math.sin(xx) * ans1 + z * Math.cos(xx) * ans2); } }
java
public static double Y(int n, double x) { double by, bym, byp, tox; if (n == 0) return Y0(x); if (n == 1) return Y(x); tox = 2.0 / x; by = Y(x); bym = Y0(x); for (int j = 1; j < n; j++) { byp = j * tox * by - bym; bym = by; by = byp; } return by; }
java
public static double I0(double x) { double ans; double ax = Math.abs(x); if (ax < 3.75) { double y = x / 3.75; y = y * y; ans = 1.0 + y * (3.5156229 + y * (3.0899424 + y * (1.2067492 + y * (0.2659732 + y * (0.360768e-1 + y * 0.45813e-2))))); } else { double y = 3.75 / ax; ans = (Math.exp(ax) / Math.sqrt(ax)) * (0.39894228 + y * (0.1328592e-1 + y * (0.225319e-2 + y * (-0.157565e-2 + y * (0.916281e-2 + y * (-0.2057706e-1 + y * (0.2635537e-1 + y * (-0.1647633e-1 + y * 0.392377e-2)))))))); } return ans; }
java
public static double I(int n, double x) { if (n < 0) throw new IllegalArgumentException("the variable n out of range."); else if (n == 0) return I0(x); else if (n == 1) return I(x); if (x == 0.0) return 0.0; double ACC = 40.0; double BIGNO = 1.0e+10; double BIGNI = 1.0e-10; double tox = 2.0 / Math.abs(x); double bip = 0, ans = 0.0; double bi = 1.0; for (int j = 2 * (n + (int) Math.sqrt(ACC * n)); j > 0; j--) { double bim = bip + j * tox * bi; bip = bi; bi = bim; if (Math.abs(bi) > BIGNO) { ans *= BIGNI; bi *= BIGNI; bip *= BIGNI; } if (j == n) ans = bip; } ans *= I0(x) / bi; return x < 0.0 && n % 2 == 1 ? -ans : ans; }
java
@Override public ImageSource apply(ImageSource input) { int w = input.getWidth(); int h = input.getHeight(); MatrixSource output = new MatrixSource(input); Vector3 n = new Vector3(0, 0, 1); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (x < border || x == w - border || y < border || y == h - border) { output.setRGB(x, y, VectorHelper.Z_NORMAL); continue; } float s0 = input.getR(x - 1, y + 1); float s1 = input.getR(x, y + 1); float s2 = input.getR(x + 1, y + 1); float s3 = input.getR(x - 1, y); float s5 = input.getR(x + 1, y); float s6 = input.getR(x - 1, y - 1); float s7 = input.getR(x, y - 1); float s8 = input.getR(x + 1, y - 1); float nx = -(s2 - s0 + 2 * (s5 - s3) + s8 - s6); float ny = -(s6 - s0 + 2 * (s7 - s1) + s8 - s2); n.set(nx, ny, scale); n.nor(); int rgb = VectorHelper.vectorToColor(n); output.setRGB(x, y, rgb); } } return new MatrixSource(output); }
java
@Deprecated public static TraceContextHolder wrap(TraceContext traceContext) { return (traceContext != null) ? new TraceContextHolder(traceContext) : TraceContextHolder.EMPTY; }
java
public static double Sinc(double x) { return Math.sin(Math.PI * x) / (Math.PI * x); }
java
public static int Mod(int x, int m) { if (m < 0) m = -m; int r = x % m; return r < 0 ? r + m : r; }
java
public static int NextPowerOf2(int x) { --x; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return ++x; }
java
public static float Sum(float[] data) { float sum = 0; for (int i = 0; i < data.length; i++) { sum += data[i]; } return sum; }
java
public static double TruncatedPower(double value, double degree) { double x = Math.pow(value, degree); return (x > 0) ? x : 0.0; }
java
public static int[] Unique(int[] values) { HashSet<Integer> lst = new HashSet<Integer>(); for (int i = 0; i < values.length; i++) { lst.add(values[i]); } int[] v = new int[lst.size()]; Iterator<Integer> it = lst.iterator(); for (int i = 0; i < v.length; i++) { v[i] = it.next(); } return v; }
java
public void setT(int t) { this.t = Math.min((radius * 2 + 1) * (radius * 2 + 1) / 2, Math.max(0, t)); }
java
public static double Sin(double x, int nTerms) { if (nTerms < 2) return x; if (nTerms == 2) { return x - (x * x * x) / 6D; } else { double mult = x * x * x; double fact = 6; double sign = 1; int factS = 5; double result = x - mult / fact; for (int i = 3; i <= nTerms; i++) { mult *= x * x; fact *= factS * (factS - 1); factS += 2; result += sign * (mult / fact); sign *= -1; } return result; } }
java
public static double Sinh(double x, int nTerms) { if (nTerms < 2) return x; if (nTerms == 2) { return x + (x * x * x) / 6D; } else { double mult = x * x * x; double fact = 6; int factS = 5; double result = x + mult / fact; for (int i = 3; i <= nTerms; i++) { mult *= x * x; fact *= factS * (factS - 1); factS += 2; result += mult / fact; } return result; } }
java
public static double Cosh(double x, int nTerms) { if (nTerms < 2) return x; if (nTerms == 2) { return 1 + (x * x) / 2D; } else { double mult = x * x; double fact = 2; int factS = 4; double result = 1 + mult / fact; for (int i = 3; i <= nTerms; i++) { mult *= x * x; fact *= factS * (factS - 1); factS += 2; result += mult / fact; } return result; } }
java
public static double Exp(double x, int nTerms) { if (nTerms < 2) return 1 + x; if (nTerms == 2) { return 1 + x + (x * x) / 2; } else { double mult = x * x; double fact = 2; double result = 1 + x + mult / fact; for (int i = 3; i <= nTerms; i++) { mult *= x; fact *= i; result += mult / fact; } return result; } }
java
public double[][] getU() { double[][] X = new double[n][n]; double[][] U = X; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i <= j) { U[i][j] = LU[i][j]; } else { U[i][j] = 0.0; } } } return X; }
java
public double determinant() { if (m != n) { throw new IllegalArgumentException("Matrix must be square."); } double d = (double) pivsign; for (int j = 0; j < n; j++) { d *= LU[j][j]; } return d; }
java
public static ComplexNumber Add(ComplexNumber z1, ComplexNumber z2) { return new ComplexNumber(z1.real + z2.real, z1.imaginary + z2.imaginary); }
java
public static ComplexNumber Add(ComplexNumber z1, double scalar) { return new ComplexNumber(z1.real + scalar, z1.imaginary); }
java
public static ComplexNumber Subtract(ComplexNumber z1, ComplexNumber z2) { return new ComplexNumber(z1.real - z2.real, z1.imaginary - z2.imaginary); }
java
public static ComplexNumber Subtract(ComplexNumber z1, double scalar) { return new ComplexNumber(z1.real - scalar, z1.imaginary); }
java
public static double Magnitude(ComplexNumber z) { return Math.sqrt(z.real * z.real + z.imaginary * z.imaginary); }
java
public static ComplexNumber Multiply(ComplexNumber z1, ComplexNumber z2) { double z1R = z1.real, z1I = z1.imaginary; double z2R = z2.real, z2I = z2.imaginary; return new ComplexNumber(z1R * z2R - z1I * z2I, z1R * z2I + z1I * z2R); }
java
public static ComplexNumber Multiply(ComplexNumber z1, double scalar) { return new ComplexNumber(z1.real * scalar, z1.imaginary * scalar); }
java
public static ComplexNumber Divide(ComplexNumber z1, ComplexNumber z2) { ComplexNumber conj = ComplexNumber.Conjugate(z2); double a = z1.real * conj.real + ((z1.imaginary * conj.imaginary) * -1); double b = z1.real * conj.imaginary + (z1.imaginary * conj.real); double c = z2.real * conj.real + ((z2.imaginary * conj.imaginary) * -1); return new ComplexNumber(a / c, b / c); }
java
public static ComplexNumber Pow(ComplexNumber z1, double n) { double norm = Math.pow(z1.getMagnitude(), n); double angle = 360 - Math.abs(Math.toDegrees(Math.atan(z1.imaginary / z1.real))); double common = n * angle; double r = norm * Math.cos(Math.toRadians(common)); double i = norm * Math.sin(Math.toRadians(common)); return new ComplexNumber(r, i); }
java
public static ComplexNumber Sin(ComplexNumber z1) { ComplexNumber result = new ComplexNumber(); if (z1.imaginary == 0.0) { result.real = Math.sin(z1.real); result.imaginary = 0.0; } else { result.real = Math.sin(z1.real) * Math.cosh(z1.imaginary); result.imaginary = Math.cos(z1.real) * Math.sinh(z1.imaginary); } return result; }
java
public static ComplexNumber Tan(ComplexNumber z1) { ComplexNumber result = new ComplexNumber(); if (z1.imaginary == 0.0) { result.real = Math.tan(z1.real); result.imaginary = 0.0; } else { double real2 = 2 * z1.real; double imag2 = 2 * z1.imaginary; double denom = Math.cos(real2) + Math.cosh(real2); result.real = Math.sin(real2) / denom; result.imaginary = Math.sinh(imag2) / denom; } return result; }
java
private void srand(int ijkl) { u = new double[97]; int ij = ijkl / 30082; int kl = ijkl % 30082; // Handle the seed range errors // First random number seed must be between 0 and 31328 // Second seed must have a value between 0 and 30081 if (ij < 0 || ij > 31328 || kl < 0 || kl > 30081) { ij = ij % 31329; kl = kl % 30082; } int i = ((ij / 177) % 177) + 2; int j = (ij % 177) + 2; int k = ((kl / 169) % 178) + 1; int l = kl % 169; int m; double s, t; for (int ii = 0; ii < 97; ii++) { s = 0.0; t = 0.5; for (int jj = 0; jj < 24; jj++) { m = (((i * j) % 179) * k) % 179; i = j; j = k; k = m; l = (53 * l + 1) % 169; if (((l * m) % 64) >= 32) { s += t; } t *= 0.5; } u[ii] = s; } c = 362436.0 / 16777216.0; cd = 7654321.0 / 16777216.0; cm = 16777213.0 / 16777216.0; i97 = 96; j97 = 32; }
java
public float DistanceTo(IntPoint anotherPoint) { float dx = this.x - anotherPoint.x; float dy = this.y - anotherPoint.y; return (float) Math.sqrt(dx * dx + dy * dy); }
java
public static double Entropy( int[] values ){ int n = values.length; int total = 0; double entropy = 0; double p; // calculate total amount of hits for ( int i = 0; i < n; i++ ) { total += values[i]; } if ( total != 0 ) { // for all values for ( int i = 0; i < n; i++ ) { // get item's probability p = (double) values[i] / total; // calculate entropy if ( p != 0 ) entropy += ( -p * (Math.log10(p)/Math.log10(2)) ); } } return entropy; }
java
public static IntRange GetRange( int[] values, double percent ){ int total = 0, n = values.length; // for all values for ( int i = 0; i < n; i++ ) { // accumalate total total += values[i]; } int min, max, hits; int h = (int) ( total * ( percent + ( 1 - percent ) / 2 ) ); // get range min value for ( min = 0, hits = total; min < n; min++ ) { hits -= values[min]; if ( hits < h ) break; } // get range max value for ( max = n - 1, hits = total; max >= 0; max-- ) { hits -= values[max]; if ( hits < h ) break; } return new IntRange( min, max ); }
java
public static int Median( int[] values ){ int total = 0, n = values.length; // for all values for ( int i = 0; i < n; i++ ) { // accumalate total total += values[i]; } int halfTotal = total / 2; int median = 0, v = 0; // find median value for ( ; median < n; median++ ) { v += values[median]; if ( v >= halfTotal ) break; } return median; }
java
public static int Mode( int[] values ){ int mode = 0, curMax = 0; for ( int i = 0, length = values.length; i < length; i++ ) { if ( values[i] > curMax ) { curMax = values[i]; mode = i; } } return mode; }
java
public static double StdDev( int[] values, double mean ){ double stddev = 0; double diff; int hits; int total = 0; // for all values for ( int i = 0, n = values.length; i < n; i++ ) { hits = values[i]; diff = (double) i - mean; // accumulate std.dev. stddev += diff * diff * hits; // accumalate total total += hits; } return ( total == 0 ) ? 0 : Math.sqrt( stddev / (total - 1) ); }
java
public static void Forward(double[] data) { double[] result = new double[data.length]; double sum; double scale = Math.sqrt(2.0 / data.length); for (int f = 0; f < data.length; f++) { sum = 0; for (int t = 0; t < data.length; t++) { double cos = Math.cos(((2.0 * t + 1.0) * f * Math.PI) / (2.0 * data.length)); sum += data[t] * cos * alpha(f); } result[f] = scale * sum; } for (int i = 0; i < data.length; i++) { data[i] = result[i]; } }
java
public static void Forward(double[][] data) { int rows = data.length; int cols = data[0].length; double[] row = new double[cols]; double[] col = new double[rows]; for (int i = 0; i < rows; i++) { for (int j = 0; j < row.length; j++) row[j] = data[i][j]; Forward(row); for (int j = 0; j < row.length; j++) data[i][j] = row[j]; } for (int j = 0; j < cols; j++) { for (int i = 0; i < col.length; i++) col[i] = data[i][j]; Forward(col); for (int i = 0; i < col.length; i++) data[i][j] = col[i]; } }
java
public static void Backward(double[] data) { double[] result = new double[data.length]; double sum; double scale = Math.sqrt(2.0 / data.length); for (int t = 0; t < data.length; t++) { sum = 0; for (int j = 0; j < data.length; j++) { double cos = Math.cos(((2 * t + 1) * j * Math.PI) / (2 * data.length)); sum += alpha(j) * data[j] * cos; } result[t] = scale * sum; } for (int i = 0; i < data.length; i++) { data[i] = result[i]; } }
java
public void setInRGB(IntRange inRGB) { this.inRed = inRGB; this.inGreen = inRGB; this.inBlue = inRGB; CalculateMap(inRGB, outRed, mapRed); CalculateMap(inRGB, outGreen, mapGreen); CalculateMap(inRGB, outBlue, mapBlue); }
java
public void setOutRGB(IntRange outRGB) { this.outRed = outRGB; this.outGreen = outRGB; this.outBlue = outRGB; CalculateMap(inRed, outRGB, mapRed); CalculateMap(inGreen, outRGB, mapGreen); CalculateMap(inBlue, outRGB, mapBlue); }
java
private void CalculateMap(IntRange inRange, IntRange outRange, int[] map) { double k = 0, b = 0; if (inRange.getMax() != inRange.getMin()) { k = (double) (outRange.getMax() - outRange.getMin()) / (double) (inRange.getMax() - inRange.getMin()); b = (double) (outRange.getMin()) - k * inRange.getMin(); } for (int i = 0; i < 256; i++) { int v = (int) i; if (v >= inRange.getMax()) v = outRange.getMax(); else if (v <= inRange.getMin()) v = outRange.getMin(); else v = (int) (k * v + b); map[i] = v; } }
java
public static int Maximum(ImageSource fastBitmap, int startX, int startY, int width, int height) { int max = 0; if (fastBitmap.isGrayscale()) { for (int i = startX; i < height; i++) { for (int j = startY; j < width; j++) { int gray = fastBitmap.getRGB(j, i); if (gray > max) { max = gray; } } } } else { for (int i = startX; i < height; i++) { for (int j = startY; j < width; j++) { int gray = fastBitmap.getG(j, i); if (gray > max) { max = gray; } } } } return max; }
java
public static int Minimum(ImageSource fastBitmap, int startX, int startY, int width, int height) { int min = 255; if (fastBitmap.isGrayscale()) { for (int i = startX; i < height; i++) { for (int j = startY; j < width; j++) { int gray = fastBitmap.getRGB(j, i); if (gray < min) { min = gray; } } } } else { for (int i = startX; i < height; i++) { for (int j = startY; j < width; j++) { int gray = fastBitmap.getG(j, i); if (gray < min) { min = gray; } } } } return min; }
java
public static double Bhattacharyya(double[] histogram1, double[] histogram2) { int bins = histogram1.length; // histogram bins double b = 0; // Bhattacharyya's coefficient for (int i = 0; i < bins; i++) b += Math.sqrt(histogram1[i]) * Math.sqrt(histogram2[i]); // Bhattacharyya distance between the two distributions return Math.sqrt(1.0 - b); }
java
public static double ChiSquare(double[] histogram1, double[] histogram2) { double r = 0; for (int i = 0; i < histogram1.length; i++) { double t = histogram1[i] + histogram2[i]; if (t != 0) r += Math.pow(histogram1[i] - histogram2[i], 2) / t; } return 0.5 * r; }
java
public static double Correlation(double[] p, double[] q) { double x = 0; double y = 0; for (int i = 0; i < p.length; i++) { x += -p[i]; y += -q[i]; } x /= p.length; y /= q.length; double num = 0; double den1 = 0; double den2 = 0; for (int i = 0; i < p.length; i++) { num += (p[i] + x) * (q[i] + y); den1 += Math.abs(Math.pow(p[i] + x, 2)); den2 += Math.abs(Math.pow(q[i] + x, 2)); } return 1 - (num / (Math.sqrt(den1) * Math.sqrt(den2))); }
java
public static int Hamming(String first, String second) { if (first.length() != second.length()) throw new IllegalArgumentException("The size of string must be the same."); int diff = 0; for (int i = 0; i < first.length(); i++) if (first.charAt(i) != second.charAt(i)) diff++; return diff; }
java
public static double JaccardDistance(double[] p, double[] q) { double distance = 0; int intersection = 0, union = 0; for (int x = 0; x < p.length; x++) { if ((p[x] != 0) || (q[x] != 0)) { if (p[x] == q[x]) { intersection++; } union++; } } if (union != 0) distance = 1.0 - ((double) intersection / (double) union); else distance = 0; return distance; }
java
public static double JensenShannonDivergence(double[] p, double[] q) { double[] m = new double[p.length]; for (int i = 0; i < m.length; i++) { m[i] = (p[i] + q[i]) / 2; } return (KullbackLeiblerDivergence(p, m) + KullbackLeiblerDivergence(q, m)) / 2; }
java
public static double KumarJohnsonDivergence(double[] p, double[] q) { double r = 0; for (int i = 0; i < p.length; i++) { if (p[i] != 0 && q[i] != 0) { r += Math.pow(p[i] * p[i] - q[i] * q[i], 2) / 2 * Math.pow(p[i] * q[i], 1.5); } } return r; }
java
public static double KullbackLeiblerDivergence(double[] p, double[] q) { boolean intersection = false; double k = 0; for (int i = 0; i < p.length; i++) { if (p[i] != 0 && q[i] != 0) { intersection = true; k += p[i] * Math.log(p[i] / q[i]); } } if (intersection) return k; else return Double.POSITIVE_INFINITY; }
java
public static double SquaredEuclidean(double[] x, double[] y) { double d = 0.0, u; for (int i = 0; i < x.length; i++) { u = x[i] - y[i]; d += u * u; } return d; }
java
public static double SymmetricChiSquareDivergence(double[] p, double[] q) { double r = 0; for (int i = 0; i < p.length; i++) { double den = p[i] * q[i]; if (den != 0) { double p1 = p[i] - q[i]; double p2 = p[i] + q[i]; r += (p1 * p1 * p2) / den; } } return r; }
java
public static double SymmetricKullbackLeibler(double[] p, double[] q) { double dist = 0; for (int i = 0; i < p.length; i++) { dist += (p[i] - q[i]) * (Math.log(p[i]) - Math.log(q[i])); } return dist; }
java
public static double Taneja(double[] p, double[] q) { double r = 0; for (int i = 0; i < p.length; i++) { if (p[i] != 0 && q[i] != 0) { double pq = p[i] + q[i]; r += (pq / 2) * Math.log(pq / (2 * Math.sqrt(p[i] * q[i]))); } } return r; }
java
public static double TopsoeDivergence(double[] p, double[] q) { double r = 0; for (int i = 0; i < p.length; i++) { if (p[i] != 0 && q[i] != 0) { double den = p[i] + q[i]; r += p[i] * Math.log(2 * p[i] / den) + q[i] * Math.log(2 * q[i] / den); } } return r; }
java
public boolean contains(Vector3 p) { boolean ans = false; if(this.halfplane || p== null) return false; if (isCorner(p)) { return true; } PointLinePosition a12 = PointLineTest.pointLineTest(a,b,p); PointLinePosition a23 = PointLineTest.pointLineTest(b,c,p); PointLinePosition a31 = PointLineTest.pointLineTest(c,a,p); if ((a12 == PointLinePosition.LEFT && a23 == PointLinePosition.LEFT && a31 == PointLinePosition.LEFT ) || (a12 == PointLinePosition.RIGHT && a23 == PointLinePosition.RIGHT && a31 == PointLinePosition.RIGHT ) || (a12 == PointLinePosition.ON_SEGMENT ||a23 == PointLinePosition.ON_SEGMENT || a31 == PointLinePosition.ON_SEGMENT)) { ans = true; } return ans; }
java
public static float calcDet(Vector3 a ,Vector3 b, Vector3 c) { return (a.x*(b.y-c.y)) - (a.y*(b.x-c.x)) + (b.x*c.y-b.y*c.x); }
java
public int sharedSegments(Triangle t2) { int counter = 0; if(a.equals(t2.a)) { counter++; } if(a.equals(t2.b)) { counter++; } if(a.equals(t2.c)) { counter++; } if(b.equals(t2.a)) { counter++; } if(b.equals(t2.b)) { counter++; } if(b.equals(t2.c)) { counter++; } if(c.equals(t2.a)) { counter++; } if(c.equals(t2.b)) { counter++; } if(c.equals(t2.c)) { counter++; } return counter; }
java
@Override public ImageSource apply(ImageSource source) { if (radius != 0) { if (source.isGrayscale()) { return applyGrayscale(source, radius); } else { return applyRGB(source, radius); } } else { if (source.isGrayscale()) { return applyGrayscale(source, kernel); } else { return applyRGB(source, kernel); } } }
java
public <T> T handleResponse(Response response, Type returnType) throws ApiException { if (response.isSuccessful()) { if (returnType == null || response.code() == 204) { // returning null if the returnType is not defined, // or the status code is 204 (No Content) return null; } else { return deserialize(response, returnType); } } else { String respBody = null; if (response.body() != null) { try { respBody = response.body().string(); } catch (IOException e) { throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap()); } } throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody); } }
java
public void add(int ds, Object value) throws SerializationException, InvalidDataSetException { if (value == null) { return; } DataSetInfo dsi = dsiFactory.create(ds); byte[] data = dsi.getSerializer().serialize(value, activeSerializationContext); DataSet dataSet = new DefaultDataSet(dsi, data); dataSets.add(dataSet); }
java
public void addDateTimeHelper(int ds, Date date) throws SerializationException, InvalidDataSetException { if (date == null) { return; } DataSetInfo dsi = dsiFactory.create(ds); SimpleDateFormat df = new SimpleDateFormat(dsi.getSerializer().toString()); String value = df.format(date); byte[] data = dsi.getSerializer().serialize(value, activeSerializationContext); DataSet dataSet = new DefaultDataSet(dsi, data); add(dataSet); }
java
public Object get(int dataSet) throws SerializationException { Object result = null; for (Iterator<DataSet> i = dataSets.iterator(); i.hasNext();) { DataSet ds = i.next(); DataSetInfo info = ds.getInfo(); if (info.getDataSetNumber() == dataSet) { result = getData(ds); break; } } return result; }
java
public List<Object> getAll(int dataSet) throws SerializationException { List<Object> result = new ArrayList<Object>(); for (Iterator<DataSet> i = dataSets.iterator(); i.hasNext();) { DataSet ds = i.next(); DataSetInfo info = ds.getInfo(); if (info.getDataSetNumber() == dataSet) { result.add(getData(ds)); } } return result; }
java
public void readFrom(IIMReader reader, int recover) throws IOException, InvalidDataSetException { final boolean doLog = log != null; for (;;) { try { DataSet ds = reader.read(); if (ds == null) { break; } if (doLog) { log.debug("Read data set " + ds); } DataSetInfo info = ds.getInfo(); Serializer s = info.getSerializer(); if (s != null) { if (info.getDataSetNumber() == IIM.DS(1, 90)) { setCharacterSet((String) s.deserialize(ds.getData(), activeSerializationContext)); } } dataSets.add(ds); if (stopAfter9_10 && info.getDataSetNumber() == IIM.DS(9, 10)) break; } catch (IIMFormatException e) { if (recoverFromIIMFormat && recover-- > 0) { boolean r = reader.recover(); if (doLog) { log.debug(r ? "Recoved from " + e : "Failed to recover from " + e); } if (!r) break; } else { throw e; } } catch (UnsupportedDataSetException e) { if (recoverFromUnsupportedDataSet && recover-- > 0) { boolean r = reader.recover(); if (doLog) { log.debug(r ? "Recoved from " + e : "Failed to recover from " + e); } if (!r) break; } else { throw e; } } catch (InvalidDataSetException e) { if (recoverFromInvalidDataSet && recover-- > 0) { boolean r = reader.recover(); if (doLog) { log.debug(r ? "Recoved from " + e : "Failed to recover from " + e); } if (!r) break; } else { throw e; } } catch (IOException e) { if (recover-- > 0 && !dataSets.isEmpty()) { if (doLog) { log.error("IOException while reading, however some data sets where recovered, " + e); } return; } else { throw e; } } } }
java
public void writeTo(IIMWriter writer) throws IOException { final boolean doLog = log != null; for (Iterator<DataSet> i = dataSets.iterator(); i.hasNext();) { DataSet ds = i.next(); writer.write(ds); if (doLog) { log.debug("Wrote data set " + ds); } } }
java
public Set<ConstraintViolation> validate(DataSetInfo info) { Set<ConstraintViolation> errors = new LinkedHashSet<ConstraintViolation>(); try { if (info.isMandatory() && get(info.getDataSetNumber()) == null) { errors.add(new ConstraintViolation(info, ConstraintViolation.MANDATORY_MISSING)); } if (!info.isRepeatable() && getAll(info.getDataSetNumber()).size() > 1) { errors.add(new ConstraintViolation(info, ConstraintViolation.REPEATABLE_REPEATED)); } } catch (SerializationException e) { errors.add(new ConstraintViolation(info, ConstraintViolation.INVALID_VALUE)); } return errors; }
java
public Set<ConstraintViolation> validate(int record) { Set<ConstraintViolation> errors = new LinkedHashSet<ConstraintViolation>(); for (int ds = 0; ds < 250; ++ds) { try { DataSetInfo dataSetInfo = dsiFactory.create(IIM.DS(record, ds)); errors.addAll(validate(dataSetInfo)); } catch (InvalidDataSetException ignored) { // DataSetFactory doesn't know about this ds, so will skip it } } return errors; }
java
public Set<ConstraintViolation> validate() { Set<ConstraintViolation> errors = new LinkedHashSet<ConstraintViolation>(); for (int record = 1; record <= 3; ++record) { errors.addAll(validate(record)); } return errors; }
java
public int compare(Vector3 o1, Vector3 o2) { int ans = 0; if (o1 != null && o2 != null) { Vector3 d1 = o1; Vector3 d2 = o2; if (d1.x > d2.x) return 1; if (d1.x < d2.x) return -1; // x1 == x2 if (d1.y > d2.y) return 1; if (d1.y < d2.y) return -1; } else { if (o1 == null && o2 == null) return 0; if (o1 == null && o2 != null) return 1; if (o1 != null && o2 == null) return -1; } return ans; }
java
public double nextDouble(double lo, double hi) { if (lo < 0) { if (nextInt(2) == 0) return -nextDouble(0, -lo); else return nextDouble(0, hi); } else { return (lo + (hi - lo) * nextDouble()); } }
java
public ApiResponse<TagsEnvelope> getTagCategoriesWithHttpInfo() throws ApiException { com.squareup.okhttp.Call call = getTagCategoriesValidateBeforeCall(null, null); Type localVarReturnType = new TypeToken<TagsEnvelope>(){}.getType(); return apiClient.execute(call, localVarReturnType); }
java
public RuleEnvelope getRule(String ruleId) throws ApiException { ApiResponse<RuleEnvelope> resp = getRuleWithHttpInfo(ruleId); return resp.getData(); }
java
protected void addNeighbor(Queue<ColorPoint> queue, int px, int py, int color, Feature component) { if (!inBoundary(px, py, component)) { return; } if (!mask.isTouched(px, py)) { queue.add(new ColorPoint(px, py, color)); } }
java
public double Function1D(double x) { return Math.exp(x * x / (-2 * sqrSigma)) / (Math.sqrt(2 * Math.PI) * sigma); }
java
public double Function2D(double x, double y) { return Math.exp(-(x * x + y * y) / (2 * sqrSigma)) / (2 * Math.PI * sqrSigma); }
java
public double[] Kernel1D(int size) { if (((size % 2) == 0) || (size < 3) || (size > 101)) { try { throw new Exception("Wrong size"); } catch (Exception e) { e.printStackTrace(); } } int r = size / 2; // kernel double[] kernel = new double[size]; // compute kernel for (int x = -r, i = 0; i < size; x++, i++) { kernel[i] = Function1D(x); } return kernel; }
java
public double[][] Kernel2D(int size) { if (((size % 2) == 0) || (size < 3) || (size > 101)) { try { throw new Exception("Wrong size"); } catch (Exception e) { e.printStackTrace(); } } int r = size / 2; double[][] kernel = new double[size][size]; // compute kernel double sum = 0; for (int y = -r, i = 0; i < size; y++, i++) { for (int x = -r, j = 0; j < size; x++, j++) { kernel[i][j] = Function2D(x, y); sum += kernel[i][j]; } } for (int i = 0; i < kernel.length; i++) { for (int j = 0; j < kernel[0].length; j++) { kernel[i][j] /= sum; } } return kernel; }
java
public ArrayList<IntPoint> process(ImageSource fastBitmap) { //FastBitmap l = new FastBitmap(fastBitmap); if (points == null) { apply(fastBitmap); } int width = fastBitmap.getWidth(); int height = fastBitmap.getHeight(); points = new ArrayList<IntPoint>(); if (fastBitmap.isGrayscale()) { for (int x = 0; x < height; x++) { for (int y = 0; y < width; y++) { if (fastBitmap.getRGB(y, x) == 255) points.add(new IntPoint(y, x)); } } } else { for (int x = 0; x < height; x++) { for (int y = 0; y < width; y++) { // TODO Check for green and blue? if (fastBitmap.getR(y, x) == 255) points.add(new IntPoint(y, x)); } } } return points; }
java
public static void Forward(double[] data) { double[] result = new double[data.length]; for (int k = 0; k < result.length; k++) { double sum = 0; for (int n = 0; n < data.length; n++) { double theta = ((2.0 * Math.PI) / data.length) * k * n; sum += data[n] * cas(theta); } result[k] = (1.0 / Math.sqrt(data.length)) * sum; } for (int i = 0; i < result.length; i++) { data[i] = result[i]; } }
java
public static void Forward(double[][] data) { double[][] result = new double[data.length][data[0].length]; for (int m = 0; m < data.length; m++) { for (int n = 0; n < data[0].length; n++) { double sum = 0; for (int i = 0; i < result.length; i++) { for (int k = 0; k < data.length; k++) { sum += data[i][k] * cas(((2.0 * Math.PI) / data.length) * (i * m + k * n)); } result[m][n] = (1.0 / data.length) * sum; } } } for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[0].length; j++) { data[i][j] = result[i][j]; } } }
java
public PropertiesEnvelope createUserProperties(String userId, AppProperties properties, String aid) throws ApiException { ApiResponse<PropertiesEnvelope> resp = createUserPropertiesWithHttpInfo(userId, properties, aid); return resp.getData(); }
java
public PropertiesEnvelope getUserProperties(String userId, String aid) throws ApiException { ApiResponse<PropertiesEnvelope> resp = getUserPropertiesWithHttpInfo(userId, aid); return resp.getData(); }
java
protected <T extends Listener> Collection<T> copyList(Class<T> listenerClass, Stream<Object> listeners, int sizeHint) { if (sizeHint == 0) { return Collections.emptyList(); } return listeners .map(listenerClass::cast) .collect(Collectors.toCollection(() -> new ArrayList<>(sizeHint))); }
java
public List<Cluster> cluster(final Collection<Point2D> points) { final List<Cluster> clusters = new ArrayList<Cluster>(); final Map<Point2D, PointStatus> visited = new HashMap<Point2D, DBScan.PointStatus>(); KDTree<Point2D> tree = new KDTree<Point2D>(2); // Populate the kdTree for (final Point2D point : points) { double[] key = {point.x, point.y}; tree.insert(key, point); } for (final Point2D point : points) { if (visited.get(point) != null) { continue; } final List<Point2D> neighbors = getNeighbors(point, tree); if (neighbors.size() >= minPoints) { // DBSCAN does not care about center points final Cluster cluster = new Cluster(clusters.size()); clusters.add(expandCluster(cluster, point, neighbors, tree, visited)); } else { visited.put(point, PointStatus.NOISE); } } for (Cluster cluster : clusters) { cluster.calculateCentroid(); } return clusters; }
java
private Cluster expandCluster(final Cluster cluster, final Point2D point, final List<Point2D> neighbors, final KDTree<Point2D> points, final Map<Point2D, PointStatus> visited) { cluster.addPoint(point); visited.put(point, PointStatus.PART_OF_CLUSTER); List<Point2D> seeds = new ArrayList<Point2D>(neighbors); int index = 0; while (index < seeds.size()) { Point2D current = seeds.get(index); PointStatus pStatus = visited.get(current); // only check non-visited points if (pStatus == null) { final List<Point2D> currentNeighbors = getNeighbors(current, points); if (currentNeighbors.size() >= minPoints) { seeds = merge(seeds, currentNeighbors); } } if (pStatus != PointStatus.PART_OF_CLUSTER) { visited.put(current, PointStatus.PART_OF_CLUSTER); cluster.addPoint(current); } index++; } return cluster; }
java
private List<Point2D> merge(final List<Point2D> one, final List<Point2D> two) { final Set<Point2D> oneSet = new HashSet<Point2D>(one); for (Point2D item : two) { if (!oneSet.contains(item)) { one.add(item); } } return one; }
java
public ImageSource apply(ImageSource input) { ImageSource originalImage = input; int width = originalImage.getWidth(); int height = originalImage.getHeight(); boolean[][] matrix = new boolean[width][height]; // black n white boolean matrix; true = blck, false = white // Copy ImageSource filteredImage = new MatrixSource(input); int[] histogram = OtsuBinarize.imageHistogram(originalImage); int totalNumberOfpixels = height * width; int threshold = OtsuBinarize.threshold(histogram, totalNumberOfpixels); int black = 0; int white = 255; int gray; int alpha; int newColor; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { gray = originalImage.getGray(i, j); if (gray > threshold) { matrix[i][j] = false; } else { matrix[i][j] = true; } } } int blackTreshold = letterThreshold(originalImage, matrix); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { gray = originalImage.getGray(i, j); alpha = originalImage.getA(i, j); if (gray > blackTreshold) { newColor = white; } else { newColor = black; } newColor = ColorHelper.getARGB(newColor, newColor, newColor, alpha); filteredImage.setRGB(i, j, newColor); } } return filteredImage; }
java
public static int[] Argsort(final float[] array, final boolean ascending) { Integer[] indexes = new Integer[array.length]; for (int i = 0; i < indexes.length; i++) { indexes[i] = i; } Arrays.sort(indexes, new Comparator<Integer>() { @Override public int compare(final Integer i1, final Integer i2) { return (ascending ? 1 : -1) * Float.compare(array[i1], array[i2]); } }); return asArray(indexes); }
java
README.md exists but content is empty.
Downloads last month
35