import java.util.ArrayList;
import java.util.List;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.integration.SimpsonIntegrator;
import org.apache.commons.math3.analysis.integration.UnivariateIntegrator;
interface TestCase
{
public Object run(List<Object> params) throws Exception;
public List<Object> getParams();
public void printResult(Object result) throws Exception;
}
public class TimeCostCalculator
{
public TimeCostCalculator()
{
}
/**
* 计算指定对象的运行时间开销。
*
* @param testCase 指定被测对象。
* @return 返回sub.run的时间开销,单位为s。
* @throws Exception
*/
private double calcTimeCost(TestCase testCase) throws Exception
{
List<Object> params = testCase.getParams();
long startTime = System.nanoTime();
Object result = testCase.run(params);
long stopTime = System.nanoTime();
testCase.printResult(result);
double timeCost = (stopTime - startTime) * 1.0e-9;
return timeCost;
}
public void runTest(TestCase testCase) throws Exception
{
double timeCost = calcTimeCost(testCase);
System.out.println("时间开销:: " + timeCost + "s");
System.out.println("-------------------------------------------------------------------------------");
}
public static void main(String[] args) throws Exception
{
TimeCostCalculator tcc = new TimeCostCalculator();
tcc.runTest(new CalcSimpsonIntegrator());
}
}
/**
* 使用辛普森法求解数值积分。Apache.Common.Math3中所用的辛普森法是采用逼近法,即先对整个积分区间用矩形积分,然后将区间分解为4份,再次积分,比较两次积分的差值,若想对误差大于某个预订数值,
* 则认为还需要继续细分区间,因此会将区间以2倍再次细分后求积分,并将结果与前一次积分的结果比较,直至差值小于指定的误差,就停止。
* @author kingfox
*
*/
class CalcSimpsonIntegrator implements TestCase
{
public CalcSimpsonIntegrator()
{
System.out.print("本算例用于测试使用辛普森法计算积分。正在初始化计算数据 ... ...");
inputData = new double[arrayLength];
for (int index = 0; index < inputData.length; index++) // 鏂滃潯鍑芥暟
{
inputData[index] = Math.sin(2 * Math.PI * index * MyFunction.factor * 4);
}
func = new MyFunction();
integrator = new SimpsonIntegrator();
System.out.println("初始化完成!");
}
@Override
public Object run(List<Object> params) throws Exception
{
double result = ((SimpsonIntegrator)(params.get(1))).integrate(steps, (UnivariateFunction)(params.get(0)), lower, upper);
return result;
}
/**
* 获取运行参数
* @return List对象,第一个元素是求积函数,第二个参数是积分器。
*/
@Override
public List<Object> getParams()
{
List<Object> params = new ArrayList<Object>();
params.add(func);
params.add(integrator);
return params;
}
@Override
public void printResult(Object result) throws Exception
{
System.out.println(">>> integration value: " + result);
}
UnivariateFunction func = null;
UnivariateIntegrator integrator = null;
class MyFunction implements UnivariateFunction
{
@Override
public double value(double x)
{
// double y = x * factor; // 1.
// double y = 4.0 * x * x * x - 3.0 * x * x + 2.0 * x - 1.0; // 2.
// double y = -1.0 * Math.sin(x) + 2.0 * Math.cos(x) - 3.0; // 3.
double y = inputData[(int)(x / factor)];
// 4.
// System.out.println(x + ", " + y);
return y;
}
private static final double factor = 0.0001;
}
private double[] inputData = null;
private static final int arrayLength = 5000;
private static final double lower = 0.0;
// private static final double upper = 2.0 * Math.PI; // 3.
private static final double upper = (arrayLength - 1) * MyFunction.factor;
// 1. 2. 4.
private static final int steps = 1000000;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有