您的位置:首页 > 编程语言 > Java开发

java矩阵运算包ujmp中的一些小示例和注意事项

2016-05-15 16:25 706 查看
本人最近在用ujmp包写一些程序,ujmp包是针对于超大数据量计算的矩阵的运算包,并且有图形显示的功能且支持多种文件格式的读取和输出,还支持连接数据库,matlab数据类型和weka数据类型,总体来说非常好用,但是有一个很大的缺陷就是基本没有相关的示例和文档,官网上的示例有基本全都过时不能用了,本人总结了一下相关用法,仅供大家参考,代码并不能运行,知识给大家列出了相应的矩阵运算方式和构造方式,希望能对大家有所帮助

LINK 可以用来进行矩阵求逆,矩阵相乘,矩阵的行列选取,大体来说是在矩阵大小变换是依然可用,但是不可用于矩阵数值的改变,试图改变LINK后的矩阵中的值,其实不会改变,要改变原来矩阵的值才有用;

对一个原始矩阵进行转置,当矩阵转置后返回的如果是LINK,随后进行赋值操作,则LINK矩阵和转置前的矩阵都不会有变化,即使从转置后的矩阵抽取一列或一行进行赋值,所有矩阵也不会有变化。

对一个原始矩阵选取其行或列是返回的是LINK,随后进行赋值操作,则原矩阵和LINK矩阵都会变化

对LINK后的矩阵再进行LIK转置,矩阵是会改变的

在至今的测试中,除了设值,其他的都可以改变,也可以获取值

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型矩阵再进行运算
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: