lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 8/05/2009, at 7:10 PM, steve donovan wrote:

On Fri, May 8, 2009 at 12:01 AM, Geoff Leyland
<> wrote:
I've had a quick look at your code, but I can't work this one out: what's the result of differentiating 3*x^2 + 5*x^2? That is, does it gather terms
that aren't exactly alike?

Yes, Houston, we do have a problem:

+ 	3 * 2 * x + 5 * 2 * x   (unfolded result)
- 	6 * x + 10 * x       (folded result)

Obviously it's missing an opportunity for simplification, but I don't
know how to express it generally yet.

I cheat for this: I separate any coefficient from the rest of the expression, make sure that the rest of the terms are ordered alphabetically, and then compare string representations:

3 * x * y + 5 * y * x =>

terms["x*y"] = { coeff=3, expression=x * y}
and then
terms["x*y"].coeff += 5

It's dirty but it works well enough. For my purposes, I don't want to transform x * z + y * z into (x + y) * z, which would be quite a bit tougher.

And collaboration would be most cool.

Yes. I've already pinched some good ideas from your code, thanks, so I figure I owe you.