Dani AI

Generated

The posted class provides input/print routines but no arithmetic implementations. Common issues to address before adding methods: allocate the storage after reading rows/columns (avoid a fixed 10x10), use double if inversion/division is required, and always check dimensions (and singularity for inversion). was correct to ask for more detail; the code below is a concise, drop-in utility showing safe add/subtract/multiply and a simple Gauss–Jordan inverse (partial pivoting). Division is implemented as A * inverse(B).

public final class MatrixOps {
    private MatrixOps() {}

    public static double[][] add(double[][] A, double[][] B) {
        int r = A.length, c = A[0].length;
        if (B.length != r || B[0].length != c) throw new IllegalArgumentException("Dimension mismatch");
        double[][] R = new double[r][c];
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                R[i][j] = A[i][j] + B[i][j];
        return R;
    }

    public static double[][] sub(double[][] A, double[][] B) {
        int r = A.length, c = A[0].length;
        if (B.length != r || B[0].length != c) throw new IllegalArgumentException("Dimension mismatch");
        double[][] R = new double[r][c];
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                R[i][j] = A[i][j] - B[i][j];
        return R;
    }

    public static double[][] mul(double[][] A, double[][] B) {
        int r = A.length, inner = A[0].length;
        if (B.length != inner) throw new IllegalArgumentException("Inner dimensions must match");
        int c = B[0].length;
        double[][] R = new double[r][c];
        for (int i = 0; i < r; i++) {
            for (int k = 0; k < inner; k++) {
                double aik = A[i][k];
                for (int j = 0; j < c; j++) R[i][j] += aik * B[k][j];
            }
        }
        return R;
    }

    public static double[][] invert(double[][] a) {
        int n = a.length;
        if (a[0].length != n) throw new IllegalArgumentException("Matrix must be square");
        double[][] aug = new double[n][2*n];
        for (int i = 0; i < n; i++) {
            System.arraycopy(a[i], 0, aug[i], 0, n);
            aug[i][n + i] = 1.0;
        }
        for (int i = 0; i < n; i++) {
            int pivot = i;
            for (int r = i + 1; r < n; r++)
                if (Math.abs(aug[r][i]) > Math.abs(aug[pivot][i])) pivot = r;
            if (Math.abs(aug[pivot][i]) < 1e-12) throw new IllegalArgumentException("Singular or nearly singular");
            if (pivot != i) { double[] tmp = aug[i]; aug[i] = aug[pivot]; aug[pivot] = tmp; }
            double div = aug[i][i];
            for (int j = 0; j < 2*n; j++) aug[i][j] /= div;
            for (int r = 0; r < n; r++) if (r != i) {
                double f = aug[r][i];
                for (int j = 0; j < 2*n; j++) aug[r][j] -= f * aug[i][j];
            }
        }
        double[][] inv = new double[n][n];
        for (int i = 0; i < n; i++) System.arraycopy(aug[i], n, inv[i], 0, n);
        return inv;
    }

    public static double[][] div(double[][] A, double[][] B) {
        return mul(A, invert(B));
    }
}

Notes and cautions: the Gauss–Jordan inverter shown is fine for small matrices and learning, but is not tuned for stability or performance—use a tested library (Apache Commons Math, EJML, etc.) for production. For integer-only problems, avoid inverses (integers often do not form a field); prefer solving linear systems or using rational arithmetic. To integrate with the posted class, allocate the storage after reading dimensions, fill a double[][], and call these utilities.

Recommended Answers

All 5 Replies

Thanks for what?

We are definately not going to do it for you, especially when you can't even take the time to describe your problem.

Show your code, provide a good, but short, description of the current problems with it, and provide any and all error/compiler messages and we may help you solve it (although I don't know about that after this pitiful plea).

import java.util.Scanner;
public class Matrix
{
   public Matrix() 
   {
     
   }
   public void enterMatrix()
   {
      Scanner input = new Scanner(System.in);
      System.out.print("Enter number of rows -> ");
      r=input.nextInt();
      System.out.print("Enter numerof columns -> ");
      c=input.nextInt();
      for(int i=0;i<r;i++)
      {
         for (int j=0;j<c;j++)
         {
             System.out.print("Enter value at row "+i+", column "+j+" -> ");
             m[i][j]=input.nextInt();
         }
      }
   }
   public void printMatrix()
   {
       for (int i=0;i<r;i++)
       {
           for (int j=0;j<c;j++)
           {
               System.out.print(m[i][j]+" ");
           }
           System.out.println();
       }
    }
   public void multMatrix()
   {
       
    }
   int[][] m = new int[10][10];
   int r;
   int c;
   }

and the test

public class MatrixTest
{
  public static void main (String[] args)
  {
      Matrix a=new Matrix();
      a.enterMatrix();
      a.printMatrix();
  }
}

Okay? And the rest of the methods?

Yeah... that's all I got.
I was hoping you guys could help with the rest of the methods

Well, I believe I said in the first reply that we weren't going to do it for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.