Skip to content

Commit

Permalink
add tvar qdep
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Jul 25, 2024
1 parent b10f4ca commit 2657ecf
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .mill-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.7
0.11.10
4 changes: 2 additions & 2 deletions build.sc
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -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.
}

Expand Down
4 changes: 4 additions & 0 deletions vecxt/src/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// package vecxt

// export vecxt.rpt.tVar
// export vecxt.rpt.qdep
54 changes: 54 additions & 0 deletions vecxt/src/tvar.scala
Original file line number Diff line number Diff line change
@@ -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
51 changes: 47 additions & 4 deletions vecxt/test/src/arrayExtensions.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package vecxt
import narr.*
import scala.util.chaining.*


class ArrayExtensionSuite extends munit.FunSuite:
import BoundsCheck.DoBoundsCheck.yes

Expand Down Expand Up @@ -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

0 comments on commit 2657ecf

Please sign in to comment.