use of java.util.function.BinaryOperator in project radixdlt by radixdlt.
the class UInt256sTest method testPrimeLCM.
private static <T> void testPrimeLCM(IntFunction<T[]> arrayBuilder, LongFunction<T> builder, BinaryOperator<T> multiply, T one, T max, Function<T, BigInteger> toBI, BiFunction<T, T[], T> lcmFunction) {
List<Integer> primeList = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149);
IntFunction<LongStream> primeNumbers = size -> primeList.stream().mapToLong(i -> i).limit(size);
BigInteger maxBI = toBI.apply(max);
for (int size = 1; size < primeList.size(); size++) {
T[] values = primeNumbers.apply(size).mapToObj(builder).toArray(arrayBuilder);
BigInteger biExpected = primeNumbers.apply(size).mapToObj(BigInteger::valueOf).reduce(BigInteger.ONE, BigInteger::multiply);
final T expected;
if (biExpected.compareTo(maxBI) > 0) {
expected = null;
} else {
expected = primeNumbers.apply(size).mapToObj(builder).reduce(one, multiply);
}
assertThat(lcmFunction.apply(max, values)).isEqualTo(expected);
}
}
use of java.util.function.BinaryOperator in project random-cut-forest-by-aws by aws.
the class RandomCutForest method getAnomalyAttribution.
public DiVector getAnomalyAttribution(float[] point) {
// getAnomalyScore
if (!isOutputReady()) {
return new DiVector(dimensions);
}
IVisitorFactory<DiVector> visitorFactory = new VisitorFactory<>((tree, y) -> new AnomalyAttributionVisitor(tree.projectToTree(y), tree.getMass()), (tree, x) -> x.lift(tree::liftFromTree));
BinaryOperator<DiVector> accumulator = DiVector::addToLeft;
Function<DiVector, DiVector> finisher = x -> x.scale(1.0 / numberOfTrees);
return traverseForest(transformToShingledPoint(point), visitorFactory, accumulator, finisher);
}
use of java.util.function.BinaryOperator in project random-cut-forest-by-aws by aws.
the class RandomCutForest method getDynamicAttribution.
/**
* Same as above, but for dynamic scoring. See the params of
* getDynamicScoreParallel
*
* @param point point to be scored
* @param ignoreLeafMassThreshold said threshold
* @param seen score function for seen points
* @param unseen score function for unseen points
* @param newDamp dampening function for duplicates in the seen
* function
* @return dynamic scoring attribution DiVector
*/
public DiVector getDynamicAttribution(float[] point, int ignoreLeafMassThreshold, BiFunction<Double, Double, Double> seen, BiFunction<Double, Double, Double> unseen, BiFunction<Double, Double, Double> newDamp) {
if (!isOutputReady()) {
return new DiVector(dimensions);
}
VisitorFactory<DiVector> visitorFactory = new VisitorFactory<>((tree, y) -> new DynamicAttributionVisitor(tree.projectToTree(y), tree.getMass(), ignoreLeafMassThreshold, seen, unseen, newDamp), (tree, x) -> x.lift(tree::liftFromTree));
BinaryOperator<DiVector> accumulator = DiVector::addToLeft;
Function<DiVector, DiVector> finisher = x -> x.scale(1.0 / numberOfTrees);
return traverseForest(transformToShingledPoint(point), visitorFactory, accumulator, finisher);
}
use of java.util.function.BinaryOperator in project Alchemist by AlchemistSimulator.
the class ConrecIsolinesFinder method findIsolines.
/**
* {@inheritDoc}
*/
@Override
public Collection<Isoline> findIsolines(final BinaryOperator<Number> function, final Number x1, final Number y1, final Number x2, final Number y2, final Collection<Number> levels) {
Objects.requireNonNull(function);
Objects.requireNonNull(x1);
Objects.requireNonNull(y1);
Objects.requireNonNull(x2);
Objects.requireNonNull(y2);
Objects.requireNonNull(levels);
// preparing parameters for the algorithm
final double startX = x1.doubleValue();
final double startY = y1.doubleValue();
final double endX = x2.doubleValue();
final double endY = y2.doubleValue();
// the rectangular region defined by start and end is continuous,
// this means we will sample a discrete amount of points in that region.
// How many points will be sampled on each side is defined by SAMPLE,
// step is the distance between two of these points
final double stepX = (endX - startX) / SAMPLES;
final double stepY = (endY - startY) / SAMPLES;
// indexes for accessing x, y and d arrays
final int ilb = 0;
final int jlb = 0;
final int iub = SAMPLES;
final int jub = SAMPLES;
// x and y contains respectively the x and y coordinates of the sampling points
// +1 because iub, jub are included
final double[] x = new double[iub + 1];
final double[] y = new double[jub + 1];
for (int i = ilb; i <= iub; i++) {
if (i == ilb) {
x[i] = startX;
} else {
x[i] = x[i - 1] + stepX;
}
}
for (int j = jlb; j <= jub; j++) {
if (j == jlb) {
y[j] = startY;
} else {
y[j] = y[j - 1] + stepY;
}
}
// d contains the data of the sampling points, d[i][j] = f(x[i], x[j])
final double[][] d = new double[iub + 1][jub + 1];
for (int i = ilb; i <= iub; i++) {
for (int j = jlb; j <= jub; j++) {
d[i][j] = function.apply(x[i], y[j]).doubleValue();
}
}
// finding the isolines
final Map<Double, List<Segment2D>> isolines = new HashMap<>();
try {
new Conrec((startX1, startY1, endX1, endY1, contourLevel) -> {
// render
final Segment2D segment = factory.makeSegment(startX1, startY1, endX1, endY1);
final double flooredValue = Math.floor(contourLevel * 10) / 10;
if (!isolines.containsKey(flooredValue)) {
isolines.put(flooredValue, new ArrayList<>());
}
isolines.get(flooredValue).add(segment);
}).contour(d, ilb, iub, jlb, jub, x, y, levels.size(), levels.stream().mapToDouble(Number::doubleValue).toArray());
} catch (Exception e) {
// NOPMD
// this is horrible but necessary
L.warn("couldn't find isolines, conrec threw an exception: " + e.getMessage());
}
return isolines.entrySet().stream().map(entry -> factory.makeIsoline(entry.getKey(), entry.getValue())).collect(Collectors.toList());
}
use of java.util.function.BinaryOperator in project Mycat2 by MyCATApache.
the class DrdsSqlWithParams method getHintDataNodeFilter.
public Optional<List<PartitionGroup>> getHintDataNodeFilter() {
for (MycatHint hint : this.getHints()) {
for (MycatHint.Function hintFunction : hint.getFunctions()) {
if ("SCAN".equalsIgnoreCase(hintFunction.getName())) {
List<MycatHint.Argument> arguments = hintFunction.getArguments();
Map<String, List<String>> collect = arguments.stream().collect(Collectors.toMap(m -> SQLUtils.toSQLString(m.getName()), v -> Arrays.stream(SQLUtils.normalize(SQLUtils.toSQLString((v.getValue())).replace("'", "").replace("(", "").replace(")", "").trim()).split(",")).map(i -> SQLUtils.normalize(i)).collect(Collectors.toList())));
NameMap<List<String>> nameMap = NameMap.immutableCopyOf(collect);
List<String> condition = new LinkedList<>(Optional.ofNullable(nameMap.get("CONDITION", false)).orElse(Collections.emptyList()));
List<String> logicalTables = new LinkedList<>(Optional.ofNullable(nameMap.get("TABLE", false)).orElse(Collections.emptyList()));
List<List<String>> physicalTables = new LinkedList<>(Optional.ofNullable(nameMap.get("PARTITION", false)).orElse(Collections.emptyList()).stream().map(i -> Arrays.asList(i.split(","))).collect(Collectors.toList()));
if (!physicalTables.isEmpty()) {
for (MycatHint.Argument argument : arguments) {
if ("PARTITION".equals(argument.getName().toString())) {
if (argument.getValue() instanceof SQLListExpr) {
physicalTables.clear();
SQLListExpr argumentValue = (SQLListExpr) argument.getValue();
for (Object o : argumentValue.getItems()) {
ArrayList<String> objects = new ArrayList<>();
physicalTables.add(objects);
for (String s : Arrays.asList(SQLUtils.normalize(o.toString()).split(","))) {
objects.add(SQLUtils.normalize(s));
}
}
}
}
}
}
HashSet<String> targets = new HashSet<>(Optional.ofNullable(nameMap.get("TARGET", false)).orElse(Collections.emptyList()));
Predicate<PartitionGroup> targetFilter = targets.isEmpty() ? (u) -> true : new Predicate<PartitionGroup>() {
@Override
public boolean test(PartitionGroup stringPartitionMap) {
String targetName = stringPartitionMap.getTargetName();
boolean contains = targets.contains(targetName);
return contains;
}
};
SQLStatement parameterizedStatement = this.getParameterizedStatement();
Detector detector = new Detector();
parameterizedStatement.accept(detector);
if (logicalTables.isEmpty()) {
logicalTables.addAll(detector.tableHandlerMap.keySet());
}
if (condition.isEmpty()) {
condition.add("true");
}
if (!detector.isSharding) {
List<Partition> partitions = Collections.emptyList();
List<Partition> newPartitions = Collections.emptyList();
if (!logicalTables.isEmpty()) {
if (!condition.isEmpty()) {
String sql = "select * from " + logicalTables.get(0) + "where " + condition;
QueryPlanner queryPlanner = MetaClusterCurrent.wrapper(QueryPlanner.class);
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
CodeExecuterContext codeExecuterContext = queryPlanner.innerComputeMinCostCodeExecuterContext(drdsSqlWithParams);
List<PartitionGroup> collect2 = codeExecuterContext.getRelContext().values().stream().flatMap(i -> AsyncMycatDataContextImpl.getSqlMap(codeExecuterContext.getConstantMap(), i.getRelNode(), drdsSqlWithParams, Optional.empty()).stream()).filter(targetFilter).collect(Collectors.toList());
return Optional.of(collect2);
} else if (!physicalTables.isEmpty()) {
newPartitions = physicalTables.get(0).stream().map(dataNodeParser()).collect(Collectors.toList());
}
if (!targets.isEmpty()) {
newPartitions = partitions.stream().filter(dataNode -> {
return targets.contains(dataNode.getTargetName());
}).collect(Collectors.toList());
}
List<Partition> finalNewPartitions = newPartitions;
List<PartitionGroup> maps = finalNewPartitions.stream().map(i -> {
return new PartitionGroup(i.getTargetName(), ImmutableMap.of(logicalTables.get(0), i));
}).collect(Collectors.toList());
return Optional.of(maps);
}
} else {
Map<String, List<Partition>> logicalPhysicalMap = new HashMap<>();
int physicalTableCount = 0;
if (!physicalTables.isEmpty()) {
int size = logicalTables.size();
for (int i = 0; i < size; i++) {
String alias = logicalTables.get(i);
List<Partition> phyTableList = physicalTables.get(i).stream().map(dataNodeParser()).collect(Collectors.toList());
physicalTableCount += phyTableList.size();
alias = detector.tableHandlerMap.get(alias).getUniqueName();
List<Partition> partitions = logicalPhysicalMap.computeIfAbsent(alias, s -> new ArrayList<>());
partitions.addAll(phyTableList);
}
}
if (!logicalTables.isEmpty() && !condition.isEmpty()) {
String where = condition.get(0);
SQLExpr oneSqlExpr = SQLUtils.toMySqlExpr(where);
List<SQLExpr> sqlExprs;
if (oneSqlExpr instanceof SQLBinaryOpExpr && ((SQLBinaryOpExpr) oneSqlExpr).getOperator() == SQLBinaryOperator.BooleanAnd) {
sqlExprs = SQLUtils.split((SQLBinaryOpExpr) oneSqlExpr);
} else {
sqlExprs = Collections.singletonList(oneSqlExpr);
}
Map<String, TableHandler> aliasTableMap = detector.tableHandlerMap;
int whereIndex = 0;
List<List<PartitionGroup>> partitionGroupMap = new ArrayList<>();
for (Map.Entry<String, TableHandler> e : aliasTableMap.entrySet()) {
TableHandler table = e.getValue();
String schemaName = table.getSchemaName();
String tableName = table.getTableName();
String sql = "select * from `" + schemaName + "`.`" + tableName + "` as `" + e.getKey() + "` where " + ((whereIndex < sqlExprs.size()) ? sqlExprs.get(whereIndex).toString() : "true");
whereIndex++;
DrdsSqlWithParams hintSql = DrdsRunnerHelper.preParse(sql, null);
QueryPlanner planner = MetaClusterCurrent.wrapper(QueryPlanner.class);
ParamHolder paramHolder = ParamHolder.CURRENT_THREAD_LOCAL.get();
paramHolder.setData(hintSql.getParams(), hintSql.getTypeNames());
try {
CodeExecuterContext codeExecuterContext = planner.innerComputeMinCostCodeExecuterContext(hintSql);
partitionGroupMap.add(codeExecuterContext.getRelContext().values().stream().flatMap(i -> AsyncMycatDataContextImpl.getSqlMap(codeExecuterContext.getConstantMap(), i.getRelNode(), hintSql, Optional.empty()).stream()).filter(targetFilter).collect(Collectors.toList()));
} finally {
paramHolder.clear();
}
}
List<PartitionGroup> partitionGroups = partitionGroupMap.stream().reduce(new BinaryOperator<List<PartitionGroup>>() {
@Override
public List<PartitionGroup> apply(List<PartitionGroup> partitionGroups, List<PartitionGroup> partitionGroups2) {
for (PartitionGroup partitionGroup : partitionGroups) {
Optional<PartitionGroup> first = partitionGroups2.stream().filter(i -> i.getTargetName().equals(partitionGroup.getTargetName())).findFirst();
first.ifPresent(group -> partitionGroup.map.putAll(group.map));
}
return partitionGroups;
}
}).orElse(Collections.emptyList());
if (!logicalPhysicalMap.isEmpty()) {
int index = 0;
for (PartitionGroup group : new ArrayList<>(partitionGroups)) {
for (Map.Entry<String, Partition> entry : new ArrayList<>(group.getMap().entrySet())) {
List<Partition> partitions = logicalPhysicalMap.get(entry.getKey());
if (partitions == null)
continue;
if (index >= partitions.size()) {
partitionGroups.remove(group);
continue;
}
entry.setValue(partitions.get(index));
}
++index;
}
}
return Optional.of(partitionGroups);
}
}
}
}
}
return Optional.empty();
}
Aggregations