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 4013466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:22:30+00:00 2026-05-20T09:22:30+00:00

I was trying to implement a Matrix.class to learn some Java. Right now, I

  • 0

I was trying to implement a Matrix.class to learn some Java.
Right now, I have some difficulties with a method that should return the matrix after Gaussian elimination which will be used to find the inverse of a matrix later on.
Here is what I came up with so far:

public Matrix gaussianElimination() {
    Matrix inv = this.clone();
    int i = 0;
    int j = 0;

    while (i<inv.getHeight() && j<inv.getWidth()) {
        int pivot = i;
        for (int k=i+1; k<inv.getHeight(); k++) {
            if (Math.abs(inv.getArray()[k][j]) > Math.abs(inv.getArray()[pivot][j])) {
                pivot = k;
            }
        }
        if (inv.getArray()[pivot][j] != 0) {
            inv = inv.swapRow(i, pivot);
            double div = inv.getArray()[i][j];
            for (double value : inv.getArray()[i]) {
                value = value/div;
            }
            for (int u=i+1; u < inv.getHeight(); u++) {
                double mult = inv.getArray()[u][j];
                for (int l=0; l<inv.getWidth(); l++) {
                    inv.getArray()[u][l] = mult * inv.getArray()[i][l];
                }
            }
        }
        j++;
        i++;
    }
    return inv;
}

Whereas the getArray() function returns the double[][] of the matrix, getHeight() and getWidth() return inv.length and inv[0].length respectably.

I followed the pseudocode of this wikipedia page to implement the algorithm.
The method returns a matrix with line of the first pivot element on top but does not calculate the lower rows correctly.

For instance:

A
0.2635522849474877 0.10001114673002853 0.442971040143471
0.2986277338922876 0.7517642579959294 0.09150190333830721
0.8913610667753092 0.8898546572478708 0.25592546060133237

Inv
0.8913610667753092 0.8898546572478708 0.25592546060133237
0.26618513545092265 0.26573527978742995 0.07642644034471581
0.062426597261833985 0.06232109565941264 0.017923775508624545

I would be very thankful for any help as I can’t find a solution. I’ve probably mixed a pointer somewhere or implemented the algorithm wrong.

  • 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-20T09:22:30+00:00Added an answer on May 20, 2026 at 9:22 am

    I see two problems.

    In these lines:

            for (double value : inv.getArray()[i]) {
                value = value/div;
            }
    

    you aren’t modifying the values stored in the matrix; you’re simply modifying the value of value then discarding it. You want something like:

    for (int idx=0; idx<inv.getWidth(); idx++) {
      inv.getArray()[i,idx] = inv.getArray()[i,idx] / div;
    }
    

    Also, in this line:

    inv.getArray()[u][l] = mult * inv.getArray()[i][l];
    

    You should change = to -=. The algorithm says “subtract A[u,j] * row i from row u”. You are simply replacing the values in row u with the product.

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

Sidebar

Related Questions

Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
I am trying to implement matrix multiplication usign OpenCL. I have an ATI Radeon
I am trying to implement a Point3.Slerp method, so was looking for some samples
I've been trying to implement GLSL into my program, however given that i have
I have a matrix class very tailored for the algorithm I need to implement.
I trying to implement some relatively simple 2D sprite batching in OpenGL ES 2.0
I'm trying to implement the distance matrix in parallel using openmp in which I
I am trying to implement a perspective projection. For example, I have a square
I'm trying to implement SURF with Ransac for fundamental(F) matrix estimation in Matlab. Then
I' m trying to implement a weighted graph. I know that there are two

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.