user3170899
user3170899

Reputation: 40

How to algebraically expand a mathematical operation String

Lets say I have

String equation = "-2(3u-x)-v"

from user input

I want to know if there is a library that would convert this into: -6u+2x-v

I can't seem to find any thus I have tackled this on my own but it seems a lot of work and I am doing it inefficiently (I only account for u, x, and v).

Below I am modifying the the list to group integers for easier multiplication.

char[] charArray = equation.toCharArray();
ArrayList<String> eq= new ArrayList<String>();


    for (int i = 0; i <= charArray.length-1; i++) {
        if (charArray[i] == 'x' || charArray[i] == 'u' || charArray[i] == 'v') {
            eq.add(String.valueOf(charArray[i]));
        }
        else if (charArray[i] == '-' || charArray[i] == '+') {
            int x = 1;
            if (charArray[i+1] != 'u' && charArray[i+1] != 'v' && charArray[i+1] != 'x')
                x = charArray[i+1]-48;
            if (charArray[i] == '-')
                x = x*-1;
            eq.add(String.valueOf(x));
        }
        else if (charArray[i] == '(' || charArray[i] == ')' || charArray[i] == '=') {
            eq.add(String.valueOf(charArray[i]));
        }
    }

PS I am new to programming so it might not look professional, sorry about that

Upvotes: 1

Views: 296

Answers (1)

axelclk
axelclk

Reputation: 958

With the Symja library you can expand expressions like this:

package org.matheclipse.core.examples;

import static org.matheclipse.core.expression.F.*;

import org.matheclipse.core.eval.ExprEvaluator;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.parser.client.SyntaxError;
import org.matheclipse.parser.client.math.MathException;

public class SO_ExpandExample {
    public static void main(String[] args) {
        try {
            ExprEvaluator util = new ExprEvaluator(false, 100);

            // expand an expression by using Expand() or ExpandAll() function
            IExpr result = util.evaluate("ExpandAll(-2(3u-x)-v)");
            // print: -6*u-v+2*x
            System.out.println(result.toString());

            // Show an expression in the Java form:
            String javaForm = util.toJavaForm("ExpandAll(-2(3u-x)-v)");
            // prints: ExpandAll(Plus(Times(CN2,Plus(Times(C3,u),Negate(x))),Negate(v)))
            System.out.println(javaForm.toString());

            // use the JavaForm:
            IAST function = ExpandAll(Plus(Times(CN2, Plus(Times(C3, u), Negate(x))), Negate(v)));
            result = util.evaluate(function);
            // print: -6*u-v+2*x
            System.out.println(result.toString());

        } catch (SyntaxError e) {
            // catch Symja parser errors here
            System.out.println(e.getMessage());
        } catch (MathException me) {
            // catch Symja math errors here
            System.out.println(me.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 2

Related Questions