# DMelt:Math/3 Non Parametric Functions

## Contents

# Non-parametric functions

The most flexible way to draw functions is to use codding with objects and calling third-party Java libraries directly, instead of defining a function using strings as it was done for jhplot.F1D or jhplot.F2D classes. Here we will show how to create functions completely programmically using any arbitrary definition. Below we will show several approaches.

## Extending FNon Java class

Let us consider a function of the form:

```
if x>3: y=x*x*sqrt(x)
if x<3: y=0
```

To construct the mathematical functions defined above and plot it, one can build a new class by extending the class jhplot.FNon. You will need to overwrite the method "value()" that returns the calculated value.

This example shows how to do this assuming no free parameters:

Protected code example! Request membership or login to this member link first. |

Here we used functions from the Java class java.lang.Math with mathematical functions. Note that x[0] indicates a variable (0 means that the dimension of this function is one). As you can see, we define the function using Python codding, and then use it as input for the class jhplot.F1D that can be plotted. The result of this script is shown below.

You can extend this example by adding two free parameters, p[0] and p[1]. This example shows how to do this:

Protected code example! Request membership or login to this member link first. |

with the image is

Note that you can also construct functions in a similar way using Java codding.

## Using user-defined steps

Here is an example of some complex function of the form:

```
if x>3: exp(sqrt(x))+log10(x*exp(x))
if x<3: 1
```

This time we will draw this function using Java methods from the class java.lang.Math:

```
from jhplot import *
from java.lang.Math import *
def func(min,max):
f=P1D("exp(sqrt(x))+log10(x*exp(x))")
step=(max-min)/100.0 # define step
for i in range(100):
x=min+step*i
y=exp(sqrt(x))+log10(x*exp(x)) # get value of function
if (x<3): y=1
f.add(x,y)
return f
c1 = HPlot() # plot
c1.visible(); c1.setAutoRange()
p=func(1,10) # plot between 1 and 100
p.setStyle('l')
p.setPenWidth(3)
c1.draw(p)
```

As you see, it takes more codding to create such function, but the function can be as complex as you want. In this example we used the class jhplot.P1D which keeps X-Y values and used 100 steps for evaluation. Note that you can call various special functions in this example if you will import appropriate Java class.

The result of this script is:

## Using Java coding

Since all classes are implemented in Java, one can use the standard Java syntax to draw functions:

No access to this part. Use Member area to request membership. If you are already a member, login to Member area and come back to this wiki. |

## Using Python definition

So far we have considered functions that can be contracted using Jython, as well as Java, and any other Java scripting language. The reason is that we used only Java classes. Next we will consider an approach that can be used using Python-only approach.

Let us illustrate this approach here. We will plot the function:

[math] if x\gt0 \quad y=a*x^{2}+b [/math]

[math] x=0 \quad y=0 [/math]

[math] if x\lt 0 \quad y=a*x*cos(x)*b [/math]

where a and b are constants. To build such function using a single string is not easy, but it is possible to construct such function using the standard Jython. Below we will construct such function and make a plot for a=20 and b=30:

Protected code example! Request membership or login to this member link first. |

The output is:

In a similar way one can build any function with several variables. Note that we create a class of this function "cmt", where v[0] denotes variable "x". For several variables, say x and y, one should use v[0] (for x) and v[1] for y. Analogously, one can use the same approach for x variable.

A second approach to show functions is discussed in the section Distribution Function where we fill function using a third-party class. You can find more example how to create mathematical functions in 3D programmically in later sections.

More information on this topic is in DMelt books |