MathExp: KISS or All-Out?

Fawzi Mohamed fmohamed at mac.com
Thu Oct 15 01:51:04 PDT 2009


On 2009-10-14 23:49:07 +0200, dsimcha <dsimcha at yahoo.com> said:

> 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.
> 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");
> 
> 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.
> 
> 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?

Looks interesting, I would vote for KISS... maybe in the future with 
possible runtime compilation of a function... :)

Fawzi





More information about the Digitalmars-d mailing list