Search in sources :

Example 1 with PatternMatcherEquals

use of org.matheclipse.core.patternmatching.PatternMatcherEquals in project symja_android_library by axkr.

the class Symbol method reassignSymbolValue.

/**
	 * {@inheritDoc}
	 */
@Override
public IExpr[] reassignSymbolValue(Function<IExpr, IExpr> function, ISymbol functionSymbol, EvalEngine engine) {
    IExpr[] result = new IExpr[2];
    IExpr symbolValue;
    if (hasLocalVariableStack()) {
        symbolValue = get();
        result[0] = symbolValue;
        IExpr calculatedResult = function.apply(symbolValue);
        if (calculatedResult.isPresent()) {
            set(calculatedResult);
            result[1] = calculatedResult;
            return result;
        }
    } else {
        if (fRulesData != null) {
            PatternMatcherEquals pme = fRulesData.getEqualDownRules().get(this);
            if (pme != null) {
                symbolValue = pme.getRHS();
                if (symbolValue != null) {
                    result[0] = symbolValue;
                    IExpr calculatedResult = function.apply(symbolValue);
                    if (calculatedResult.isPresent()) {
                        pme.setRHS(calculatedResult);
                        result[1] = calculatedResult;
                        return result;
                    }
                }
            }
        }
    }
    engine.printMessage(toString() + " is not a variable with a value, so its value cannot be changed.");
    return null;
}
Also used : PatternMatcherEquals(org.matheclipse.core.patternmatching.PatternMatcherEquals) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with PatternMatcherEquals

use of org.matheclipse.core.patternmatching.PatternMatcherEquals in project symja_android_library by axkr.

the class Symbol method reassignSymbolValue.

/**
	 * {@inheritDoc}
	 */
@Override
public IExpr[] reassignSymbolValue(IAST ast, ISymbol functionSymbol, EvalEngine engine) {
    IExpr[] result = new IExpr[2];
    IExpr symbolValue;
    if (hasLocalVariableStack()) {
        symbolValue = get();
        result[0] = symbolValue;
        // IExpr calculatedResult = function.apply(symbolValue);
        ast.set(1, symbolValue);
        // F.binaryAST2(this, symbolValue, value));
        IExpr calculatedResult = engine.evaluate(ast);
        if (calculatedResult != null) {
            set(calculatedResult);
            result[1] = calculatedResult;
            return result;
        }
    } else {
        if (fRulesData != null) {
            PatternMatcherEquals pme = fRulesData.getEqualDownRules().get(this);
            if (pme != null) {
                symbolValue = pme.getRHS();
                if (symbolValue != null) {
                    result[0] = symbolValue;
                    // IExpr calculatedResult = function.apply(symbolValue);
                    ast.set(1, symbolValue);
                    IExpr calculatedResult = engine.evaluate(ast);
                    if (calculatedResult != null) {
                        pme.setRHS(calculatedResult);
                        result[1] = calculatedResult;
                        return result;
                    }
                }
            }
        }
    }
    throw new WrongArgumentType(this, functionSymbol.toString() + " - Symbol: " + toString() + " has no value! Reassignment with a new value is not possible");
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) PatternMatcherEquals(org.matheclipse.core.patternmatching.PatternMatcherEquals) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IExpr (org.matheclipse.core.interfaces.IExpr)2 PatternMatcherEquals (org.matheclipse.core.patternmatching.PatternMatcherEquals)2 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1