Results 1 to 15 of 15
Thread: Mathematical functions
 11272010, 01:59 AM #1Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
 11272010, 02:14 AM #2
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?
 11272010, 02:31 AM #3Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
it has to accept a function from user input, then parse it.
 11272010, 03:57 AM #4
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+23, then there's definitely a way you can achieve this.
Is this part of a homework assignment?

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!).
 11272010, 04:02 AM #6Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
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.
 11272010, 04:06 AM #7Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
The function can be complex like this:as 1+cos(2*pi()^2),or more complex
 11272010, 04:15 AM #8
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
Operatorprecedence 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.
 11272010, 06:21 AM #9Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
Thx for the links
 11272010, 09:18 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 11272010, 01:28 PM #11Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
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.
 11272010, 01:36 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 11272010, 02:14 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
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(); } } }
JosBuild a wall around Donald Trump; I'll pay for it.
 11272010, 02:45 PM #14Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
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
 11272010, 03:08 PM #15
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads

How to evaluate the mathematical expression from the string in J2ME
By SIVAKUMAR.J in forum CLDC and MIDPReplies: 1Last Post: 11222010, 10:10 PM 
Help: Mathematical Expressions
By JohnAtick in forum New To JavaReplies: 3Last Post: 03092009, 07:02 PM 
Demonstration of the mathematical operators
By Java Tip in forum java.utilReplies: 0Last Post: 04172008, 12:02 AM 
Help with mathematical part in java
By lenny in forum Advanced JavaReplies: 3Last Post: 08092007, 02:55 PM 
Help with mathematical methods
By Marcus in forum Advanced JavaReplies: 2Last Post: 07012007, 09:20 PM
Bookmarks