生活随笔
收集整理的這篇文章主要介紹了
Java矩阵类库
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
java 有一個(gè)非常強(qiáng)大的矩陣學(xué)習(xí)庫(kù)UJMP(Universal Java Matrix Package),就去其官方網(wǎng)站學(xué)習(xí)了下,感覺(jué)非常棒,對(duì)其中自己比較常用的運(yùn)算進(jìn)行了測(cè)試。
下載
通過(guò) maven?
這是下載UJMP最簡(jiǎn)單的方法了,你至少要下載ujmp-core這個(gè)jar 包,它包含最基礎(chǔ)的矩陣類和線性代數(shù)函數(shù)。
<dependency><groupId>org.ujmp</groupId><artifactId>ujmp-core</artifactId><version>0.3.0</version>
</dependency>
其他的 jar 包你可以去官網(wǎng)查看。
下載 jar 包?
如果你沒(méi)有 maven,你可以下載ujmp-complete.jar,它包含了所有的 ujmp jar 包。
?
import org.junit.Test;
import org.ujmp.core.DenseMatrix;
import org.ujmp.core.Matrix;/*** Created by lionel on 16/11/29.*/
public class MatrixTest {@Testpublic void test() {//創(chuàng)建4*4的 零矩陣Matrix dense = DenseMatrix.Factory.zeros(4, 4);System.out.println(dense);/*0.0000 0.0000 0.0000 0.00000.0000 0.0000 0.0000 0.00000.0000 0.0000 0.0000 0.00000.0000 0.0000 0.0000 0.0000*///設(shè)置矩陣dense第三行第四列的元素為5.0dense.setAsDouble(5.0, 2, 3);//其他設(shè)置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);/*1.0000 0.0000 0.0000 0.00000.0000 3.0000 0.0000 -2.00000.0000 0.0000 4.0000 5.00000.0000 0.0000 0.0000 -2.0000*///矩陣dense的轉(zhuǎn)置Matrix transpose = dense.transpose();System.out.println(transpose);/*1.0000 0.0000 0.0000 0.00000.0000 3.0000 0.0000 0.00000.0000 0.0000 4.0000 0.00000.0000 -2.0000 5.0000 -2.0000*///矩陣dense與矩陣transpose相加System.out.println(dense.plus(transpose));/*2.0000 0.0000 0.0000 0.00000.0000 6.0000 0.0000 -2.00000.0000 0.0000 8.0000 5.00000.0000 -2.0000 5.0000 -4.0000*///矩陣dense與矩陣transpose相減System.out.println(dense.minus(transpose));/*0.0000 0.0000 0.0000 0.00000.0000 0.0000 0.0000 -2.00000.0000 0.0000 0.0000 5.00000.0000 2.0000 -5.0000 0.0000*///矩陣dense與矩陣transpose相乘Matrix matrixProduct = dense.mtimes(transpose);System.out.println(matrixProduct);/*1.0000 0.0000 0.0000 0.00000.0000 13.0000 -10.0000 4.00000.0000 -10.0000 41.0000 -10.00000.0000 4.0000 -10.0000 4.0000*///矩陣dense 所有元素*2Matrix scaled = dense.times(2);System.out.println(scaled);/*2.0000 0.0000 0.0000 0.00000.0000 6.0000 0.0000 -4.00000.0000 0.0000 8.0000 10.00000.0000 0.0000 0.0000 -4.0000*///矩陣 dense 的逆矩陣System.out.println(dense.inv());/*1.0000 0.0000 0.0000 0.00000.0000 0.3333 0.0000 -0.33330.0000 0.0000 0.2500 0.6250-0.0000 -0.0000 -0.0000 -0.5000*///生成4*4隨機(jī)矩陣,元素值在0,1之間Matrix rand = Matrix.Factory.rand(4, 4);System.out.println(rand);/*0.5478 0.5100 0.7078 0.06000.8316 0.4039 0.2553 0.01730.4354 0.7132 0.7865 0.70060.0394 0.4839 0.4374 0.6241*///生成4*4隨機(jī)矩陣,元素值在-1,1之間Matrix randn = Matrix.Factory.randn(4, 4);System.out.println(randn);/*0.8655 0.6231 -0.4234 0.08020.7217 -0.7399 -0.5692 0.6421-1.5557 0.4745 2.1110 1.5489-0.8520 -0.7722 0.9025 -0.4664*///產(chǎn)生2*3 元素值都為1.0000的矩陣Matrix ones = Matrix.Factory.ones(2, 3);System.out.println(ones);/*1.0000 1.0000 1.00001.0000 1.0000 1.0000*/}
}
?
總結(jié)
以上是生活随笔為你收集整理的Java矩阵类库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。