0
点赞
收藏
分享

微信扫一扫

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


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

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

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

一. 矩阵

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

老蝴蝶用 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;
}

一.二 矩阵乘法

一.二.一 乘法讲解

矩阵的加法,减法,乘法和转置(二十)_矩阵操作_02

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

一.二.二 编码

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

一.三 矩阵转置

一.三.一 矩阵讲解

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

一.三.二 编码

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

}
}

运行,控制台打印输出:

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

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


举报

相关推荐

0 条评论