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

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    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

    Multivariable functions have been added. add(3) is as valid as add(3,4,5)




    Any questions? Comments? Things you want added?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Crap... forgot to attach...

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Here's the executable:

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Anything you'd like to see added?

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    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


    [edit]
    hmmm.... how about max, min, inflection, convex, concave...
    Last edited by *ClownPimp*; 06-16-2003 at 07:04 PM.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Rather impressive. I opened expression.hpp, and could have sworn I was looking at an STL implementation.

    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
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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.

    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.)

    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.
    Rather impressive. I opened expression.hpp, and could have sworn I was looking at an STL implementation.
    Actually, the for_each() template stuff there is borrowed from the gnu header file, except changed a bit to suit my needs.
    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::
    My next step should probably include posting documentation about this.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >>> 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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.
    Away.

  12. #12
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Here's a new version, with a better console interface.

    In short:
    to quit: #quit()
    to ask for help: #help()
    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.)

  13. #13
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Here's the executable:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Expression Manipulator v0.4 (bugs fixed, derivatives work with trig funcs)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-08-2003, 08:10 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM