Search in sources :

Example 1 with SparseArrayExpr

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;
}
Also used : SparseArrayExpr(org.matheclipse.core.expression.data.SparseArrayExpr) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) PrettyPrint(edu.jas.kern.PrettyPrint)

Example 2 with SparseArrayExpr

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;
}
Also used : SparseArrayExpr(org.matheclipse.core.expression.data.SparseArrayExpr) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with SparseArrayExpr

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);
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) SparseArrayExpr(org.matheclipse.core.expression.data.SparseArrayExpr) HashMap(java.util.HashMap) IExpr(org.matheclipse.core.interfaces.IExpr) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Example 4 with SparseArrayExpr

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);
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) ExprEdge(org.matheclipse.core.expression.data.ExprEdge) SparseArrayExpr(org.matheclipse.core.expression.data.SparseArrayExpr) HashMap(java.util.HashMap) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 5 with SparseArrayExpr

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);
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) SparseArrayExpr(org.matheclipse.core.expression.data.SparseArrayExpr) HashMap(java.util.HashMap) IExpr(org.matheclipse.core.interfaces.IExpr) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Aggregations

SparseArrayExpr (org.matheclipse.core.expression.data.SparseArrayExpr)5 IExpr (org.matheclipse.core.interfaces.IExpr)5 HashMap (java.util.HashMap)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 ExprWeightedEdge (org.matheclipse.core.expression.data.ExprWeightedEdge)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 PrettyPrint (edu.jas.kern.PrettyPrint)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 ExprEdge (org.matheclipse.core.expression.data.ExprEdge)1