标签:generated 自己 ram new pre tps basic link com
这里实现了java编程求和、平均值、方差、标准差,求标准差的时候使用到了java提供的方法求算术平方根。
改天利用算法自己编写一个算术平方根,先贴上以上几种求法,方法的复用性和拓展性就不写了---这里只有求平均数两种情况的方法写了。
package com.math; /** * * @author cisco lee * @version 1.0 * Mathematical Analysis Encyclopedia * {@link https://baike.baidu.com/item/%E6%95%B0%E5%AD%A6%E5%88%86%E6%9E%90/3123?fr=aladdin} * * */ public class MathAnalyse { public static void main(String[] args) { AddThread addobject = new AddThread(); Avg avgobject = new Avg(); Variance varobject = new Variance(); StandardDeviation standardobject = new StandardDeviation(); double []a = new double[5]; System.out.println("all a[i]:"); for (int i = 0; i <a.length; i++) { a[i]=i+2;//2,3,4,5,6 System.out.print(a[i]+" "); } System.out.println(); double sum = addobject.add(a); double average = avgobject.avg(sum,5); double var = varobject.populationVariance(a, average); standardobject.standardDeviation(var); addobject.start(); avgobject.start(); varobject.start(); standardobject.start(); } } //this is a class of summation methods. class AddThread extends Thread { double thisresult=0; public double add(double []result){ for (int i = 0; i <result.length; i++) { thisresult=result[i]+thisresult; } return thisresult; } public void run() { // TODO Auto-generated method stub System.out.println("sum:"+thisresult); } } //This is a class of averaging methods. class Avg extends Thread{ double avg=0; public double avg(double sum,int num){ avg = sum/num; return avg; } public double avg(double[]sum){ for (int i = 0; i < sum.length; i++) { avg = sum[i]+avg; } return avg; } public void run() { // TODO Auto-generated method stub System.out.println("average:"+avg); } } //Calculation variance #求方差 class Variance extends Thread{ double variance=0; /** * This is a method of calculating variance to know of the population sample。 #这是一个计算知道总体样本的方法 * @param a This is a Sample。 #这是一个样本 * @param avg this is the average of the sample. #这是样本的平均值 * @return variance * Pay attention: a.length This is the number of sample. #这是样本成员个数 */ public double populationVariance(double[]a,double avg) { for (int j = 0; j < a.length; j++) { variance =((a[j]-avg)*(a[j]-avg))+variance; } variance = variance/a.length; return variance; } public void run() { // TODO Auto-generated method stub System.out.println("Variance:"+variance);; } } //Sdandard deviation class StandardDeviation extends Thread{ double stand; public double standardDeviation( double variance) { stand = Math.sqrt(variance); return stand; } public void run() { // TODO Auto-generated method stub System.out.println("StandardDeviation:"+stand); } }
拓展:
求算术平方根的算法:
求√A 的算法: 输入正数A 令X=A/2 (1) Y=(X+A/X)/2 如果|Y-X|<ε(你要求的精确度),则打印Y,就是√A ,结束 否则,令X=Y,执行语句(1) 用QBASIC语言写: INPUT A X=A/2
下面,是使用java编写的算术平方根:
【后面加上,休息...】
标签:generated 自己 ram new pre tps basic link com
原文地址:https://www.cnblogs.com/ciscolee/p/13034899.html