不一定对:public class Toeplz { public float[] toeplz(float r[], float y[], int n) //Solves the Toeplitz system PN //j=1 R(N+i.j)xj = yi (i = 1; : : :;N). The Toeplitz matrix need //not be symmetric. y[1..n] and r[1..2*n-1] are input arrays; x[1..n] is the output array. { int j,k,m,m1,m2; float pp,pt1,pt2,qq,qt1,qt2,sd,sgd,sgn,shn,sxn; float[] x,g,h; if (r[n] == 0.0) { System.out.println("toeplz-1 singular principal minor"); return null; }
x=new float[n]; g=new float[n]; h=new float[n]; x[0]=y[0]/r[n-1]; //Initialize for the recursion. if (n == 1) return x; g[0]=r[n-2]/r[n-1]; h[0]=r[n]/r[n-1];
boolean flag = false; for (m=0;m<n;m++) { //Main loop over the recursion. m1=m+1; sxn = -y[m1]; //Compute numerator and denominator for x, sd = -r[n-1]; for (j=0;j<=m;j++) { sxn += r[n-1+m1-j]*x[j]; sd += r[n-1+m1-j]*g[m-j+1]; } if (sd == 0.0) { System.out.println("toeplz-2 singular principal minor"); break; } x[m1]=sxn/sd; //whence x.
for (j=0;j<=m;j++) x[j] -= x[m1]*g[m-j+1];
if (m1 == n-1) { flag = true; break; }
sgn = -r[n-1-m1]; //Compute numerator and denominator for G and H, shn = -r[n-1+m1]; sgd = -r[n-1]; for (j=0;j<=m;j++) { sgn += r[n-1+j-m1]*g[j]; shn += r[n-1+m1-j]*h[j]; sgd += r[n-1+j-m1]*h[m-j+1]; } if (sd == 0.0 || sgd == 0.0) { System.out.println("toeplz-3 singular principal minor"); break; }
g[m1]=sgn/sgd; //whence G and H. h[m1]=shn/sd; k=m; m2=(m+1) >> 1; pp=g[m1]; qq=h[m1]; for (j=0;j<=m2;j++) { pt1=g[j]; pt2=g[k]; qt1=h[j]; qt2=h[k]; g[j]=pt1-pp*qt2; g[k]=pt2-pp*qt1; h[j]=qt1-qq*pt2; h[k--]=qt2-qq*pt1; } } //Back for another recurrence. if(flag) return x; else { System.out.println("toeplz - should not arrive here!"); return null; } } }
肯定对 public class Toeplz { public float[] toeplz(float r[], float y[], int n) //Solves the Toeplitz system PN //j=1 R(N+i.j)xj = yi (i = 1; : : :;N). The Toeplitz matrix need //not be symmetric. y[1..n] and r[1..2*n-1] are input arrays; x[1..n] is the output array. { int j,k,m,m1,m2; float pp,pt1,pt2,qq,qt1,qt2,sd,sgd,sgn,shn,sxn; float[] x,g,h; if (r[n] == 0.0) { System.out.println("toeplz-1 singular principal minor"); return null; }
x=new float[n]; g=new float[n]; h=new float[n]; x[0]=y[0]/r[n-1]; //Initialize for the recursion. if (n == 1) return x; g[0]=r[n-2]/r[n-1]; h[0]=r[n]/r[n-1];
boolean flag = false; for (m=0;m<n;m++) { //Main loop over the recursion. m1=m+1; sxn = -y[m1]; //Compute numerator and denominator for x, sd = -r[n-1]; for (j=0;j<=m;j++) { sxn += r[n-1+m1-j]*x[j]; sd += r[n-1+m1-j]*g[m-j+1]; } if (sd == 0.0) { System.out.println("toeplz-2 singular principal minor"); break; } x[m1]=sxn/sd; //whence x.
for (j=0;j<=m;j++) x[j] -= x[m1]*g[m-j+1];
if (m1 == n-1) { flag = true; break; }
sgn = -r[n-1-m1]; //Compute numerator and denominator for G and H, shn = -r[n-1+m1]; sgd = -r[n-1]; for (j=0;j<=m;j++) { sgn += r[n-1+j-m1]*g[j]; shn += r[n-1+m1-j]*h[j]; sgd += r[n-1+j-m1]*h[m-j+1]; } if (sd == 0.0 || sgd == 0.0) { System.out.println("toeplz-3 singular principal minor"); break; }
g[m1]=sgn/sgd; //whence G and H. h[m1]=shn/sd; k=m; m2=(m+1) >> 1; pp=g[m1]; qq=h[m1]; for (j=0;j<=m2;j++) { pt1=g[j]; pt2=g[k]; qt1=h[j]; qt2=h[k]; g[j]=pt1-pp*qt2; g[k]=pt2-pp*qt1; h[j]=qt1-qq*pt2; h[k--]=qt2-qq*pt1; } } //Back for another recurrence. if(flag) return x; else { System.out.println("toeplz - should not arrive here!"); return null; } } }
这是我的测试程序: public class Toeplz { public static void main(String[] args) { double[] r = new double[] { 1,2,3,4,5,6,7,8 }; double[] y = new double[] { 7,5,12,2,24,14,13,5 }; double[] x = Toeplz.toeplz(r,y,r.length-1); for ( int i=0; i<x.length;i++ ) { System.out.println("a["+i+"]="+x[i]); } } public static double[] toeplz(double r[], double y[], int n) //Solves the Toeplitz system PN //j=1 R(N+i.j)xj = yi (i = 1; : : :;N). The Toeplitz matrix need //not be symmetric. y[1..n] and r[1..2*n-1] are input arrays; x[1..n] is the output array. { int j,k,m,m1,m2; double pp,pt1,pt2,qq,qt1,qt2,sd,sgd,sgn,shn,sxn; double[] x,g,h; if (r[n] == 0.0) { System.out.println("toeplz-1 singular principal minor"); return null; } x=new double[n]; g=new double[n]; h=new double[n]; x[0]=y[0]/r[n-1]; //Initialize for the recursion. if (n == 1) return x; g[0]=r[n-2]/r[n-1]; h[0]=r[n]/r[n-1]; boolean flag = false; for (m=0;m<n;m++) { //Main loop over the recursion. m1=m+1; sxn = -y[m1]; //Compute numerator and denominator for x, sd = -r[n-1]; for (j=0;j<=m;j++) { sxn += r[n-1+m1-j]*x[j]; sd += r[n-1+m1-j]*g[m-j+1]; } if (sd == 0.0) { System.out.println("toeplz-2 singular principal minor"); break; } x[m1]=sxn/sd; //whence x. for (j=0;j<=m;j++) x[j] -= x[m1]*g[m-j+1]; if (m1 == n-1) { flag = true; break; } sgn = -r[n-1-m1]; //Compute numerator and denominator for G and H, shn = -r[n-1+m1]; sgd = -r[n-1]; for (j=0;j<=m;j++) { sgn += r[n-1+j-m1]*g[j]; shn += r[n-1+m1-j]*h[j]; sgd += r[n-1+j-m1]*h[m-j+1]; } if (sd == 0.0 || sgd == 0.0) { System.out.println("toeplz-3 singular principal minor"); break; } g[m1]=sgn/sgd; //whence G and H. h[m1]=shn/sd; k=m; m2=(m+1) >> 1; pp=g[m1]; qq=h[m1]; for (j=0;j<=m2;j++) { pt1=g[j]; pt2=g[k]; qt1=h[j]; qt2=h[k]; g[j]=pt1-pp*qt2; g[k]=pt2-pp*qt1; h[j]=qt1-qq*pt2; h[k--]=qt2-qq*pt1; } } //Back for another recurrence. if(flag) return x; else { System.out.println("toeplz - should not arrive here!"); return null; } } }出现以下错误: java.lang.ArrayIndexOutOfBoundsException at Toeplz.toeplz(Toeplz.java:48) at Toeplz.main(Toeplz.java:13) Exception in thread "main"
什么作业?! 我在编程的时候还没有Java呢!
{
public float[] toeplz(float r[], float y[], int n)
//Solves the Toeplitz system PN
//j=1 R(N+i.j)xj = yi (i = 1; : : :;N). The Toeplitz matrix need
//not be symmetric. y[1..n] and r[1..2*n-1] are input arrays; x[1..n] is the output array.
{
int j,k,m,m1,m2;
float pp,pt1,pt2,qq,qt1,qt2,sd,sgd,sgn,shn,sxn;
float[] x,g,h;
if (r[n] == 0.0)
{
System.out.println("toeplz-1 singular principal minor");
return null;
}
x=new float[n];
g=new float[n];
h=new float[n];
x[0]=y[0]/r[n-1]; //Initialize for the recursion.
if (n == 1) return x;
g[0]=r[n-2]/r[n-1];
h[0]=r[n]/r[n-1];
boolean flag = false;
for (m=0;m<n;m++) { //Main loop over the recursion.
m1=m+1;
sxn = -y[m1]; //Compute numerator and denominator for x,
sd = -r[n-1];
for (j=0;j<=m;j++) {
sxn += r[n-1+m1-j]*x[j];
sd += r[n-1+m1-j]*g[m-j+1];
}
if (sd == 0.0)
{
System.out.println("toeplz-2 singular principal minor");
break;
}
x[m1]=sxn/sd; //whence x.
for (j=0;j<=m;j++) x[j] -= x[m1]*g[m-j+1];
if (m1 == n-1)
{
flag = true;
break;
}
sgn = -r[n-1-m1]; //Compute numerator and denominator for G and H,
shn = -r[n-1+m1];
sgd = -r[n-1];
for (j=0;j<=m;j++) {
sgn += r[n-1+j-m1]*g[j];
shn += r[n-1+m1-j]*h[j];
sgd += r[n-1+j-m1]*h[m-j+1];
}
if (sd == 0.0 || sgd == 0.0)
{
System.out.println("toeplz-3 singular principal minor");
break;
}
g[m1]=sgn/sgd; //whence G and H.
h[m1]=shn/sd;
k=m;
m2=(m+1) >> 1;
pp=g[m1];
qq=h[m1];
for (j=0;j<=m2;j++) {
pt1=g[j];
pt2=g[k];
qt1=h[j];
qt2=h[k];
g[j]=pt1-pp*qt2;
g[k]=pt2-pp*qt1;
h[j]=qt1-qq*pt2;
h[k--]=qt2-qq*pt1;
}
} //Back for another recurrence.
if(flag) return x;
else
{
System.out.println("toeplz - should not arrive here!");
return null;
}
}
}
public class Toeplz
{
public float[] toeplz(float r[], float y[], int n)
//Solves the Toeplitz system PN
//j=1 R(N+i.j)xj = yi (i = 1; : : :;N). The Toeplitz matrix need
//not be symmetric. y[1..n] and r[1..2*n-1] are input arrays; x[1..n] is the output array.
{
int j,k,m,m1,m2;
float pp,pt1,pt2,qq,qt1,qt2,sd,sgd,sgn,shn,sxn;
float[] x,g,h;
if (r[n] == 0.0)
{
System.out.println("toeplz-1 singular principal minor");
return null;
}
x=new float[n];
g=new float[n];
h=new float[n];
x[0]=y[0]/r[n-1]; //Initialize for the recursion.
if (n == 1) return x;
g[0]=r[n-2]/r[n-1];
h[0]=r[n]/r[n-1];
boolean flag = false;
for (m=0;m<n;m++) { //Main loop over the recursion.
m1=m+1;
sxn = -y[m1]; //Compute numerator and denominator for x,
sd = -r[n-1];
for (j=0;j<=m;j++) {
sxn += r[n-1+m1-j]*x[j];
sd += r[n-1+m1-j]*g[m-j+1];
}
if (sd == 0.0)
{
System.out.println("toeplz-2 singular principal minor");
break;
}
x[m1]=sxn/sd; //whence x.
for (j=0;j<=m;j++) x[j] -= x[m1]*g[m-j+1];
if (m1 == n-1)
{
flag = true;
break;
}
sgn = -r[n-1-m1]; //Compute numerator and denominator for G and H,
shn = -r[n-1+m1];
sgd = -r[n-1];
for (j=0;j<=m;j++) {
sgn += r[n-1+j-m1]*g[j];
shn += r[n-1+m1-j]*h[j];
sgd += r[n-1+j-m1]*h[m-j+1];
}
if (sd == 0.0 || sgd == 0.0)
{
System.out.println("toeplz-3 singular principal minor");
break;
}
g[m1]=sgn/sgd; //whence G and H.
h[m1]=shn/sd;
k=m;
m2=(m+1) >> 1;
pp=g[m1];
qq=h[m1];
for (j=0;j<=m2;j++) {
pt1=g[j];
pt2=g[k];
qt1=h[j];
qt2=h[k];
g[j]=pt1-pp*qt2;
g[k]=pt2-pp*qt1;
h[j]=qt1-qq*pt2;
h[k--]=qt2-qq*pt1;
}
} //Back for another recurrence.
if(flag) return x;
else
{
System.out.println("toeplz - should not arrive here!");
return null;
}
}
}
public class Toeplz
{
public static void main(String[] args)
{
double[] r = new double[]
{ 1,2,3,4,5,6,7,8 };
double[] y = new double[]
{
7,5,12,2,24,14,13,5
};
double[] x = Toeplz.toeplz(r,y,r.length-1);
for ( int i=0; i<x.length;i++ )
{
System.out.println("a["+i+"]="+x[i]);
} }
public static double[] toeplz(double r[], double y[], int n)
//Solves the Toeplitz system PN
//j=1 R(N+i.j)xj = yi (i = 1; : : :;N). The Toeplitz matrix need
//not be symmetric. y[1..n] and r[1..2*n-1] are input arrays; x[1..n] is the output array.
{
int j,k,m,m1,m2;
double pp,pt1,pt2,qq,qt1,qt2,sd,sgd,sgn,shn,sxn;
double[] x,g,h;
if (r[n] == 0.0)
{
System.out.println("toeplz-1 singular principal minor");
return null;
} x=new double[n];
g=new double[n];
h=new double[n];
x[0]=y[0]/r[n-1]; //Initialize for the recursion.
if (n == 1) return x;
g[0]=r[n-2]/r[n-1];
h[0]=r[n]/r[n-1]; boolean flag = false;
for (m=0;m<n;m++) { //Main loop over the recursion.
m1=m+1;
sxn = -y[m1]; //Compute numerator and denominator for x,
sd = -r[n-1];
for (j=0;j<=m;j++) {
sxn += r[n-1+m1-j]*x[j];
sd += r[n-1+m1-j]*g[m-j+1];
}
if (sd == 0.0)
{
System.out.println("toeplz-2 singular principal minor");
break;
}
x[m1]=sxn/sd; //whence x. for (j=0;j<=m;j++) x[j] -= x[m1]*g[m-j+1]; if (m1 == n-1)
{
flag = true;
break;
} sgn = -r[n-1-m1]; //Compute numerator and denominator for G and H,
shn = -r[n-1+m1];
sgd = -r[n-1];
for (j=0;j<=m;j++) {
sgn += r[n-1+j-m1]*g[j];
shn += r[n-1+m1-j]*h[j];
sgd += r[n-1+j-m1]*h[m-j+1];
}
if (sd == 0.0 || sgd == 0.0)
{
System.out.println("toeplz-3 singular principal minor");
break;
} g[m1]=sgn/sgd; //whence G and H.
h[m1]=shn/sd;
k=m;
m2=(m+1) >> 1;
pp=g[m1];
qq=h[m1];
for (j=0;j<=m2;j++) {
pt1=g[j];
pt2=g[k];
qt1=h[j];
qt2=h[k];
g[j]=pt1-pp*qt2;
g[k]=pt2-pp*qt1;
h[j]=qt1-qq*pt2;
h[k--]=qt2-qq*pt1;
}
} //Back for another recurrence.
if(flag) return x;
else
{
System.out.println("toeplz - should not arrive here!");
return null;
}
}
}出现以下错误:
java.lang.ArrayIndexOutOfBoundsException
at Toeplz.toeplz(Toeplz.java:48)
at Toeplz.main(Toeplz.java:13)
Exception in thread "main"
x=new double[n];
g=new double[n];
h=new double[n];
的大小是从1-n,而不是从0-(n-1),
sxn += r[n+m1-j]*x[j];
n应该小于r.length/2