**Table of Content**

**Search clouds**

**Licenses**

**Author's resources**

You are a guest. Restricted access. Read more.

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.

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:

Download for this example is disabled for non-members

1: # Function. Plotting non-parametric function 2: from java.lang import Math 3: from jhplot import * 4: 5: class MyFunc(FNon): 6: def value(self, x): 7: y=x[0]*x[0]*Math.sqrt(x[0]) 8: if (x[0]<3): y=0 9: return y 10: 11: c1 = HPlot() # plot data and the fit function 12: c1.visible(); c1.setAutoRange() 13: pl = MyFunc("Title",1,0) # assume 1 variable, 0 parameters 14: f1=F1D(pl,0,10) # convert to plottable function (0-10) 15: c1.draw(f1)

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:

Download for this example is disabled for non-members

1: # Function. Plotting non-parametric function 2: from java.lang import Math 3: from jhplot import * 4: 5: class MyFunc(FNon): 6: def value(self, x): 7: y=x[0]*x[0]*Math.sqrt(x[0])*self.p[0]+self.p[1] 8: if (x[0]<4): y=0 9: return y 10: 11: c1 = HPlot() # plot data and the fit function 12: c1.visible() 13: c1.setAutoRange() 14: pl = MyFunc("test",1,2) 15: print "f(2)= ",pl.value([2]) 16: print pl.numberOfParameters() 17: print pl.dimension() 18: print pl.parameterNames().tolist() 19: pl.setParameter("par0",100) 20: pl.setParameter("par1",20) 21: print pl.parameters() 22: f1=F1D(pl,0,10) # convert to plottable function 23: c1.draw(f1)

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

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`

:

Download for this example is disabled for non-members

1: # Functions. Show complex function 2: from jhplot import * 3: from java.lang.Math import * 4: 5: def func(min,max): 6: f=P1D("exp(sqrt(x))+log10(x*exp(x))") 7: step=(max-min)/100.0 # define step 8: for i in range(100): 9: x=min+step*i 10: y=exp(sqrt(x))+log10(x*exp(x)) # get value of function 11: if (x<3): y=1 12: f.add(x,y) 13: return f 14: 15: c1 = HPlot() # plot 16: c1.visible(); c1.setAutoRange() 17: p=func(1,10) # plot between 1 and 100 18: p.setStyle('l') 19: p.setPenWidth(3) 20: 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:

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

You are not full member and have a limited access to this section.
One can unlock this part after becoming a full
member.

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:

<m 12> if x>0 right y=a*x^{2}+b </m>

<m 12> x=0 right y=0 </m>

<m> if x< 0 right y=a*x*cos(x)*b </m>

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:

Download for this example is disabled for non-members

1: from shplot import * 2: from jhplot import * 3: import math 4: 5: class cmt(ifunc): 6: def value(self, v): 7: d=0 8: if (v[0]>0): d=self.p[0]*v[0]*v[0]+self.p[1] 9: if (v[0]<0): d=self.p[0]*v[0]*math.cos(v[0])*self.p[1] 10: return d 11: 12: p=cmt('test',1,2) # create function object with one variable and 2 parameters 13: print p.dimension() # print properties of this function 14: print p.numberOfParameters() 15: print p.parameterNames() 16: print p.variableNames() 17: print p.variableName(0) 18: p.setParameters([20,30]) # set parameters a=20, b=30 19: print p.value([30]) # print value of this function at x=30 20: f1=F1D(p,-100,100) # convert it to the F1D function 21: 22: c1 = HPlot('Special') 23: c1.setGTitle('Special') 24: c1.visible() 25: c1.setAutoRange() 26: c1.draw(f1)

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 functions where we fill function using a third-party class. You can find more example how to create mathematical functions in 3D programmically in Section 3D functions

Unregistered users have a limited access to this section.
You can unlock advanced pages after becoming a full member.

If you are a registered SCaVis user, please go to this link Working with IDE for advanced users

click here if you want to know more

click here if you want to know more

A complete description of how to use Java, Jython and SCaVis for scientific analysis is described in the book Scientific data analysis using Jython and Java published by Springer Verlag, London, 2010 (by S.V.Chekanov)

— *Sergei Chekanov 2010/03/07 16:37*