### Question:
MultiMean extends SingleParentVariable<Matrix> { @Override public Tensor<?> gradient(Variable<?> parent, ComputationContext ctx) { double[] multiMeanGradient = ctx.gradient(this).data(); Tensor<?> result = ctx.data(parent).zeros(); for (int col = 0; col < cols; col++) { for (int row = 0; row < rows; row++) { int degree = adjacency[row].length + 1; int gradientElementIndex = row * cols + col; for (int neighbor : adjacency[row]) { int neighborElementIndex = neighbor * cols + col; result.addDataAt( neighborElementIndex, 1d / degree * multiMeanGradient[gradientElementIndex] ); } result.addDataAt( selfAdjacency[row] * cols + col, 1d / degree * multiMeanGradient[gradientElementIndex] ); } } return result; } MultiMean(
        Variable<?> parent,
        int[][] adjacency,
        int[] selfAdjacency
    ); @Override Matrix apply(ComputationContext ctx); @Override Tensor<?> gradient(Variable<?> parent, ComputationContext ctx);  }### Answer:
@Test void testGradient() { double[] matrix = { 1, 2, 4, 5, 3, 6 }; int[][] adj = new int[2][]; adj[0] = new int[] {1}; adj[1] = new int[] {0, 2}; int[] selfAdjacency = {0, 1}; Weights<Matrix> weights = new Weights<>(new Matrix(matrix, 3, 2)); finiteDifferenceShouldApproximateGradient(weights, new ElementSum(List.of(new MultiMean(weights, adj, selfAdjacency)))); }