0

after having looked at many discussions like for example :

I am still stuck and I am not able to make the following code to work :

    import org.apache.commons.math3.linear;

class linearAlgebraLearning{
    public static void main(String[] args){


    // Create a real matrix with two rows and three columns, using a factory
    // method that selects the implementation class for us.
    double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
    RealMatrix m = MatrixUtils.createRealMatrix(matrixData);

    // One more with three rows, two columns, this time instantiating the
    // RealMatrix implementation class directly.
    double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};
    RealMatrix n = new Array2DRowRealMatrix(matrixData2);

    // Note: The constructor copies  the input double[][] array in both cases.

    // Now multiply m by n
    RealMatrix p = m.multiply(n);
    System.out.println(p.getRowDimension());    // 2
    System.out.println(p.getColumnDimension()); // 2

    // Invert p, using LU decomposition
RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();

    }
}

So here are what I have done step by step.

First I installed Apache using

sudo apt-get install libcommons-math3-java

Then I have looked where commons-math3-java has been installed.

dpkg -L libcommons-math3-java 
/.
/usr
/usr/share
/usr/share/maven-repo
/usr/share/maven-repo/org
/usr/share/maven-repo/org/apache
/usr/share/maven-repo/org/apache/commons
/usr/share/maven-repo/org/apache/commons/commons-math3
/usr/share/maven-repo/org/apache/commons/commons-math3/3.2
/usr/share/maven-repo/org/apache/commons/commons-math3/3.2/commons-math3-3.2.pom
/usr/share/maven-repo/org/apache/commons/commons-math3/debian
/usr/share/maven-repo/org/apache/commons/commons-math3/debian/commons-math3-debian.pom
/usr/share/doc
/usr/share/doc/libcommons-math3-java
/usr/share/doc/libcommons-math3-java/changelog.Debian.gz
/usr/share/doc/libcommons-math3-java/copyright
/usr/share/java
/usr/share/java/commons-math3.jar
/usr/share/maven-repo/org/apache/commons/commons-math3/3.2/commons-math3-3.2.jar
/usr/share/maven-repo/org/apache/commons/commons-math3/debian/commons-math3-debian.jar
/usr/share/java/commons-math3-3.2.jar

then I used ( as told in the Install commons math library for java in Ubuntu )

javac -cp .:/usr/share/java/commons-math3-3.2.jar linearAlgebraLearning.java 

however I still an import error message :

linearAlgebraLearning.java:1: error: cannot find symbol
import org.apache.commons.math3.linear;

And additional errors since the compiler does not find the classes (like RealMatrix). I know that this kind question has been asked many times. People here might be tired of seeing this question... But I would be really happy if you could help me.

Ps : Because there is some bug with Eclipse on my linux distribution I am not using IDE and use gedit and the terminal.

Community
  • 1
  • 1
Varconis
  • 1
  • 2
  • make your class public, class names are uppercase,can you try to copy jar file into same folder with the java file and try again – HRgiger Nov 25 '15 at 11:18
  • Hi, thank for the proposition. howerver after copying the jar files to the same folder where the java file is I still get the the same error... I tried javac -cp ./commons-math3-3.5.jar LinearAlgebraTest.java and javac LinearAlgebraTest.java since to what I understood from the man ./ is normally the default path and normally I should not use -cp. – Varconis Nov 25 '15 at 13:19
  • http://stackoverflow.com/questions/2096283/including-jars-in-classpath-on-commandline-javac-or-apt – HRgiger Nov 25 '15 at 14:29
  • I am a bit confuse to what I should pay attention in this post. After reading it I have again tried the fallowing javac -cp /usr/share/java/commons-math3-3.2.jar:. linearAlgebraLearning.java putting the .jar in ~/javalib javac -cp ~/javalib:. linearAlgebraLearning.java putting the jar in the same folder where LinearAlgebra is javac -cp ./name_of_the_jar.jar:. LinearAlgebraTest.java I don't know. What is wrong. – Varconis Nov 25 '15 at 15:26
  • You need to either change the import to end with a * or (better) explicitly import the RealMatrix class, i.e. import org.apache.commons.math3.linear.RealMatrix; – Phil Steitz Dec 18 '15 at 13:43

1 Answers1

0

I installed every libcommons package first:

sudo apt install libcommons\*

then set the classpath:

export CLASSPATH="/usr/share/java/commons-math3.jar:/usr/share/java/commons-lang3.jar"

Your java should pick it up automatically. I tested it with jshell and it was able to autocomplete/import BlockRealMatrix for example:

jshell> import org.apache.commons.math3.linear.BlockRealMatrix
jshell> BlockRealMatrix foo = new BlockRealMatrix(2, 2);
foo ==> BlockRealMatrix{{0.0,0.0},{0.0,0.0}}
sabujp
  • 989
  • 1
  • 11
  • 12