将UJMP添加到项目中的最简单方法是通过Maven引入它。您至少需要ujmp-core
——包含基本矩阵类和线性代数函数的软件包。将这些行添加到pom.xml
文件中的<dependencies>
部分 :
<dependency>
<groupId> org.ujmp </ groupId>
<artifactId> ujmp-core </ artifactId>
<version> 0.3.0 </ version>
</dependency>
//创建一个4行4列的密集空矩阵
Matrix dense = DenseMatrix.Factory.zeros(4, 4);
//将第2行第3列的值设置为5.0
dense.setAsDouble(5.0, 2, 3);
//设置其他值
dense.setAsDouble(1.0, 0, 0);
dense.setAsDouble(3.0, 1, 1);
dense.setAsDouble(4.0, 2, 2);
dense.setAsDouble(-2.0, 3, 3);
dense.setAsDouble(-2.0, 1, 3);
//在控制台上打印最终矩阵
System.out.println(dense);
//创建一个4行4列的稀疏空矩阵
Matrix sparse = SparseMatrix.Factory.zeros(4, 4);
sparse.setAsDouble(2.0, 0, 0);
//基本计算
Matrix transpose = dense.transpose();
Matrix sum = dense.plus(sparse);
Matrix difference = dense.minus(sparse);
Matrix matrixProduct = dense.mtimes(sparse);
Matrix scaled = dense.times(2.0);
Matrix inverse = dense.inv();
Matrix pseudoInverse = dense.pinv();
double determinant = dense.det();
Matrix[] singularValueDecomposition = dense.svd();
Matrix[] eigenValueDecomposition = dense.eig();
Matrix[] luDecomposition = dense.lu();
Matrix[] qrDecomposition = dense.qr();
Matrix choleskyDecomposition = dense.chol();
//创建随机值介于0和1之间的矩阵
Matrix rand = Matrix.Factory.rand(100, 10);
//创建介于-1和-1之间的随机值的矩阵
Matrix randn = Matrix.Factory.randn(100, 10);
//在屏幕上显示
rand.showGUI();
randn.showGUI();
//创建包含10个相关列,100行,相关0.1的矩阵
Matrix correlated = Matrix.Factory.correlatedColumns(100, 10, 0.1);
//计算相似度并存储在新矩阵中
Matrix similarity = correlated.cosineSimilarity(Ret.NEW, true);
//在屏幕上显示
correlated.showGUI();
similarity.showGUI();
//获取图片
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("org/ujmp/examples/data/bigdata.jpg");
//将图片加载到矩阵中。当然,这也适用于文件。
Matrix imageMatrix = new ImageMatrix(is);
//在屏幕上显示
imageMatrix.showGUI();
is.close();
//创建一个代表本地计算机及其服务的矩阵
Matrix localhost = Matrix.Factory.localhostMatrix();
//在屏幕上显示
localhost.showGUI();
//创建一个非常大的稀疏矩阵
SparseMatrix m1 = SparseMatrix.Factory.zeros(1000000, 500000);
//设置一些值
m1.setAsDouble(MathUtil.nextGaussian(), 0, 0);
m1.setAsDouble(MathUtil.nextGaussian(), 1, 1);
for (int i = 0; i < 10000; i++) {
m1.setAsDouble(MathUtil.nextGaussian(), MathUtil.nextInteger(0, 1000), MathUtil.nextInteger(0, 1000));
}
//在屏幕上显示
m1.showGUI();
//创建另一个矩阵
SparseMatrix m2 = SparseMatrix.Factory.zeros(3000000, 500000);
//设置一些值
m2.setAsDouble(MathUtil.nextGaussian(), 0, 0);
m2.setAsDouble(MathUtil.nextGaussian(), 1, 1);
for (int i = 0; i < 10000; i++) {
m2.setAsDouble(MathUtil.nextGaussian(), MathUtil.nextInteger(0, 1000), MathUtil.nextInteger(0, 1000));
}
//在屏幕上显示
m2.showGUI();
//做一些操作
Matrix m3 = m1.mtimes(m2.transpose());
//在屏幕上显示
m3.showGUI();
//在一个目录中查找所有Excel文件
File[] files = new File("c:/temp/").listFiles();
//创建矩阵以存储结果
Matrix result = Matrix.Factory.zeros(files.length, 2);
//遍历所有文件
for (int i = 0; i < files.length; i++) {
//将文件导入为矩阵
Matrix m = Matrix.Factory.importFrom().file(files[i]).asDenseCSV();
//将文件名存储在结果矩阵中
result.setAsString(files[i].getName(), i, 0);
//搜索“Invoice”
if (m.containsString("Invoice"))
//提取第10行第3列的值并将其存储在结果中
result.setAsDouble(m.getAsDouble(10, 3), i, 1);
}
//在屏幕上显示结果
result.showGUI();
//创建一个以字符串为节点,双精度为边的图矩阵
GraphMatrix<String, Double> graphMatrix = new DefaultGraphMatrix<String, Double>();
graphMatrix.setLabel("Interface Inheritance Graph");
//从UJMP收集所有矩阵接口
Class<?>[] classArray = new Class[] { DenseMatrix.class, DenseMatrix2D.class, Matrix.class, Matrix2D.class,
SparseMatrix.class, SparseMatrix2D.class, BaseBigDecimalMatrix.class, BigDecimalMatrix2D.class,
DenseBigDecimalMatrix.class, DenseBigDecimalMatrix2D.class, SparseBigDecimalMatrix.class,
SparseBigDecimalMatrix2D.class, BigIntegerMatrix.class, BigIntegerMatrix2D.class,
DenseBigIntegerMatrix.class, DenseBigIntegerMatrix2D.class, SparseBigIntegerMatrix.class,
SparseBigIntegerMatrix2D.class, BooleanMatrix.class, BooleanMatrix2D.class, DenseBooleanMatrix.class,
DenseBooleanMatrix2D.class, SparseBooleanMatrix.class, SparseBooleanMatrix2D.class,
ByteArrayMatrix.class, ByteArrayMatrix2D.class, DenseByteArrayMatrix.class,
DenseByteArrayMatrix2D.class, SparseByteArrayMatrix.class, SparseByteArrayMatrix2D.class,
ByteMatrix.class, ByteMatrix2D.class, DenseByteMatrix.class, DenseByteMatrix2D.class,
SparseByteMatrix.class, SparseByteMatrix2D.class, CharMatrix.class, CharMatrix2D.class,
DenseCharMatrix.class, DenseCharMatrix2D.class, SparseCharMatrix.class, SparseCharMatrix2D.class,
DoubleMatrix.class, DoubleMatrix2D.class, DenseDoubleMatrix.class, DenseDoubleMatrix2D.class,
SparseDoubleMatrix.class, SparseDoubleMatrix2D.class, FloatMatrix.class, FloatMatrix2D.class,
DenseFloatMatrix.class, DenseFloatMatrix2D.class, SparseFloatMatrix.class, SparseFloatMatrix2D.class,
GenericMatrix.class, GenericMatrix2D.class, DenseGenericMatrix.class, DenseGenericMatrix2D.class,
SparseGenericMatrix.class, SparseGenericMatrix2D.class, IntMatrix.class, IntMatrix2D.class,
DenseIntMatrix.class, DenseIntMatrix2D.class, SparseIntMatrix.class, SparseIntMatrix2D.class,
LongMatrix.class, LongMatrix2D.class, DenseLongMatrix.class, DenseLongMatrix2D.class,
SparseLongMatrix.class, SparseLongMatrix2D.class, ObjectMatrix.class, ObjectMatrix2D.class,
DenseObjectMatrix.class, DenseObjectMatrix2D.class, SparseObjectMatrix.class,
SparseObjectMatrix2D.class, ShortMatrix.class, ShortMatrix2D.class, DenseShortMatrix.class,
DenseShortMatrix2D.class, SparseShortMatrix.class, SparseShortMatrix2D.class, StringMatrix.class,
StringMatrix2D.class, DenseStringMatrix.class, DenseStringMatrix2D.class, SparseStringMatrix.class,
SparseStringMatrix2D.class };
//了解接口如何相互扩展
for (Class<?> c1 : classArray) {
for (Class<?> c2 : classArray) {
if (c2.getSuperclass() == c1) {
// add edge when class2 extends class1
graphMatrix.setEdge(1.0, c1.getSimpleName(), c2.getSimpleName());
}
for (Class<?> c3 : c2.getInterfaces()) {
if (c1 == c3) {
// add edge when class2 implements class1
graphMatrix.setEdge(1.0, c1.getSimpleName(), c2.getSimpleName());
}
}
}
}
//在屏幕上显示
graphMatrix.showGUI();
//根据Mandelbrot集创建矩阵
Matrix m = new MandelbrotMatrix();
//在屏幕上显示
m.showGUI();
//创建一个以字符串为元素的树矩阵
TreeMatrix<String> treeMatrix = new DefaultTreeMatrix<String>();
//创建数据
treeMatrix.setRoot("root");
treeMatrix.addChild("root", "child1");
treeMatrix.addChild("root", "child2");
treeMatrix.addChild("root", "child3");
treeMatrix.addChild("child1", "subChild11");
treeMatrix.addChild("child1", "subChild12");
treeMatrix.addChild("child1", "subChild13");
treeMatrix.addChild("child2", "subChild21");
treeMatrix.addChild("child3", "subChild31");
treeMatrix.addChild("child3", "subChild32");
treeMatrix.addChild("subChild12", "subSubChild121");
treeMatrix.addChild("subChild12", "subSubChild122");
treeMatrix.addChild("subSubChild122", "subSubSubChild1221");
//在屏幕上显示
treeMatrix.showGUI();