本人最近在用ujmp包写一些程序,ujmp包是针对于超大数据量计算的矩阵的运算包,并且有图形显示的功能且支持多种文件格式的读取和输出,还支持连接数据库,matlab数据类型和weka数据类型,总体来说非常好用,但是有一个很大的缺陷就是基本没有相关的示例和文档,官网上的示例有基本全都过时不能用了,本人总结了一下相关用法,仅供大家参考,代码并不能运行,知识给大家列出了相应的矩阵运算方式和构造方式,希望能对大家有所帮助
LINK 可以用来进行矩阵求逆,矩阵相乘,矩阵的行列选取,大体来说是在矩阵大小变换是依然可用,但是不可用于矩阵数值的改变,试图改变LINK后的矩阵中的值,其实不会改变,要改变原来矩阵的值才有用;
对一个原始矩阵进行转置,当矩阵转置后返回的如果是LINK,随后进行赋值操作,则LINK矩阵和转置前的矩阵都不会有变化,即使从转置后的矩阵抽取一列或一行进行赋值,所有矩阵也不会有变化。
对一个原始矩阵选取其行或列是返回的是LINK,随后进行赋值操作,则原矩阵和LINK矩阵都会变化
对LINK后的矩阵再进行LIK转置,矩阵是会改变的
在至今的测试中,除了设值,其他的都可以改变,也可以获取值
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(); } }
res.minus(Ret.LINK, false,res2.mtimes(Ret.LINK, false, res1)); 效率最高
在对矩阵指定列进行排序时,用sortrows,但是!!一定要注意的是要将矩阵用(toDoubleMatrix)转化为double型矩阵再进行运算