Skip to content

Commit

Permalink
massive refactor packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Sep 29, 2024
1 parent da0c35a commit a70a67a
Show file tree
Hide file tree
Showing 19 changed files with 208 additions and 232 deletions.
2 changes: 1 addition & 1 deletion .mill-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.12.0-RC2
0.11.12
2 changes: 1 addition & 1 deletion site/docs/_blog/_posts/2024-08-01-Motivation.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ For example if your data acquisition is serverside, but do parts of a calculatio

```scala mdoc

import vecxt.*
import vecxt.all.*
import narr.*
import vecxt.BoundsCheck.DoBoundsCheck.yes

Expand Down
12 changes: 6 additions & 6 deletions site/docs/_docs/bounds.check.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ It is left to the developer, to decide whether, or where BoundsChecks are desira
In this case, we disable bounds checks, maximising performance.

```scala
import vecxt._
import BoundsCheck.DoBoundsCheck.no
import vecxt.all.*
import vecxt.BoundsCheck.DoBoundsCheck.no

val v1 = Array[Double](1, 2, 3)
val v2 = Array[Double](4, 5, 6)
Expand All @@ -29,8 +29,8 @@ println(v1 + v2)
In this case, we disable bounds checks, maximising performance, and generate undefined runtime behaviour. It may fail, it may not, but the results will be unpredictable, wrong and potentially hard to track - it is _your_ responsibility.

```scala
import vecxt._
import BoundsCheck.DoBoundsCheck.no
import vecxt.all.*
import vecxt.BoundsCheck.DoBoundsCheck.no

val v1 = Array[Double](1, 2, 3, 7)
val v2 = Array[Double](4, 5, 6)
Expand Down Expand Up @@ -58,11 +58,11 @@ Finally, one may opt in, or out at any individual callsite, should it be desirab

```scala
import vecxt.all.*
import BoundsCheck.DoBoundsCheck.no
import vecxt.BoundsCheck.DoBoundsCheck.no

val v1 = Array[Double](1, 2, 3, 7)
val v2 = Array[Double](4, 5, 6)

println(v1.+(v2)(using BoundsCheck.DoBoundsCheck.yes) )
println(v1.+(v2)(using vecxt.BoundsCheck.DoBoundsCheck.yes) )

```
16 changes: 8 additions & 8 deletions site/docs/_docs/examples.mdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ v1.dot(v2)

cosineSimilarity(v1, v2)

(v1 + v2).print
(v1 + v2).printArr

(v1 - v2).print
(v1 - v2).printArr

(v1 * 2.0).print
(v1 * 2.0).printArr

(v1 / 2.0).print
(v1 / 2.0).printArr

(v1 > 2).print
(v1 >= 2).print
(v1 > 2).printArr
(v1 >= 2).printArr

(v1 < 2).print
(v1 <= 2).print
(v1 < 2).printArr
(v1 <= 2).printArr

```
4 changes: 2 additions & 2 deletions site/docs/_docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ ivy"io.github.quafadas::vecxt::{{projectVersion}}"
```

```scala
import vecxt._
import BoundsCheck.DoBoundsCheck.no
import vecxt.all.*
import vecxt.BoundsCheck.DoBoundsCheck.no

val v1 = Array[Double](1, 2, 3)
val v2 = Array[Double](4, 5, 6)
Expand Down
3 changes: 1 addition & 2 deletions site/docs/_docs/js.mdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ import com.raquo.laminar.DomApi

import narr.*

import vecxt.Matrix.*
import vecxt.extensions.*
import vecxt.all.*
import vecxt_extensions.MathTagsLaminar.*
import vecxt.BoundsCheck.DoBoundsCheck.yes

Expand Down
34 changes: 16 additions & 18 deletions site/docs/_docs/matrix.mdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ val matrix2 = Matrix.fromColumns(nestedArr)

matrix.shape

matrix.print
matrix.printMat

matrix2.print
matrix2.printMat

matrix.col(1).print
matrix.col(1).printArr

matrix.row(2).print
matrix.row(2).printArr

// Note that indexing is done via a tuple.
matrix((1, 2))
Expand All @@ -38,29 +38,28 @@ More matrix operations...

```scala mdoc:to-string

import vecxt.Matrix.*
import vecxt.all.*
import vecxt.BoundsCheck.DoBoundsCheck.yes
import narr.*
import vecxt.extensions.*

val mat1 = Matrix(NArray(1.0, 4.0, 2.0, 5.0, 3.0, 6.0), (2, 3))
val mat2 = Matrix(NArray(7.0, 9.0, 11.0, 8.0, 10, 12.0), (3, 2))
val result = mat1.matmul(mat2)

result.print
result.printMat

// @ is a reserved character, so we can't just copy numpy syntax... experimental
val result2 = mat1 @@ mat2

result2.print
result2.printMat

// opperator precedence...
val result3 = Matrix.eye(2) + mat1 @@ mat2

result3.print
result3.printMat

val mat3 = mat2.transpose + mat1
mat3.print
mat3.printMat

```

Expand All @@ -69,10 +68,9 @@ mat3.print
Index via a `Int`, `NArray[Int]` or a `Range` to slice a matrix. The `::` operator is used to select all elements in a dimension.

```scala mdoc:to-string
import vecxt.Matrix.*
import vecxt.all.*
import vecxt.BoundsCheck.DoBoundsCheck.yes
import narr.*
import vecxt.extensions.*

val mat = Matrix.fromRows(
NArray(
Expand All @@ -81,12 +79,12 @@ val mat = Matrix.fromRows(
NArray[Double](7.0, 8.0, 9.0)
)
)
mat(::, ::).print
mat(1, ::).print
mat(::, 1).print
mat(1, 1).print
mat(0 to 1, 0 to 1).print
mat(NArray.from[Int](Array(0, 2)), 0 to 1).print
mat(::, ::).printMat
mat(1, ::).printMat
mat(::, 1).printMat
mat(1, 1).printMat
mat(0 to 1, 0 to 1).printMat
mat(NArray.from[Int](Array(0, 2)), 0 to 1).printMat


```
5 changes: 5 additions & 0 deletions site/src/fake.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fake

object totally:
def s = ???
end totally
7 changes: 6 additions & 1 deletion vecxt/js/src/array.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import scala.util.chaining.*
import narr.*
import vecxt.BoundsCheck.BoundsCheck

object arrayUtil:
// extension [A](d: Array[A]) def print: String = d.mkString("[", ",", "]")
end arrayUtil

object arrays:
extension (d: Float64Array) def printArr: String = d.mkString("[", ",", "]")

extension (v: Float64Array)
inline def nativeSort(): Unit = v.asInstanceOf[TypedArrayFacade].sort()
Expand Down Expand Up @@ -77,7 +82,7 @@ object arrays:
// end copy
end extension

extension (vec: Float64Array)
extension (vec: NArray[Double])
inline def update(idx: Int, d: Double)(using inline boundsCheck: BoundsCheck.BoundsCheck): Unit =
indexCheck(vec, idx)
vec(idx) = d
Expand Down
6 changes: 6 additions & 0 deletions vecxt/js/src/dimCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ package vecxt
import scala.scalajs.js
import scala.scalajs.js.typedarray.Float64Array
import vecxt.BoundsCheck.BoundsCheck
import narr.*

protected[vecxt] object dimCheckLen:
inline def apply(a: Float64Array, b: Int)(using inline doCheck: BoundsCheck) =
inline if doCheck then if a.length != b then throw VectorDimensionMismatch(a.length, b)
end dimCheckLen

protected[vecxt] object indexCheck:
inline def apply[A](a: Float64Array, idx: Int)(using inline doCheck: BoundsCheck) =
Expand Down
71 changes: 14 additions & 57 deletions vecxt/js/src/matrix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,17 @@ object matrix:

inline def numel: Int = m._1.length

/** element retrieval
*/
inline def apply(b: Tuple2[Int, Int])(using inline boundsCheck: BoundsCheck) =
indexCheckMat(m, b)
val idx = b._1 * m._2._2 + b._2
m._1(idx)
end apply
inline def tupleFromIdx(b: Int)(using inline boundsCheck: BoundsCheck) =
val r: NArray[Double] = m.raw
dimCheckLen(r, b)
(b / m.rows, b % m.rows)
end tupleFromIdx

/** element update
*/
inline def update(loc: Tuple2[Int, Int], value: Double)(using inline boundsCheck: BoundsCheck) =
indexCheckMat(m, loc)
val idx = loc._1 * m._2._2 + loc._2
val idx = loc._2 * m.rows + loc._1
m._1(idx) = value
end update

Expand Down Expand Up @@ -171,11 +169,15 @@ object matrix:

end apply

inline def raw: NArray[Double] = m._1

inline def @@(b: Matrix)(using inline boundsCheck: BoundsCheck): Matrix = m.matmul(b)
/** element retrieval
*/
inline def apply(b: Tuple2[Int, Int])(using inline boundsCheck: BoundsCheck) =
indexCheckMat(m, b)
val idx = b._2 * m.rows + b._1
m._1(idx)
end apply

inline def *=(d: Double): Unit = m._1.multInPlace(d)
inline def raw: NArray[Double] = m._1

inline def +(m2: Matrix)(using inline boundsCheck: BoundsCheck): Matrix =
sameDimMatCheck(m, m2)
Expand All @@ -187,51 +189,6 @@ object matrix:

inline def cols: Int = m._2._2

inline def shape: String = s"${m.rows} x ${m.cols}"

inline def row(i: Int): NArray[Double] =
val result = new NArray[Double](m.cols)
val cols = m.cols
var j = 0
while j < m.cols do
result(j) = m._1(i + j * m.rows)
j += 1
end while
result
end row

inline def print: String =
val arrArr = for i <- 0 until m.rows yield m.row(i).mkString(" ")
arrArr.mkString("\n")
end print

inline def col(i: Int): NArray[Double] =
val result = new NArray[Double](m.rows)
val cols = m.cols
var j = 0
while j < m.rows do
result(j) = m._1(i * m.cols + j)
j += 1
end while
result
end col

inline def transpose: Matrix =
val newArr = NArray.ofSize[Double](m._1.length)
var i = 0
while i < m.cols do
var j = 0
while j < m.rows do
newArr(i * m.rows + j) = m._1(j * m.cols + i)
j += 1
end while
i += 1
end while
Matrix(newArr, (m.cols, m.rows))(using
BoundsCheck.DoBoundsCheck.no
) // we already have a valid matrix if we are transposing it, so this check is redundant if this method works as intended.
end transpose

inline def matmul(b: Matrix)(using inline boundsCheck: BoundsCheck): Matrix =
dimMatCheck(m, b)
val newArr = Float64Array(m.rows * b.cols)
Expand Down
6 changes: 6 additions & 0 deletions vecxt/jvm-native/src/array.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package vecxt

object arrayUtil:

extension [A](d: Array[A]) def printArr: String = d.mkString("[", ",", "]")
end arrayUtil
5 changes: 5 additions & 0 deletions vecxt/jvm-native/src/dimCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ package vecxt
import vecxt.matrix.*
import vecxt.BoundsCheck.BoundsCheck

protected[vecxt] object dimCheckLen:
inline def apply[A, B](a: Array[A], b: Int)(using inline doCheck: BoundsCheck) =
inline if doCheck then if a.length != b then throw VectorDimensionMismatch(a.length, b)
end dimCheckLen

protected[vecxt] object dimCheck:
inline def apply[A, B](a: Array[A], b: Array[B])(using inline doCheck: BoundsCheck) =
inline if doCheck then if a.length != b.length then throw VectorDimensionMismatch(a.length, b.length)
Expand Down
Loading

0 comments on commit a70a67a

Please sign in to comment.