use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class LinearAlgebra method rowReduced2List.
/**
* Return the solution of the given (augmented-)matrix interpreted as a system of linear
* equations.
*
* @param matrix
* @param quiet suppress warning messages if <code>true</code>
* @param engine the evaluation engine
* @return {@link F#NIL} if the linear system is inconsistent and has no solution
*/
public static IAST rowReduced2List(FieldMatrix<IExpr> matrix, boolean quiet, EvalEngine engine) {
int rows = matrix.getRowDimension();
int cols = matrix.getColumnDimension();
if (rows == 2 && cols == 3) {
IAST list = cramersRule2x3(matrix, quiet, engine);
if (list.isPresent()) {
return list;
}
} else if (rows == 3 && cols == 4) {
IAST list = cramersRule3x4(matrix, quiet, engine);
if (list.isPresent()) {
return list;
}
}
FieldReducedRowEchelonForm ref = new FieldReducedRowEchelonForm(matrix, AbstractMatrix1Expr.POSSIBLE_ZEROQ_TEST);
FieldMatrix<IExpr> rowReduced = ref.getRowReducedMatrix();
IExpr lastVarCoefficient = rowReduced.getEntry(rows - 1, cols - 2);
if (lastVarCoefficient.isZero()) {
if (!rowReduced.getEntry(rows - 1, cols - 1).isZero()) {
LOGGER.log(engine.getLogLevel(), "Row reduced linear equations have no solution.");
return F.NIL;
}
}
IASTAppendable list = F.ListAlloc(rows < cols - 1 ? cols - 1 : rows);
list.appendArgs(0, rows, j -> S.Together.of(engine, rowReduced.getEntry(j, cols - 1)));
if (rows < cols - 1) {
list.appendArgs(rows, cols - 1, i -> F.C0);
}
return list;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class PatternMatching method optionValueReplace.
/**
* Determine the current <code>OptionValue(...)</code> currently associated with an expreesion.
*
* @param ast
* @param quiet if <code>true</code> print no message if an option value cannot be found
* @param engine
* @return {@link F#NIL} if an option value cannot be found; otherwise get the optional value
*/
public static IExpr optionValueReplace(final IAST ast, boolean quiet, EvalEngine engine) {
IASTAppendable optionsPattern = null;
IExpr arg1 = engine.evaluate(ast.arg1());
IExpr rhsRuleValue = F.NIL;
IAST optionsList = null;
if (ast.size() > 2 && arg1.isSymbol()) {
optionsList = optionsList((ISymbol) arg1, true);
}
IExpr optionValue;
if (ast.isAST3()) {
IExpr arg2 = ast.arg2();
IExpr arg3 = ast.arg3();
if (arg3.isList()) {
return ((IAST) arg3).mapThread(ast, 3);
}
optionsPattern = F.ListAlloc(10);
extractRules(arg2, optionsPattern);
extractRules(optionsList, optionsPattern);
optionValue = arg3;
if (arg3.isSymbol()) {
optionValue = F.$str(((ISymbol) arg3).getSymbolName());
}
if (optionsPattern != null) {
rhsRuleValue = optionsRHSRuleValue(optionValue, optionsPattern);
if (rhsRuleValue.isPresent()) {
return rhsRuleValue;
}
if (!quiet) {
// Option name `2` not found in defaults for `1`
IOFunctions.printMessage(ast.topHead(), "optnf", F.list(optionsPattern, optionValue), engine);
}
return optionValue;
}
return F.NIL;
} else if (ast.isAST2()) {
IExpr arg2 = ast.arg2();
if (arg2.isList()) {
return ((IAST) arg2).mapThread(ast, 2);
}
optionValue = arg2;
if (arg2.isSymbol()) {
optionValue = F.$str(((ISymbol) arg2).getSymbolName());
}
if (arg1.isSymbol()) {
Iterator<IdentityHashMap<ISymbol, IASTAppendable>> iter = engine.optionsStackIterator();
while (iter.hasNext()) {
IdentityHashMap<ISymbol, IASTAppendable> map = iter.next();
if (map != null) {
optionsPattern = map.get(arg1);
if (optionsPattern != null) {
rhsRuleValue = optionsRHSRuleValue(optionValue, optionsPattern);
if (rhsRuleValue.isPresent()) {
return rhsRuleValue;
}
}
}
}
} else {
if (arg1.isAST()) {
optionsList = (IAST) arg1;
}
}
if (optionsPattern == null) {
optionsPattern = F.ListAlloc(10);
}
extractRules(optionsList, optionsPattern);
if (optionsPattern != null) {
rhsRuleValue = optionsRHSRuleValue(optionValue, optionsPattern);
if (rhsRuleValue.isPresent()) {
return rhsRuleValue;
}
if (!quiet) {
// Option name `2` not found in defaults for `1`
IOFunctions.printMessage(ast.topHead(), "optnf", F.list(optionsPattern, optionValue), engine);
}
return optionValue;
}
return F.NIL;
} else {
// ast.isAST1()
optionValue = arg1;
if (arg1.isSymbol()) {
optionValue = F.$str(((ISymbol) arg1).getSymbolName());
}
Iterator<IdentityHashMap<ISymbol, IASTAppendable>> iter = engine.optionsStackIterator();
while (iter.hasNext()) {
IdentityHashMap<ISymbol, IASTAppendable> map = iter.next();
if (map != null) {
optionsPattern = map.get(S.LHS_HEAD);
if (optionsPattern != null) {
ISymbol lhsHead = optionsPattern.topHead();
optionsPattern = map.get(lhsHead);
rhsRuleValue = optionsRHSRuleValue(optionValue, optionsPattern);
if (rhsRuleValue.isPresent()) {
return rhsRuleValue;
}
}
}
}
// return arg1;
}
if (optionsPattern != null) {
if (!quiet) {
// Option name `2` not found in defaults for `1`
IOFunctions.printMessage(ast.topHead(), "optnf", F.list(optionsPattern, optionValue), engine);
}
return optionValue;
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class GraphFunctions method weightedGraphToIExpr.
public static IExpr weightedGraphToIExpr(AbstractBaseGraph<IExpr, ExprWeightedEdge> g) {
IASTAppendable vertexes = vertexToIExpr(g);
IASTAppendable[] res = weightedEdgesToIExpr(g);
IExpr graph = F.Graph(vertexes, res[0], F.list(F.Rule(S.EdgeWeight, res[1])));
return graph;
}
use of org.matheclipse.core.interfaces.IASTAppendable 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.interfaces.IASTAppendable in project symja_android_library by axkr.
the class GraphFunctions method vertexToVisjs.
private static void vertexToVisjs(Map<IExpr, Integer> map, StringBuilder buf, Graph<IExpr, ?> g) {
Set<IExpr> vertexSet = g.vertexSet();
IASTAppendable vertexes = F.ListAlloc(vertexSet.size());
buf.append("var nodes = new vis.DataSet([\n");
boolean first = true;
int counter = 1;
for (IExpr expr : vertexSet) {
// {id: 1, label: 'Node 1'},
if (first) {
buf.append(" {id: ");
} else {
buf.append(", {id: ");
}
buf.append(counter);
map.put(expr, counter++);
buf.append(", label: '");
buf.append(expr.toString());
buf.append("'}\n");
first = false;
}
buf.append("]);\n");
}
Aggregations