MathExp: KISS or All-Out?

Bill Baxter wbaxter at gmail.com
Thu Oct 15 04:05:52 PDT 2009


On Wed, Oct 14, 2009 at 2:49 PM, dsimcha <dsimcha at yahoo.com> wrote:
> I'm working on some mathy modules that I'd like to eventually contribute to
> Phobos, or, if they're too niche, to a standalone lib.
> One that I've alluded
> to here in the past few days is MathExp.  Basically what it does is
> parse/interpret/evaluate/manipulate mathematical expressions at runtime.

I kinda think it's too niche.  A nice thing to have if you're
implementing a calculator or some other program where users can input
their own expressions.  But other than that I can't think of any time
I've run across the need to evaluate expressions at runtime.  What
sort of use cases did you have in mind?

> Here's a summary of what I've got working so far:
>
> MathExp numerator = mathExp("x^2 * cos(x)", "x");
> MathExp denominator = mathExp("exp(-x)", "x");
> MathExp ratio = numerator / denominator;
>
> // Print the result of
> // (x^2 * cos(x)) / (exp(-x)) evaluated at x = 5.
> writeln(ratio(5));
>
> // Compute a derivative symbolically.
> MathExp derivDenominator = denominator.derivative("x");

Not really relevant to your lib, but for cases where you have
compile-time expressions and you want to evaluate the derivative at
runtime, there is something called automatic differentiation tends to
be much more effective than symbolic differentiation if the goal is to
actually compute a numerical result.
http://en.wikipedia.org/wiki/Automatic_differentiation
I just think it's a neat trick, so I mention it here.  :-)

> I'm trying to figure out which one makes more sense in the grand scheme of
> things.  MathExp might be best kept very simple and stupid and pretty much
> left as is, possibly even with the symbolic differentiation capabilities
> removed (though these contribute surprisingly little to the weight of the
> code; I wrote most of them in one evening just to see if I could).  It would
> be easy to use, understand and debug, but be very stupid and have virtually no
> semantic understanding of the expressions it's manipulating.  It wouldn't even
> be able to simplify, for example, (1 + x + 1) -> (x + 2).
>
> On the other hand, I could take this to the nth degree and add things like
> decent printing capabilities (right now, my toString() method is a huge
> kludge, is intended for debugging purposes, and puts in tons of unnecessary
> parentheses).  I could figure out how to add some semantic analysis to
> simplify expressions, maybe symbolic equation solving in some simple cases,
> etc.  The downside to this is that I would be reinventing the computer algebra
> system in D, which might be a bit too much for a small hobby project that's
> intended as a Phobos module.  I'd likely make the code an order of magnitude
> more complicated than it is now and might end up reinventing a square wheel or
> writing some really buggy code.

Yeh, it sounds like it to me.  Writing a serious CAS is a huge project
and most of the time people who need such functionality don't really
need it in library form.  They need an interactive program like Maple
to get some result which they then plug into their own program.

If you do decide to do it, though, the SymPy project might be a nice
one to look at.  It's coming along quite nicely.  I don't know
anything about the internals, but their stated goals are to maintain
readability and simplicity of the code as much as possible, even if it
leads to less efficiency.

> If the goal here is to build a Phobos module or standalone plain old library,
> do you think the KISS approach or the full-fledged approach is more useful to
> the D community?

I'd stick with KISS unless you really want CAS to become your full-time hobby.

--bb



More information about the Digitalmars-d mailing list