矩阵的加法,减法,乘法和转置(二十)

简介: 矩阵的加法,减法,乘法和转置(二十)

一. 矩阵


矩阵,有常见的几种操作, 矩阵加法,矩阵减法,矩阵乘法,矩阵转置,矩阵求逆等。


老蝴蝶用 Java 语言实现 矩阵加法,矩阵减法,矩阵乘法和矩阵转置操作。


在学习之前,一定要了解矩阵的相关知识。


一.一 矩阵加法和减法

20200611074717224.png


一.一.一 矩阵加法


/**
     *
     * @param first
     * @param second
     * @return 返回两个矩阵的和
     */
    public static int[][] add(int[][] first,int[][] second){
        //获取行列数,进行比较
        int row1=first.length;
        int col1=first[0].length;
        int row2=second.length;
        int col2=second[0].length;
        //判断比较,行列数是否一致
        if(row1!=row2){
            throw new IllegalArgumentException("两个矩阵的行数不一致");
        }
        if(col1!=col2){
            throw new IllegalArgumentException("两个矩阵的列数不一致");
        }
        //构建结果集合
        int[][] result=new int[row1][col1];
        //进行集合运算
        for(int i=0;i<row1;i++){
            for(int j=0;j<col1;j++){
                //相加
                result[i][j]=first[i][j]+second[i][j];
            }
        }
        return result;
    }


一.一.二 矩阵减法


 /**
     *
     * @param first
     * @param second
     * @return 返回两个矩阵的差
     */
    public static int[][] minus(int[][] first,int[][] second){
        //获取行列数,进行比较
        int row1=first.length;
        int col1=first[0].length;
        int row2=second.length;
        int col2=second[0].length;
        //判断比较,行列数是否一致
        if(row1!=row2){
            throw new IllegalArgumentException("两个矩阵的行数不一致");
        }
        if(col1!=col2){
            throw new IllegalArgumentException("两个矩阵的列数不一致");
        }
        //构建结果集合
        int[][] result=new int[row1][col1];
        //进行集合运算
        for(int i=0;i<row1;i++){
            for(int j=0;j<col1;j++){
                //相加
                result[i][j]=first[i][j]-second[i][j];
            }
        }
        return result;
    }


一.二 矩阵乘法


一.二.一 乘法讲解


20200611074729505.png


一.二.二 编码


 /**
     *
     * @param first
     * @param second
     * @return 返回两个矩阵的积
     */
    public static int[][] multiply(int[][] first,int[][] second){
        //获取行列数,进行比较
        int row1=first.length;
        int col1=first[0].length;
        int row2=second.length;
        int col2=second[0].length;
        //判断比较,行列数是否一致
        if(col1!=row2){
            throw new IllegalArgumentException("第一个矩阵的列与第二个矩阵的行数不一致");
        }
        //构建结果集合
        int[][] result=new int[row1][col2];
       for(int i=0;i<row1;i++){
           for(int j=0;j<col2;j++){
               for(int k=0;k<col1;k++){
                   result[i][j]+=first[i][k]*second[k][j];
               }
           }
       }
        return result;
    }


一.三 矩阵转置


一.三.一 矩阵讲解


20200611074744212.png


一.三.二 编码


 /**
     *
     * @param first
     * @return 返回矩阵转置
     */
    public static int[][] transpose(int[][] first){
        if(first==null){
            return null;
        }
        int row1=first.length;
        int col1=first[0].length;
        //行列互换
        int[][] result=new int[col1][row1];
        for(int i=0;i<row1;i++){
            for(int j=0;j<col1;j++){
                    //值互换
                    result[j][i]=first[i][j];
            }
        }
        return result;
    }


一.四 矩阵打印输出


 public static String print(int[][] result){
        if(result==null){
            return "{}";
        }
        StringBuilder sb=new StringBuilder();
        sb.append("{");
        for(int i=0;i<result.length;i++){
            sb.append("{");
            for(int j=0;j<result[0].length;j++){
                sb.append(result[i][j]);
                if(j!=result[0].length-1) {
                    sb.append(",");
                }
            }
            if(i!=result.length-1){
                sb.append("},");
            }else{
                sb.append("}");
            }
        }
        sb.append("}");
        return sb.toString();
    }


二. 矩阵方法汇总和测试


二.一 矩阵工具类 MatrixUtils


package com.yjl.collection;
/**
 * package: com.yjl.collection
 * className: MatrixUtils
 * Description: 矩阵的工具
 *
 * @author : yuezl
 * @Date :2020/6/11 5:56
 */
