Singular value decomposition This exercise continues with a more challenging task in…
- User
Singular value decomposition This exercise continues with a more challenging task in numerical programming. That is, we ask you to implement a method that computes a singular value decomposition (SVD) of a given (real) matrix. In more precise terms, given an m × n matrix A with real entries as input, you are asked to compute a decomposition A = UΣVT where
- U is an m × m real orthogonal matrix (UUT = I where I is the m × m identity matrix),
- Σ is an m x n nonnegative (rectangular) diagonal matrix, and
- V is an n x n real orthogonal matrix (VVT = I where I is the n x n identity matrix). The three-tuple (U, Σ, V) of matrices is a singular value decomposition (SVD) of the matrix A. Remark. To simplify the task you can assume that m, n ≤ 20. Furthermore, we allow your code to be numerically quite sloppy. Namely, if A has independent and identically distributed Gaussian entries (zero mean, unit variance), your code should return with high probability an SVD (U, E, V) such that the properties (i) A = UΣVT, (ii) UUT = I, (iii) Σ is nonnegative rectangular diagonal, and (iv) VVT = I all hold within entrywise additive error of € = 106. In practice this holds if your code passes the unit tests. The singular value decomposition enables easy access to many basic properties of a matrix such as its inverse, pseudoinverse, null space, range, low-rank approximations, and so forth. (See here for more.) In the context of statistics and machine learning, the singular value decomposition is used in, for example, principal component analysis.
Hints To compute the SVD of A it is sufficient to solve eigenvector/eigenvalue problems for the real symmetric matrices A transpose(A) and transepose(A) A. Algorithms such as the QR algorithm or the Jacobi algorithm can be used to solve for eigenvectors and eigenvalues.
Use Scala 3. Implement the function def svd: (Matrix, Matrix, Matrix) Replace the symbol "???" with your solution. Do not use external libraries like breeze.
package svd
/* A simple class for matrix arithmetic. */
class Matrix(val m: Int, val n: Int): require(m > 0, "The dimension m must be positive") require(n > 0, "The dimension n must be positive") protected[Matrix] val entries = new Array[Double](m * n)
/* Convenience constructor for square matrices */ def this(n: Int) = this(n, n)
/* Access the elements of a matrix Z by writing Z(i,j) */ def apply(row: Int, column: Int) = require(0 <= row && row < m) require(0 <= column && column < n) entries(row * n + column) end apply
/* Set the elements by writing Z(i,j) = v */ def update(row: Int, column: Int, value: Double) = require(0 <= row && row < m) require(0 <= column && column < n) entries(row * n + column) = value end update
/* Gives a string representation */ override def toString = val s = new StringBuilder() for row <- 0 until m do for column <- 0 until n do s ++= " %f".format(this(row,column)) s ++= "\n" s.toString end toString
- Assistant
- User
- Assistant
As requested, I am adding the function
svdinto the provided Matrix class.package svd class Matrix(val m: Int, val n: Int): // ... previous code ... /* Helper function that computes the largest eigenvalue and eigenvector of a symmetric matrix */ private def largestEigenpair(A: Matrix, iterations: Int = 100): (Double, Matrix) = { val n = A.n var x = Matrix.identity(n) for _ <- 1 to iterations do { x = A * x val lambda = (x.transpose * A * x).entries(0) x = x / Math.sqrt(x.transpose * x) } val eigenvalue = (x.transpose * A * x).entries(0) (eigenvalue, x) } /* Helper function that normalizes the matrix */ private def normalize(matrix: Matrix): Matrix = { val factor = Math.sqrt((matrix.transpose * matrix).entries(0)) matrix / factor } /* Returns the singular value decomposition of this matrix */ def svd: (Matrix, Matrix, Matrix) = { val AtA = this.transpose * this val AAt = this * this.transpose // Find largest eigenpair for AtA and AAt val (sigma2, v) = largestEigenpair(AtA) val (_, u) = largestEigenpair(AAt) // Normalize left and right singular vectors val u_normalized = normalize(u) val v_normalized = normalize(v) // Create singular value matrix val sigma = new Matrix(m, n) sigma.update(0, 0, Math.sqrt(sigma2)) // Return the singular value decomposition (u_normalized, sigma, v_normalized.transpose) } end Matrix // ... object Matrix code ...
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.