From db4a8625339a1327d1311b115d34fe3df151668b Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 6 Nov 2023 14:58:55 +0100 Subject: [PATCH] formatting --- .../main/scala/vecxt/array.extensions.scala | 45 +++++++++---------- .../src/main/scala/vecxt/retention.rpt.scala | 3 +- tests/shared/src/test/scala/array.tests.scala | 1 + .../src/test/scala/arrayExtensions.test.scala | 43 +++++++++--------- tests/shared/src/test/scala/makeLayer.scala | 1 - 5 files changed, 44 insertions(+), 49 deletions(-) diff --git a/core/shared/src/main/scala/vecxt/array.extensions.scala b/core/shared/src/main/scala/vecxt/array.extensions.scala index 17d9df9..3143a8d 100644 --- a/core/shared/src/main/scala/vecxt/array.extensions.scala +++ b/core/shared/src/main/scala/vecxt/array.extensions.scala @@ -24,7 +24,6 @@ enum LossCalc: case Agg, Occ end LossCalc - extension (vec: Array[Boolean]) inline def countTrue: Int = var sum = 0 @@ -57,12 +56,12 @@ end extension extension (vec: Array[Double]) - def idx(index : Array[Boolean]) = + def idx(index: Array[Boolean]) = val trues = index.countTrue val newVec = new Array[Double](trues) var j = 0 for i <- 0 to trues do - //println(s"i: $i || j: $j || ${index(i)} ${vec(i)} ") + // println(s"i: $i || j: $j || ${index(i)} ${vec(i)} ") if index(i) then newVec(j) = vec(i) j += 1 @@ -81,23 +80,24 @@ extension (vec: Array[Double]) out end increments - inline def stdDev: Double = { - // https://www.cuemath.com/data/standard-deviation/ + inline def stdDev: Double = + // https://www.cuemath.com/data/standard-deviation/ val mu = vec.mean - val diffs_2 = vec.map( num => Math.pow(num - mu, 2) ) - Math.sqrt( diffs_2.sum / (vec.length - 1 ) ) - } + val diffs_2 = vec.map(num => Math.pow(num - mu, 2)) + Math.sqrt(diffs_2.sum / (vec.length - 1)) + end stdDev inline def mean: Double = vec.sum / vec.length - inline def sum: Double = { + inline def sum: Double = var sum = 0.0 - var i = 0; while (i < vec.length) { + var i = 0; + while i < vec.length do sum = sum + vec(i) i = i + 1 - } + end while sum - } + end sum def cumsum = var i = 1 @@ -107,7 +107,7 @@ extension (vec: Array[Double]) end while end cumsum - def - (vec2: Array[Double]) = + def -(vec2: Array[Double]) = val out = new Array[Double](vec.length) var i = 0 while i < vec.length do @@ -117,7 +117,7 @@ extension (vec: Array[Double]) out end - - def -= (vec2: Array[Double]) : Unit = + def -=(vec2: Array[Double]): Unit = var i = 0 while i < vec.length do vec(i) = vec(i) - vec2(i) @@ -125,7 +125,7 @@ extension (vec: Array[Double]) end while end -= - def + (vec2: Array[Double]) = + def +(vec2: Array[Double]) = val out = new Array[Double](vec.length) var i = 0 while i < vec.length do @@ -135,7 +135,7 @@ extension (vec: Array[Double]) out end + - def += (vec2: Array[Double]) : Unit = + def +=(vec2: Array[Double]): Unit = var i = 0 while i < vec.length do vec(i) = vec(i) + vec2(i) @@ -143,7 +143,7 @@ extension (vec: Array[Double]) end while end += - def *= (d: Double) = + def *=(d: Double) = var i = 0 while i < vec.length do vec(i) = vec(i) * d @@ -152,7 +152,7 @@ extension (vec: Array[Double]) vec end *= - def * (d: Double) = + def *(d: Double) = val out = new Array[Double](vec.length) var i = 0 while i < vec.length do @@ -188,7 +188,6 @@ extension (vec: Array[Double]) idx end logicalIdx - /* Retention and limit are known constants @@ -197,7 +196,7 @@ In excel f(x) = min(max(x - retention, 0), limit)) The implementation takes advantage of their existence or not, to optimise the number of operations required. - */ + */ inline def reinsuranceFunction(limitOpt: Option[Limit], retentionOpt: Option[Retention]): Unit = (limitOpt, retentionOpt) match case (Some(limit), Some(retention)) => @@ -239,7 +238,7 @@ Retention and limit are known constants In excel f(x) = if(x < retention, 0, if(x > limit, limit, x) - */ + */ inline def franchiseFunction(inline limitOpt: Option[Limit], inline retentionOpt: Option[Retention]): Unit = (limitOpt, retentionOpt) match case (None, None) => () @@ -287,11 +286,11 @@ extension (vec: Array[Array[Double]]) var j = 0 while j < vec.length do sum += vec(j)(i) - //pprint.pprintln(s"j : $j i : $i vecij : ${vec(j)(i)} out : ${out(i)} sum : $sum") + // pprint.pprintln(s"j : $j i : $i vecij : ${vec(j)(i)} out : ${out(i)} sum : $sum") j = j + 1 end while out(i) = sum i = i + 1 end while out -end extension \ No newline at end of file +end extension diff --git a/core/shared/src/main/scala/vecxt/retention.rpt.scala b/core/shared/src/main/scala/vecxt/retention.rpt.scala index ff335d2..53d68fd 100644 --- a/core/shared/src/main/scala/vecxt/retention.rpt.scala +++ b/core/shared/src/main/scala/vecxt/retention.rpt.scala @@ -16,7 +16,6 @@ package vexct - object Retentions: opaque type Retention = Double @@ -30,4 +29,4 @@ object Retentions: inline def -(l: Retention): Double = loss - l inline def <(l: Retention): Boolean = loss < l end extension -end Retentions \ No newline at end of file +end Retentions diff --git a/tests/shared/src/test/scala/array.tests.scala b/tests/shared/src/test/scala/array.tests.scala index e69de29..8b13789 100644 --- a/tests/shared/src/test/scala/array.tests.scala +++ b/tests/shared/src/test/scala/array.tests.scala @@ -0,0 +1 @@ + diff --git a/tests/shared/src/test/scala/arrayExtensions.test.scala b/tests/shared/src/test/scala/arrayExtensions.test.scala index dea0611..7d58b37 100644 --- a/tests/shared/src/test/scala/arrayExtensions.test.scala +++ b/tests/shared/src/test/scala/arrayExtensions.test.scala @@ -16,10 +16,8 @@ package vexct - import scala.util.chaining.* - class ArrayExtensionSuite extends munit.FunSuite: lazy val v_fill = Array.tabulate(5)(i => i.toDouble) @@ -49,9 +47,9 @@ class ArrayExtensionSuite extends munit.FunSuite: val v1 = Array[Double](1.0, 2.0, 3.0) v1 += Array[Double](3.0, 2.0, 1.0) - assertEqualsDouble(v1(0) , 4, 0.00001) - assertEqualsDouble(v1(1) , 4, 0.00001) - assertEqualsDouble(v1(2) , 4, 0.00001) + assertEqualsDouble(v1(0), 4, 0.00001) + assertEqualsDouble(v1(1), 4, 0.00001) + assertEqualsDouble(v1(2), 4, 0.00001) } @@ -61,19 +59,19 @@ class ArrayExtensionSuite extends munit.FunSuite: val v3 = v1 + v2 - assertEqualsDouble(v3(0) , 4, 0.00001) - assertEqualsDouble(v3(1) , 4, 0.00001) - assertEqualsDouble(v3(2) , 4, 0.00001) + assertEqualsDouble(v3(0), 4, 0.00001) + assertEqualsDouble(v3(1), 4, 0.00001) + assertEqualsDouble(v3(2), 4, 0.00001) } - test("Array -= ") { + test("Array -= ") { val v1 = Array[Double](1.0, 2.0, 3.0) v1 -= Array[Double](3.0, 2.0, 1.0) - assertEqualsDouble(v1(0) , -2, 0.00001) - assertEqualsDouble(v1(1) , 0, 0.00001) - assertEqualsDouble(v1(2) , 2, 0.00001) + assertEqualsDouble(v1(0), -2, 0.00001) + assertEqualsDouble(v1(1), 0, 0.00001) + assertEqualsDouble(v1(2), 2, 0.00001) } @@ -83,18 +81,18 @@ class ArrayExtensionSuite extends munit.FunSuite: val v3 = v1 - v2 - assertEqualsDouble(v3(0) , -2, 0.00001) - assertEqualsDouble(v3(1) , 0, 0.00001) - assertEqualsDouble(v3(2) , 2, 0.00001) + assertEqualsDouble(v3(0), -2, 0.00001) + assertEqualsDouble(v3(1), 0, 0.00001) + assertEqualsDouble(v3(2), 2, 0.00001) } test("Array *= ") { val v1 = Array[Double](1.0, 2.0, 3.0) v1 *= 2 - assertEqualsDouble(v1(0) , 2, 0.00001) - assertEqualsDouble(v1(1) , 4, 0.00001) - assertEqualsDouble(v1(2) , 6, 0.00001) + assertEqualsDouble(v1(0), 2, 0.00001) + assertEqualsDouble(v1(1), 4, 0.00001) + assertEqualsDouble(v1(2), 6, 0.00001) } @@ -103,12 +101,12 @@ class ArrayExtensionSuite extends munit.FunSuite: val v2 = v1 * 2 - assertEqualsDouble(v1(0) , 1, 0.00001) - assertEqualsDouble(v2(1) , 4, 0.00001) - assertEqualsDouble(v2(2) , 6, 0.00001) + assertEqualsDouble(v1(0), 1, 0.00001) + assertEqualsDouble(v2(1), 4, 0.00001) + assertEqualsDouble(v2(2), 6, 0.00001) } - test("<=") { + test("<=") { val v_idx2 = v_fill < 2.5 assertEquals(v_idx2.countTrue, 3) } @@ -141,7 +139,6 @@ class ArrayExtensionSuite extends munit.FunSuite: assertEquals(v_idx3.countTrue, 1) } - test("Array indexing") { val v1 = Array[Double](1.0, 2.0, 3.0) val vIdx = Array[Boolean](true, false, true) diff --git a/tests/shared/src/test/scala/makeLayer.scala b/tests/shared/src/test/scala/makeLayer.scala index dde59bc..5730fe3 100644 --- a/tests/shared/src/test/scala/makeLayer.scala +++ b/tests/shared/src/test/scala/makeLayer.scala @@ -13,4 +13,3 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -