「java矩阵运算课程设计」数据结构课程设计矩阵的运算

博主:adminadmin 2022-11-30 00:03:07 105

今天给各位分享java矩阵运算课程设计的知识,其中也会对数据结构课程设计矩阵的运算进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java编写一个程序实现矩阵的运算加减乘除,(并对其中的异常进行处理)

/**

 * 矩阵:由 m × n 个数Aij排成的m行n列的数表称为m行n列的矩阵,简称m × n矩阵

 * 说白了就是一个二维数组,下面的程序用整形作为数据类型,其他类型运算大同小异

 * 

 */

public class MatrixUtils {

    /**

     * 矩阵运算:加(减法与之类似)

     */

    public static int[][] matrixAdd(int[][] addend, int[][] summand) {

        if (addend == null || addend.length == 0) {

            throw new IllegalArgumentException("addend matrix is empty!");

        }

        if (summand == null || summand.length == 0) {

            throw new IllegalArgumentException("summand matrix is empty!");

        }

        //矩阵加减要求两个矩阵类型一致,即行列数相同

        int row = addend.length;

        int col = addend[0].length;

        if (row != summand.length || col != summand[0].length) {

            throw new IllegalArgumentException("summand and summand not the same type!");

        }

        int[][] sum = new int[row][col];

        for (int i = 0; i  row; i++) {

            for (int j = 0; j  col; j++) {

                sum[i][j] = addend[i][j] + summand[i][j];

                // sum[i][j] = addend[i][j] - summand[i][j]; //减法

            }

        }

        return sum;

    }

    /**

     * 矩阵运算:乘法,没找到除法的运算规则

     */

    public static int[][] matrixMultiply(int[][] addend, int[][] summand) {

        if (addend == null || addend.length == 0) {

            throw new IllegalArgumentException("addend matrix is empty!");

        }

        if (summand == null || summand.length == 0) {

            throw new IllegalArgumentException("summand matrix is empty!");

        }

        //两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义。如A是m×n矩阵和B是n×p矩阵,它们的乘积C是一个m×p矩阵 

        int row = addend.length;

        int col = summand[0].length;

        if (addend[0].length != summand.length) {

            throw new IllegalArgumentException("summand and summand not the same type!");

        } 

        int[][] sum = new int[row][col];

        for (int i = 0; i  row; i++) {

            for (int j = 0; j  col; j++) {

                for (int z = 0; z  addend[0].length; z++) {

                    sum[i][j] += addend[i][z] * summand[z][j];

                    System.out.println("sum[" + i+  "]["+ j+"]= " + sum[i][j]);

                }

            }

        }

        return sum;

    }

}

用java设计一个支持矩阵加减乘运算的程序

Const N = 5

Dim aa(M, N), bb(M, N), cc(M, N)

Sub printit(a()) '打印矩阵

For i = 1 To M

For j = 1 To N

Print Tab(j * 10); a(i, j);

Next j

Print

Next i

End Sub

Sub jzjia(a(), b(), c()) '矩阵相加 c=a+b

For i = 1 To M

For j = 1 To N

c(i, j) = a(i, j) + b(i,

JAVA程序设计:设A为m行n列矩阵,B为n行k列矩阵,C为m行k列矩阵。

public class Matrix {

private int m, n;

private double[][] matrix;

public Matrix(int m, int n) {

this.n = n;

this.m = m;

if (n == 0 || m == 0) {

throw new RuntimeException();

}

matrix = new double[m][n];

}

public void init(double... value) {

if (value.length != m * n) {

throw new RuntimeException();

}

for (int i = 0, a = 0; i m; i++) {

for (int j = 0; j n; j++) {

matrix[i][j] = value[a];

a++;

}

}

}

public Matrix mul(Matrix x) {

if (n != x.m) {

throw new RuntimeException();

}

Matrix c = new Matrix(n, x.m);

for (int i1 = 0; i1 m; i1++) {

for (int i3 = 0; i3 x.n; i3++) {

for (int i2 = 0; i2 n; i2++) {

c.matrix[i1][i3] += matrix[i1][i2] * x.matrix[i2][i3];

}

}

}

return c;

}

public String toString() {

StringBuffer s = new StringBuffer();

for (int i = 0; i m; i++) {

for (int j = 0; j n; j++) {

s.append(matrix[i][j] + " ");

}

s.append("\n");

}

return s.toString();

}

public static void main(String[] args) {

Matrix a = new Matrix(2, 2);

Matrix b = new Matrix(2, 2);

a.init(new double[] { 1, 7, 8, 3 });

b.init(new double[] { 2, 5, 6, 4 });

Matrix c = a.mul(b);

System.out.println(c);

}

}

一、用JAVA编写程序实现矩阵乘积;int a[][]={{1,2,3},{4,5,6},{7,8,9}};求大神解答……谢谢

public class TestArrays{

public static void main(String []args){

int[][] a=new int[3][3];

int[][] b=new int[3][3];

int[][] c=new int[3][3];

//循环出两个二维数组,如果是给定的数组,就可以直接写最后的一个循环了

Ststem.out.println("矩阵A是:");

for(int i=0;ia.length;i++){

for(int j=0;ja[0].length;j++){

a[i][j]=(int)((Math.random())*10);

System.out.print(a[i][j]+" ");

}

}

Ststem.out.println("矩阵B是:");

for(int i=0;ib.length;i++){

for(int j=0;jb[0].length;j++){

b[i][j]=(int)((Math.random())*10);

System.out.print(b[i][j]+" ");

}

}

Ststem.out.println("矩阵A X B是:");

for(int i=0;ic.length;i++){

for(int j=0;jc[0].length;j++){

c[i][j]=0;

for(int k=0;kc.length;k++){

c[i][j]=a[i][k]*b[k][j]|d[i][j];

}

System.out.print(d[i][j]+" ");

}

System.out.println();

}

}

}

java矩阵运算课程设计的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于数据结构课程设计矩阵的运算、java矩阵运算课程设计的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-30,除非注明,否则均为首码项目网原创文章,转载请注明出处。