Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

[Linear Algebra] Add det and slogdet #604

Merged
merged 14 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Sources/TensorFlow/Operators/LinearAlgebra.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ public func trace<T: TensorFlowNumeric>(_ matrix: Tensor<T>) -> Tensor<T> {
return matrix.diagonalPart().sum(squeezingAxes: -1)
}

/// Computes the determinant of one or more square matrices.
///
/// - Parameter matrix: A tensor of shape `[..., M, M]`.
/// - Returns: A tensor containing the determinants of all input submatrices.
@inlinable
func det<T:TensorFlowFloatingPoint>(_ matrix: Tensor<T>) -> Tensor<T> {
_Raw.matrixDeterminant(matrix)
}

/// Computes the sign and the log of the absolute value of the determinant of
/// one or more square matrices.
///
/// - Parameter matrix: A tensor of shape `[..., N, M, M]`.
@inlinable
func slogdet<T:TensorFlowFloatingPoint>(_ matrix: Tensor<T>) -> (Tensor<T>, Tensor<T>) {
_Raw.logMatrixDeterminant(matrix)
}

/// Computes the natural logarithm of the determinant of a hermitian positive definite matrix.
///
/// - Parameter matrix: A tensor of shape `[..., M, N]`.
Expand Down
18 changes: 18 additions & 0 deletions Tests/TensorFlowTests/OperatorTests/LinearAlgebraTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ final class LinearAlgebraTests: XCTestCase {
assertEqual(computedGradient, expectedGradient, accuracy: 1e-16)
}

func testDet() {
let matrix = Tensor<Float>(shape: [1, 4, 4], scalars: (0..<16).map(Float.init))
let computedDet = det(matrix)
let expectedDet = Tensor<Float>([0])
XCTAssertEqual(computedDet, expectedDet)
}

func testSlogdet() {
let input = Tensor<Float>(shape: [1, 2, 2], scalars: (0..<4).map(Float.init))
let expectedSigns = Tensor<Float>([-1])
let expectedLogs = Tensor<Float>([0.6931472])
let (computedSigns, computedLogs) = slogdet(input)
XCTAssertEqual(computedSigns, expectedSigns)
XCTAssertEqual(computedLogs, expectedLogs)
}

func testLogdet() {
let input = Tensor<Float>([[[6.0, 4.0], [4.0, 6.0]], [[2.0, 6.0], [6.0, 20.0]]])
let expected = Tensor<Float>([2.9957323, 1.3862934])
Expand Down Expand Up @@ -117,6 +133,8 @@ final class LinearAlgebraTests: XCTestCase {
("testQRDecompositionApproximation", testQRDecompositionApproximation),
("testTrace", testTrace),
("testTraceGradient", testTraceGradient),
("testDet", testDet),
("testSlogdet", testSlogdet),
("testLogdet", testLogdet),
("testLogdetGradient", testLogdetGradient)
]
Expand Down
2 changes: 1 addition & 1 deletion Tests/TensorFlowTests/OperatorTests/MatrixTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ final class MatrixTests: XCTestCase {
let expectedGrad = 2 * t3.bandPart(subdiagonalCount: 0, superdiagonalCount: -1)
XCTAssertEqual(computedGrad, expectedGrad)
}

static var allTests = [
("testDiagonalPart", testDiagonalPart),
("testDiagonal", testDiagonal),
Expand Down