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
图片

转载于:https://www.cnblogs.com/longjin2018/p/9848721.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值