public class MatrixUtils {
    /**
     *
     * @param first
     * @param second
     * @return 返回两个矩阵的和
     */
    public static int[][] add(int[][] first,int[][] second){
        //获取行列数,进行比较
        int row1=first.length;
        int col1=first[0].length;
        int row2=second.length;
        int col2=second[0].length;
        //判断比较,行列数是否一致
        if(row1!=row2){
            throw new IllegalArgumentException("两个矩阵的行数不一致");
        }
        if(col1!=col2){
            throw new IllegalArgumentException("两个矩阵的列数不一致");
        }
        //构建结果集合
        int[][] result=new int[row1][col1];
        //进行集合运算
        for(int i=0;i<row1;i++){
            for(int j=0;j<col1;j++){
                //相加
                result[i][j]=first[i][j]+second[i][j];
            }
        }
        return result;
    }
    /**
     *
     * @param first
     * @param second
     * @return 返回两个矩阵的差
     */
    public static int[][] minus(int[][] first,int[][] second){
        //获取行列数,进行比较
        int row1=first.length;
        int col1=first[0].length;
        int row2=second.length;
        int col2=second[0].length;
        //判断比较,行列数是否一致
        if(row1!=row2){
            throw new IllegalArgumentException("两个矩阵的行数不一致");
        }
        if(col1!=col2){
            throw new IllegalArgumentException("两个矩阵的列数不一致");
        }
        //构建结果集合
        int[][] result=new int[row1][col1];
        //进行集合运算
        for(int i=0;i<row1;i++){
            for(int j=0;j<col1;j++){
                //相加
                result[i][j]=first[i][j]-second[i][j];
            }
        }
        return result;
    }
    /**
     *
     * @param first
     * @param second
     * @return 返回两个矩阵的积
     */
    public static int[][] multiply(int[][] first,int[][] second){
        //获取行列数,进行比较
        int row1=first.length;
        int col1=first[0].length;
        int row2=second.length;
        int col2=second[0].length;
        //判断比较,行列数是否一致
        if(col1!=row2){
            throw new IllegalArgumentException("第一个矩阵的列与第二个矩阵的行数不一致");
        }
        //构建结果集合
        int[][] result=new int[row1][col2];
       for(int i=0;i<row1;i++){
           for(int j=0;j<col2;j++){
               for(int k=0;k<col1;k++){
                   result[i][j]+=first[i][k]*second[k][j];
               }
           }
       }
        return result;
    }
    /**
     *
     * @param first
     * @return 返回矩阵转置
     */
    public static int[][] transpose(int[][] first){
        if(first==null){
            return null;
        }
        int row1=first.length;
        int col1=first[0].length;
        //行列互换
        int[][] result=new int[col1][row1];
        for(int i=0;i<row1;i++){
            for(int j=0;j<col1;j++){
                    //值互换
                    result[j][i]=first[i][j];
            }
        }
        return result;
    }
    public static String print(int[][] result){
        if(result==null){
            return "{}";
        }
        StringBuilder sb=new StringBuilder();
        sb.append("{");
        for(int i=0;i<result.length;i++){
            sb.append("{");
            for(int j=0;j<result[0].length;j++){
                sb.append(result[i][j]);
                if(j!=result[0].length-1) {
                    sb.append(",");
                }
            }
            if(i!=result.length-1){
                sb.append("},");
            }else{
                sb.append("}");
            }
        }
        sb.append("}");
        return sb.toString();
    }
}


二.二 矩阵测试


package com.yjl.collection;
/**
 * package: com.yjl.collection
 * className: MatrixUtilsTest
 * Description: 请输入相应的描述
 *
 * @author : yuezl
 * @Date :2020/6/11 6:14
 */
public class MatrixUtilsTest {
    public static void main(String[] args) {
        int[][] first={{1,3},{1,0},{1,2}};
        int[][] second={{0,0},{7,5},{2,1}};
        int[][] result=MatrixUtils.add(first,second);
        System.out.println("相加:\n"+MatrixUtils.print(result));
        result=MatrixUtils.minus(first,second);
        System.out.println("相减:\n"+MatrixUtils.print(result));
        int[][] arr1={{5,2,4},{3,8,2},{6,0,4},{0,1,6}};
        int[][] arr2={{2,4},{1,3},{3,2}};
        result=MatrixUtils.multiply(arr1,arr2);
        System.out.println("相乘:\n"+MatrixUtils.print(result));
        int [][] t1={{1,0,2},{-2,1,3}};
        result=MatrixUtils.transpose(t1);
        System.out.println("转置:\n"+MatrixUtils.print(result));
    }
}


运行,控制台打印输出:


20200611074756403.png


谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!

相关文章
|
3天前
|
BI
1051 复数乘法 (15 分)
1051 复数乘法 (15 分)
|
1月前
|
计算机视觉
加法运算
【5月更文挑战第8天】加法运算。
18 4
|
1月前
|
人工智能 小程序 BI
矩阵的转置、加和乘法写入C++
矩阵的转置、加和乘法写入C++
21 0
|
1月前
|
存储 C++
[C++/PTA] 矩阵的乘法运算
[C++/PTA] 矩阵的乘法运算
89 0
|
9月前
|
算法
矩阵的加法
矩阵的加法
36 0
7-2 一元多项式的乘法与加法运算 (20 分)
7-2 一元多项式的乘法与加法运算 (20 分)
111 0
7-237 有理数加法 (15 分)
7-237 有理数加法 (15 分)
83 0
|
存储 算法
高精度加法,模拟大数的加法运算
在处理特别大的数相加特别大的数的时候,long long不能直接通过加法算出结果的时候,可以通过高精度算法处理这些数的相加具体·思路如下; 首先 1 . 这些数存到数组的时候该如何排列,是个位放在第一位还是最后一位放到第一位,由于数的相加的候常常出现进位,常在最后一位加上一个数,而加上数的话往往在数组最后一位加上数比较方便,所以我们把第个位放在数组第一位 2.其次在调用模拟大数相加的函数中,我们该如何处理同一位上数相加出现的进位呢,我们可以设置一个 t 存储数组上某位相加最后吧 t%10 ,就可以得到想要的数,同时在 t / 10 如果 t 会的得到 1 或者 0.
102 0