/** * Calculates the greatest common denominator (gcd) of an array of numbers * * @param numbers Array of numbers * @return gcd of array of numbers */ public static OptionalInt gcd(int[] numbers) { return Arrays.stream(numbers) .reduce((a, b) -> gcd(a, b)); }
default OptionalInt reduce(IntPipeline pipeline, IntBinaryOperator op) { requireNonNull(pipeline); requireNonNull(op); return optimize(pipeline).getAsIntStream().reduce(op); }
/** * Calculates the lowest common multiple (lcm) of an array of numbers. * * @param numbers Array of numbers * @return lcm of array of numbers */ public static OptionalInt lcm(int[] numbers) { IntBinaryOperator lcm = (x, y) -> (x * y) / gcd(x, y); return Arrays.stream(numbers) .reduce((a, b) -> lcm.applyAsInt(a, b)); }
default int reduce(IntPipeline pipeline, int identity, IntBinaryOperator op) { requireNonNull(pipeline); return optimize(pipeline).getAsIntStream().reduce(identity, op); }
public static int phi(int m) { return P33.primeFactorsMult(m) .stream() .map(entry -> (entry.getKey() - 1) * Math.pow(entry.getKey(), entry.getValue() - 1)) .mapToInt(Double::intValue) .reduce(1, (i1, i2) -> i1 * i2); } }
@SuppressWarnings("unchecked") public static <T extends Tensor> T tensor(int... dimensions) { float[] elements = new float[IntStream.of(dimensions).reduce(1, (a, b) -> a * b)]; int[][] dimensionsLimit = new int[2][dimensions.length]; IntStream.range(0, dimensions.length).forEach(i -> dimensionsLimit[1][i] = dimensions[i] - 1); T result = null; if (dimensions.length == 2) { result = (T) new Matrix(0, elements, dimensions, dimensionsLimit); } else { result = (T) new Tensor(0, elements, dimensions, dimensionsLimit); } return result; }
/** * Create multiple combined tensors using shared elements array * * @param dimensions * - dimensions for each tensor * @return array of tensors */ public static Tensor[] tensor(int[]... dimensions) { Tensor[] result = new Tensor[dimensions.length]; float[] elements = new float[Arrays.stream(dimensions).map(d -> { return IntStream.of(d).reduce(1, (a, b) -> a * b); }).reduce(0, (a, b) -> a + b)]; for (int i = 0, offset = 0; i < dimensions.length; i++) { int[] d = dimensions[i]; int[][] dimensionsLimit = new int[2][d.length]; IntStream.range(0, d.length).forEach(j -> dimensionsLimit[1][j] = d[j] - 1); if (d.length == 2) { result[i] = new Matrix(offset, elements, d, dimensionsLimit); } else { result[i] = new Tensor(offset, elements, d, dimensionsLimit); } offset += IntStream.of(d).reduce(1, (a, b) -> a * b); } return result; }
@Override public int reduce(int identity, IntBinaryOperator op) { return finallyClose(() -> stream().reduce(identity, op)); }
@Override public OptionalInt reduce(IntBinaryOperator op) { return finallyClose(() -> stream().reduce(op)); }
/** * creates a new tensor based on a parent tensor * @param parent - the parent tensor * @param dimensionsLimit - * @param reduceChildDimensions */ public Tensor(Tensor parent, int[][] dimensionsLimit, boolean reduceChildDimensions) { this.startOffset = parent.startOffset; this.globalDimensions = parent.globalDimensions; this.elements = parent.elements; this.dimMultiplicators = parent.dimMultiplicators; this.globalDimensionsLimit = dimensionsLimit; this.dimTmp = new int[globalDimensions.length]; this.dimensions = new int[reduceChildDimensions ? (int) IntStream.range(0, globalDimensions.length).filter(i -> dimensionsLimit[0][i] != dimensionsLimit[1][i]).count() : parent.dimensions.length]; for (int i = 0, j = 0; i < globalDimensions.length; i++) { if (dimensionsLimit[0][i] != dimensionsLimit[1][i] || !reduceChildDimensions) { dimensions[j++] = dimensionsLimit[1][i] - dimensionsLimit[0][i] + 1; } } size = IntStream.range(0, dimensions.length).map(i -> dimensions[i]).reduce(1, (a, b) -> a * b); }
int younger = peoples.stream().filter((p) -> p.getName().contains("e")).mapToInt(People::getAge).reduce((s1, s2) -> s1 < s2 ? s1 : s2).orElse(0); System.out.println("younger = " + younger); // print 23
public static Map<String, Histogram> buildAll (int[] times, PointSet targets) { try { int size = IntStream.of(times).reduce(0, (memo, i) -> i != Integer.MAX_VALUE ? Math.max(i, memo) : memo) + 1;
public Tensor(int startOffset, float[] elements, int[] globalDimensions, int[][] globalDimensionsLimit) { super(); if (globalDimensions == null || globalDimensions.length == 0) { throw new IllegalArgumentException("Please provide dimensions"); } this.startOffset = startOffset; this.elements = elements; this.globalDimensions = globalDimensions; this.globalDimensionsLimit = globalDimensionsLimit; this.dimTmp = new int[globalDimensions.length]; this.dimensions = new int[(int) IntStream.range(0, globalDimensions.length) .filter(i -> globalDimensionsLimit[0][i] != globalDimensionsLimit[1][i] || globalDimensionsLimit[1][i] - globalDimensionsLimit[0][i] + 1 == globalDimensions[i]).count()]; for (int i = 0, j = 0; i < globalDimensions.length; i++) { if (globalDimensionsLimit[0][i] != globalDimensionsLimit[1][i] || globalDimensionsLimit[1][i] - globalDimensionsLimit[0][i] + 1 == globalDimensions[i]) { dimensions[j++] = globalDimensionsLimit[1][i] - globalDimensionsLimit[0][i] + 1; } } this.dimMultiplicators = new int[dimensions.length]; IntStream.range(0, dimMultiplicators.length).forEach(i -> { dimMultiplicators[i] = 1; Arrays.stream(globalDimensions).skip(i + 1).limit(globalDimensions.length).forEach(j -> dimMultiplicators[i] *= j); }); size = IntStream.range(0, dimensions.length).map(i -> dimensions[i]).reduce(1, (a, b) -> a * b); }
newBaseExpressions = Math.multiplyExact(subPredicates.stream() .mapToInt(Set::size) .reduce(Math::multiplyExact) .getAsInt(), subPredicates.size());
int size = Arrays.stream(dimensions).reduce(1, (a, b) -> a * b); float[] elements = null; int offset = 0;
.stream() .mapToInt(entry -> entry.getKey() * entry.getValue()) .reduce(Integer::sum) .getAsInt();
private static int hashOXR(List lst) { return lst.stream().mapToInt(Object::hashCode) .reduce(0, (l, r) -> l ^ r); }
/** * Calculates the greatest common denominator (gcd) of an array of numbers * * @param numbers Array of numbers * @return gcd of array of numbers */ public static OptionalInt gcd(int[] numbers) { return Arrays.stream(numbers) .reduce((a, b) -> gcd(a, b)); }
/** * Calculates the lowest common multiple (lcm) of an array of numbers. * * @param numbers Array of numbers * @return lcm of array of numbers */ public static OptionalInt lcm(int[] numbers) { IntBinaryOperator lcm = (x, y) -> (x * y) / gcd(x, y); return Arrays.stream(numbers) .reduce((a, b) -> lcm.applyAsInt(a, b)); }