Skip to content

Commit 03af012

Browse files
Added inverse and unary minus of Matrix.
1 parent e5a7b11 commit 03af012

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ A ′∖ .eye(3): 3x3-matrix:
430430
⎝-0.01953124999999994 -0.08984374999999999 -0.19531249999999994⎠
431431
```
432432

433+
The inverse of `A` can more concisely also be obtained via `A.inverse!`.
434+
433435
## Matrix Decompositions
434436

435437
The following matrix decompositions are currently supported:

Sources/LANumerics/LANumeric.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public extension LANumeric {
440440
return nil
441441
}
442442
}
443-
443+
444444
}
445445

446446
infix operator ′* : MultiplicationPrecedence
@@ -510,6 +510,10 @@ public extension Matrix where Element : LANumeric {
510510
return result
511511
}
512512

513+
static prefix func -(matrix : Matrix) -> Matrix {
514+
return -1 * matrix
515+
}
516+
513517
static func += (left : inout Matrix, right : Matrix) {
514518
left.accumulate(1, 1, right)
515519
}
@@ -625,7 +629,7 @@ public extension Matrix where Element : LANumeric {
625629
guard let result = Element.schurDecomposition(&A) else { return nil }
626630
return (eigenValues: result.eigenValues, schurForm: A, schurVectors: result.schurVectors)
627631
}
628-
632+
629633
static func (lhs : Matrix, rhs : Matrix) -> Matrix {
630634
return lhs.solveLeastSquares(rhs)!
631635
}
@@ -641,6 +645,11 @@ public extension Matrix where Element : LANumeric {
641645
static func ′∖ (lhs : Matrix, rhs : Vector<Element>) -> Vector<Element> {
642646
return lhs.solveLeastSquares(transpose: .adjoint, rhs)!
643647
}
648+
649+
var inverse : Matrix? {
650+
guard rows == columns else { return nil }
651+
return self.solveLeastSquares(.eye(rows))
652+
}
644653

645654
}
646655

0 commit comments

Comments
 (0)