当前位置: 首页 > 工具软件 > UJMP > 使用案例 >

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

吴炎彬
2023-12-01

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的时候不能使用任何有可能改变矩阵大小的操作(除非自己知道确实不会改变),例如转置、选取行列、子矩阵等~~~~~

packageMatrixPFTest.yi.maytwenty;importorg.ujmp.core.Matrix;importorg.ujmp.core.MatrixFactory;importorg.ujmp.core.calculation.Calculation.Ret;public classPerfomaceTest {public static voidmain(String[] args) {longbegin, 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();

}

}

 类似资料: