打印矩阵乘法步骤

时间:2014-08-28 16:55:59

标签: java swing for-loop matrix

我需要逐步解释2个矩阵是如何相乘的。例如,如果有两个矩阵:

    ┌      ┐
    │ 1  2 │
A = │ 4  3 │
    └      ┘
    ┌        ┐
    │  6   9 │
B = │ -8  -5 │
    └        ┘

......我需要打印:

**Explanation:**

C11 = 1•6 + 2•(-8) = -10
C12 = 1•9 + 2•(-5) = -1

C21 = 4•6 + 3•(-8) = 0
C22 = 4•9 + 3•(-5) = 21

**Result**

      ┌         ┐
      │ -10  -1 │
A•B = │   0  21 │
      └         ┘

我怎样才能做到这一点?请注意,矩阵可能不是2 X 2,尺寸肯定会有所不同。

修改

我尝试过类似的事情:

int frstMtxLen = frstMtx.length;
int scndMtxLen = secMtx.length;

for(int i  = 0; i < frstMtxLen; i++)
{
    for(int j = 0; j < frstMtx[i].length; j++)
    {
        resltMtxP[i][j] = "";

        for (int k = 0; k < scndMtxLen; k++)
        {
            resltMtxP[i][j] = "\t"+resltMtxP[i][j]+" + "+ frstMtx[i][k] +" X "+secMtx[k][j]+" ;
        }

        try
        {                               
            doc.insertString(doc.getLength(), resltMtxP[i][j]+"  ", headings); //I'm writing to a JTextPane
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    try 
    {
        doc.insertString(doc.getLength(), "\n", keyWord);
    }
    catch (BadLocationException e1) 
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

打印矩阵为:

+ 2.0 X 6.0   + 4.0 X 7.0            + 2.0 X 9.0   + 4.0 X 8.0    
+ 5.0 X 6.0   + 8.0 X 7.0            + 5.0 X 9.0   + 8.0 X 8.0    

告诉我哪里出错了。

1 个答案:

答案 0 :(得分:1)

该程序解释了两个给定矩阵的乘法。输出传递给Consumer<String>。在此示例中,此使用者仅将字符串打印到控制台。您可以将其更改为将给定字符串插入文档的使用者,或者......等等。

import java.util.Locale;

interface Consumer<T>
{
    void accept(T t);
}

public class MatMulExplainer
{
    public static void main(String[] args)
    {
        test2x2_2x2();
        test3x2_2x4();
    }

    private static void test2x2_2x2()
    {
        double A[][] = {
            { 1, 2 },
            { 4, 3 },
        };
        double B[][] = {
            {  6,  9 },
            { -8, -5 },
        };
        double C[][] = explain(A, B, console());
    }

    private static void test3x2_2x4()
    {
        double A[][] = {
            { 1, 2 },
            { 4, 3 },
            { 5, 6 },
        };
        double B[][] = {
            {  6,  9, -3,  8},
            { -8, -5, -1,  7},
        };
        double C[][] = explain(A, B, console());
    }

    private static Consumer<String> console()
    {
        return new Consumer<String>()
        {
            @Override
            public void accept(String t)
            {
                System.out.println(t);
            }
        };
    }


    private static double[][] explain(
        double A[][], double B[][], Consumer<String> consumer)
    {
        consumer.accept("Multiply\n"+toString(A));
        consumer.accept("and\n"+toString(B));

        int rA = A.length;
        int cA = A[0].length;
        int cB = B[0].length;
        double C[][] = new double[rA][cB];
        for (int r=0; r<rA; r++)
        {
            for (int c=0; c<cB; c++)
            {
                float sum = 0;
                StringBuilder sb = new StringBuilder();
                sb.append("C"+r+","+c+" = ");
                for (int n=0; n<cA; n++)
                {
                    sb.append(toString(A[r][n])+" * "+toString(B[n][c]));
                    sum += A[r][n] * B[n][c];
                    if (n < cA - 1)
                    {
                        sb.append(" + ");
                    }
                }
                sb.append(" = "+toString(sum));
                consumer.accept(sb.toString());
                C[r][c] = sum;
            }
        }

        consumer.accept("Result:\n"+toString(C));
        return C;
    }

    private static String toString(double A[][])
    {
        StringBuilder sb = new StringBuilder();
        for (int r=0; r<A.length; r++)
        {
            for (int c=0; c<A[r].length; c++)
            {
                sb.append(toString(A[r][c])+" ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }


    private static String toString(double d)
    {
        final String format = "%6.2f";
        return String.format(Locale.ENGLISH, format, d);
    }

}

对于您提供的示例,它会打印

Multiply
  1.00   2.00 
  4.00   3.00 

and
  6.00   9.00 
 -8.00  -5.00 

C0,0 =   1.00 *   6.00 +   2.00 *  -8.00 = -10.00
C0,1 =   1.00 *   9.00 +   2.00 *  -5.00 =  -1.00
C1,0 =   4.00 *   6.00 +   3.00 *  -8.00 =   0.00
C1,1 =   4.00 *   9.00 +   3.00 *  -5.00 =  21.00
Result:
-10.00  -1.00 
  0.00  21.00