CovarianceMatricesAggregator implements Serializable { static List<CovarianceMatricesAggregator> reduce(List<CovarianceMatricesAggregator> l, List<CovarianceMatricesAggregator> r) { A.ensure(l != null || r != null, "Both partitions cannot equal to null"); if (l == null || l.isEmpty()) return r; if (r == null || r.isEmpty()) return l; A.ensure(l.size() == r.size(), "l.size() == r.size()"); List<CovarianceMatricesAggregator> res = new ArrayList<>(); for (int i = 0; i < l.size(); i++) res.add(l.get(i).plus(r.get(i))); return res; } CovarianceMatricesAggregator(Vector mean);   CovarianceMatricesAggregator(Vector mean, Matrix weightedSum, int rowCount); int rowCount();  }
@Test public void testReduce() { Vector mean1 = VectorUtils.of(1, 0); Vector mean2 = VectorUtils.of(0, 1); CovarianceMatricesAggregator agg11 = new CovarianceMatricesAggregator(mean1, identity(2), 1); CovarianceMatricesAggregator agg12 = new CovarianceMatricesAggregator(mean1, identity(2), 1); CovarianceMatricesAggregator agg21 = new CovarianceMatricesAggregator(mean2, identity(2), 2); CovarianceMatricesAggregator agg22 = new CovarianceMatricesAggregator(mean2, identity(2), 2); List<CovarianceMatricesAggregator> result = CovarianceMatricesAggregator.reduce( Arrays.asList(agg11, agg21), Arrays.asList(agg12, agg22) ); assertEquals(2, result.size()); CovarianceMatricesAggregator res1 = result.get(0); CovarianceMatricesAggregator res2 = result.get(1); assertArrayEquals(mean1.asArray(), res1.mean().asArray(), 1e-4); assertArrayEquals(identity(2).times(2).getStorage().data(), res1.weightedSum().getStorage().data(), 1e-4); assertEquals(2, res1.rowCount()); assertArrayEquals(mean2.asArray(), res2.mean().asArray(), 1e-4); assertArrayEquals(identity(2).times(2).getStorage().data(), res2.weightedSum().getStorage().data(), 1e-4); assertEquals(4, res2.rowCount()); }