From fcad65fc51673898054cc31db1a79dcb3718b6aa Mon Sep 17 00:00:00 2001 From: nasr Date: Tue, 25 Nov 2025 23:28:17 +0100 Subject: [PATCH] feature: cholesky decompostiion finised i said finished but i get an out of bounds exception lololol --- .../nsrddyn/FPU/CholeskyDecomposition.scala | 37 ++++++++----------- .../Tests/CholeskyDecompositionTest.scala | 5 ++- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/main/scala/com/nsrddyn/FPU/CholeskyDecomposition.scala b/src/main/scala/com/nsrddyn/FPU/CholeskyDecomposition.scala index 80a1778..895473a 100644 --- a/src/main/scala/com/nsrddyn/FPU/CholeskyDecomposition.scala +++ b/src/main/scala/com/nsrddyn/FPU/CholeskyDecomposition.scala @@ -23,31 +23,24 @@ class CholeskyDecomposition { * This is true because of the special case of A being a square, conjugate symmetric matrix. */ - def run(matrix: List[List[Int]]): Unit = { + def run(matrix: Vector[Vector[Int]]): Unit = { - val n: Int = matrix.size + val size: Int = matrix.size + val lower: ArrayBuffer[ArrayBuffer[Int]] = ArrayBuffer[ArrayBuffer[Int]]() - // store the lower triangular matrix - val lower = Vector[Vector[Int]]() + for + i <- 0 to size + j <- 0 until i + do + if i == j then lower(i)(j) = getSquaredSummation(lower, i, j, matrix) else lower(j)(j) = getReversedSummation(lower, i, j, matrix) - - for (i <- 0 until n) - { - for (j <- 0 until i) - var sum: Double = 0 - - if j == i then - sum += math.pow(lowerBuffer(i)(j), 2) - - end if - lower(i)(j) = (sqrt(matrix(i)(j))()) - - j += 1 - } - i += 1 } - - def (matrix: Vector[Vector[Int]], index: int, jindex: int ): Int = if j == 1 then return math.pow(matrix(index)(jindex)) else - + private def getReversedSummation(lower: ArrayBuffer[ArrayBuffer[Int]], i: Int, j: Int, matrix: Vector[Vector[Int]]) = { + math.sqrt(matrix(j)(j) - (0 until j).map { k => lower(i)(k) * lower(j)(k) }.sum).toInt + } + private def getSquaredSummation(lower: ArrayBuffer[ArrayBuffer[Int]], i: Int, j: Int, matrix: Vector[Vector[Int]]) = { + ((matrix(i)(j) - (0 until j).map { k => math.pow(lower(j)(k), 2)}.sum) / lower(j)(j)).toInt + } } + diff --git a/src/main/scala/com/nsrddyn/Tests/CholeskyDecompositionTest.scala b/src/main/scala/com/nsrddyn/Tests/CholeskyDecompositionTest.scala index 340caa3..8361547 100644 --- a/src/main/scala/com/nsrddyn/Tests/CholeskyDecompositionTest.scala +++ b/src/main/scala/com/nsrddyn/Tests/CholeskyDecompositionTest.scala @@ -8,9 +8,12 @@ class CholeskyDecompositionTest extends CholeskyDecomposition { def test(): Unit = { val cdp: CholeskyDecomposition = new CholeskyDecomposition - val matrix: List[List[Int]] = List.empty[List[Int]] + val matrix: Vector[Vector[Int]] = Vector(Vector(1,2,3),Vector(1,2,3),Vector(1,2,3)) println(cdp.run(matrix)) } + + + }