Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Nov 6, 2023
1 parent b09e9a7 commit db4a862
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 49 deletions.
45 changes: 22 additions & 23 deletions core/shared/src/main/scala/vecxt/array.extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ enum LossCalc:
case Agg, Occ
end LossCalc


extension (vec: Array[Boolean])
inline def countTrue: Int =
var sum = 0
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -117,15 +117,15 @@ 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)
i = i + 1
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
Expand All @@ -135,15 +135,15 @@ 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)
i = i + 1
end while
end +=

def *= (d: Double) =
def *=(d: Double) =
var i = 0
while i < vec.length do
vec(i) = vec(i) * d
Expand All @@ -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
Expand Down Expand Up @@ -188,7 +188,6 @@ extension (vec: Array[Double])
idx
end logicalIdx


/*
Retention and limit are known constants
Expand All @@ -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)) =>
Expand Down Expand Up @@ -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) => ()
Expand Down Expand Up @@ -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
end extension
3 changes: 1 addition & 2 deletions core/shared/src/main/scala/vecxt/retention.rpt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package vexct


object Retentions:
opaque type Retention = Double

Expand All @@ -30,4 +29,4 @@ object Retentions:
inline def -(l: Retention): Double = loss - l
inline def <(l: Retention): Boolean = loss < l
end extension
end Retentions
end Retentions
1 change: 1 addition & 0 deletions tests/shared/src/test/scala/array.tests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

43 changes: 20 additions & 23 deletions tests/shared/src/test/scala/arrayExtensions.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

}

Expand All @@ -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)

}

Expand All @@ -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)

}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion tests/shared/src/test/scala/makeLayer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

0 comments on commit db4a862

Please sign in to comment.