The P0D class is among the simplest classes of jHepWork: it does not have any graphical attributes. This class is similar to a Java ArrayList to keep double numbers in one dimension. To plot the data as a histogram, one should use either H1D methods or the method getH1D(No of bins) of the P0D class.
To construct a P0D and add some values, use:
>>> from jhplot import P0D >>> >>> p0=P0D("example") # build a P0D object >>> p0.add(1.0) # append some values >>> p0.add(-4.0) >>> p0.add(2.0) >>> p0.set(2,200) # set the value at ith position >>> >>> # fast methods to fill P0D: >>> array=p0.getArray() # get array with double numbers >>> array=p0.getArrayInt() # get array with integer numbers >>> p0.setArray(array[]) # fill with numbers from an array >>> p0.clear() # clear
All values are transformed to the double precision values automatically. It should be noted that for scripting with Jython, one should use the setArray() and getArray() methods which are optimized for speed and are much faster than when one uses add() or set() methods.
One can fill the current P0D from a file (see below), generate a sequence of numbers or fill with random numbers. For example, to fill a sequence between 0 and 100, use:
>>> from jhplot import P0D >>> p0=P0D("sequence") >>> p0.fill(101,0,100) # get 101 numbers from 0 - 100 with step 1
Below is an example which shows how to generate random numbers and transform them to a function. Then the output is shown as a histogram on the HPlot canvas:
>>> from java.awt import Color >>> from jhplot import HPlot,H1D,P0D >>> >>> # build a canvas >>> c1 = HPlot("Canvas",600,400) >>> c1.setGTitle("Example of P0D data array", Color.blue) #put title >>> c1.visible() >>> c1.setAutoRange() >>> p0= P0D("Normal distribution") >>> p0.randomNormal(1000, 0.0, 1.0) # random normal distribution >>> >>> # make a new copy and transform to a function >>> func1="x*cos(x)+2" >>> p01=p0.copy(func1) # copy with the title "x*cos(x)+2" >>> p01.func(func1) >>> >>> func1="exp(x)-2" make a new copy and transform again >>> p02=p0.copy(func1) >>> p02.func(func1) >>> >>> h1=p0.getH1D(20) # show as histogram with 20 bins >>> c1.draw(h1) >>> >>> h1=p01.getH1D(100) # show as histogram with 100 bins >>> c1.draw(h1) >>> >>> h1=p02.getH1D(200) # show again >>> c1.draw(h1)
One can obtain several useful characteristics of a P0D as:
>>> m=p0.size() # get size of P0D >>> m=p0.getMin() # get min value >>> m=p0.getMax() # get max value >>> m=p0.getMinIndex() # get index of min value >>> m=p0.getMaxIndex() # get index of max value >>> m=p0.mean() # mean >>> m=p0.variance() # variance >>> m=p0.stddeviation() # standard deviation >>> m=p0.correlation(P0D p00) # return correlation coefficient >>> m=p0.covariance(P0D p00) # return covariance >>> m=p0.getSum() # sum of all values
To build a P0D from an ASCII file, use this constructor:
>>> p0=P0D("file name")In the input file, each number should be on a separate line. One can use # or * for comments at the beginning of a line.
To write P0D to an ASCII file, use:
>>> p0.toFile("file name") # output to a fileData can be written to a binary file (big endian by default):
>>> p0.writeBinary("file name") # output to a binary fileTo read the data from an existing binary file, use
>>> p=p0.readBinary("file name") # p0 with numbers from a file(in this case all old content of p0 will be lost).
One can view the data stored in a P0D using a table:
>>> p0.toTable() # look at data in a tablewhich can be used to sort the data.
One can build a histogram from a P0D as:
>>> h=p0.getH1D(No of bins) # get a H1D with No bins >>> h=p0.getH1D(bins, min, max) # get a H1D with bins between min and max
Finally, one can use the toString() method to convert numbers into a string or use print() to print them in System.out.
One can add, subtract, multiply or divide two P0Ds using the following operations:
>>> p1=p0.oper(P0D p0d, "new title","+") # add >>> p1=p0.oper(P0D p0d, "new title","-") # subtract >>> p1=p0.oper(P0D p0d, "new title","*") # multiply >>> p1=p0.oper(P0D p0d, "new title","/") # divide
To sort or reverse the order, use
>>> p0.sort() # sort in the natural order >>> p0.reverse() # reverseHere is the method to search for a particular value:
>>> m=p0.search(value) # first occurrence of the given argument
Here are some other useful methods:
>>> p0=p0.merge(P0D p1) # merge with p1 >>> p0.range(min,max) # get range between min and max >>> p0.func("function") # transform all values to a function >>> # example: >>> p0.func("sqrt(x)") # transform to sqrt(x) all values.To define a function, the user should use the variable x and the following operands: +, -, *, /, pi, e, exp, ln, log, sin,cos, sqrt, rand, acos, asin, atan, tan and the scientific notation using "e", "E", "d", "D".
Note: in all cases, the objects remain to be the same, only the values will be modified. If you what to get a copy of the object, use copy() method. One can clear P0D array using clear() method.
One can get the detailed information on the P0D class using the Code Assist:
>>> p0. # press F4