Java 打印矩阵乘法步骤

Java 打印矩阵乘法步骤,java,swing,for-loop,matrix,Java,Swing,For Loop,Matrix,我需要打印两个矩阵如何相乘的分步解释。例如,如果有两个矩阵: ┌ ┐ │ 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

我需要打印两个矩阵如何相乘的分步解释。例如,如果有两个矩阵:

    ┌      ┐
    │ 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();
    }
}

告诉我哪里出错。

这个程序解释了两个给定矩阵的乘法。输出被传递到
消费者
。在本例中,此使用者仅将字符串打印到控制台。您可以将其更改为将给定字符串插入文档的使用者,或者。。。不管怎样

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);
    }

}

您当前的代码在哪里?您有什么具体问题?如果您是来这里获取代码的,那么很抱歉!可能没有人会免费给你代码,而你自己必须为此而努力!是否希望完整的GUI解决方案是Swing?@admdrew我已编辑了问题以添加代码和问题。请看一下,让我知道问题出在哪里is@shekharsuman别担心!提供的答案对我有用。。谢谢你的关心。正是我需要的:)非常感谢:)
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