使用数组矩阵乘法 [英] Matrix multiplication using arrays

查看:227
本文介绍了使用数组矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用多维数组简单的矩阵乘法方法( [2] [2] )。我在这个还挺新的,我只是找不到它是什么,我做错了。我真的AP preciate在告诉我它是什么任何帮助。我宁可不使用图书馆之类的东西,我主要做是为了了解它是如何工作的。非常感谢你提前。

我宣布我的主要方法一阳如下:

 双击[] [] A = {{4.00,3.00},{2.00,1.00}};
双[] [] B = {{ - 0.500,1.500},{1.000,-2.0000}};

A * B应该返回单位矩阵。事实并非如此。

 公共静态双[] [] multiplicar(双人间[] [] A,双[] [] B){
//方法运行和返回正确维度的矩阵
//(其实我改变了。长度功能为特定值,以消除
//它作为一种可能的问题),但不正确的值    双[] [] C =新双[2] [2];
    INT I,J;    ////我填用零矩阵,如果我不这样做它给了我一个错误
    对于(I = 0; I&2;我++){
        为(J = 0; J&2; J ++){
            C [I] [J] = 0.00000;
        }
    }
    ///这就是我应该执行每一个元素在增加
    // A的行乘以相应的元素
    //相应的B柱,您在B在A中的所有列和所有行
    对于(I = 0; I&2;我++){
        为(J = 0; J&2; J ++)
            C [I] [J] + =(A [i] [j]的* B [j]的[I]);
    }
    返回℃;
}


解决方案

您可以试试这个code:

 公共类于myMatrix {
    双[] [] A = {{4.00,3.00} {2.00,1.00}};
    双[] [] B = {{-0.500,1.500},{1.000,-2.0000}};    公共静态双[] [] multiplicar(双人间[] [] A,双[] [] B){        INT aRows =则为a.length;
        INT aColumns = A [0]。长度;
        诠释眉毛= b.length个;
        INT bColumns = B [0]。长度;        如果(aColumns!=眉毛){
            抛出新抛出:IllegalArgumentException(A:行:+ aColumns +极不相称乙:列+眉毛+。);
        }        双[] [] C =新双[aRows] [bColumns];
        对(INT I = 0; I&2;我++){
            为(中间体J = 0; J&2; J ++){
                C [I] [J] = 0.00000;
            }
        }        的for(int i = 0; I< aRows;我++){// AROW
            对于(INT J = 0; J< bColumns; J ++){// bColumn
                对于(INT K = 0; K< aColumns; k ++){//将aColumn
                    C [I] [J] + = A [I] [K] * B [k]的[J]。
                }
            }
        }        返回℃;
    }    公共静态无效的主要(字串[] args){        于myMatrix矩阵=新于myMatrix();
        双[] []结果= multiplicar(matrix.A,matrix.B);        对(INT I = 0; I&2;我++){
            为(中间体J = 0; J&2; J ++)
                System.out.print(结果[I] [J] +);
            的System.out.println();
        }
    }
}

I'm trying to make a simple matrix multiplication method using multidimensional arrays ([2][2]). I'm kinda new at this, and I just can't find what it is I'm doing wrong. I'd really appreciate any help in telling me what it is. I'd rather not use libraries or anything like that, I'm mostly doing this to learn how it works. Thank you so much in advance.

I'm declaring my arays in the main method as follows:

Double[][] A={{4.00,3.00},{2.00,1.00}}; 
Double[][] B={{-0.500,1.500},{1.000,-2.0000}};

A*B should return the identity matrix. It doesn't.

public static Double[][] multiplicar(Double[][] A, Double[][] B){
//the method runs and returns a matrix of the correct dimensions
//(I actually changed the .length function to a specific value to eliminate 
//it as a possible issue), but not the correct values

    Double[][] C= new Double[2][2];
    int i,j;

    ////I fill the matrix with zeroes, if I don't do this it gives me an error
    for(i=0;i<2;i++) {
        for(j=0;j<2;j++){
            C[i][j]=0.00000;
        }
    } 
    ///this is where I'm supposed to perform the adding of every element in
    //a row of A multiplied by the corresponding element in the
    //corresponding column of B, for all columns in B and all rows in A
    for(i=0;i<2;i++){
        for(j=0;j<2;j++)
            C[i][j]+=(A[i][j]*B[j][i]);
    }
    return C;
}

解决方案

You can try this code:

public class MyMatrix {
    Double[][] A = { { 4.00, 3.00 }, { 2.00, 1.00 } };
    Double[][] B = { { -0.500, 1.500 }, { 1.000, -2.0000 } };

    public static Double[][] multiplicar(Double[][] A, Double[][] B) {

        int aRows = A.length;
        int aColumns = A[0].length;
        int bRows = B.length;
        int bColumns = B[0].length;

        if (aColumns != bRows) {
            throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
        }

        Double[][] C = new Double[aRows][bColumns];
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                C[i][j] = 0.00000;
            }
        }

        for (int i = 0; i < aRows; i++) { // aRow
            for (int j = 0; j < bColumns; j++) { // bColumn
                for (int k = 0; k < aColumns; k++) { // aColumn
                    C[i][j] += A[i][k] * B[k][j];
                }
            }
        }

        return C;
    }

    public static void main(String[] args) {

        MyMatrix matrix = new MyMatrix();
        Double[][] result = multiplicar(matrix.A, matrix.B);

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++)
                System.out.print(result[i][j] + " ");
            System.out.println();
        }
    }
}

这篇关于使用数组矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