0

I am trying to use the solve method but it gave me an error, Can anyone please help me with how to write the solve method? I tried running this code, the code before the solve method works fine, but after that it gave me an error. Thank you so much

import java.lang.Math;
public class MatrixDriver {

  public static void main(String[] args) {
    double[][] d = { { 1, 2, 3 }, { 4, 5, 6 }, { 9, 1, 3} };

     System.out.println("Matrix D - Testing Constructor that take double integer array as a parameter");
    Matrix D = new Matrix(d);
    System.out.println(D);
    System.out.println();

    System.out.println("Matrix A - Testing Named Constructor -- random\nTakes two parameters (row, col) and returns a \"new\" Matrix");
    Matrix A = Matrix.random(5, 5);
    System.out.println(A);
    System.out.println();

    System.out.println("Matrix A - Testing the swap rows method");
    A.swapRows(1,2);
    System.out.println(A);
    System.out.println();

    System.out.println("Matrix B - Testing the transpose method on Matrix A");
    Matrix B = A.transpose();
    System.out.println(B);
    System.out.println();

    System.out.println("Matrix C - Testing named constructor - identity - which takes a single parameter (i) and returns a 'new' (i x i) Identity Matrx");
    Matrix C = Matrix.identity(5);
    System.out.println(C);
    System.out.println();

    System.out.println("Matrix E - Testing matrix addition (A + B)");
    Matrix E = A.plus(B);
    System.out.println(E);
    System.out.println();

    System.out.println("Matrix F - Testing matrix subtraction (A - B)");
    Matrix F = A.minus(B);
    System.out.println(F);
    System.out.println();

    System.out.println("Matrix G - Testing matrix scalar multiplication (c * A), c = 1.2");
    double c = 1.2;
    Matrix G = A.times(c);
    System.out.println(G);
    System.out.println();

    System.out.println("Matrix AB - Testing matrix multiplication (A X B)");
    Matrix AB = A.times(B);
    System.out.println(AB);
    System.out.println();

  System.out.println("Matrix BA - Testing matrix multiplication (B X A)");
  Matrix BA = B.times(A);
  System.out.println(BA);
  System.out.println();

    System.out.println("Matrices AB and BA - Testing Matrix Equality (AB equals BA)"); 
    try 
    {
      System.out.println("  " + AB.equals(BA));
      System.out.println();
    } 
    catch(RuntimeException e) 
    {
      System.out.println(e.getMessage());
      System.out.println();

    }
  System.out.println("Matrix b - Creating a random (5 X 1)");
  Matrix b = Matrix.random(5,1);
  System.out.println(b);
  System.out.println();

  System.out.println("Matrix X - Testing the solve method (X = A^-1 * b)");
  Matrix L= Matrix.solve(X);
  double X = Math.pow(A,-1) * b;
  //b.solve(b);
  System.out.println(b);
  System.out.println();

  System.out.println("Matrix d - Testing the sove again (A x X) = b ... is it?");
  Matrix b = Matrix.solve();
  System.out.println(b);
  System.out.println();
   }
}
  • What's the error and what's the solve method? And where are you getting Matrix whatever = new Matrix()? Did you build your own Matrix class? Or are you using JAMA? – Stephen Nov 18 '16 at 18:12
  • JAMA package which is used for Java matrix class.Can you please add library for java matrix. – Abu Sufian Nov 18 '16 at 18:13
  • non-static method solve(Matrix) cannot be referenced from a static context – Vishwajeet Singh Nov 18 '16 at 18:15
  • the desired output would be Matrix X − Testing the s o l v e method (X = A^−1 ∗ b ) 2 . 9 2, ,2 . 1 2 ,2 . 4 5 ,5 . 3 3 ,−10.25 I put comma just to separate them – Vishwajeet Singh Nov 18 '16 at 18:16
  • @GerMex the desired output would be Matrix X − Testing the s o l v e method (X = A^−1 ∗ b ) 2 . 9 2, ,2 . 1 2 ,2 . 4 5 ,5 . 3 3 ,−10.25 I put comma just to separate them – Vishwajeet Singh Nov 18 '16 at 18:25

2 Answers2

0

You're not using JAMA you're using: http://introcs.cs.princeton.edu/java/95linear/Matrix.java.html arn't you? That will be helpful next time.

To answer your question though what is 'x' the method Matrix.solve doesn't not know what 'X' is.

Matrix L= Matrix.solve(X);
double X = Math.pow(A,-1) * b;

Should be:

double X = Math.pow(A,-1) * b;
Matrix L= Matrix.solve(X);

But even then, you'll still have an error because Math.pow does not work with Matrixes you'll need to use another method if you want to ^2 a whole matrix. Which is here: https://stackoverflow.com/a/22901024/4329778

Community
  • 1
  • 1
Stephen
  • 1,072
  • 1
  • 19
  • 33
  • Appreciate it, but it still give me an error " incompatible types: Matrix cannot be converted to double" which you described in your answer. but I just need a method for driver class as part of my assignment – Vishwajeet Singh Nov 18 '16 at 18:35
0

May I advise this post. I think JAMA is no longer updated, org.apache.commons.math3.linear seems the most advanced current library for matrix. In Android Studio, you can add in Gradle:

implementation 'org.apache.commons:commons-math3:3.6.1'

Bruno L.
  • 457
  • 6
  • 8