Results 1 to 15 of 15
  1. #1
    Stev is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Mathematical functions

    Hy,
    I'm a new java programmer and I have a big problem. I have to
    Implement a code which can read a mathematical function given by someone and then integrate this one. Did everyone here know how I can do this?
    Thx

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Do you mean it has to accept a function from user input, then parse it? Or do you just have to include a mathematical function in your program?

  3. #3
    Stev is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    it has to accept a function from user input, then parse it.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You mean something as complex as 1+cos(2*pi()^2)?

    If so, I hate to say it, but that took me three days and help from a friend to put together (and I've been programming for eight years).

    If it's something simpler, like 1+2-3, then there's definitely a way you can achieve this.

    Is this part of a homework assignment?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    When he says "integrate" I have to wonder if his equation is even more complex than the one you show, one that involves integral calculus (shudder!).

  6. #6
    Stev is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    it can be complex like the first mathematical function(as 1+cos(2*pi()^2)) u already work on. I know that's not simple ,I tried it today the whole day.

  7. #7
    Stev is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    The function can be complex like this:as 1+cos(2*pi()^2),or more complex

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You're new to Java... this isn't a great starting project.

    Have a look at these links if you're still interested...
    Lexical analysis - Wikipedia, the free encyclopedia
    Operator Precedence Parsing
    Operator-precedence parser - Wikipedia, the free encyclopedia

    ...if you're still interested, those are just theory papers. You'll need a good understanding of the language before attempting this.

  9. #9
    Stev is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Thx for the links

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    You mean something as complex as 1+cos(2*pi()^2)?

    If so, I hate to say it, but that took me three days and help from a friend to put together (and I've been programming for eight years).

    If it's something simpler, like 1+2-3, then there's definitely a way you can achieve this.

    Is this part of a homework assignment?
    Those examples are very special functions, i.e. they are constant numerical values; normally mathematical functions have one or more 'bound' variables ('parameters' in Java). e.g. f(x) = 1+cos(2*x)^2. Here 'x' is the bound variables. But mathematical functions can also be 'higher order' functions where the parameter(s) itself can also be function(s); e.g. f(g, x) = g(x); here 'g' is a parameter (or bound variable) but also a function itself.

    Functions need not be specified as mathematical expressions, e.g. f(x) = if x < 0 then -1 else if x > 0 then 1 else 0; this is a perfect mathematical function given an interval definition.

    Mahematical functions can also have 'free' variables where the result of the function is a function itself: e.g. f(x) = x+y. This function has as result another function given a binding for variable x.

    The op should define the problem in a more narrow and more elaborate way for us to be able to solve it.

    If the op wants to integrate functions s/he should specify whether or not the integration should be done numerically (possibly an approximation can do) or symbolically. Are those functions simple functions that bind a single variable?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Stev is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    i have to parser that kind of function "f(x) = 1+cos(2*x)^2",or more complex with only one bound,i just dont know where im supposed to beginn wit the parse.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stev View Post
    i have to parser that kind of function "f(x) = 1+cos(2*x)^2",or more complex with only one bound,i just dont know where im supposed to beginn wit the parse.
    A few years ago I wrote a couple of articles on compilers, lexical analyzers (including Java source code) for a little forum: here. I hope the articles are still there because they did'nt organize the articles very well. a.a.m.o.f. the lexical analyzers took up most of the code; the (recursive descent) parsers are a breeze.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stev View Post
    i have to parser that kind of function "f(x) = 1+cos(2*x)^2",or more complex with only one bound,i just dont know where im supposed to beginn wit the parse.
    There's also another approach: leave it all to a Javascript implementation. Java 1.6 comes bundled with Rhino, a Javscript implementation written in Java. The disadvantage is that you can only use the functions implemented in Java. The advantage is that you can define your own functions. Here's a small example:

    Java Code:
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
     
    public class ScriptDemo {
     
        public static void main(String[] args) {
            
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("JavaScript");
            
            try {
            	engine.put("x", Math.PI);
            	System.out.println("result: "+engine.eval("1+Math.pow(Math.cos(2*x), 2)"));
    
            } catch(ScriptException se) {
                se.printStackTrace();
            }
        }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Stev is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    oh cool,that's what i need. i thank you
    but m y question,how long are u already programming,where do u know such classes like javax.script.ScriptEngine
    javax.script.ScriptEngineManager;

    i didnt hear for it before.
    thx

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stev View Post
    oh cool,that's what i need. i thank you
    but m y question,how long are u already programming,where do u know such classes like javax.script.ScriptEngine
    javax.script.ScriptEngineManager;

    i didnt hear for it before.
    thx
    You're welcome; I've been programming since 1975 (Java wasn't even known as a coffee brand in those days yet ;-) I stumbled on those Engines by reading the API documentation (huh? What's this? Lemme check). It's a nice mechanism and I've implemented those interfaces for some of my own little languages too. It was a JSR so it's heavily standardized ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 11-22-2010, 09:10 PM
  2. Help: Mathematical Expressions
    By JohnAtick in forum New To Java
    Replies: 3
    Last Post: 03-09-2009, 06:02 PM
  3. Demonstration of the mathematical operators
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-16-2008, 11:02 PM
  4. Help with mathematical part in java
    By lenny in forum Advanced Java
    Replies: 3
    Last Post: 08-09-2007, 01:55 PM
  5. Help with mathematical methods
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 08:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •