最小二乘法JAVA代码
在网上找到的代码是错的,自己写了一个
最小二乘法的代码,二个构造方法,一个是不带权的,一个是带权的
/**
* 最小二乘法计算类
*
* @author Administrator
*
*/
public class LeastSquareMethod {
private double[] x;
private double[] y;
private double[] weight;
private int m;
private double[] coefficient;
public LeastSquareMethod(double[] x, double[] y, int m) {
if (x == null || y == null || x.length < 2 || x.length != y.length
|| m < 2)
throw new IllegalArgumentException("无效的参数");
this.x = x;
this.y = y;
this.m = m;
weight = new double[x.length];
for (int i = 0; i < x.length; i++) {
weight[i] = 1;
}
}
public LeastSquareMethod(double[] x, double[] y, double[] weight, int m) {
if (x == null || y == null || weight == null || x.length < 2
|| x.length != y.length || x.length != weight.length || m < 2)
throw new IllegalArgumentException("无效的参数");
this.x = x;
this.y = y;
this.m = m;
this.weight = weight;
}
public double[] getCoefficient() {
if (coefficient == null)
compute();
return coefficient;
}
public double fit(double v) {
if (coefficient == null)
compute();
if (coefficient == null)
return 0;
double sum = 0;
for (int i = 0; i < coefficient.length; i++) {
sum += Math.pow(v, i) * coefficient[i];
}
return sum;
}
private void compute() {
if (x == null || y == null || x.length <= 1 || x.length != y.length
|| x.length < m || m < 2)
return;
double[] s = new double[(m - 1) * 2 + 1];
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < x.length; j++)
s[i] += Math.pow(x[j], i) * weight[j];
}
double[] f = new double[m];
for (int i = 0; i < f.length; i++) {
for (int j = 0; j < x.length; j++)
f[i] += Math.pow(x[j], i) * y[j] * weight[j];
}
double[][] a = new double[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
a[i
相关文档:
一、cookie机制和session机制的区别
*****************************************************************
具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案。
同时我们也看到,由于才服务器端保持状态的方案在客户端也需要保存一个标识,所以session
机制可能需要借 ......
ThreadLocal的核心思想很简单:为每个独立的线程提供一个变量的副本。
ThreadLocal则使用了“拷贝副本”的方式,人人有份,你用你的,我用我的,大家互不影响,是“以空间换时间”。每个线程修改变量时,实际上修改的是变量的副本,不怕影响到其它线程。
& ......
Java第三方library ecosystem是一个很广阔的范畴。不久前有人撰文:每个项目中,你必须知道的11个Java第三方类库。
单元测试
1.DBUnit
DBunit是一个基于junit扩展的数据库测试框架。它提供了大量的类对与数据库相关的操作进行了抽象和封装。
2.MOckito
Mockito是一个针对Java的mocking框架。你可以使用简洁的API编写 ......
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。他们的关系为:
并发编程的一种编程方式是把任务拆分为一些列的小任务,即Runnable,然后在提交给一个Executor执行,Executor.execute(Runnal ......
Java对象类型转换时Java开发中经常遇到的,本文向您介绍Java对象类型转换的一些技巧和注意事项,包括向上转型与向下转型都操作和主要点。
一、向上转型与向下转型。
对象类型的转换在Java语言平台中经常遇到,主要包括向上转型与向下转型操作。程序开发人员需要熟练 ......