SymRational, Computer Algebra

dsimcha dsimcha at yahoo.com
Sun Oct 11 10:24:57 PDT 2009


I've been thinking about where a Rational struct could lead, and realized that
the next step is to give D some capabilities for dealing with symbols.  For
example, it would be nice to have something like:

auto foo = symRational!SomeBigInt("1 / e + pi / 2 + G");
// Do a whole bunch of manipulations on foo.

writeln(foo);  // Symbolic expression.
writeln(toReal(foo, constVal("G", 6e-11), constVal("pi", 3.14),
    constVal("e", 2.718));

Of course, now we're starting to talk about building a basic computer algebra
system into Phobos, or at least into some stand-alone lib.  While we clearly
don't have the manpower to do a "real" CAS, some basic CAS-like capabilities
would be nice.

Programmers in most languages tend to use floating-point arithmetic as their
default way of doing math.  It really only makes sense to do this when speed
is more important than precision or interpretability of the answer.  However,
it seems to get used even when this isn't the case simply because it's what's
readily available.  If D/Phobos had some built-in (very basic) CAS-like
capabilities that were as easy to use as floats, people might actually
consider using either symbolic or numerical manipulation on the relevant
tradeoffs rather than just choosing numeric because it's what's easily available.

The problem with using a CAS's builtin language whenever you want basic
CAS-like capabilities is that it's too domain-specific.  You can't easily
integrate it into a larger program that requires a full-fledged general
purpose programming language.  The beauty of D is that it has so many features
for library designers that libraries can start to look like DSLs yet still
integrate smoothly with the general-purpose subset of D.  Half the reason why
I wrote the dstats statistics lib was to prove this concept, since similar
issues arise in performing statistical calculations.

The question, then, becomes, how far do we go toward trying to put some basic
CAS-like capabilities like symbolic arithmetic into Phobos?  Could things like
this really be D's killer application?



More information about the Digitalmars-d mailing list