Algs4-1.3.33矩阵库
1.3.33矩阵库。编写一个Matri库并实现以下API:
public class Matrix
static double dot(double[] x,double[] y) 向量点乘
static double[][] mult(double[][] a,double[][] b) 矩阵和矩阵之积
static double[][] transpose(double[][] a) 转置矩阵
static double[] mult(double[][] a,double[] x) 矩阵和向量之积
static double[] mult(double[] y,double[][] a) 向量和矩阵之积
编写一个测试用例,从标准输入读取矩阵并测试所有方法。
public class Matrix
{
public static void main(String[] args)
{
/////vector dot product/////
double[] dotX=new double[3];
double[] dotY=new double[3];
dotX[0]=1;
dotX[1]=2;
dotX[2]=3;
//
dotY[0]=1;
dotY[1]=2;
dotY[2]=3;
StdOut.println("=====vector dot product=====" );
StdOut.printf("%8f ",dot(dotX,dotY));
/////matrix-matrix product/////
double[][] a=new double[4][3];
double[][] b=new double[3][2];
a[0][0]=1;
a[0][1]=0;
a[0][2]=4;
//
a[1][0]=2;
a[1][1]=1;
a[1][2]=1;
//
a[2][0]=3;
a[2][1]=1;
a[2][2]=0;
//
a[3][0]=0;
a[3][1]=2;
a[3][2]=2;
/////
b[0][0]=2;
b[0][1]=4;
//
b[1][0]=1;
b[1][1]=1;
//
b[2][0]=3;
b[2][1]=0;
double[][] c = mult(a,b);
StdOut.println();
StdOut.println();
StdOut.println("=====matrix-matrix product=====" );
for (int row =0;row<c.length;row++)
{
for (int col=0;col<c[0].length;col++)
StdOut.printf("%8f ",c[row][col]);
StdOut.println();
}
/////transpose/////
double[][] d=new double[2][3];
d[0][0]=1;
d[0][1]=2;
d[0][2]=3;
//
d[1][0]=4;
d[1][1]=5;
d[1][2]=6;
StdOut.println();
StdOut.println();
StdOut.println("=====transpose=====");
double[][] e=transpose(d);
for (int row =0;row<e.length;row++)
{
for (int col=0;col<e[0].length;col++)
StdOut.printf("%8f ",e[row][col]);
StdOut.println();
}
/////matrix-vector product/////
double[][] f=new double[3][4];
f[0][0]=1;
f[0][1]=2;
f[0][2]=3;
f[0][3]=4;
//
f[1][0]=1;
f[1][1]=2;
f[1][2]=3;
f[1][3]=4;
//
f[2][0]=1;
f[2][1]=2;
f[2][2]=3;
f[2][3]=4;
//
double[] g=new double[4];
g[0]=1;
g[1]=2;
g[2]=3;
g[3]=4;
double[] colVerctor=new double[4];
colVerctor=mult(f,g);
StdOut.println();
StdOut.println();
StdOut.println("=====matrix-vector product=====");
for (int i=0;i<4;i++)
StdOut.printf("%8f ",colVerctor[i]);
/////vector-matrix product/////
double[] h=new double[3];
h[0]=1;
h[1]=2;
h[2]=3;
double[] rowVerctor=new double[3];
rowVerctor=mult(h,f);
StdOut.println();
StdOut.println();
StdOut.println("=====vector-matrix product=====");
for (int i=0;i<3;i++)
StdOut.printf("%8f ",rowVerctor[i]);
}//end main
public static double dot(double[] x,double[] y)
{
if (x.length!=y.length) return 0;
double result=0;
for (int i=0;i<x.length;i++)
result=result+x[i]*y[i];
return result;
}
//
public static double[][] mult(double[][] a,double b[][])
{
if (a[0].length!=b.length) return null;
int rowLength=a.length;
int colLength=b[0].length;
int sharedLength=a[0].length;
double[][] result=new double[rowLength][colLength];
for(int row=0;row<rowLength;row++)
for(int col=0;col<colLength;col++)
for(int s=0;s<sharedLength;s++)
result[row][col]=result[row][col]+a[row][s]*b[s][col];
return result;
}
//
public static double[][] transpose(double[][] a)
{
int rowLength=a.length;
int colLength=a[0].length;
double[][] transposeMatrix=new double[colLength][rowLength];
for(int row=0;row<rowLength;row++)
for(int col=0;col<colLength;col++)
transposeMatrix[col][row]=a[row][col];
return transposeMatrix;
}
//
public static double[] mult(double[][] a,double x[])
{
if (a[0].length!=x.length) return null;
int rowLength=a.length;
int colLength=x.length;
double[] result=new double[colLength];
for(int row=0;row<rowLength;row++)
for(int col=0;col<colLength;col++)
result[col]=result[col]+a[row][col]*x[col];
return result;
}
//
public static double[] mult(double y[],double[][] a)
{
if (y.length!=a.length) return null;
int rowLength=y.length;
int colLength=a[0].length;
double[] result=new double[rowLength];
for(int col=0;col<colLength;col++)
for(int row=0;row<rowLength;row++)
result[row]=result[row]+a[row][col]*y[row];
return result;
}
}
参考资料:
《离散数学及其应用》原书第七版中文版、维基百科英文版。
vector dot product
matrix-matrix product
transpose
matrix-vector product
vector-matrix product