From 2657ecf92e31c48ce33831311efd7e89f763bf75 Mon Sep 17 00:00:00 2001 From: partens Date: Thu, 25 Jul 2024 17:30:07 +0200 Subject: [PATCH] add tvar qdep --- .mill-version | 2 +- build.sc | 4 +- vecxt/src/package.scala | 4 ++ vecxt/src/tvar.scala | 54 +++++++++++++++++++++++ vecxt/test/src/arrayExtensions.test.scala | 51 +++++++++++++++++++-- 5 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 vecxt/src/package.scala create mode 100644 vecxt/src/tvar.scala diff --git a/.mill-version b/.mill-version index 12edb29..d5a2f50 100644 --- a/.mill-version +++ b/.mill-version @@ -1 +1 @@ -0.11.7 \ No newline at end of file +0.11.10 \ No newline at end of file diff --git a/build.sc b/build.sc index f16065c..621e315 100644 --- a/build.sc +++ b/build.sc @@ -1,6 +1,6 @@ import $ivy.`com.github.lolgab::mill-crossplatform::0.2.4` -import $ivy.`io.github.quafadas::millSite::0.0.22` +import $ivy.`io.github.quafadas::millSite::0.0.24` import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.4.0` import de.tobiasroeser.mill.vcs.version._ @@ -41,7 +41,7 @@ trait Common extends ScalaModule with PublishModule { val vecIncubatorFlag = Seq("""--add-modules=jdk.incubator.vector""") trait CommonJS extends ScalaJSModule { def scalaJSVersion = "1.15.0" - + // def ivyDeps = super.ivyDeps() ++ Seq(ivy"com.raquo::ew::0.2.0") // def moduleKind = ModuleKind. } diff --git a/vecxt/src/package.scala b/vecxt/src/package.scala new file mode 100644 index 0000000..ecd1659 --- /dev/null +++ b/vecxt/src/package.scala @@ -0,0 +1,4 @@ +// package vecxt + +// export vecxt.rpt.tVar +// export vecxt.rpt.qdep \ No newline at end of file diff --git a/vecxt/src/tvar.scala b/vecxt/src/tvar.scala new file mode 100644 index 0000000..a512de7 --- /dev/null +++ b/vecxt/src/tvar.scala @@ -0,0 +1,54 @@ +package vecxt.rpt + +import narr.NArray +import narr.native.Extensions.sort + + +extension [N <: Int](thisVector: NArray[Double]) + + def qdep(alpha: Double, thatVector: NArray[Double]): Double = + val numYears = thisVector.length + val nte = numYears * (1.0 - alpha); + + val fte = Math.floor(nte).toInt; + + val order = Ordering.Double.TotalOrdering.reverse + + val (_, originalPositionThis) = thisVector.asInstanceOf[NArray[Double]].toVector.zipWithIndex.sortBy(_._1)(using order).unzip + val (_, originalPositionThat) = thatVector.asInstanceOf[NArray[Double]].toVector.zipWithIndex.sortBy(_._1)(using order).unzip + + val tailIdxThis = originalPositionThis.slice(0, fte).toSet + val tailIdxThat = originalPositionThat.slice(0, fte).toSet + + val sharedTail = tailIdxThis.intersect(tailIdxThat).size + sharedTail.toDouble / nte + end qdep + + def tVar(alpha: Double): Double = + val numYears = thisVector.length + val nte = numYears * (1.0 - alpha); + val fte = Math.floor(nte).toInt; + val sorted = thisVector.sort() + var i = 0 + var tailSum = 0.0; + while i < fte do + tailSum += sorted(i); + i += 1; + end while + tailSum / fte.toDouble; + end tVar + + def tVarIdx(alpha: Double): NArray[Boolean] = + val numYears = thisVector.length + val nte = numYears * (1.0 - alpha); + val fte = Math.floor(nte).toInt; + val sorted = thisVector.zipWithIndex.sortBy(_._1).map(_._2) + val idx = NArray.fill[Boolean](numYears)(false) + var i = 0 + while i < fte do + idx(sorted(i)) = true; + i = i + 1 + end while + idx + + end tVarIdx \ No newline at end of file diff --git a/vecxt/test/src/arrayExtensions.test.scala b/vecxt/test/src/arrayExtensions.test.scala index 3af51d2..131c513 100644 --- a/vecxt/test/src/arrayExtensions.test.scala +++ b/vecxt/test/src/arrayExtensions.test.scala @@ -19,6 +19,7 @@ package vecxt import narr.* import scala.util.chaining.* + class ArrayExtensionSuite extends munit.FunSuite: import BoundsCheck.DoBoundsCheck.yes @@ -157,9 +158,51 @@ class ArrayExtensionSuite extends munit.FunSuite: assertEqualsDouble(afterIndex.last, 3.0, 0.0001) } - // test("max element"){ - // val v2 = NArray[Double](3.0, 2.0, 4.0, 1.0) - // assertEqualsDouble(v2.max, 4.0, 0.00001) - // } + test("tvar") { + import vecxt.rpt.tVar + val v1 = NArray.tabulate(100)(_.toDouble) + val tvar = v1.tVar(0.95) + assertEqualsDouble(tvar, 2, 0.0001) + } + + test("qdep".only) { + import vecxt.rpt.qdep + val v1 = NArray.tabulate(100)(_.toDouble) + val v2 = v1 * 2 + val qdep = v1.qdep(0.95, v2) + assertEqualsDouble(qdep, 1, 0.0001) + + val v3 = v1.clone + v3(1) = 100 + assertEqualsDouble(v1.qdep(0.95, v3), 0.8, 0.0001) + } + + test("tvar index".only) { + import vecxt.rpt.tVarIdx + val v1 = NArray.tabulate(100)(_.toDouble) + val tvar = v1.tVarIdx(0.95) + assertEquals(tvar.countTrue, 5) + for i <- 0 until 5 do + assert(tvar(i)) + end for + for(i <- 5 until 100) do + assert(!tvar(i)) + end for + } + + test("tvar idx 2".only) { + import vecxt.rpt.tVarIdx + val v1 = NArray.tabulate(100)(_.toDouble).reverse + val tvar = v1.tVarIdx(0.95) + assertEquals(tvar.countTrue, 5) + for i <- 0 until 95 do + assert(!tvar(i)) + end for + for(i <- 96 until 100) do + assert(tvar(i)) + end for + } + + end ArrayExtensionSuite