private static void polynomials() {
// TODO Auto-generated method stub
double[] f1_coeff = { 3.0, 6.0, -2.0, 1.0 };
double[] f2_coeff = { 1.0, 2.0, -1.0, -2.0 };
PolynomialFunction f1 = new PolynomialFunction(f1_coeff);
PolynomialFunction f2 = new PolynomialFunction(f2_coeff);
// output directly
System.out.println("f1(x) is : " + f1);
System.out.println("f2(x) is : " + f2);
// polynomial degree
System.out.println("f1(x)'s degree is " + f1.degree());
// get the value when x = 2
System.out.println("f1(2) = " + f1.value(2));
// function add
System.out.println("f1(x)+f2(x) = " + f1.add(f2));
// function substract
System.out.println("f1(x)-f2(x) = " + f1.subtract(f2));
// function multiply
System.out.println("f1(x)*f2(x) = " + f1.multiply(f2));
// function derivative
System.out.println("f1'(x) = " + f1.derivative());
System.out.println("f2''(x) = "
+ ((PolynomialFunction) f2.derivative()).derivative());
}
}
5.随机生成和统计初步
package apache.commons.math.test;
import org.apache.commons.math3.random.RandomDataGenerator;
import org.apache.commons.math3.stat.Frequency;
import org.apache.commons.math3.stat.StatUtils;
/**
*
* @ClassName: RandomTest
* @Description: 随机生成和统计初步
* @author zengfh
* @date 2014年11月21日 下午2:23:04
*
*/
public class RandomTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
random();
}
private static void random() {
// TODO Auto-generated method stub
RandomDataGenerator randomData = new RandomDataGenerator();
// Generate a random int value uniformly distributed between lower and
// upper, inclusive
System.out.println("a uniform value: " + randomData.nextInt(1, 6));
// Returns a random value from an Exponential distribution with the
// given mean
System.out.println("a Exponential value: "
+ randomData.nextExponential(5));
// Generate a random value from a Normal
System.out.println("a Normal value: " + randomData.nextGaussian(0, 1));
// Generates a random value from the Poisson distribution with the given
// mean
System.out.println("a Poisson value: " + randomData.nextPoisson(3));
// Generates an integer array of length k whose entries are selected
// randomly, without repetition, from the integers 0 through n-1
int[] a = randomData.nextPermutation(10, 3);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
// generate 1000 numbers between 0 and 3 inclusive, then using frequency
// to see the distribution
Frequency freq = new Frequency();
int value = 0;
for (int i = 0; i < 1000; i++) {
value = randomData.nextInt(0, 3);
freq.addValue(value);
}
long[] observed = new long[4];
double[] perc = new double[4];
for (int i = 0; i < 4; i++) {
observed[i] = freq.getCount(i);
perc[i] = freq.getPct(i);
System.out.println("there are " + observed[i] + " " + i
+ " in dataset with " + (perc[i] * 100) + "%");
}