DMelt:Math/4 Special Functions

From HandWiki
Revision as of 17:00, 2 October 2021 by imported>MainEditor
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Member

Special functions

The simplest way to create complicated functions including special function is to call external Java libraries. You can use any Java library in the function definitions, mixing freely Java and Python code (or any other scripting language). Here is a small example that creates 1-erf(x) function from Apache math3.special Apache math3.special package. to define the "erf" function.

from  jhplot import *
from org.apache.commons.math3.special.Erf import *

class MyFunc(FNon):
      def value(self, x):
                return 1-erf(x[0])
c1 = HPlot()
c1.visible();   c1.setAutoRange()
pl = MyFunc("function",1,0)
f1=F1D(pl,-4,4)
c1.draw(f1)

The function "erf" is taken from Erf Erf Java class. Here "MyFunc" is a class that defines a new function (1-erf) and returns its values. x[0] defines "x" (0 means one-dimensional function). You can also define functions in multiple dimensions using x[0], x[1], x[2] etc. notations.


The output of this script is shown in this plot:

DMelt example: Special function using Apache Common Math


The above approach works only for Jython/Python codding. A more general approach is to call Java directly and extend the class jhplot.FNon jhplot.FNon that is a pure-Java approach, as in the above example. In this case you can create not only Python scripts, but also write code in pure Java, or mix Java, Groovy and JRuby.

Look at the section Non-parametric functions for more details.

First,let us try to call, say, the "Beta" function:

from org.apache.commons.math3.special import Beta
a=Beta.regularizedBeta(0.1, 1, 0.3) 
print a

The value returned by this function will be 0.0311138388027.

Now let us try to plot this function. We will use the same approach as in the previous section: we will build a class from this function and convert it to F1D.

from jhplot import *
from java.awt import Color

class beta(FNon):
   def value(self, v):
      from cern.jet.stat.Probability import beta
      return beta(self.p[0],self.p[1],v[0]) 

p=beta('β function',1,2) 
print p.dimension() 
print p.numberOfParameters()
print p.parameterNames()
print p.variableNames() 
print p.variableName(0)
p.setParameters([0.2,0.1])
print p.value([0.3])
f1=F1D(p,0.1,0.8)
f1.setPenWidth(3)
f1.setColor(Color.red)


c1 = HPlot('Special function')
c1.setGTitle('β function')
c1.visible()
c1.setAutoRange()
c1.draw(f1)

As you can see, you can mix Java, Jython and your custom code. A beta function created with the above code is shown here:

DMelt example: Special (beta) function shown as F1D

Here is another example: let us plot a Crystal Ball function as defined Crystal_Ball_function. We will use the Python language to create this function, and then we will plot it:

from java.awt import Color
from jhplot import *
from java.lang.Math import *
from org.apache.commons.math3.special.Erf import *

"""
Crystal Ball function as described in http://en.wikipedia.org/wiki/Crystal_Ball_function
"""

class MyFunc(FNon):
      def value(self, x):
                xmean=self.p[0]
                sigma= self.p[1]
                n = int(self.p[2])
                alpha=self.p[3]
                s=(x[0] - xmean) / sigma
                A=pow( (n / abs(alpha)), n) * exp(-0.5*abs(alpha)*abs(alpha))
                B= (n /  abs(alpha)) -  abs(alpha)
                C=(n/abs(alpha)) * (1.0/(n-1)) * exp(-0.5*abs(alpha)*abs(alpha))
                D=sqrt(3.14/2) * (1 +  erf(abs(alpha)/1.4))
                N=1.0/(sigma*(C+D))
                if s> -alpha:
                     return N*exp( -0.5*s*s );
                if s<= -alpha:
                     return N*A*pow((B-s), -n);

c1 = HPlot()
c1.visible();   c1.setAutoRange()
pl = MyFunc("Crystal Ball function",1,4)
pl.setParameters([0.0,1.0,3,1.0])
print pl.value([0.4])
f1=F1D(pl,-10,4)
c1.draw(f1)

with the output shown below:

Not allowed to read dmelt 20415788 : Not datamelt.org URL!

List of Java libraries for special functions

Here is a short list of special functions supported by DataMelt:


In addition, DataMelt supports distribution functions: