BoroFel
BoroFel

Reputation: 49

How to use the Gaussian Elimination algorithm in Java?

I have the task to calculate u with the gaussian Eliminiation algorithm in:

Au = b

A is a Matrix (2-dimensional-Array) looking the following:

[1.0, 0.0, 0.0, 0.0, 0.0]
[-25.0, 50.0, -25.0, 0.0, 0.0]
[0.0, -25.0, 50.0, -25.0, 0.0]
[0.0, 0.0, -25.0, 50.0, -25.0]
[0.0, 0.0, 0.0, 0.0, 1.0]

b is a Vector (Array) looking the following:

[0.0, 1.115696, 1.115696, 1.115696, 0.0]

How do i use the Gaussian Elimination algorithm on this? Is there already something that java has implemented? Or how would a code look like to calculate this? Im thankful for any kind of help!

Upvotes: 1

Views: 330

Answers (1)

iroli
iroli

Reputation: 458

You can use the open source la4j library for example. Then your code might look like

    ...
    double[][] numbers_for_A = {
            {1.0, 0.0, 0.0, 0.0, 0.0},
            {-25.0, 50.0, -25.0, 0.0, 0.0},
            {0.0, -25.0, 50.0, -25.0, 0.0},
            {0.0, 0.0, -25.0, 50.0, -25.0},
            {0.0, 0.0, 0.0, 0.0, 1.0}};
    double[] numbers_for_b = {0.0, 1.115696, 1.115696, 1.115696, 0.0};

    Matrix A = Matrix.from2DArray(numbers_for_A);
    Vector b = Vector.fromArray(numbers_for_b);
    GaussianSolver gs = new GaussianSolver(A);
    Vector u = gs.solve(b);

    System.out.println(u);

You can import library using maven dependency

    <dependency>
        <groupId>org.la4j</groupId>
        <artifactId>la4j</artifactId>
        <version>0.6.0</version>
    </dependency>

Upvotes: 2

Related Questions