You are a guest. Restricted access. Read more

ND functions

So far we have learned how to build functions in 1D (F1D class), 2D (F2D class) and 3D (F3D class). In addition to these “fixed dimension” classes, there is a FND class which can be used to build a function with arbitrary parameters and variables. To build such function is very easy and similar to the above styles, only now one should be more explicit about the names of variables. As example, below we make a function with 4 variables, called xx, yy,bb,kk:

from jhplot import FND
print ans

Such functions can be evaluated at any variable values and plotted.


You have a limited access to this part. Unlock this text after becoming a full member.


You have a limited access to this part. Unlock this text after becoming a full member.

Expression Builder

Multidimensional functions can also be built using the package jhplot.math.exp4j.

In the example below we build a function of 4 variables x,y,z,u and evaluate it:

from de.congrace.exp4j import *
calc = ExpressionBuilder("f(x,y,z,u)=x * y *cos()- 2+z-u^2").build()
print calc.calculate()

Same can be done in a somewhat different way:

from de.congrace.exp4j import *
calc = ExpressionBuilder("x * y *cos(x)- 2+z-u^2")
print calc.claculate()

You can evaluate function for arrays or even make X-Y array to draw the function. For example, let us evaluate this function for an array:

print calc.eval("x",[1,2,3,4,5,6,7,9])

The output will be shown as :

array('d', [-384.1705803038421, -364.6281029269727, -392.532799516408, -461.54419962463425, -496.89242746631385, -434.5298597838711, -309.02187617936954, -326.81867265648384])

Next, let us draw this function as symbols with some step.

from jhplot  import *
from de.congrace.exp4j import *
c1 = HPlot("Canvas",600,400)
calc = ExpressionBuilder("x * y*sin(x) - 2+z-u^2")

This will draw the function as symbols:

You can build an arbitrary function using Python and plot it, mixing Java and Jython libraries in one code. You can put an arbitrary complex expressions and “if” statements. In the example below we make a custom function using a Jython class and build an “ExpressionBuilder” object. then we evaluate this function for array of “X” values and plot it.

You have a limited access to this part. Unlock this text after becoming a full member.

Functions using arbitrary definition

So far we have considered functions contracted using a string. One can define an arbitrary function using Jython syntax and call any Python or Java library inside such definition. We will illustrate this approach in this example: Let us consider a function which looks as:

if x>0  right y=a*x^{2}+b

x=0  right  y=0

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

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 snippet
 1: from shplot import *
 2: from jhplot import *
 3: import math
 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
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
22: c1 = HPlot('Special')
23: c1.setGTitle('Special')
24: c1.visible()
25: c1.setAutoRange()
26: c1.draw(f1)

Click here to see the output of this script

Click here to see the output of this script

Click here to see the output of this script

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.

You can find more example how to create mathematical functions in 3D programmically in Section 3D functions

scavis/functions/fnd.txt · Last modified: 2013/04/14 16:52 (external edit)
Back to top
CC Attribution-Share Alike 3.0 Unported = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0