zoukankan      html  css  js  c++  java
  • 算法 《霍纳的方法java实践》

    【历史背景】

    霍纳的方法是中国南宋时期的数学家秦九韶表述求解一元高次多项式的值的算法——正负开方术。

    它也能够配合牛顿法用来求解一元高次多项式的根。在西方被称作霍纳算法(Horner algorithm或Horner scheme)。是以英国数学家威廉·乔治·霍纳命名的。


    【原理解释】

    设有n+1项的n次函数

    f(x)=anxn+ an-1xn-1+an-2xn-2+ an-3xn-3+…… a2x2+a1x+ a0

    将前n项提取公因子x,得

    f(x)=(anxn-1+ an-1xn-2+an-2xn-3+…… a2x+a1)+ a0

    再将括号内的前n-1项提取公因子x。得

    f(x)=((anxn-2+ an-1xn-3+an-2xn-4+…… a2)x+ a1)x+a0

    如此重复提取公因子x,最后将函数化为

    f(x)=(((anx+ an-1)x+ an-2)x+……+a1)x+a0

    f1= anx+ an-1

    f2=f1x+ an-2

    f3=f2x+ an-3

    。。。

    fn=fn-1x+ a0    fn即为所求


    【代码演示样例】

    AlgorithmCalculatorService.java

           /**
    	 * 获取系数集合
    	 * @param N 多项式个数
    	 * @return 系数集合
    	 */
    	public static ArrayList<Double> getCoefficientList(Integer N) {
    		ArrayList<Double> AnList = new ArrayList<Double>();
    		//随机生成N个0到10之间的系数
    		Random random = new Random();
    		while(N > 0) {
    			AnList.add((double) (Math.round(random.nextFloat()*100)/10.0)); //随机一个[0-10]之间的一位小数
    			N--;
    		}
    		return AnList;
    	}
    	
    	/**
    	 * 运行传统算法并获取结果
    	 * 模板为 An*X^n + An-1*X^n-1 + ... A1X + A0
    	 * @param AnList 系数集合
    	 * @param X X值
    	 * @return 计算结果
    	 */
    	public static String getResultByTrandition(ArrayList<Double> AnList, Float X) {
    		int N = AnList.size() - 1;
    		Double result = 0D;
    		long start = System.currentTimeMillis(); //获取当前时间。单位为毫秒
    		for(Double An : AnList) {//遍历系数集合,每一次循环计算出一项的值。并相加
    			Double Xn = 1D;
    			int i = 0;
    			while(i<N) {
    				Xn = Xn*X;
    				i++;
    			}
    			N--;
    			result = result + An*Xn;
    		}
    		long end = System.currentTimeMillis();
    		String resultMsg = "计算结果: " + result + " ,耗时为:" + (end - start) + "毫秒!

    "; return resultMsg; } /** * 运行秦九韶算法并获取结果 * 模板为((...(An*X + An-1)X + An-2)X + ... + A1)X + A0 * @param AnList 系数集合 * @return 计算结果 */ @SuppressWarnings("unchecked") public static String getResultByHorner(ArrayList<Double> anList, Float X) { ArrayList<Double> AnList = (ArrayList<Double>) anList.clone(); Double V = (double)AnList.get(0); AnList.remove(0); long start = System.currentTimeMillis(); //获取当前时间,单位为毫秒 for(Double An : AnList) { V = X*V + An; } long end = System.currentTimeMillis(); String resultMsg = "计算结果: " + V + " 。耗时为:" + (end - start) + "毫秒!"; return resultMsg; } public static void main(String[] args) { ArrayList<Double> anlist = getCoefficientList(5); String msg1 = getResultByTrandition(anlist, 3F); System.out.println("传统算法: " + msg1); String msg2 = getResultByHorner(anlist, 3F); System.out.println("九韶算法: " + msg2); }


    【执行结果】

    传统算法: 计算结果: 8.992778799971419E137 。耗时为:20毫秒。
    九韶算法: 计算结果: 8.992778799971422E137 。耗时为:0毫秒!


    【结果分析】

    当数据量较大时,在耗时方面秦九韶算法相比传统算法占领明显优势!




    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    android实现简单计算器
    象牙塔尖
    Jquery 限制文本框输入字数【转】
    jquery 文字向上滚动+CSS伪类before和after的应用
    鼠标移入 移出div div会消失的处理
    ionic 项目分享【转】
    JS+CSS简单实现DIV遮罩层显示隐藏【转藏】
    封装鼠标滚轮事件,兼容方法。。。拿去用吧
    3、bootstrap3.0 栅格偏移 布局中的一个特产
    html input[type=file] css样式美化【转藏】
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4635496.html
Copyright © 2011-2022 走看看