use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class F method expand.
/**
* Apply <code>Expand()</code> to the given expression if it's an <code>IAST</code>. If expanding wasn't possible
* this method returns the given argument.
*
* @param a
* the expression which should be evaluated
* @param expandNegativePowers
* TODO
* @param distributePlus
* TODO
* @return the evaluated expression
* @see EvalEngine#evaluate(IExpr)
*/
public static IExpr expand(IExpr a, boolean expandNegativePowers, boolean distributePlus) {
if (a.isAST()) {
EvalEngine engine = EvalEngine.get();
IAST ast = engine.evalFlatOrderlessAttributesRecursive((IAST) a);
if (!ast.isPresent()) {
ast = (IAST) a;
}
return Algebra.expand(ast, null, expandNegativePowers, distributePlus).orElse(a);
}
return a;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class AbstractIntegerSym method factorInteger.
/** {@inheritDoc} */
@Override
public IAST factorInteger() {
IInteger factor;
IInteger last = F.CN2;
int count = 0;
final IAST iFactors = factorize(F.ListAlloc(10));
final IAST list = List();
IAST subList = null;
for (int i = 1; i < iFactors.size(); i++) {
factor = (IInteger) iFactors.get(i);
if (!last.equals(factor)) {
if (subList != null) {
subList.append(AbstractIntegerSym.valueOf(count));
list.append(subList);
}
count = 0;
subList = F.ListAlloc(2);
subList.append(factor);
}
count++;
last = factor;
}
if (subList != null) {
subList.append(AbstractIntegerSym.valueOf(count));
list.append(subList);
}
return list;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class IntegerSym method divisors.
/**
* Return the divisors of this integer number.
*
* <pre>
* divisors(24) ==> {1,2,3,4,6,8,12,24}
* </pre>
*/
@Override
public IAST divisors() {
if (isOne() || isMinusOne()) {
return F.List(F.C1);
}
Set<IInteger> set = new TreeSet<IInteger>();
final IAST primeFactorsList = factorize(F.List());
int len = primeFactorsList.size() - 1;
// build the k-subsets from the primeFactorsList
for (int k = 1; k < len; k++) {
final KSubsetsList iter = Subsets.createKSubsets(primeFactorsList, k, F.List(), 1);
for (IAST subset : iter) {
if (subset == null) {
break;
}
// create the product of all integers in the k-subset
IInteger factor = F.C1;
for (int j = 1; j < subset.size(); j++) {
factor = factor.multiply((IInteger) subset.get(j));
}
// add this divisor to the set collection
set.add(factor);
}
}
// build the final divisors list from the tree set
final IAST resultList = F.ListAlloc(set.size() + 1);
resultList.append(F.C1);
for (IInteger entry : set) {
resultList.append(entry);
}
resultList.append(this);
return resultList;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class F method eval.
/**
* Create a function with 1 argument and evaluate it.
*
* @param head
* @param a0
* @return the evaluated object
*/
public static IExpr eval(final ISymbol head, final IExpr a0) {
final IAST ast = ast(head);
ast.append(a0);
return EvalEngine.get().evaluate(ast);
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class F method Times.
public static IAST Times(final IExpr a0, final IExpr a1) {
if (a0 != null && a1 != null) {
if (a0.isTimes() || a1.isTimes()) {
int size = 0;
if (a0.isTimes()) {
size += ((IAST) a0).size();
} else {
size++;
}
if (a1.isTimes()) {
size += ((IAST) a1).size();
} else {
size++;
}
IAST result = TimesAlloc(size);
if (a0.isTimes()) {
result.appendArgs((IAST) a0);
} else {
result.append(a0);
}
if (a1.isTimes()) {
result.appendArgs((IAST) a1);
} else {
result.append(a1);
}
EvalAttributes.sort(result);
return result;
}
if (a0.compareTo(a1) > 0) {
// swap arguments
return binary(Times, a1, a0);
}
}
return binary(Times, a0, a1);
}
Aggregations