attor / symja

Automatically exported from code.google.com/p/symja
0 stars 0 forks source link

use minus instead of times -1 #36

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. ASTNode ast = parser.parse("-a");
2. ASTNode ast2 = parser.parse("a-b");
3.

What is the expected output? What do you see instead?
expected : ast.toString() -> Minus[a]
instead : ast.toString() -> Times[-1,a]

expected2 : ast2.toString() -> Minus[a,b]
instead2: ast2.toString() -> Plus[a,Times[-1,b]]

What version of the product are you using? On what operating system?
matheclipse-parser-0.0.10.jar

Please provide any additional information below.
can i modify operator.txt to have this behavior and how to?

Original issue reported on code.google.com by ponroy...@gmail.com on 1 Sep 2011 at 7:57

GoogleCodeExporter commented 9 years ago
These two classes PreMinusOperator and SubtractOperator define the behaviour 
for the '-' operator:

You can modify the createFunction() methods in these two classes like this:
org.matheclipse.parser.client.operator.PreMinusOperator
...
    public ASTNode createFunction(final IParserFactory factory,
            final ASTNode argument) {
        return factory.createFunction(factory.createSymbol("Minus"), argument);
    }
...

org.matheclipse.parser.client.operator.SubtractOperator
...
    public ASTNode createFunction(final IParserFactory factory, final ASTNode lhs,
            final ASTNode rhs) {
        return factory.createFunction(factory.createSymbol("Subtract"), lhs, rhs);
    }
...

Original comment by axelclk@gmail.com on 2 Sep 2011 at 7:14

GoogleCodeExporter commented 9 years ago
Thanks a lot! your suggestion works. 
I did the same kind of modification with the DivideOperator : 

org.matheclipse.parser.client.operator.DivideOperator
...
    public ASTNode createFunction(final IParserFactory factory,
            final ASTNode lhs, final ASTNode rhs) {
                factory.createInteger(-1)));
        if (rhs instanceof IntegerNode && lhs instanceof IntegerNode) {
            return new FractionNode((IntegerNode) lhs, (IntegerNode) rhs);
        }
        return factory.createFunction(factory.createSymbol("Divide"),lhs,rhs);
    }

Original comment by ponroy...@gmail.com on 5 Sep 2011 at 11:31

GoogleCodeExporter commented 9 years ago

Original comment by axelclk@gmail.com on 5 Sep 2011 at 5:02