Horner's Law

Jul 18, 2017 pm 05:28 PM
analyze Base

Generate random numbers

package cn.xf.algorithm.ch02;

import java.util.ArrayList;
import java.util.List;

/**
 * 生产随机数
 * @author xiaof
 *
 */
public class Random {

	/**
	 * 生产一个随机数的数列
	 * @param n  生成n个数列
	 * @param m  数据在0和m-1之间
	 * @param seed  随机初始种子
	 * @param a		参数
	 * @param b		参数
	 * @return
	 */
	public static List<Integer> randomNum(int n, int m, int seed, int a, int b)
	{
		List<Integer> numbers = new ArrayList<Integer>();
		int initData = (a * seed + b) % m;
		numbers.add(Math.abs(initData));	//初始化一个数据
		
		for(int i = 1; i < n; ++i)
		{
			int newData = (a * numbers.get(i - 1) + b) % m;
			numbers.add(Math.abs(newData));
		}
		
		return numbers;
	}
	
	/**
	 * 生产一个随机数的数列
	 * @param n 生成n个数列
	 * @param m  数据在0和m-1之间
     * @param seed  随机初始种子
     * @param a     参数
     * @param b     参数
     * @return
	 */
	public static List<Double> randomNumDouble(int n, int m, int seed, int a, int b) {
	    //创建结果数组
	    List<Double> numbers = new ArrayList<Double>();
	    int initData = (a * seed + b) % m; //取出一个初始值,在0到m之间
	    numbers.add((double) Math.abs(initData));   //加入第一个值
	    //后续数值以前一个数据作为基础种子进行变换
	    for(int i = 1; i < n; ++i) {
	        double newData = (a * numbers.get(i - 1) + b) % m;
	        numbers.add(Math.abs(newData));
	    }
	    
	    return numbers;
	}
	
	public static void main(String[] args) {
//		List<Integer> res = Random.randomNum(10, 10, 998, 58797676, 1);
		List<Double> res = Random.randomNumDouble(10, 10, 998, 58797676, 1);
		for(Double a : res)
		{
			System.out.print(a + "\t");
		}
 	}
}
Copy after login

 

Random value coefficient

Evaluation

package cn.xf.algorithm.ch06ChangeRule;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import cn.xf.algorithm.ch02.Random;

/**
 * 
 * 功能:霍纳法则
 * @author xiaofeng
 * @date 2017年7月13日
 * @fileName HornerRule.java
 *
 */
public class HornerRule {
	/**
	 * 用霍纳法则求一个多项式在一个给定点的值
	 * 输入:一个n次多项式的系数数组P【0...n】(从低到高存储),以及一个数字x
	 * 输出:多项式在x点的值
	 * @param p
	 * @param x
	 */
	public Double horner(List<Double> p, int x) {
		if(p == null || p.size() <=0) {
			return 0d;
		}
		//求结果集
		Double result = p.get(p.size() - 1);
		for(int i = p.size() - 2; i >= 0; --i) {
			//累计往后添加系数数据
			//一次从大到小吧X的系数乘以X,  然后加上下一个次数等级的系数,然后求和,作为新的下一个次数的系数乘数
			result = result * x + p.get(i);
		}
		
		return result;
	}
	
	/**
	 * 普通计算方式
	 * @param p
	 * @param x
	 * @return
	 */
	public Double notHorner(List<Double> p, int x) {
	    if(p == null || p.size() <=0) {
            return 0d;
        }
	    
	    //p是系数存储列表
	    Double result = 0d;  //0次幂的
	    for(int i = 0; i < p.size(); ++i) {
	        result += p.get(i) * doublePow(x, i);
	    }
	    
	    return result;
	}
	
	//求x的n次幂
	public static Double doublePow(double x, int n) {
	    if(x == 0) 
	        return 0d;
	    
	    if(n == 0)
	        return 1d;
	    Double result = 1d;
	    for(int i = 0; i < n; ++i) {
	        result *= x;
	    }
	    
	    return result;
	}
	
	@Test
	public void test1() {
	    //定义的一个数组是方程式的系数,第二个参数是未知数的值
	    //方程:y=5x^5 + 3x^4 + 2x^2 + 3
	    //当x为4的时候
	    HornerRule hr = new HornerRule();
	    List<Double> xishus = new ArrayList<Double>();
	    //这个数组的顺序要按照,0次幂到N次幂的顺序来
	    xishus.addAll(Arrays.asList(3d, 0d, 2d, 0d, 3d, 5d));
	    System.out.println(hr.horner(xishus, 4));
	    //一般方式计算
	    System.out.println(hr.notHorner(xishus, 4));
	    System.out.printf("JOB START OUTPUT: %tF %<tT%n", System.currentTimeMillis());
	}
	
	@Test
    public void compare() {
        // 当x为4的时候
        HornerRule hr = new HornerRule();
        // 建造100个随机数
        List<Double> xishus = Random.randomNumDouble(600, 3, 998, 58797676, 1);
        //求值
        System.out.printf("JOB HORNER START OUTPUT: %tF %<tT%n", System.currentTimeMillis());
        System.out.println(hr.notHorner(xishus, 3));
        System.out.printf("JOB HORNER END OUTPUT: %tF %<tT%n", System.currentTimeMillis());
        System.out.println("######################################################################################");
        System.out.printf("JOB NOTHORNER START OUTPUT: %tF %<tT%n", System.currentTimeMillis());
        System.out.println(hr.notHorner(xishus, 3));
        System.out.printf("JOB NOTHORNER END OUTPUT: %tF %<tT%n", System.currentTimeMillis());
        
    }
}
Copy after login

 

The above is the detailed content of Horner's Law. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement data statistics and analysis in uniapp How to implement data statistics and analysis in uniapp Oct 24, 2023 pm 12:37 PM

How to implement data statistics and analysis in uniapp 1. Background introduction Data statistics and analysis are a very important part of the mobile application development process. Through statistics and analysis of user behavior, developers can have an in-depth understanding of user preferences and usage habits. Thereby optimizing product design and user experience. This article will introduce how to implement data statistics and analysis functions in uniapp, and provide some specific code examples. 2. Choose appropriate data statistics and analysis tools. The first step to implement data statistics and analysis in uniapp is to choose the appropriate data statistics and analysis tools.

Analysis of the reasons why the secondary directory of DreamWeaver CMS cannot be opened Analysis of the reasons why the secondary directory of DreamWeaver CMS cannot be opened Mar 13, 2024 pm 06:24 PM

Title: Analysis of the reasons and solutions for why the secondary directory of DreamWeaver CMS cannot be opened. Dreamweaver CMS (DedeCMS) is a powerful open source content management system that is widely used in the construction of various websites. However, sometimes during the process of building a website, you may encounter a situation where the secondary directory cannot be opened, which brings trouble to the normal operation of the website. In this article, we will analyze the possible reasons why the secondary directory cannot be opened and provide specific code examples to solve this problem. 1. Possible cause analysis: Pseudo-static rule configuration problem: during use

Case analysis of Python application in intelligent transportation systems Case analysis of Python application in intelligent transportation systems Sep 08, 2023 am 08:13 AM

Summary of case analysis of Python application in intelligent transportation systems: With the rapid development of intelligent transportation systems, Python, as a multifunctional, easy-to-learn and use programming language, is widely used in the development and application of intelligent transportation systems. This article demonstrates the advantages and application potential of Python in the field of intelligent transportation by analyzing application cases of Python in intelligent transportation systems and giving relevant code examples. Introduction Intelligent transportation system refers to the use of modern communication, information, sensing and other technical means to communicate through

Analyze whether Tencent's main programming language is Go Analyze whether Tencent's main programming language is Go Mar 27, 2024 pm 04:21 PM

Title: Is Tencent’s main programming language Go: An in-depth analysis. As China’s leading technology company, Tencent has always attracted much attention in its choice of programming languages. In recent years, some people believe that Tencent mainly adopts Go as its main programming language. This article will conduct an in-depth analysis of whether Tencent's main programming language is Go, and give specific code examples to support this view. 1. Application of Go language in Tencent Go is an open source programming language developed by Google. Its efficiency, concurrency and simplicity are loved by many developers.

Analyze the advantages and disadvantages of static positioning technology Analyze the advantages and disadvantages of static positioning technology Jan 18, 2024 am 11:16 AM

Analysis of the advantages and limitations of static positioning technology With the development of modern technology, positioning technology has become an indispensable part of our lives. As one of them, static positioning technology has its unique advantages and limitations. This article will conduct an in-depth analysis of static positioning technology to better understand its current application status and future development trends. First, let’s take a look at the advantages of static positioning technology. Static positioning technology achieves the determination of position information by observing, measuring and calculating the object to be positioned. Compared with other positioning technologies,

Analyze and solve the reasons why Tomcat crashes Analyze and solve the reasons why Tomcat crashes Jan 13, 2024 am 10:36 AM

Tomcat crash cause analysis and solutions Introduction: With the rapid development of the Internet, more and more applications are developed and deployed on servers to provide services. As a common JavaWeb server, Tomcat has been widely used in application development. However, sometimes we may encounter problems with Tomcat crashing, which will cause the application to not run properly. This article will introduce the analysis of the causes of Tomcat crash, provide solutions, and give specific code examples.

ThinkPHP6 code performance analysis: locating performance bottlenecks ThinkPHP6 code performance analysis: locating performance bottlenecks Aug 27, 2023 pm 01:36 PM

ThinkPHP6 code performance analysis: locating performance bottlenecks Introduction: With the rapid development of the Internet, more efficient code performance analysis has become increasingly important for developers. This article will introduce how to use ThinkPHP6 to perform code performance analysis in order to locate and solve performance bottlenecks. At the same time, we will also use code examples to help readers understand better. Importance of Performance Analysis Code performance analysis is an integral part of the development process. By analyzing the performance of the code, we can understand where a lot of resources are consumed

How to use C++ for real-time image processing and analysis? How to use C++ for real-time image processing and analysis? Aug 26, 2023 am 10:39 AM

How to use C++ for real-time image processing and analysis? With the development of computer vision and image processing, more and more applications require the processing and analysis of real-time images. As an efficient and powerful programming language, C++ is widely used in the field of image processing. This article will introduce how to use C++ for real-time image processing and analysis, and provide some code examples. 1. Image reading and display Before image processing, the image data needs to be read from a file or camera first, and the processed image also needs to be displayed. first

See all articles