# Expression Manipulator v0.5 (bugs fixed, derivatives completed)

• 06-15-2003
ygfperson
Expression Manipulator v0.5 (bugs fixed, derivatives completed)
Basically, this program takes some mathematical expression you type in, parses it, and deals with it in different ways. (Like taking the derivative, or combining like terms, etc...)

See included 'Documentation' file for instructions for use. It's just source code, but I'll attach an executable soon, maybe in the morning, maybe earlier.

Derivatives should be completed. If you see a hole, let me know so I can patch it. I added some fairly obvious functions: add(), sub(), mult(), div()
expt(x,y,z,...) = x ^ (y * z * ...)
exp(x) = e^x where e is the constant

• 06-15-2003
ygfperson
Crap... forgot to attach...
• 06-15-2003
ygfperson
Here's the executable:
• 06-16-2003
ygfperson
Anything you'd like to see added?
• 06-16-2003
Zach L.
Looks quite interesting...
I'll get back to you later about it though. My head is about to hit the keyboard, so I'm not sure I'd be able to intelligently look over it, and come up with something coherent to say.
• 06-16-2003
*ClownPimp*
one suggestion, you should rename your derive function to diff or differential, since the output of derive is actually the differential of the input:

if f(x) = sin(x), then df (which is the differential of f) = cos(x)dx.

I dont know if you already implemented this, but you should add the ability to define your own functions and variables and then use them as experiments. Something like:

fun = sinx + cosx
> fun = sinx + cosx

derive(fun)
> cosx - sinx

hmmm.... how about max, min, inflection, convex, concave...
• 06-16-2003
Zach L.
Rather impressive. I opened expression.hpp, and could have sworn I was looking at an STL implementation. :D

Out of curiosity, have you considered storing the expression as a tree (binary functions get two child nodes, unary functions get one, etc)? To me, that seems like a more natural way to represent an expression.

Anyways, very nice.

Cheers
• 06-16-2003
ygfperson
Quote:

one suggestion, you should rename your derive function to diff or differential, since the output of derive is actually the differential of the input:
You're right... but there's no harm done for the time being...

maybe I'll define diff() as derive() is now, and have derive() include an extra argument to cancel out the differential stuff.

Quote:

I dont know if you already implemented this, but you should add the ability to define your own functions and variables and then use them as experiments.
You can define functions at run-time. I just haven't thought up what people should type in the command-line to do it.

Actually, I'm thinking of overhauling the command-line system so it would only quit when you say so, and not right after you type in something. (It's not a complicated system, so overhaul'll be easy.)

Quote:

hmmm.... how about max, min, inflection, convex, concave...
Good idea... I'll need to invent ways to figure out the zeros of expressions first, though.
Quote:

Rather impressive. I opened expression.hpp, and could have sworn I was looking at an STL implementation. :D
Actually, the for_each() template stuff there is borrowed from the gnu header file, except changed a bit to suit my needs.
Quote:

Out of curiosity, have you considered storing the expression as a tree (binary functions get two child nodes, unary functions get one, etc)? To me, that seems like a more natural way to represent an expression.
It's sort of like a tree already. Vector arrays are used to allow objects to contain other objects. (Another STL container class may be a better choice, but I don't know enough to make a good choice)

Here are my classes at a glance (if you're interested):
• Expression -- contains Terms
• One -- a type of Expression which is deliberately left blank, used to stop recursion
• Term -- contains Factors
• Factor -- pure abstract class containing mathematical information or symbols
• Float -- type of Factor that is a number
• Variable -- type of Factor that is a variable
• Factor_Expression -- type of Factor which represents anything inside parentheses
• Function -- type of Factor that is a function
• Derived_Variable or Derived_Function -- specialized type caused by differentiation
• Function_Definition -- pure abstract class which shows a type of function
• Constant_Function -- type of function that is permanent and universal. All constant functions are loaded at once.
• Sine, Cosine, Square_Root etc... -- types of constant functions
• Polynomial_Function -- function definition that can be defined at run-time with any expression
• Binary_Constant -- type of Polynomial_Function which allows two arguments
• Variable_Arguments_Function -- type of constant function that allows any number of arguments
• Addition, Multiplication, etc... -- types of Variable_Arguments_Function. These are defined with the other Constant_Functions
• Domain -- An object describing at what values a function is defined
• Float_Pair -- object used by Domain to describe a range
• Expression_Set -- contains Expressions. Used in function arguments and equations
• Inequality -- set of two expressions and an operator between them
• Equation -- an Inequality with an equals sign

::takes breather::
• 06-16-2003
Zach L.
>>> It's sort of like a tree already. Vector arrays are used to allow objects to contain other objects. (Another STL container class may be a better choice, but I don't know enough to make a good choice)

Okay... There was a lot of code there to try to figure out how it worked.

>>> I'll need to invent ways to figure out the zeros of expressions first, though.

Secant and bisection methods are good. Newton-Raphson is fast, but unstable. Ridders/false position are also relatively fast, and fairly stable.
• 06-16-2003
Zach L.
Oh... A couple other things that might be of use:

You should be able to get the complex number functionality from <complex>, and Numerical Recipes in C is a really good book on numerical algorithms.

Cheers
• 06-16-2003
confuted
Not quitting until the user specifies would be good. I'm lazy and tired and using Windows and couldn't see the output. Too lazy and tired to use the command prompt right now. A built-in "help" function would be nice. If you don't have them, some people might also find a use for hyperbolic trig functions.
• 06-17-2003
ygfperson
Here's a new version, with a better console interface.

In short:
to quit: #quit()
to define a function at run-time:
Code:

`#definefunc(func_name, expression, domain variable)`
Where func_name is the function's name; expression is what the function's definition is, and domain_variable is the variable for its domain:
For example:
#definefunc(f,x^2+1,x)
creates this function:
f(x) = x^2+1
Functions may be redefined as often as wanted.
#apply(func_name, expression) will put what was last parsed inside a function of your choice.

And the rest is pretty much the same as it was before. Don't forget the '#' symbol. The program will stay open uptil you tell it to quit.

(An executable will be attached later.)
• 06-17-2003
ygfperson
Here's the executable: