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

Printable View

- 11-27-2010, 01:59 AMStevMathematical 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 - 11-27-2010, 02:14 AMZack
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?

- 11-27-2010, 02:31 AMStev
it has to accept a function from user input, then parse it.

- 11-27-2010, 03:57 AMZack
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? - 11-27-2010, 04:01 AMFubarable
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!).

- 11-27-2010, 04:02 AMStev
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.

- 11-27-2010, 04:06 AMStev
The function can be complex like this:as 1+cos(2*pi()^2),or more complex

- 11-27-2010, 04:15 AMZack
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. - 11-27-2010, 06:21 AMStev
Thx for the links

- 11-27-2010, 09:18 AMJosAH
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 - 11-27-2010, 01:28 PMStev
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.

- 11-27-2010, 01:36 PMJosAH
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 - 11-27-2010, 02:14 PMJosAH
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:

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();

}

}

}

Jos - 11-27-2010, 02:45 PMStev
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 - 11-27-2010, 03:08 PMJosAH
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