我已经在Scala中初始化了两个矩阵(X和Y),如下所示,
var x = ofDim[Int](a1,b1)
var y = ofDim[Int](a2,b2)
x,y,a1,a2,b1和b2是变量。 现在我需要通过Y(X / Y)来决定X.怎么能实现呢?
答案 0 :(得分:2)
还有其他使用Apache Commons的方法。但是,重要的是观察除法运算应用乘法运算和反演运算,并且某些矩阵是可逆的,而其他矩阵则是:https://en.wikipedia.org/wiki/Invertible_matrix
以下示例应用Apache Commons库( Study.scala ):
import org.apache.commons.math3.linear._
object Study {
def main(args: Array[String]): Unit = {
val xArray = Array(Array(1.0, 2.0), Array(3.0, 4.0))
val yArray = Array(Array(1.0, 2.0), Array(3.0, 4.0))
val x = new Array2DRowRealMatrix(xArray)
val y = new Array2DRowRealMatrix(yArray)
val yInverse = new LUDecomposition(y).getSolver().getInverse();
val w = x.multiply(yInverse)
for(i <- 0 until w.getRowDimension())
for(j <- 0 until w.getColumnDimension())
println(w.getEntry(i, j))
}
}
提示:如果您打算使用scala控制台,则需要指定classpath ...
scala -classpath .../commons-math3/3.2/commons-math3-3.2.jar
...在scala会话中加载算法......
:load .../Study.scala
...结果出来调用Study的主要功能(可以应用近似值)......
scala> Study.main(null)
0.99 / 1.11E-16 / 0.0 / 1.02
答案 1 :(得分:1)
尝试:
let imageCache = NSCache<NSString, UIImage>()
extension UIImageView {
func loadImageUsingCacheWithUrlString(_ urlString: String) {
self.image = nil
//check cache for image first
if let cachedImage = imageCache.object(forKey: urlString as NSString){
self.image = cachedImage
return
}
//otherwise fire off a new download
guard let url = URL(string: urlString) else {
return
}
URLSession.shared.dataTask(with: url) {
data, response, error in
//download hit an error so lets return out
if error != nil {
return
}
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
})
}.resume()
}
}
答案 2 :(得分:1)
图书馆Beeze,正如其他回应中所说,是必要的。您可以使用SBT或Maven
安装它
Breeze项目可以从GitHub
下载这是Maven方法:
<dependency>
<groupId>org.scalanlp</groupId>
<artifactId>breeze_2.10</artifactId> <!-- or 2.11 -->
<version>0.12</version>
</dependency>
代码......
import breeze.linalg.DenseMatrix
object Division {
def main(args: Array[String]): Unit = {
var a1 = 10
var a2 = 11
var b1 = 12
var b2 = 13
//var x = Array.ofDim[Int](a1,b1)
//var y = Array.ofDim[Int](a2,b2)
var x = DenseMatrix(a1,b1)
var y = DenseMatrix(a2,b2)
var result = x/y
print(result)
}
}