Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:07:05+00:00 2026-05-13T08:07:05+00:00

I’m trying to calculate the inverse matrix in Java. I’m following the adjoint method

  • 0

I’m trying to calculate the inverse matrix in Java.

I’m following the adjoint method (first calculation of the adjoint matrix, then transpose this matrix and finally, multiply it for the inverse of the value of the determinant).

It works when the matrix is not too big. I’ve checked that for matrixes up to a size of 12×12 the result is quickly provided. However, when the matrix is bigger than 12×12 the time it needs to complete the calculation increases exponentially.

The matrix I need to invert is 19×19, and it takes too much time. The method that more time consumes is the method used for the calculation of the determinant.

The code I’m using is:

public static double determinant(double[][] input) {
  int rows = nRows(input);        //number of rows in the matrix
  int columns = nColumns(input); //number of columns in the matrix
  double determinant = 0;

  if ((rows== 1) && (columns == 1)) return input[0][0];

  int sign = 1;     
  for (int column = 0; column < columns; column++) {
    double[][] submatrix = getSubmatrix(input, rows, columns,column);
    determinant = determinant + sign*input[0][column]*determinant(submatrix);
    sign*=-1;
  }
  return determinant;
}   

Does anybody know how to calculate the determinant of a large matrix more efficiently? If not, does anyone knows how to calcultate the inverse of a large matrix using other algorithm?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:07:06+00:00Added an answer on May 13, 2026 at 8:07 am

    Exponentially? No, I believe matrix inversion is O(N^3).

    I would recommend using LU decomposition to solve a matrix equation. You don’t have to solve for the determinant when you use it.

    Better yet, look into a package to help you. JAMA comes to mind.

    12×12 or 19×19 are not large matricies. It’s common to solve problems with tens or hundreds of thousands of degrees of freedom.

    Here’s a working example of how to use JAMA. You have to have the JAMA JAR in your CLASSPATH when you compile and run:

    package linearalgebra;
    
    import Jama.LUDecomposition;
    import Jama.Matrix;
    
    public class JamaDemo
    {
        public static void main(String[] args)
        {
            double [][] values = {{1, 1, 2}, {2, 4, -3}, {3, 6, -5}};  // each array is a row in the matrix
            double [] rhs = { 9, 1, 0 }; // rhs vector
            double [] answer = { 1, 2, 3 }; // this is the answer that you should get.
    
            Matrix a = new Matrix(values);
            a.print(10, 2);
            LUDecomposition luDecomposition = new LUDecomposition(a);
            luDecomposition.getL().print(10, 2); // lower matrix
            luDecomposition.getU().print(10, 2); // upper matrix
    
            Matrix b = new Matrix(rhs, rhs.length);
            Matrix x = luDecomposition.solve(b); // solve Ax = b for the unknown vector x
            x.print(10, 2); // print the solution
            Matrix residual = a.times(x).minus(b); // calculate the residual error
            double rnorm = residual.normInf(); // get the max error (yes, it's very small)
            System.out.println("residual: " + rnorm);
        }
    }
    

    Here’s the same problem solved using Apache Commons Math, per quant_dev’s recommendation:

    package linearalgebra;
    
    import org.apache.commons.math.linear.Array2DRowRealMatrix;
    import org.apache.commons.math.linear.ArrayRealVector;
    import org.apache.commons.math.linear.DecompositionSolver;
    import org.apache.commons.math.linear.LUDecompositionImpl;
    import org.apache.commons.math.linear.RealMatrix;
    import org.apache.commons.math.linear.RealVector;
    
    public class LinearAlgebraDemo
    {
        public static void main(String[] args)
        {
            double [][] values = {{1, 1, 2}, {2, 4, -3}, {3, 6, -5}};
            double [] rhs = { 9, 1, 0 };
    
            RealMatrix a = new Array2DRowRealMatrix(values);
            System.out.println("a matrix: " + a);
            DecompositionSolver solver = new LUDecompositionImpl(a).getSolver();
    
            RealVector b = new ArrayRealVector(rhs);
            RealVector x = solver.solve(b);
            System.out.println("solution x: " + x);;
            RealVector residual = a.operate(x).subtract(b);
            double rnorm = residual.getLInfNorm();
            System.out.println("residual: " + rnorm);
        }
    }
    

    Adapt these to your situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 349k
  • Answers 349k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You were having a typo in your input class name… May 14, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer It looks like get_friends will return a list of User… May 14, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer jQuery 1.6+ jQuery added a :focus selector so we no… May 14, 2026 at 6:52 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.