Skip to content

Commit 46a3f7f

Browse files
committed
New function MeanClusteringCoefficient
1 parent 79acdd7 commit 46a3f7f

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.matheclipse.core.reflection.system;
2+
3+
import org.jgrapht.Graph;
4+
import org.matheclipse.core.builtin.GraphFunctions;
5+
import org.matheclipse.core.eval.EvalEngine;
6+
import org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator;
7+
import org.matheclipse.core.expression.F;
8+
import org.matheclipse.core.expression.ImplementationStatus;
9+
import org.matheclipse.core.expression.data.GraphExpr;
10+
import org.matheclipse.core.interfaces.IAST;
11+
import org.matheclipse.core.interfaces.IASTAppendable;
12+
import org.matheclipse.core.interfaces.IExpr;
13+
14+
/**
15+
* Returns the mean clustering coefficient for a graph. This is computed as the arithmetic mean of
16+
* the local clustering coefficients of all the vertices in the graph.
17+
*/
18+
public class MeanClusteringCoefficient extends AbstractFunctionEvaluator {
19+
20+
@Override
21+
public IExpr evaluate(final IAST ast, EvalEngine engine) {
22+
if (ast.argSize() == 1) {
23+
GraphExpr<?> gex = GraphFunctions.getGraphExpr(ast.arg1());
24+
if (gex == null) {
25+
return F.NIL;
26+
}
27+
28+
@SuppressWarnings("unchecked")
29+
Graph<IExpr, ?> g = gex.toData();
30+
boolean isDirected = g.getType().isDirected();
31+
32+
IAST vertices = GraphExpr.vertexToIExpr(g);
33+
int n = vertices.argSize();
34+
if (n == 0) {
35+
return F.C0;
36+
}
37+
38+
// Sum the individual local clustering coefficients
39+
IASTAppendable sumPlus = F.PlusAlloc(n);
40+
for (int i = 1; i <= n; i++) {
41+
sumPlus.append(LocalClusteringCoefficient.getLocalCC(g, vertices.get(i), isDirected));
42+
}
43+
44+
// Automatically evaluate and simplify the fraction: (Sum / N)
45+
return engine.evaluate(F.Divide(sumPlus, F.ZZ(n)));
46+
}
47+
return F.NIL;
48+
}
49+
50+
@Override
51+
public int[] expectedArgSize(IAST ast) {
52+
return ARGS_1_1;
53+
}
54+
55+
@Override
56+
public int status() {
57+
return ImplementationStatus.PARTIAL_SUPPORT;
58+
}
59+
}

0 commit comments

Comments
 (0)