use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class AbstractAST method linearPower.
@Override
public IExpr[] linearPower(IExpr variable) {
int size = size();
int counter = 0;
if (isPlus()) {
// a + b + c....
IASTAppendable plusClone = copyAppendable();
IExpr[] subLinear = null;
int j = 1;
for (int i = 1; i < size; i++) {
if (get(i).isFree(variable, true)) {
j++;
} else {
if (counter > 0 || get(i).isPlus()) {
return null;
}
subLinear = get(i).linearPower(variable);
if (subLinear != null) {
counter++;
plusClone.remove(j);
} else {
return null;
}
}
}
if (subLinear != null) {
return new IExpr[] { plusClone.oneIdentity0(), subLinear[1], subLinear[2] };
}
return new IExpr[] { plusClone.oneIdentity0(), F.C0, F.C1 };
} else if (isTimes()) {
IInteger exp = F.C1;
// a * b * c....
IASTAppendable timesClone = copyAppendable();
int j = 1;
for (int i = 1; i < size; i++) {
if (get(i).isFree(variable, true)) {
j++;
} else {
if (get(i).equals(variable)) {
if (counter > 0) {
return null;
}
counter++;
timesClone.remove(j);
continue;
} else if (get(i).isPower()) {
if (counter > 0) {
return null;
}
IAST power = (IAST) get(i);
if (power.base().equals(variable) && power.exponent().isInteger()) {
exp = (IInteger) power.exponent();
counter++;
timesClone.remove(j);
continue;
}
}
return null;
}
}
return new IExpr[] { F.C0, timesClone.oneIdentity1(), exp };
} else if (isPower() && base().equals(variable) && exponent().isInteger()) {
return new IExpr[] { F.C0, F.C1, exponent() };
} else if (this.equals(variable)) {
return new IExpr[] { F.C0, F.C1, F.C1 };
} else if (isFree(variable, true)) {
return new IExpr[] { this, F.C0, F.C1 };
}
return null;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class ASTRealMatrix method setAtCopy.
@Override
public IASTMutable setAtCopy(int i, IExpr expr) {
if (expr instanceof ASTRealVector) {
IASTMutable ast = copy();
ast.set(i, expr);
return ast;
}
IASTAppendable ast = copyAppendable();
ast.set(i, expr);
return ast;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class ASTRealMatrix method mapMatrixColumns.
/**
* {@inheritDoc}
*/
@Override
public IExpr mapMatrixColumns(int[] dim, Function<IExpr, IExpr> f) {
final int columnSize = dim[1];
IASTAppendable result = F.ListAlloc(columnSize);
return result.appendArgs(0, columnSize, j -> f.apply(new ASTRealVector(matrix.getColumnVector(j), false)));
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class B2 method copyAppendable.
@Override
public IASTAppendable copyAppendable(int additionalCapacity) {
IASTAppendable result = F.ast(head(), additionalCapacity + 2);
result.append(arg1);
result.append(arg2);
return result;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class AST method parse.
/**
* simple parser to simplify unit tests. The parser assumes that the String contains no syntax
* errors.
*
* <p>
* Example "List[x,List[y]]"
*
* @param inputString
* @return
*/
public static IAST parse(final String inputString) {
final StringTokenizer tokenizer = new StringTokenizer(inputString, "[],", true);
String token = tokenizer.nextToken();
final IASTAppendable list = newInstance(StringX.valueOf(token));
token = tokenizer.nextToken();
if ("[".equals(token)) {
parseList(tokenizer, list);
return list;
}
// syntax fError occured
return null;
}
Aggregations