Skip to content

Commit

Permalink
nope
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Nov 7, 2023
1 parent 26fadad commit 3233c14
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 51 deletions.
13 changes: 13 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,24 @@ ThisBuild / scalaVersion := "3.3.1"

lazy val root = tlCrossRootProject.aggregate(core, tests)

lazy val shim = crossProject(
JSPlatform,
JVMPlatform,
NativePlatform
).crossType(CrossType.Full)
.settings(
)
.jvmSettings(
)
.jsSettings()
.nativeSettings()

lazy val core = crossProject(
JSPlatform,
JVMPlatform,
NativePlatform
).crossType(CrossType.Full)
.dependsOn(shim)
.settings(
)
.jvmSettings(
Expand Down
10 changes: 7 additions & 3 deletions core/jvm/src/main/scala/conv.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package vecxt

import scala.language.implicitConversions
// import scala.language.implicitConversions


type vecxting = Array[Double] with vecxt

given Conversion[Array[Double], vecxting] with
def apply(in: Array[Double]): vecxting = in.asInstanceOf[vecxting]
// given Conversion[Array[Double], vecxting] with
// def apply(in: Array[Double]): vecxting = in.asInstanceOf[vecxting]


extension (inline a: Array[Double])
inline def vecxtable = a.asInstanceOf[vecxting]
4 changes: 1 addition & 3 deletions core/jvm/src/main/scala/main.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

import scala.language.implicitConversions

import vecxt.given_Conversion_Array_vecxting

import vecxt.*

@main def checkBytecode =
val a = Array[Double](1,2,3)
val a = Array[Double](1,2,3).vecxtable
val a1 = Array[Double](1,2,3)
// val b = Array[Boolean](true, false, true)
// val c = Array[Boolean](false, true, true)
Expand Down
15 changes: 3 additions & 12 deletions core/shared/src/main/scala/vecxt/array.extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ enum LossCalc:
case Agg, Occ
end LossCalc

transparent trait vecxt {

def update(i: Int, d: Double): Unit
def apply(i:Int) : Double
def length: Int

}


extension (vec: Array[Boolean])
inline def countTrue: Int =
var sum = 0
Expand Down Expand Up @@ -117,7 +108,7 @@ extension (vec: vecxt)
end while
end cumsum

inline def -(vec2: Array[Double])(using inline boundsCheck : BoundsCheck) =
transparent inline def -(vec2: Array[Double])(using inline boundsCheck : BoundsCheck) =
//dimCheck(vec, vec2)
val out = new Array[Double](vec.length)
var i = 0
Expand All @@ -136,7 +127,7 @@ extension (vec: vecxt)
end while
end -=

inline def +(vec2: Array[Double]) =
transparent inline def +(vec2: Array[Double]) =
val out = new Array[Double](vec.length)
var i = 0
while i < vec.length do
Expand Down Expand Up @@ -288,7 +279,7 @@ In excel f(x) = if(x < retention, 0, if(x > limit, limit, x)

end extension

extension (vec: Array[Array[Double]])
extension [A <: vecxt](vec: Array[A])
inline def horizontalSum: Array[Double] =
val out = new Array[Double](vec.head.length)
var i = 0
Expand Down
Binary file added sbt-launch.jar
Binary file not shown.
8 changes: 8 additions & 0 deletions shim/js/src/main/scala/conv.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package vecxt

import scala.scalajs.js.typedarray.Float64Array

type vecxting = Float64Array with vecxt

given Conversion[Float64Array, vecxting] with
def apply(in: Float64Array): vecxting = in.asInstanceOf[vecxting]
15 changes: 15 additions & 0 deletions shim/jvm/src/main/scala/conv.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package vecxt

// import scala.language.implicitConversions


type vecxting = Array[Double] with vecxt

// given Conversion[Array[Double], vecxting] with
// def apply(in: Array[Double]): vecxting = in.asInstanceOf[vecxting]

extension (inline a: Array[Double])
inline def vecxtable = a.asInstanceOf[vecxting]

// extension (inline a: vecxting)
// inline def vecxtable = a
13 changes: 13 additions & 0 deletions shim/jvm/src/main/scala/main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

import scala.language.implicitConversions

import vecxt.*

@main def checkBytecode =
val a = Array[Double](1,2,3).vecxtable
val a1 = Array[Double](1,2,3)
// val b = Array[Boolean](true, false, true)
// val c = Array[Boolean](false, true, true)

import vecxt.BoundsCheck.yes
a - a1
8 changes: 8 additions & 0 deletions shim/shared/src/main/scala/vecxt/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package vecxt

transparent trait vecxt {
def update(i: Int, d: Double): Unit
def apply(i:Int) : Double
def length: Int
}

50 changes: 28 additions & 22 deletions tests/shared/src/test/scala/arrayExtensions.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@
package vecxt

import scala.util.chaining.*
import scala.language.implicitConversions
import vecxt.given_Conversion_Array_vecxting
// import vecxt.given_Conversion_Array_vecxting

// type vecxting = Array[Double] with vecxt


extension (inline a: Array[Double])
inline def vecxtable = a.asInstanceOf[vecxting]

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

//import vecxt.given_Conversion_Array_vecxting
import vecxt.BoundsCheck.yes

lazy val v_fill = Array.tabulate(5)(i => i.toDouble)

test("Array horizontal sum") {
val v1 = Array[Double](1.0, 2.0, 3.0)
val v1 = Array[Double](1.0, 2.0, 3.0).vecxtable
val v2 = v1 * 2.0
val v3 = v1 * 3.0

val l = Array(v1, v2, v3)
val l = Array(v1, v2.vecxtable, v3.vecxtable)
val summed = l.horizontalSum

assert(summed(0) == 1 + 2 + 3)
Expand All @@ -42,14 +46,14 @@ class ArrayExtensionSuite extends munit.FunSuite:
}

test("cumsum") {
val v1 = Array[Double](1.0, 2.0, 3.0).tap(_.cumsum)
val v1 = Array[Double](1.0, 2.0, 3.0).tap(_.vecxtable.cumsum)
assert(v1(0) == 1)
assert(v1(1) == 3)
assert(v1(2) == 6)
}

test("Array += ") {
val v1 = Array[Double](1.0, 2.0, 3.0)
val v1 = Array[Double](1.0, 2.0, 3.0).vecxtable
v1 += Array[Double](3.0, 2.0, 1.0)

assertEqualsDouble(v1(0), 4, 0.00001)
Expand All @@ -59,7 +63,7 @@ class ArrayExtensionSuite extends munit.FunSuite:
}

test("Array + ") {
val v1 = Array[Double](1.0, 2.0, 3.0)
val v1 = Array[Double](1.0, 2.0, 3.0).vecxtable
val v2 = Array[Double](3.0, 2.0, 1.0)

val v3 = v1 + v2
Expand All @@ -71,7 +75,7 @@ class ArrayExtensionSuite extends munit.FunSuite:
}

test("Array -= ") {
val v1 = Array[Double](1.0, 2.0, 3.0)
val v1 = Array[Double](1.0, 2.0, 3.0).vecxtable
v1 -= Array[Double](3.0, 2.0, 1.0)

assertEqualsDouble(v1(0), -2, 0.00001)
Expand All @@ -81,7 +85,7 @@ class ArrayExtensionSuite extends munit.FunSuite:
}

test("Array - ") {
val v1 = Array[Double](1.0, 2.0, 3.0)
val v1 = Array[Double](1.0, 2.0, 3.0).vecxtable
val v2 = Array[Double](3.0, 2.0, 1.0)

val v3 = v1 - v2
Expand All @@ -92,8 +96,10 @@ class ArrayExtensionSuite extends munit.FunSuite:
}

test("Array *= ") {
val v1 = Array[Double](1.0, 2.0, 3.0)
v1 *= 2

val v1: vecxting = Array[Double](1.0, 2.0, 3.0).vecxtable

v1 *= 2.0

assertEqualsDouble(v1(0), 2, 0.00001)
assertEqualsDouble(v1(1), 4, 0.00001)
Expand All @@ -102,7 +108,7 @@ class ArrayExtensionSuite extends munit.FunSuite:
}

test("Array * ") {
val v1 = Array[Double](1.0, 2.0, 3.0)
val v1 = Array[Double](1.0, 2.0, 3.0).vecxtable

val v2 = v1 * 2

Expand All @@ -112,40 +118,40 @@ class ArrayExtensionSuite extends munit.FunSuite:
}

test("<=") {
val v_idx2 = v_fill < 2.5
val v_idx2 = v_fill.vecxtable < 2.5
assertEquals(v_idx2.countTrue, 3)
}

test("<") {
val v_idx2 = v_fill < 3.0
val v_idx2 = v_fill.vecxtable < 3.0
assertEquals(v_idx2.countTrue, 3)
}

test(">") {
val v_idx2 = v_fill > 2.5
val v_idx2 = v_fill.vecxtable > 2.5
assertEquals(v_idx2.countTrue, 2)
}

test(">=") {
val v_idx2 = v_fill >= 3.0
val v_idx2 = v_fill.vecxtable >= 3.0
assertEquals(v_idx2.countTrue, 2)
}

test("&&") {
val v_idx2 = (v_fill < 3.0) && (v_fill > 1.0)
val v_idx2 = (v_fill.vecxtable < 3.0) && (v_fill.vecxtable > 1.0)
assertEquals(v_idx2.countTrue, 1)
}

test("||") {
val v_idx2 = (v_fill < 3.0) || (v_fill > 1.0)
val v_idx2 = (v_fill.vecxtable < 3.0) || (v_fill.vecxtable > 1.0)
assertEquals(v_idx2.countTrue, 5)

val v_idx3 = (v_fill < 1.0) || (v_fill > 4.0)
val v_idx3 = (v_fill.vecxtable < 1.0) || (v_fill.vecxtable > 4.0)
assertEquals(v_idx3.countTrue, 1)
}

test("Array indexing") {
val v1 = Array[Double](1.0, 2.0, 3.0)
val v1 = Array[Double](1.0, 2.0, 3.0).vecxtable
val vIdx = Array[Boolean](true, false, true)
val afterIndex = v1.idxBoolean(vIdx)

Expand Down
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/boundsCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class BoundsCheckSuite extends munit.FunSuite:
lazy val v_fill = Array.tabulate(5)(i => i.toDouble)

test("Bounds check") {
intercept[vecxt.VectorDimensionMismatch](v_fill.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.yes))
intercept[vecxt.VectorDimensionMismatch](v_fill.vecxtable.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.yes))
}

test("no bound check") {
intercept[java.lang.ArrayIndexOutOfBoundsException](v_fill.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.no))
intercept[java.lang.ArrayIndexOutOfBoundsException](v_fill.vecxtable.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.no))
}

end BoundsCheckSuite
18 changes: 9 additions & 9 deletions tests/shared/src/test/scala/rpt.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,67 @@ package vecxt

import Retentions.*
import Limits.*
import scala.language.implicitConversions


extension [A <: AnyRef](o: A) def some = Some(o)

class ReinsurancePricingSuite extends munit.FunSuite:

test("reinsurance function - ret and limit") {
val v = Array[Double](8, 11, 16)
val v = Array[Double](8, 11, 16).vecxtable
v.reinsuranceFunction(Some(Limit(5.0)), Some(Retention(10.0)))
assert(v(0) == 0.0)
assert(v(1) == 1.0)
assert(v(2) == 5.0)
}
test("reinsurance function - ret only") {
val v = Array[Double](8, 11, 16)
val v = Array[Double](8, 11, 16).vecxtable
v.reinsuranceFunction(None, Some(Retention(10.0)))
assert(v(0) == 0.0)
assert(v(1) == 1.0)
assert(v(2) == 6.0)
}
test("reinsurance function - limit only") {
val v = Array[Double](8, 11, 16)
val v = Array[Double](8, 11, 16).vecxtable
v.reinsuranceFunction(Some(Limit(15.0)), None)
assert(v(0) == 8.0)
assert(v(1) == 11.0)
assert(v(2) == 15.0)
}
test("reinsurance function - no ret or limit") {
val v = Array[Double](8, 11, 16)
val v = Array[Double](8, 11, 16).vecxtable
v.reinsuranceFunction(None, None)
assert(v(0) == 8.0)
assert(v(1) == 11.0)
assert(v(2) == 16.0)
}

test("franchise function - ret and limit") {
val v = Array[Double](8, 11, 16)
val v = Array[Double](8, 11, 16).vecxtable
v.franchiseFunction(Some(Limit(5.0)), Some(Retention(10.0)))
assert(v(0) == 0.0)
assert(v(1) == 11.0)
assert(v(2) == 15.0)
}

test("franchise function - ret only") {
val v = Array[Double](8, 11, 16)
val v = Array[Double](8, 11, 16).vecxtable
v.franchiseFunction(None, Some(Retention(10.0)))
assert(v(0) == 0.0)
assert(v(1) == 11.0)
assert(v(2) == 16.0)
}

test("franchise function - Limit only") {
val v = Array[Double](8, 11, 16)
val v = Array[Double](8, 11, 16).vecxtable
v.franchiseFunction(Some(Limit(10.0)), None)
assert(v(0) == 8.0)
assert(v(1) == 10.0)
assert(v(2) == 10.0)
}

test("franchise function - No ret or limit") {
val v = Array[Double](8, 11, 16)
val v = Array[Double](8, 11, 16).vecxtable
v.franchiseFunction(None, None)
assert(v(0) == 8.0)
assert(v(1) == 11.0)
Expand Down

0 comments on commit 3233c14

Please sign in to comment.