📄 getapplets.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class GetApplets extends Applet implements ActionListener
{
DecimalFormat form =new DecimalFormat ("0.00000");
double[][] yanshi_g1 = new double[5][3];
double[][] yanshi_g2 = new double[5][3];
double[] daipan = new double[3];//样本数据
double[] yanshi_g1_x1 = new double[3];
double[] yanshi_g2_x2 = new double[3];
double[][] yanshi_x1_x = new double[3][5];
double[][] yanshi_tr_x1 = new double[5][3];
double[][] yanshi_x2_x = new double[3][5];
double[][] yanshi_tr_x2 = new double[5][3];
double[][] yanshi_g1_s1 = new double[3][3];
double[][] yanshi_g2_s2 = new double[3][3];
double[][] yanshi_g1_s = new double[3][3]; //样本协差阵
double[][] yanshi_a = new double[3][1];
private TextArea textArea; //声明一个TextArea
public void init()
{
initForm();
yanshi_g1[0][0]=76; yanshi_g1[0][1]=99; yanshi_g1[0][2]=5374;
yanshi_g1[1][0]=79.5; yanshi_g1[1][1]=99; yanshi_g1[1][2]=5359;
yanshi_g1[2][0]=78; yanshi_g1[2][1]=99; yanshi_g1[2][2]=5372;
yanshi_g1[3][0]=72.1; yanshi_g1[3][1]=95.9; yanshi_g1[3][2]=5242;
yanshi_g1[4][0]=73.8; yanshi_g1[4][1]=77.7; yanshi_g1[4][2]=5370;
yanshi_g2[0][0]=71.2; yanshi_g2[0][1]=93; yanshi_g2[0][2]=4250;
yanshi_g2[1][0]=75.3; yanshi_g2[1][1]=94.9; yanshi_g2[1][2]=3412;
yanshi_g2[2][0]=70; yanshi_g2[2][1]=91.2; yanshi_g2[2][2]=3390;
yanshi_g2[3][0]=72.8; yanshi_g2[3][1]=99; yanshi_g2[3][2]=2300;
yanshi_g2[4][0]=62.9; yanshi_g2[4][1]=80.6; yanshi_g2[4][2]=3799;
daipan[0] = 68.5; daipan[1]=79.3; daipan[2]=1950;
Button b=new Button("演示");
b.addActionListener(this);
setLayout(new BorderLayout());
add("East",b);
textArea=new TextArea(5,40);
textArea.setEditable(false);
add("Center",textArea);
}
void initForm()
{
setSize(500,500);}
/*Button b点击后的事件处理函数*/
public void actionPerformed(ActionEvent event)
{ this.textArea.append("均值向量1:\n");
average(yanshi_g1,5,3);
this.textArea.append("\n均值向量2:\n");
bverage(yanshi_g2,5,3);
this.textArea.append("\n样本协方差阵:\n");
xfch(yanshi_g1,5,3,yanshi_g2,5,3);
this.textArea.append("\n判别函数:\n");
panbie(yanshi_g1_s,3,3,yanshi_g1_x1,yanshi_g2_x2);
}
/*求均值向量*/
public double [] average(double [][] a,int nRows,int nCols) {
for (int c = 0; c < nCols; ++c) { for(int r=0;r<nRows;++r)
{ double v = a[r][c]; yanshi_g1_x1[c] += v/nRows;
} this.textArea.append(form.format(yanshi_g1_x1[c])+"\t");
}
return yanshi_g1_x1; }
public double [] bverage(double [][] b,int nRows,int nCols) {
for (int c = 0; c < nCols; ++c) { for(int r=0;r<nRows;++r)
{ double v = b[r][c]; yanshi_g2_x2[c] += v/nRows;
} this.textArea.append(form.format(yanshi_g2_x2[c])+"\t");
}
return yanshi_g2_x2; }
/*public double [][] transpose1(double [][] a,int nRows,int nCols)
{
double[][] tv =new double[nCols][nRows];
for(int r=0;r<nRows;++r){
for (int c = 0; c < nCols; ++c){
tv[c][r]=a[r][c];}
}
return tv;
}
public double[][] add(double [][] a,double [][] b,int nRows,int nCols)
{
double[][] sum = new double[nRows][nCols];
for (int r = 0; r < nRows; ++r) {
for (int c = 0; c < nCols; ++c) {
sum[r][c] = a[r][c] + b[r][c];
}
}
return sum;
}
public double[][] multiply(double [][] a,double [][] b,int nRows,int nCols,int bCols)
{
double pv[][] = new double[nRows][bCols]; // product values
// Compute values of the product.
for (int r = 0; r < nRows; ++r) {
for (int c = 0; c < bCols; ++c) {
double dot = 0;
for (int k = 0; k < nCols; ++k) {
dot += a[r][k] * b[k][c];
}
pv[r][c] = dot;
}
}
return pv;
}*/
//求样本协方差矩阵
public double[][] xfch(double [][] a,int aRows,int aCols,double[][]b,int bRows,int bCols)
{ int c=0,r=0,i=0;
double ta[][]=new double[3][5];
double as[][]=new double[aRows][aCols];
double tas[][]=new double[aCols][aCols];
double tb[][]=new double[3][5];
double bs[][]=new double[bRows][bCols];
double tbs[][]=new double[bCols][bCols];
//第一个样本协方差矩阵
for(c=0; c < aCols; ++c)
{
for( r=0;r<aRows;++r)
{
as[r][c] = a[r][c] -yanshi_g1_x1[c] ;
// this.textArea.append(as[r][c]+ "\t ");
}
//this.textArea.append("\n");
}
// this.textArea.append("tran:");
for(r=0;r<aRows;++r){
for (c = 0; c < aCols; ++c){
ta[c][r]=as[r][c];
// this.textArea.append(ta[c][r]+"\t");
}
//this.textArea.append("\n");
}
// this.textArea.append("chengji:");
for ( i = 0; i < aCols; ++i){
for ( c = 0; c < aCols; ++c) {
tas[i][c] = 0;
for ( r= 0; r< aRows; ++r)
tas[i][c] += ta[i][r]*as[r][c] ;
// this.textArea.append(tas[i][c]+" ");
}
// this.textArea.append("\n");
}
//第二个样本协方差矩阵
for(c=0; c < bCols; ++c)
{
for( r=0;r<bRows;++r)
{
bs[r][c] = b[r][c] -yanshi_g2_x2[c] ;
// this.textArea.append(bs[r][c]+ "\t ");
}
// this.textArea.append("\n");
}
// this.textArea.append("tran:");
for(r=0;r<bRows;++r){
for (c = 0; c < bCols; ++c){
tb[c][r]=bs[r][c];
// this.textArea.append(tb[c][r]+"\t");
}
// this.textArea.append("\n");
}
// this.textArea.append("chengji:");
for ( i = 0; i < bCols; ++i){
for ( c = 0; c < bCols; ++c) {
tbs[i][c] = 0;
for ( r= 0; r< bRows; ++r)
tbs[i][c] += tb[i][r]*bs[r][c] ;
//this.textArea.append(tbs[i][c]+" ");
}
//this.textArea.append("adsf");
}
// 样本协方差矩阵的一个无偏估计
for(i=0;i<aCols;++i){
for(c=0;c<aCols;++c){
yanshi_g1_s[i][c]=(tas[i][c]+tbs[i][c])/(aRows+bRows-2);
this.textArea.append(form.format(yanshi_g1_s[i][c])+"\t");}
this.textArea.append("\n");
}
return yanshi_g1_s;
}
//线形判别函数W(x)的估计
public void panbie(double [][] a,int nRows,int nCols,double[]b,double[]c)
{ int i,j=0;
int n=3;
double p=0.0;
double l=0.0;
d1r1F g= new d1r1F ();
double a1[] = new double [10];
double b1[] = new double [4];
double c1[] = new double [3];
for(i=0;i<=n-1;i++){
for(j=0;j<=n-1;j++){
a1[i*n+j]=a[i][j];
}
b1[i]=b[i]-c[i];
}
g.gaussj (a1,n,b1);
for(i=0;i<=n-1;i++){
c1[i]=(b[i]+c[i])/2;
}
for ( i = 0; i < 3; ++i){
p += b1[i]*c1[i] ;
}
this.textArea.append("W(x)="+form.format(b1[0])+"*x1+"+form.format(b1[1])+"*x2+"+form.format(b1[2])
+"*x3-"+form.format(p)+"\n");
this.textArea.append("\n待判样品:\n"+daipan[0]+" "+daipan[1]+" "+daipan[2]+" ");
for(i=0;i<3;i++){
l +=b1[i]*daipan[i];}
l=l-p;
this.textArea.append("\n判别函数W(X)的值:\n"+l+"\n");
if(l<0)
this.textArea.append("判归类别:"+"2");
else
this.textArea.append("判归类别:"+"1");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -