use of org.matheclipse.core.expression.data.ExprWeightedEdge in project symja_android_library by axkr.
the class GraphFunctions method weightedEdgesToVisjs.
private static void weightedEdgesToVisjs(Map<IExpr, Integer> map, StringBuilder buf, Graph<IExpr, ExprWeightedEdge> graph) {
Set<ExprWeightedEdge> edgeSet = graph.edgeSet();
GraphType type = graph.getType();
boolean first = true;
if (type.isDirected()) {
buf.append("var edges = new vis.DataSet([\n");
for (Object object : edgeSet) {
if (object instanceof ExprWeightedEdge) {
ExprWeightedEdge edge = (ExprWeightedEdge) object;
// {from: 1, to: 3},
if (first) {
buf.append(" {from: ");
} else {
buf.append(", {from: ");
}
buf.append(map.get(edge.lhs()));
buf.append(", to: ");
buf.append(map.get(edge.rhs()));
buf.append(", label: '");
buf.append(edge.weight());
buf.append("'");
// , arrows: { to: { enabled: true, type: 'arrow'}}
buf.append(" , arrows: { to: { enabled: true, type: 'arrow'}}");
buf.append("}\n");
first = false;
}
}
} else {
buf.append("var edges = new vis.DataSet([\n");
for (Object object : edgeSet) {
if (object instanceof ExprWeightedEdge) {
ExprWeightedEdge edge = (ExprWeightedEdge) object;
// {from: 1, to: 3},
if (first) {
buf.append(" {from: ");
} else {
buf.append(", {from: ");
}
buf.append(map.get(edge.lhs()));
buf.append(", to: ");
buf.append(map.get(edge.rhs()));
buf.append(", label: '");
buf.append(edge.weight());
buf.append("'");
buf.append("}\n");
first = false;
}
}
}
buf.append("]);\n");
}
use of org.matheclipse.core.expression.data.ExprWeightedEdge in project symja_android_library by axkr.
the class GraphFunctions method edgesToVisjs.
private static void edgesToVisjs(Map<IExpr, Integer> map, StringBuilder buf, Graph<IExpr, ExprEdge> g) {
Set<ExprEdge> edgeSet = g.edgeSet();
GraphType type = g.getType();
boolean first = true;
if (type.isDirected()) {
buf.append("var edges = new vis.DataSet([\n");
for (Object object : edgeSet) {
if (object instanceof ExprEdge) {
ExprEdge edge = (ExprEdge) object;
// {from: 1, to: 3},
if (first) {
buf.append(" {from: ");
} else {
buf.append(", {from: ");
}
buf.append(map.get(edge.lhs()));
buf.append(", to: ");
buf.append(map.get(edge.rhs()));
// , arrows: { to: { enabled: true, type: 'arrow'}}
buf.append(" , arrows: { to: { enabled: true, type: 'arrow'}}");
buf.append("}\n");
first = false;
} else if (object instanceof ExprWeightedEdge) {
ExprWeightedEdge weightedEdge = (ExprWeightedEdge) object;
// {from: 1, to: 3},
if (first) {
buf.append(" {from: ");
} else {
buf.append(", {from: ");
}
buf.append(map.get(weightedEdge.lhs()));
buf.append(", to: ");
buf.append(map.get(weightedEdge.rhs()));
// , arrows: { to: { enabled: true, type: 'arrow'}}
buf.append(" , arrows: { to: { enabled: true, type: 'arrow'}}");
buf.append("}\n");
first = false;
}
}
} else {
//
buf.append("var edges = new vis.DataSet([\n");
for (Object object : edgeSet) {
if (object instanceof ExprEdge) {
ExprEdge edge = (ExprEdge) object;
// {from: 1, to: 3},
if (first) {
buf.append(" {from: ");
} else {
buf.append(", {from: ");
}
buf.append(map.get(edge.lhs()));
buf.append(", to: ");
buf.append(map.get(edge.rhs()));
buf.append("}\n");
first = false;
} else if (object instanceof ExprWeightedEdge) {
ExprWeightedEdge weightedEdge = (ExprWeightedEdge) object;
// {from: 1, to: 3},
if (first) {
buf.append(" {from: ");
} else {
buf.append(", {from: ");
}
buf.append(map.get(weightedEdge.lhs()));
buf.append(", to: ");
buf.append(map.get(weightedEdge.rhs()));
buf.append("}\n");
first = false;
}
}
}
buf.append("]);\n");
}
use of org.matheclipse.core.expression.data.ExprWeightedEdge in project symja_android_library by axkr.
the class GraphFunctions method edgesToIExpr.
private static IASTAppendable[] edgesToIExpr(Graph<IExpr, ?> g) {
Set<Object> edgeSet = (Set<Object>) g.edgeSet();
IASTAppendable edges = F.ListAlloc(edgeSet.size());
IASTAppendable weights = null;
GraphType type = g.getType();
for (Object edge : edgeSet) {
if (edge instanceof ExprWeightedEdge) {
ExprWeightedEdge weightedEdge = (ExprWeightedEdge) edge;
if (type.isDirected()) {
edges.append(F.DirectedEdge(weightedEdge.lhs(), weightedEdge.rhs()));
} else {
edges.append(F.UndirectedEdge(weightedEdge.lhs(), weightedEdge.rhs()));
}
if (weights == null) {
weights = F.ListAlloc(edgeSet.size());
}
weights.append(weightedEdge.weight());
} else if (edge instanceof ExprEdge) {
ExprEdge exprEdge = (ExprEdge) edge;
if (type.isDirected()) {
edges.append(F.DirectedEdge(exprEdge.lhs(), exprEdge.rhs()));
} else {
edges.append(F.UndirectedEdge(exprEdge.lhs(), exprEdge.rhs()));
}
}
}
return new IASTAppendable[] { edges, weights };
}
use of org.matheclipse.core.expression.data.ExprWeightedEdge in project symja_android_library by axkr.
the class GraphFunctions method weightedGraphToAdjacencyMatrix.
public static IExpr weightedGraphToAdjacencyMatrix(Graph<IExpr, ExprWeightedEdge> g) {
Set<IExpr> vertexSet = g.vertexSet();
int size = vertexSet.size();
Map<IExpr, Integer> map = new HashMap<IExpr, Integer>();
int indx = 1;
for (IExpr expr : vertexSet) {
map.put(expr, indx++);
}
final Trie<int[], IExpr> trie = Config.TRIE_INT2EXPR_BUILDER.build();
for (ExprWeightedEdge edge : g.edgeSet()) {
IExpr lhs = edge.lhs();
IExpr rhs = edge.rhs();
int from = map.get(lhs);
int to = map.get(rhs);
trie.put(new int[] { from, to }, F.C1);
if (g.containsEdge(rhs, lhs)) {
trie.put(new int[] { to, from }, F.C1);
}
}
return new SparseArrayExpr(trie, new int[] { size, size }, F.C0, false);
}
use of org.matheclipse.core.expression.data.ExprWeightedEdge in project symja_android_library by axkr.
the class GraphFunctions method edgesToRules.
private static IASTAppendable[] edgesToRules(Graph<IExpr, ?> g) {
Set<Object> edgeSet = (Set<Object>) g.edgeSet();
IASTAppendable edges = F.ListAlloc(edgeSet.size());
IASTAppendable weights = null;
GraphType type = g.getType();
for (Object edge : edgeSet) {
if (edge instanceof ExprWeightedEdge) {
ExprWeightedEdge weightedEdge = (ExprWeightedEdge) edge;
edges.append(F.Rule(weightedEdge.lhs(), weightedEdge.rhs()));
if (weights == null) {
weights = F.ListAlloc(edgeSet.size());
}
weights.append(weightedEdge.weight());
} else if (edge instanceof ExprEdge) {
ExprEdge exprEdge = (ExprEdge) edge;
edges.append(F.Rule(exprEdge.lhs(), exprEdge.rhs()));
}
}
return new IASTAppendable[] { edges, weights };
}
Aggregations