### Stack: removing brackets only!

Number of replies: 2### Re: Stack: removing brackets only!

Firstly

x^2-(-5x^2-14x+2) is not equal to x^2+5x^2+14x-2 up to "commutativity and associativity" because you are distributing -1 over addition! I agree that the test you ask for is very useful (and something you'd want to test for). But, I have not written a function to distribute "-" over a sum and "sort out any resulting --".

Unary minus has been the the most difficult thing for me to deal with in STACK.

I'm sure it would be possible to do so. For code which does similar things you might like to look between lines 266-336

of https://github.com/maths/moodle-qtype_stack/blob/master/stack/maxima/stackmaxima.mac

The "Sandbox" will be invaluable if you write some Maxima code.

https://github.com/maths/moodle-qtype_stack/blob/master/doc/en/CAS/STACK-Maxima_sandbox.md

Chris

### Re: Stack: removing brackets only!

I should also add that I made a start on this myself some time ago, and I have placed the code in GitHub, https://github.com/maths/moodle-qtype_stack/blob/master/stack/maxima/elementary.mac

There is also a file of "experimental code".

The point of this is to define "elementary rules" which will transform the expression repeatedly.

Start in the Maxima sandbox and

load("elementary");

As an example take your expression,

simp:false;

p:x^2-(-5*x^2-14*x+2) ;

Now, we can apply "rules" such as "distribute unary minus over addition", this is called "negDistAdd"

p:transr(p,"negDistAdd");

this gives

x^2+(-(-5)*x^2-(-14*x)-2)

Next, we need to use associativity of addition, "assAdd"

p:transr(p,"assAdd");

x^2-(-5)*x^2-(-14*x)-2

We continue with a rule which removed "--",

p:transr(p,"negNeg");

x^2-(-5)*x^2+14*x-2

Notice, this sorted out the "-(-14*x)", but not the "-(-5)*x^2", but as I said, this code is experimental and I never finished this off.....

I hope this gives you some idea of the issues involved in making this work. I'm sure it is possible to do, however.

Chris