DMelt:Math/3 Non Parametric Functions

From HandWiki
Limitted access. First login to DataMelt if you are a full DataMelt member. Then login to HandWiki as a user.

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 jhplot.F1D or jhplot.F2D 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 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:

Here we used functions from the Java class java.lang.Math 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 jhplot.F1D that can be plotted. The result of this script is shown below.

DMelt example: Defining and plotting a non-parametric function

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

with the image is

DMelt example: Defining a non-parametric function with 2 variables

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 java.lang.Math:

from jhplot import *
from java.lang.Math import *

def func(min,max):
   step=(max-min)/100.0 # define step 
   for i in range(100):
       y=exp(sqrt(x))+log10(x*exp(x))  # get value of function
       if (x<3): y=1
   return f

c1 = HPlot()   # plot
c1.visible(); c1.setAutoRange()
p=func(1,10)   # plot between 1 and 100

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 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:

DMelt example: Complex function defined programically

Using Java coding

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

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]\displaystyle{ if x\gt 0 \quad y=a*x^{2}+b }[/math]

[math]\displaystyle{ x=0 \quad y=0 }[/math]

[math]\displaystyle{ 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:

The output is:

DMelt example: Create 3D animated canvases with function + data

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.