「java大规模矩阵运算」矩阵的大小如何计算

博主:adminadmin 2023-01-06 10:36:06 522

本篇文章给大家谈谈java大规模矩阵运算,以及矩阵的大小如何计算对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用java语言做矩阵运算,输入一个m×n的矩阵,再输入一个n×o的矩阵,求其乘积。

这应该是线性数学的矩阵

逻辑思路是:取A矩阵的每一行的各个项去乘以B矩阵每个列的各个项

为了测试,我现在假设A矩阵是4*3, B矩阵是3*2, 你要做的乘操作是4*3*2=24次

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class Ideone

{

public static void main (String[] args) throws java.lang.Exception

{

int[][]A=new int[][]{{1,2,3},{4,5,6},{7,8,9},{10,11,12}};

int[][]B=new int[][]{{1,2},{3,4},{5,6}};

int len_A=A[0].length;//a每行元素数,这里是3

int col_A=A.length;//a每列元素数,这里是4

int len_B=B[0].length;//b每行元素数,这里是2

int col_B=B.length;//b每列元素数,这里是3

int len_a=0;

int col_a=0;

int len_b=0;

int col_b=0;

int sum=0;

while(len_blen_B)//使B的下一列被A乘

{

col_a=0;

while(col_acol_A)//使A可以移动至下一行

{

len_a=0;

col_b=0;

while(len_alen_A)//使A的当前行乘以B的当前列

{

sum+=A[col_a][len_a]*B[col_b][len_b];

len_a++;

col_b++;

}

col_a++;

}

len_b++;

}

System.out.println(sum);

}

}

【Java】【矩阵】相关

1、方法有很多这里简单交流一下我的思路,可以定义一个算法将3*3的矩阵执行某个计算或者代入到某个代数让其运算得出一个数字,对函数或者代数的要求是可逆,即通过一个数字能反推出原矩阵值。

2、可以定义查看顺序按照顺序来将矩阵内的数字全部展开。比如我们按照从上到下从左到右的顺义依次记录(1,3)(2,3)(3,3)(1,2)(2,2)......然后使用0来链接两个不同的数这样就可以将矩阵压缩为一个数字并快速还原为矩阵。

用java怎么写矩阵乘法?

import java.util.Scanner; 

public class Matrix { 

public double[][] create() { 

Scanner sc = new Scanner(System.in) ; 

System.out.print("请输入矩阵的行高:"); 

int a = sc.nextInt() ; 

System.out.print("请输入矩阵的列宽:"); 

int b = sc.nextInt() ; 

double[][] x = new double[a][b] ; 

for(int i=0;ilt;a;i++){ 

for(int j=0;jlt;b;j++){ 

System.out.print("请输入元素x["+i+"]["+j+"]的值:" ); 

x[i][j] = sc.nextDouble() ; 

return x ; 

public double[][] multiply(double[][] x,double[][] y){ 

double[][] result = null ; 

int a = x[0].length ; 

int b = y.length ; 

if(a != b){ 

System.out.println("输入的维数不匹配,不能进行运算"); 

}else{ 

int c = x.length ; 

int d = y[0].length ; 

result = new double[c][d] ; 

for(int i=0;ilt;c;i++){ 

for(int j=0;jlt;d;j++){ 

double sum = 0 ; 

for(int k=0;klt;a;k++){ 

sum += x[i][k]*y[k][j] ; 

result[i][j] = sum ; 

return result ; 

public void print(double[][] x){ 

System.out.println("矩阵为:"); 

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

for(int j=0;jlt;x[i].length;j++){ 

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

System.out.println(); 

测试类: 

public class TestMatrix { 

public static void main(String[] args) { 

Matrix m = new Matrix() ; 

//double[][] x = {{1,2},{3,2}} ; 

//double[][] y = {{1,2,1},{2,3,3}} ; 

System.out.println("创建第一个数组:") ; 

double[][] x = m.create() ; 

m.print(x) ; //用来验证输入的是否和你一样的,没啥作用 

System.out.println("创建第二个数组:"); 

double[][] y = m.create() ; 

m.print(y) ; //用来验证输入的是否和你一样的,没啥作用 

double[][] result = m.multiply(x, y) ; 

if(result == null){ 

return ; //如果输入的矩阵不能运算就不输出结果了。 

m.print(result) ; 

}

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

用java编程实现矩阵乘法,如下图所示,详细点。

把矩阵看做一个二维数组,如 float[][] matrix = new float[3][2];

就是一个3行2列的矩阵,取matrix[0][1],就是取该矩阵第1行第2列的值。

剩下的就是线性代数的知识了,你套着算去吧,练练就知道了。

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大规模矩阵运算和矩阵的大小如何计算的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。