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

勿以恶小而为之,勿以善小而不为--------------------------刘备

劝诸君,多行善事积福报,莫作恶

上一章简单介绍了 字符串对齐方式(十九),如果没有看过,请观看上一章

一. 矩阵

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

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

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

一.一 矩阵加法和减法

有图片

一.一.一 矩阵加法

/**
     *
     * @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();

    }

二. 矩阵方法汇总和测试

二.一 矩阵工具类 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));

    }
}

运行,控制台打印输出:

有图片

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两个蝴蝶飞

你的鼓励,是老蝴蝶更努力写作的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值