1. Member Join Date
Nov 2010
Posts
7
Rep Power
0

## 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  Reply With Quote

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?  Reply With Quote

3. Member Join Date
Nov 2010
Posts
7
Rep Power
0

## it has to accept a function from user input, then parse it.  Reply With Quote

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+2-3, then there's definitely a way you can achieve this.

Is this part of a homework assignment?  Reply With Quote

5. ## 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!).  Reply With Quote

6. Member 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.  Reply With Quote

7. Member 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  Reply With Quote

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
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.  Reply With Quote

9. Member Join Date
Nov 2010
Posts
7
Rep Power
0

##   Reply With Quote

10. ##  Originally Posted by Zack 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  Reply With Quote

11. Member 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.  Reply With Quote

12. ##  Originally Posted by Stev 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  Reply With Quote

13. ##  Originally Posted by Stev 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  Reply With Quote

14. Member 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  Reply With Quote

15. ##  Originally Posted by Stev 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  Reply With Quote

#### Posting Permissions

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