use of org.matheclipse.core.expression.data.SparseArrayExpr in project symja_android_library by axkr.
the class ExprPolynomial method coefficientArrays.
/**
* TODO currently not implemented
*
* @deprecated
*/
@Deprecated
public IAST coefficientArrays(int degree) {
int numberOfVariables = ring.nvar;
IASTAppendable result = F.ListAlloc(degree + 1);
result.append(F.C0);
for (int i = 0; i < degree; i++) {
int[] dimension = new int[i + 1];
for (int j = 0; j < dimension.length; j++) {
dimension[j] = numberOfVariables;
}
SparseArrayExpr sparse = SparseArrayExpr.newArrayRules(F.List(), dimension, i + 1, F.C0);
result.append(sparse);
}
for (Map.Entry<ExpVectorLong, IExpr> monomial : val.entrySet()) {
IExpr coeff = monomial.getValue();
ExpVectorLong exp = monomial.getKey();
int maxDegree = (int) exp.maxDeg();
if (maxDegree == 0) {
result.set(1, coeff);
} else {
for (int i = exp.length() - 1; i >= 0; i--) {
if (exp.getVal(i) != 0L) {
SparseArrayExpr sparse = (SparseArrayExpr) result.get(maxDegree + 1);
int[] positions = new int[maxDegree];
positions[0] = exp.length() - i;
Trie<int[], IExpr> data = sparse.toData();
data.put(positions, coeff);
break;
}
}
}
// int len = exp.length();
// IASTAppendable ruleList = F.ListAlloc(len);
// for (int i = 0; i < len; i++) {
// ruleList.append(exp.getVal(len - i - 1));
// }
// result.append(F.Rule(ruleList, coeff));
}
return result;
}
use of org.matheclipse.core.expression.data.SparseArrayExpr in project symja_android_library by axkr.
the class F method sparseMatrix.
/**
* Generate a <code>n x m</code> sparse matrix. The indices start in Java convention with
* <code>0</code>.
*
* @param binaryFunction if the returned value unequals <code>0</code>, the value will be stored
* in the sparse matrix
* @param n the number of rows of the matrix.
* @param m the number of columns of the matrix.
* @return
*/
public static ISparseArray sparseMatrix(BiIntFunction<? extends IExpr> binaryFunction, int n, int m) {
if (n > Config.MAX_MATRIX_DIMENSION_SIZE || m > Config.MAX_MATRIX_DIMENSION_SIZE) {
ASTElementLimitExceeded.throwIt(((long) n) * ((long) m));
}
int[] dimension = new int[] { n, m };
SparseArrayExpr sparseMatrix = SparseArrayExpr.newArrayRules(F.CEmptyList, dimension, 0, C0);
for (int i = 0; i < n; i++) {
IASTAppendable row = ListAlloc(m);
for (int j = 0; j < m; j++) {
IExpr value = binaryFunction.apply(i, j);
if (!value.isZero()) {
sparseMatrix.set(new int[] { i + 1, j + 1 }, value);
}
}
}
return sparseMatrix;
}
use of org.matheclipse.core.expression.data.SparseArrayExpr 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.SparseArrayExpr in project symja_android_library by axkr.
the class GraphFunctions method graphToAdjacencyMatrix.
public static IExpr graphToAdjacencyMatrix(Graph<IExpr, ExprEdge> 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 (ExprEdge 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.SparseArrayExpr in project symja_android_library by axkr.
the class GraphFunctions method weightedGraphToWeightedAdjacencyMatrix.
public static IExpr weightedGraphToWeightedAdjacencyMatrix(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.num(edge.weight()));
if (g.containsEdge(rhs, lhs)) {
trie.put(new int[] { to, from }, F.num(edge.weight()));
}
}
return new SparseArrayExpr(trie, new int[] { size, size }, F.C0, false);
}
Aggregations