本人最近在用ujmp包写一些程序,ujmp包是针对于超大数据量计算的矩阵的运算包,并且有图形显示的功能且支持多种文件格式的读取和输出,还支持连接数据库,matlab数据类型和weka数据类型,总体来说非常好用,但是有一个很大的缺陷就是基本没有相关的示例和文档,官网上的示例有基本全都过时不能用了,本人总结了一下相关用法,仅供大家参考,代码并不能运行,知识给大家列出了相应的矩阵运算方式和构造方式,希望能对大家有所帮助。
基本用法如下:
@Test
public void testUJMP() {
//初始化一个4X4的矩阵
Matrix dense = DenseMatrix.Factory.zeros(4, 4);
Matrix dense2 = DenseMatrix.Factory.zeros(4, 4);
//输出矩阵的行和列的长度
System.out.println("dense rowcount colcount " + dense.getRowCount() + " " + dense.getColumnCount());;
//利用行和列进行矩阵的赋值
for (int i = 0; i < dense.getRowCount(); ++i){
for (int j = 0 ; j < dense.getColumnCount(); ++j){
//可以使用setXXX来进行矩阵的赋值,其中第一个参数是值,第二个参数是行,第三个参数是列
dense.setAsInt((i*j + (int)(Math.pow(i + 1, j))), i, j);
dense2.setAsInt(i + j, i , j);
}
}
Math.pow(1,2);
//输出矩阵
System.out.println(dense);
System.out.println("dense2 \n" + dense2);
//初始化一个稀疏矩阵
Matrix spares = SparseMatrix.Factory.zeros(400,500);
//这里使用另一种方法获取其行和列
// long[] getSize() 是一个维度为2的矩阵,第一个是行,第二个数是列
for (int i = 0; i < spares.getSize()[0]; ++i){
for (int j = 0; j< spares.getSize()[1]; ++j){
spares.setAsBigDecimal(BigDecimal.valueOf(i *j), i, j);
}
}
System.out.println(spares.getSize()[0] + " " + spares.getSize()[1]);
//System.out.println("spares Matrix : \n" + spares);
/*****************************************
* 矩阵的运算
*****************************************/
//转置
Matrix transpose = dense.transpose();
System.out.println(transpose);
//两个矩阵求和
Matrix sum = dense.plus(dense2);
System.out.println("sum \n" + sum);
//两个矩阵相减
Matrix difference = dense.minus(dense2);
System.out.println("difference \n" + difference);
//矩阵相乘
Matrix matrixProduct = dense.mtimes(dense2);
System.out.println("matrixProduct\n" + matrixProduct);
//矩阵 k*M (K 为常数, M为矩阵)
Matrix scaled = dense.times(2.0);
System.out.println("scaled \n" + scaled);
//矩阵的逆
Matrix inverse = dense.inv();
System.out.println(inverse);
//伪逆矩阵 广义逆矩阵
Matrix pesudoInv = dense.pinv();
System.out.println(pesudoInv);
//求矩阵的行列式
double determiant = dense.det();
System.out.println("determiant = " + determiant);
//矩阵的奇异值分解
Matrix[] sigularValueDecompostion = dense.svd();
for (int i = 0; i < sigularValueDecompostion.length; ++i){
System.out.println("sigularValueDecompostion " + i + "= \n" + sigularValueDecompostion[i]);
}
//求矩阵的特征值
Matrix[] eigenValueDecompostion = dense.eig();
for (int i = 0; i < eigenValueDecompostion.length; ++i){
System.out.println("eigenValueDecompostion " + i + "= \n" + eigenValueDecompostion[i]);
}
//矩阵的LU分解,将矩阵分解成一个上三角矩阵和下三角矩阵的乘积
Matrix[] luValueDecompostion = dense.lu();
for (int i = 0; i < luValueDecompostion.length; ++i){
System.out.println("luValueDecompostion " + i + "= \n" + luValueDecompostion[i]);
}
//qr分解 半正交矩阵与一个上三角矩阵的积,常用来求解线性最小二乘问题
Matrix[] qrDecomposition = dense.qr();
for (int i = 0; i < qrDecomposition.length; ++i){
System.out.println("qrDecomposition " + i + "= \n" + qrDecomposition[i]);
}
//Cholesky分解 对于每一个正定矩阵 Cholesky分解都存在
Matrix choleskyDecomposition = dense.chol();
System.out.println("choleskyDecomposition \n" + choleskyDecomposition);
}
当然还有一些高级操作如下:
long m = 5;
long n = 5;
/**
* 制造一个空矩阵
*/
Matrix emptyMatrix = MatrixFactory.emptyMatrix();
/**
* 制造一个m*n随机矩阵
*/
Matrix randMatrix = Matrix.factory.rand(m, n);
/**
* 制造一个m*n零矩阵
*/
Matrix zeroMatrix = Matrix.factory.zeros(m, n);
/**
* 制造一个m*n对角线为1其余元素为0的矩阵
*/
Matrix eyeMatrix = Matrix.factory.eye(m, n);
/**
* 制造一个m*n全部元素为1的矩阵
*/
Matrix oneMatrix = Matrix.factory.ones(m, n);
/**
* 矩阵的相关操作
*/
// 矩阵与数值的相关运算,意思大家根据英语的含义就能看出,这里就不解释了
Matrix res_1 = oneMatrix.times(10);
Matrix res_2 = oneMatrix.divide(10);
Matrix res_3 = oneMatrix.plus(10);
Matrix res_4 = oneMatrix.minus(10);
/**
* 矩阵与矩阵的相关运算 加和减函数都不用变,乘的话要加上m表示matrix间计算
*/
Matrix res_5 = oneMatrix.mtimes(randMatrix);
Matrix res_7 = oneMatrix.plus(randMatrix);
Matrix res_8 = oneMatrix.minus(randMatrix);
/**
* 求转置求逆,这里有三种返回型,分别是link orig new 计算时间new > orig > link 无返回型和orig的时间类似
*/
Matrix res_9 = oneMatrix.transpose(Ret.LINK);
Matrix res_10 = oneMatrix.transpose(Ret.ORIG);
Matrix res_11 = oneMatrix.transpose(Ret.NEW);
Matrix res_12 = oneMatrix.inv();
// 选取子矩阵
Matrix res_13 = oneMatrix.subMatrix(Ret.NEW, startRow, startColumn,
endRow, endColumn);
// 选取行
Matrix res_14 = oneMatrix.selectRows(returnType, rows);
// 选取列
Matrix res_15 = oneMatrix.selectColumns(returnType, columns);
// 按第i列进行排序,reverse表示返回的排序矩阵是按正序还是逆序
Matrix res_16 = oneMatrix.sortrows(returnType, column, reverse);
// 将矩阵的所有数值相加得到的返回值
Matrix res_17 = oneMatrix.getValueSum();
// 选去矩阵的行和列
Matrix res_18 = oneMatrix.getColumnCount();
Matrix res_19 = oneMatrix.getRowCount();
//判断矩阵否和一个矩阵或一个值相等,相等的话在相应的位置设置为为true否则为false,
//如果要看相等的个数的总和则可再继续用一个getvaluecount函数即可
Matrix res_20 = oneMatrix.eq(returnType, matrix);
matrix res_21 = oneMatrix.eq(returnType, value)
当矩阵返回类型为RET.ORIG的时候不能使用任何有可能改变矩阵大小的操作(除非自己知道确实不会改变),例如转置、选取行列、子矩阵等~~~~~
最后做一点补充
package MatrixPFTest.yi.maytwenty;
import org.ujmp.core.Matrix;
import org.ujmp.core.MatrixFactory;
import org.ujmp.core.calculation.Calculation.Ret;
public class PerfomaceTest {
public static void main(String[] args) {
long begin, end;
/**
* test变test2才变 *********test2不能被改变
*/
long m = 725, n = 20;
// Matrix test_1 = Matrix.factory.rand(5, 5);
// test_1.showGUI();
// Matrix test_2 = test_1.transpose(Ret.ORIG);
// test_2.showGUI();
// Matrix test_3 = test_2.mtimes(Matrix.factory.ones(5, 5).times(2));
// test_3.showGUI();
begin = System.currentTimeMillis();
Matrix res = Matrix.factory.rand(m, n);
Matrix res0 = Matrix.factory.rand(m, n);
end = System.currentTimeMillis();
Constans.sop("构建矩阵耗时" + (end - begin) + "ms");
// res.setLabel("res");
// res.showGUI();
begin = System.currentTimeMillis();
Matrix res_1_trannull = res.transpose();
end = System.currentTimeMillis();
Constans.sop("res_1_trannull-耗时" + (end - begin) + "ms");
begin = System.currentTimeMillis();
Matrix res_2_tranlink = res.transpose(Ret.LINK);
end = System.currentTimeMillis();
Constans.sop("res_2_tranlink-耗时" + (end - begin) + "ms");
// res_2_tranlink.setLabel("res_2_tranlink");
// res_2_tranlink.setAsDouble(10, 0, 0);
// res_2_tranlink.showGUI();
/**
* 进行矩阵赋值,两个矩阵式同一个矩阵,除非用copy()
*/
Matrix xxxMatrix = res_2_tranlink;
xxxMatrix.setAsDouble(10, 0, 0);
xxxMatrix.showGUI();
/**
* 对LINK的矩阵进行赋值
*/
res_2_tranlink = MatrixFactory.ones(1, 1);
res_2_tranlink.setAsDouble(110, 0, 0);
res_2_tranlink.showGUI();
/**
* 选取特定行与列
*/
begin = System.currentTimeMillis();
Matrix res_3 = res_2_tranlink.selectColumns(Ret.NEW, 10);
end = System.currentTimeMillis();
res_3.showGUI();
Constans.sop("选取列-NEW-耗时" + (end - begin) + "ms");
begin = System.currentTimeMillis();
Matrix res_4 = res_2_tranlink.selectColumns(Ret.LINK, 0);
end = System.currentTimeMillis();
res_4.setAsDouble(10, 0, 0);
res_4.showGUI();
Constans.sop("选取列-link-耗时" + (end - begin) + "ms");
/**
* 求逆耗时较长,但是inv和invSymm相差无几
*/
for (int i = 0; i < 1; ++i) {
begin = System.currentTimeMillis();
Matrix res_5 = res_2_tranlink.inv();
end = System.currentTimeMillis();
Constans.sop("inv-耗时" + (end - begin) + "ms");
}
/**
* 获取行数,列数
*/
begin = System.currentTimeMillis();
long res_rowcount = res_2_tranlink.getRowCount();
end = System.currentTimeMillis();
Constans.sop("getRowCount-耗时" + (end - begin) + "ms");
/**
* 矩阵相乘的检测
*/
begin = System.currentTimeMillis();
Matrix res_muti_link = res_2_tranlink.mtimes(Ret.LINK, false, res0);
end = System.currentTimeMillis();
res_muti_link.setAsDouble(100, 0, 0);
// res_muti_link.showGUI();
Constans.sop("res_muti_link-耗时" + (end - begin) + "ms");
// 这里是LINK后和LINK后的矩阵相乘,但是返回的是NEW,所以可以改变值
Matrix afterlinklink = res_muti_link.mtimes(res_2_tranlink);
afterlinklink.setAsDouble(100, 0, 0);
afterlinklink.showGUI();
begin = System.currentTimeMillis();
Matrix res_muti_new = res_2_tranlink.mtimes(Ret.NEW, false, res0);
end = System.currentTimeMillis();
res_muti_new.showGUI();
Constans.sop("res_muti_new-耗时" + (end - begin) + "ms");
/**
* 对不是LINK的矩阵选取行或列再改变变量值,使用LINK的话都会受到影响
*/
Matrix beforeMatrix = Matrix.factory.rand(5, 5);
beforeMatrix.setLabel("beforeMatrix");
beforeMatrix.showGUI();
Matrix nowMatrix = beforeMatrix.selectRows(Ret.NEW, 0);
nowMatrix.setAsDouble(10, 0, 0);
nowMatrix.setLabel("nowMatrix");
nowMatrix.showGUI();
Matrix laterMatrix = beforeMatrix.transpose(Ret.LINK);
laterMatrix.setLabel("laterMatrix");
// laterMatrix.showGUI();
Matrix xx = laterMatrix.minus(Ret.LINK, false, 10);
double xxd = xx.getAsDouble(0, 0);
Constans.sop(xxd);
// xx.showGUI();
}
}