Skip to content

Commit

Permalink
Merge pull request #11 from Quafadas/higher_kinded
Browse files Browse the repository at this point in the history
Higher kinded
  • Loading branch information
Quafadas authored Nov 20, 2024
2 parents f80ffe7 + 67224ff commit f25224d
Show file tree
Hide file tree
Showing 32 changed files with 901 additions and 708 deletions.
1 change: 1 addition & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It can be confusing, why certain methods are placed in certain objects. It boils down to constraints and keeping the compiler happy.
2 changes: 1 addition & 1 deletion site/docs/_docs/matrix.mdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ val result2 = mat1 @@ mat2
result2.printMat

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

result3.printMat

Expand Down
2 changes: 1 addition & 1 deletion site/docs/_docs/xPlatform.mdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import vecxtensions.MathTagsLaminar.*
import vecxt.BoundsCheck.DoBoundsCheck.yes

val base = NArray[Double](11, 12, 13, 14, 15)
val mat1 = Matrix.fromRows(
val mat1 = Matrix.fromRows[Double](
NArray(
base,
base +:+ 10.0,
Expand Down
3 changes: 2 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
- [x] reusable plot elements
- [x] via named tuples and upickle?
- [x] unit testing
- [ ] Switch to named tuples for the Matrix type
- [ ] Make matrix high dimensional
- [ ] Test with spire
- [ ] Switch to named tuples for the Matrix type

- [ ] Beautiful display in scala JS through MathMl
- [ ] PR to scala JS dom in progress
- [ ] PR to scalal DOM types
Expand Down
Empty file removed vecxt/js-native/package.scala
Empty file.
53 changes: 53 additions & 0 deletions vecxt/js-native/src/array.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package vecxt

import narr.*
import vecxt.matrix.Matrix

// These use project panama (SIMD) on the JVM, so need own JS native implementation
object JsNativeDoubleArrays:

extension (m: Matrix[Double])
inline def >=(d: Double): Matrix[Boolean] =
Matrix[Boolean](m.raw >= d, m.shape)(using BoundsCheck.DoBoundsCheck.no)

inline def >(d: Double): Matrix[Boolean] =
Matrix[Boolean](m.raw > d, m.shape)(using BoundsCheck.DoBoundsCheck.no)

inline def <=(d: Double): Matrix[Boolean] =
Matrix[Boolean](m.raw <= d, m.shape)(using BoundsCheck.DoBoundsCheck.no)

inline def <(d: Double): Matrix[Boolean] =
Matrix[Boolean](m.raw < d, m.shape)(using BoundsCheck.DoBoundsCheck.no)
end extension

extension (vec: NArray[Double])

inline def <(num: Double): NArray[Boolean] =
logicalIdx((a, b) => a < b, num)

inline def <=(num: Double): NArray[Boolean] =
logicalIdx((a, b) => a <= b, num)

inline def >(num: Double): NArray[Boolean] =
logicalIdx((a, b) => a > b, num)

inline def >=(num: Double): NArray[Boolean] =
logicalIdx((a, b) => a >= b, num)

inline def logicalIdx(
inline op: (Double, Double) => Boolean,
inline num: Double
): NArray[Boolean] =
val n = vec.length
val idx = NArray.fill(n)(false)

var i = 0
while i < n do
if op(vec(i), num) then idx(i) = true
end if
i = i + 1
end while
idx
end logicalIdx
end extension
end JsNativeDoubleArrays
30 changes: 1 addition & 29 deletions vecxt/js/src/array.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import narr.*
import vecxt.BoundsCheck.BoundsCheck

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

object arrays:
Expand Down Expand Up @@ -281,34 +281,6 @@ object arrays:
vec.nativeSlice().tap(_ /= d)
end /

inline def <(num: Double): js.Array[Boolean] =
logicalIdx((a, b) => a < b, num)

inline def <=(num: Double): js.Array[Boolean] =
logicalIdx((a, b) => a <= b, num)

inline def >(num: Double): js.Array[Boolean] =
logicalIdx((a, b) => a > b, num)

inline def >=(num: Double): js.Array[Boolean] =
logicalIdx((a, b) => a >= b, num)

inline def logicalIdx(
inline op: (Double, Double) => Boolean,
inline num: Double
): js.Array[Boolean] =
val n = vec.length
val idx = new js.Array[Boolean](n).tap(_.fill(false))

var i = 0
while i < n do
if op(vec(i), num) then idx(i) = true
end if
i = i + 1
end while
idx
end logicalIdx

def covariance(thatVector: Float64Array): Double =
val μThis = vec.mean
val μThat = thatVector.mean
Expand Down
63 changes: 63 additions & 0 deletions vecxt/js/src/doublematrix.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package vecxt

import narr.*
import matrix.*
import vecxt.MatrixHelper.*
import vecxt.MatrixInstance.*
import scala.scalajs.js.typedarray.Float64Array
import vecxt.BoundsCheck.BoundsCheck
import vecxt.arrays.*
import vecxt.rangeExtender.MatrixRange.range
import vecxt.rangeExtender.MatrixRange.RangeExtender

object JsDoubleMatrix:

extension (m: Matrix[Double])

// inline def tupleFromIdx(b: Int)(using inline boundsCheck: BoundsCheck) =
// dimCheckLen(m.raw, b)
// (b / m.rows, b % m.rows)
// end tupleFromIdx

inline def +(m2: Matrix[Double])(using inline boundsCheck: BoundsCheck): Matrix[Double] =
sameDimMatCheck(m, m2)
val newArr: NArray[Double] = m.raw.add(m2.raw)
Matrix[Double](newArr, m.shape)(using BoundsCheck.DoBoundsCheck.no)
end +

inline def matmul(b: Matrix[Double])(using inline boundsCheck: BoundsCheck): Matrix[Double] =
dimMatCheck(m, b)
val newArr = Float64Array(m.rows * b.cols)
// Note, might need to deal with transpose later.
dgemm(
"column-major",
"no-transpose",
"no-transpose",
m.rows,
b.cols,
m.cols,
1.0,
m.raw,
m.rows,
b.raw,
b.rows,
1.0,
newArr,
m.rows
)
Matrix[Double](newArr, (m.rows, b.cols))
end matmul
end extension
end JsDoubleMatrix

object JvmDoubleMatrix:

end JvmDoubleMatrix

object NativeDoubleMatrix:

end NativeDoubleMatrix

object JvmNativeDoubleMatrix:

end JvmNativeDoubleMatrix
Loading

0 comments on commit f25224d

Please sign in to comment.