首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java导入外部库:ApacheCommons数学

Java导入外部库:ApacheCommons数学
EN

Stack Overflow用户
提问于 2015-11-25 10:11:23
回答 1查看 1.8K关注 0票数 0

在看了许多讨论之后,例如:

我仍然被困住了,无法使下列代码工作:

代码语言:javascript
复制
    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();

    }
}

下面是我一步一步地做的事情。

首先,我使用

代码语言:javascript
复制
sudo apt-get install libcommons-math3-java

然后我查看了Common-math3-java安装的位置。

代码语言:javascript
复制
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

然后我使用了(正如Install commons math library for java in Ubuntu中所讲的)

代码语言:javascript
复制
javac -cp .:/usr/share/java/commons-math3-3.2.jar linearAlgebraLearning.java 

但是,我仍然是一个导入错误消息:

代码语言:javascript
复制
linearAlgebraLearning.java:1: error: cannot find symbol
import org.apache.commons.math3.linear;

和额外的错误,因为编译器找不到类(如RealMatrix)。我知道这类问题已经问过很多次了。这里的人可能厌倦了这个问题..。但如果你能帮我我会很高兴的。

Ps :因为我的linux发行版上有Eclipse的一些bug,所以我不使用IDE,而是使用gedit和终端。

EN

回答 1

Stack Overflow用户

发布于 2019-07-29 02:38:19

我首先安装了每个libcommons包:

代码语言:javascript
复制
sudo apt install libcommons\*

然后设置类路径:

代码语言:javascript
复制
export CLASSPATH="/usr/share/java/commons-math3.jar:/usr/share/java/commons-lang3.jar"

您的java应该会自动地捡起它。我用jshell测试了它,它能够自动完成/导入BlockRealMatrix,例如:

代码语言:javascript
复制
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}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33913675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档