We are about to switch to a new forum software. Until then we have removed the registration on this forum.

- All Categories 25.7K
- Announcements & Guidelines 13
- Common Questions 30
- Using Processing 22.1K
- Programming Questions 12.2K
- Questions about Code 6.4K
- How To... 4.2K
- Hello Processing 72
- GLSL / Shaders 292
- Library Questions 4K
- Hardware, Integration & Other Languages 2.7K
- Kinect 668
- Arduino 1K
- Raspberry PI 188
- Questions about Modes 2K
- Android Mode 1.3K
- JavaScript Mode 413
- Python Mode 205
- Questions about Tools 100
- Espanol 5
- Developing Processing 548
- Create & Announce Libraries 211
- Create & Announce Modes 19
- Create & Announce Tools 29
- Summer of Code 2018 93
- Rails Girls Summer of Code 2017 3
- Summer of Code 2017 49
- Summer of Code 2016 4
- Summer of Code 2015 40
- Summer of Code 2014 22
- p5.js 1.6K
- p5.js Programming Questions 947
- p5.js Library Questions 315
- p5.js Development Questions 31
- General 1.4K
- Events & Opportunities 289
- General Discussion 365

I have an number of functions in an interface as shown below and want to create functions in the mathematical sense ( f(x)=stuff ). Is there a way in which I can dynamically implement functions to a variable while the program is running? i.e.

`float y=Function[0].Component(Function[2].Component(Function[4].Component(x,2),1), Function[2].Component(x,2), 1);`

which is y=x²+2x+1

```
interface MathOperations
{
float Component(float...comps);
}
MathOperations[] Function =
{
new MathOperations() {
float Component(float...comps) {
float add=0;
for (float comp:comps){
add+=comp;
}
return add;
}
},
new MathOperations() {
float Component(float...comps) {
float substract=0;
for (float comp:comps){
substract-=comp;
}
return substract;
}
},
new MathOperations() {
float Component(float...comps) {
float multiply=1;
for (float comp:comps){
multiply*=comp;
}
return multiply;
}
},
new MathOperations() {
float Component(float...comps) {
float devide;
devide=comps[0]/comps[1];
return devide;
}
},
new MathOperations() { //power: x, base
float Component(float...comps) {
return pow(comps[0],comps[1]);
}
},
new MathOperations() { //logarithm: base, x
float Component(float...comps) {
return log(comps[1])/log(comps[0]);
}
},
new MathOperations() { //root: x, degree
float Component(float...comps) {
return pow(comps[0], 1/comps[1]);
}
},
new MathOperations() {
float Component(float...comps) {
return sin(comps[0]);
}
},
new MathOperations() {
float Component(float...comps) {
return cos(comps[0]);
}
},
new MathOperations() {
float Component(float...comps) {
return tan(comps[0]);
}
},
new MathOperations() {
float Component(float...comps) {
return atan(comps[0]);
}
}
};
```

## Answers

You could try the

QScriptorJasminelibraries both can be installed through the PDE.http://forum.Processing.org/two/discussion/7147/some-simple-pulse-equations-using-trig

Thank you for your replies I think this doesn't really solve the problem. I want to deisign equations while the program is running and using the basic MathOperations I have posted create the wanted function. The point is to be able to compose any function within the program (given the basic components ie +,-,log,sin) and then graphically represent it

but not define it in the code!yes, QScript can do this

just read in a string with your formula and eval it:

Solver.evaluate("$x=2; println($x + ' cubed is ' + $x^3) ");

or

Solver.evaluate(myFormula);

or so

Those proposed solutions are based on calling some

eval()-like function over some on-the-fly "designed" String. Dunno what else could be more flexible than that! ^#(^I have difficulty in passing variables from processing to Solver.evaluate. And I have not found an answer in QScript documentation... Also how can I store the output in a variable? When I write

`float y=Solver.evaluate()`

it outputs an error "cannot convert from Result to float"see

http://lagers.org.uk/qscript/index.html

http://lagers.org.uk/qscript/ref/classorg_1_1qscript_1_1_result.html

Thank you for your replies! I want to have something like task="x+2" where x will be taking values from processing,

not be defined inside the scriptlike task = "$x=2; x+2"aha!

I don't know

makes no sense, since the user doesn't know the names of the vars anyway

instead implement the whole thing within QScript without the external vars...

;-)

It would be better to use the Jasmine library. This code draws the sine wave.

Produces

@quark

I think he is looking for a way that the user enters string that gets evaluated but at the same time, the user can reference vars from the underlying sketch in the String and use them in his formula....

as a matter of fact I managed to do what I wanted with QScript (thank you very much for that) but for complex functions the program is slow. And with Solver$.evaluate() i am not getting a float but this "org.qscript.Solver$@a668bc ". I will give it a try with Jasmine to see if the calculations are done faster.

how did you do it then?

1.you wrote

how did you do it then?

2.you wrote

did you try:

To enter Processing variables to the Solver.evaluate() function I exploited the String task. I broke it down as shown in the example below:

String n contains the mathematical function I wanted and also takes values for x from the program and not from inside the script. This method however is slow for more complex functions like

And slows the whole program down. In the QScript documentation the function Solver$.evaluate() is suggested for shorter execution time. However this function introduces the problem descibed above and is therefore unusable. The execution time is the only problem so far and for that reason I will also try the Jasmine library

No luck with Jasmine either. It is worse in terms of speed than QScript. Is there anything I can do about that?

A number of points here

1) In QScript it is possible to

a) create a Script object using a String to represent the equation

b) then initialise the variables in the equation

c) then evaluate the equation

d) retrieve the values in any variables in the equation

the last three steps can be repeated as often as required.

In the latest version you don't need the $ to identify variables

2) Jasmine can do the same but the syntax is different.

3)

Jasmine is faster than QScriptand I can say that in all confidence because I created both libraries. In fact Jasmine is of the same order of execution speed as pure Java. If it is not fast enough for you then you are either doing something wrong or expecting the impossible. The two programs below both calculate the hypotenuse of 3 right-angle triangles, the first uses QScript and takes 3ms and the second uses Jasmine and takes 0ms (i.e. less than 1ms) when measured on my machine.4) Statements like

without code or other information are absolutely useless. In fact it appears that you have made the assumption that the lack of speed is down to the actual libraries rather than with your code.

=====================

I never ment to offend you and I am sorry if I did. Both libraries are, if anything, awesome and I am grateful to you for writing them and distributing them freely.

You are right about that, i did not phrase what I wanted to say correctly. What I meant is that using either of the libraries inside a loop that repeats 400 times is (as I have witnessed in my computer) slower that writing (for example)

which is understandable, acceptable and expected.

The trick with using these libraries is to understand that the slow part is parsing or compiling the expression. That's line 18 in the QScript example and line 10 in the Jasmine example from my last post. If you do these inside a loop then it will be significantly slower than Java and should be avoided.

The posted QScript and Jasmine examples as well as the sine wave example show how you can parse/compile the equation once and then use it many times without slowing the program noticeably.

BTW

QScript parsing is generally faster than Jasmine compiling but Jasmine is VERY MUCH faster when evaluating the equation.I suggest that you create some simple sketches to experiment and get used to using these libraries and if you have questions post them here with the your code. :D

Wow! The execution time has improved greatly and there is no lag. I am parsing the script once in draw()

If and when the equation is changed I can simply change the boolean to parse=false and reparse fx.

I encountered the following in my program.

x is a float variable. The equation is y=x but when I retrieve the variable y, its values have error when x does not. Here is a part of the output

`19.6 19.600000381469727 19.65 19.649999618530273 19.7 19.700000762939453 19.75 19.75 19.8 19.799999237060547 19.85 19.850000381469727 19.9 19.899999618530273 19.95 19.950000762939453`

I know that float variables are not always precise and there are techniques to make them so (as per this link https://processing.org/reference/float.html). But are there any techniques for removing the error from this function?

Never mind! Variable y does not have error...!!