Input and Parse Math Functions

This is a discussion on Input and Parse Math Functions within the C Programming forums, part of the General Programming Boards category; I'm in a numerical analysis class and our assignment is to use Newton's method to estimate roots of a fuction ...

  1. #1
    Not just a squid...
    Join Date
    Sep 2004
    Posts
    25

    Input and Parse Math Functions

    I'm in a numerical analysis class and our assignment is to use Newton's method to estimate roots of a fuction after n steps. That is the easy part, but our professor decided that he wants to be able to input any function ( for example f(x) = 23x^4-3x^2-35x+2 ) at a promt and have our program still work. He doesn't care if we use any outside libraries as long as we don't use them for the method itself. Anyone have recommendations for a library (I can do it in C or C++) that will allow a user to enter a function like that and use it in calculations?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    No, but you could ease the process with lex+yacc (or rather the modern implementations, flex and bison). Both programs generate C code that you compile with your program. http://ds9a.nl/lex-yacc/cvs/lex-yacc-howto.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    If they're always expressed as polynomials, all you need is an array, a loop and a value for x

    Eg.
    double terms[5];

    Then parse the string to get
    terms[4] = 23;
    terms[3] = 0;
    terms[2] = -3;
    terms[1] = -35;
    terms[0] = 2;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Not just a squid...
    Join Date
    Sep 2004
    Posts
    25
    Quote Originally Posted by Salem View Post
    If they're always expressed as polynomials, all you need is an array, a loop and a value for x
    I don't think they will be. He should be able to input any function that is continuous and differentiable over a given range. That's why I was hoping someone out there already wrote a library that would process the input for me.

    No, but you could ease the process with lex+yacc (or rather the modern implementations, flex and bison).
    I'm familiar with these but I've never used them. I didn't think these could parse input in a program for use at runtime though. I'll look into it.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If your professor allows it, you should just input first the degree of the polynomial, and then the coefficients in a loop. There's no point in getting bogged down in how to parse a string containing a polynomial when that has nothing to do with the mathematics involved.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by TheSquid View Post
    I don't think they will be. He should be able to input any function that is continuous and differentiable over a given range. That's why I was hoping someone out there already wrote a library that would process the input for me.



    I'm familiar with these but I've never used them. I didn't think these could parse input in a program for use at runtime though. I'll look into it.
    Sure they can, that's how compilers work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Math Function Input
    By swanley007 in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 06:29 PM
  3. input parse error
    By pktcperlc++java in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2004, 12:17 AM
  4. Parse error at end of input??
    By erikcn in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2002, 09:41 AM
  5. Parse Error At End Of Input?!?!?!?!?!
    By kas2002 in forum C++ Programming
    Replies: 3
    Last Post: 06-02-2002, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21