Browse Source

Zilde sucks

master
Martijn 2 months ago
parent
commit
9a66352de5
23 changed files with 301 additions and 255 deletions
  1. +4
    -1
      src/main/kotlin/com/martmists/kapt/APLError.kt
  2. +8
    -11
      src/main/kotlin/com/martmists/kapt/FunctionRegistry.kt
  3. +1
    -2
      src/main/kotlin/com/martmists/kapt/Quad.kt
  4. +0
    -42
      src/main/kotlin/com/martmists/kapt/functions/_clone.kt
  5. +0
    -41
      src/main/kotlin/com/martmists/kapt/functions/conjugate.kt
  6. +0
    -41
      src/main/kotlin/com/martmists/kapt/functions/direction.kt
  7. +10
    -5
      src/main/kotlin/com/martmists/kapt/functions/dyadic/minus.kt
  8. +10
    -5
      src/main/kotlin/com/martmists/kapt/functions/dyadic/plus.kt
  9. +18
    -22
      src/main/kotlin/com/martmists/kapt/functions/dyadic/rho.kt
  10. +9
    -5
      src/main/kotlin/com/martmists/kapt/functions/dyadic/times.kt
  11. +17
    -17
      src/main/kotlin/com/martmists/kapt/functions/monadic/iota.kt
  12. +45
    -0
      src/main/kotlin/com/martmists/kapt/functions/monadic/minus.kt
  13. +45
    -0
      src/main/kotlin/com/martmists/kapt/functions/monadic/plus.kt
  14. +43
    -0
      src/main/kotlin/com/martmists/kapt/functions/monadic/rho.kt
  15. +44
    -0
      src/main/kotlin/com/martmists/kapt/functions/monadic/times.kt
  16. +0
    -42
      src/main/kotlin/com/martmists/kapt/functions/negate.kt
  17. +11
    -7
      src/main/kotlin/com/martmists/kapt/main.kt
  18. +2
    -2
      src/main/kotlin/com/martmists/kapt/types/APLChar.kt
  19. +2
    -0
      src/main/kotlin/com/martmists/kapt/types/APLFunction.kt
  20. +15
    -6
      src/main/kotlin/com/martmists/kapt/types/APLMatrix.kt
  21. +9
    -5
      src/main/kotlin/com/martmists/kapt/types/APLValue.kt
  22. +7
    -0
      src/main/kotlin/com/martmists/kapt/types/APLZilde.kt
  23. +1
    -1
      src/main/kotlin/com/martmists/kapt/util/ReversedStrides.kt

src/main/kotlin/com/martmists/kapt/types/APLError.kt → src/main/kotlin/com/martmists/kapt/APLError.kt View File

@@ -1,4 +1,7 @@
package com.martmists.kapt.types
package com.martmists.kapt

import com.martmists.kapt.types.APLMatrix
import com.martmists.kapt.types.APLValue

class APLError(err: String) : Exception(err) {
companion object {

+ 8
- 11
src/main/kotlin/com/martmists/kapt/FunctionRegistry.kt View File

@@ -1,6 +1,7 @@
package com.martmists.kapt

import com.martmists.kapt.functions.*
import com.martmists.kapt.functions.monadic.*
import com.martmists.kapt.functions.dyadic.*
import com.martmists.kapt.types.*

object FunctionRegistry {
@@ -44,21 +45,21 @@ object FunctionRegistry {
init {
register('+') {
monadic { omega ->
omega.conjugate()
omega.plus()
}

dyadic { alpha, omega ->
alpha + omega
alpha.plus(omega)
}
}

register('×') {
monadic { omega ->
omega.direction()
omega.times()
}

dyadic { alpha, omega ->
alpha * omega
alpha.times(omega)
}
}

@@ -75,16 +76,12 @@ object FunctionRegistry {

register('⍴') {
monadic { omega ->
TODO()
// omega.shape()
omega.rho()
}

dyadic { alpha, omega ->
omega.reshape(alpha)
omega.rho(alpha)
}

// Imagine using OF

}
}
}

+ 1
- 2
src/main/kotlin/com/martmists/kapt/Quad.kt View File

@@ -3,6 +3,5 @@ package com.martmists.kapt
import kotlin.random.Random

object Quad {
val indexOrigin: Int
get() = Random.nextInt(0,2)
var indexOrigin = 1
}

+ 0
- 42
src/main/kotlin/com/martmists/kapt/functions/_clone.kt View File

@@ -1,42 +0,0 @@
package com.martmists.kapt.functions

import com.martmists.kapt.types.*
import kscience.kmath.operations.Complex
import kscience.kmath.operations.r


fun APLValue._clone(): APLValue {
return when (this) {
is APLFunction -> {
this._clone()
}
is APLMatrix -> {
this._clone()
}
is APLScalar -> {
this._clone()
}
is APLString -> {
this._clone()
}
else -> {
throw APLError.unknownType(this)
}
}
}

fun APLFunction._clone(): APLValue {
throw APLError("Cannot _clone Function")
}

fun APLMatrix._clone(): APLMatrix {
return map { it._clone() }
}

fun APLScalar._clone(): APLScalar {
return APLScalar(Complex(value.re, value.im))
}

fun APLString._clone(): APLValue {
return APLString(value.toCharArray().joinToString())
}

+ 0
- 41
src/main/kotlin/com/martmists/kapt/functions/conjugate.kt View File

@@ -1,41 +0,0 @@
package com.martmists.kapt.functions

import com.martmists.kapt.types.*
import kscience.kmath.operations.conjugate


fun APLValue.conjugate(): APLValue {
return when (this) {
is APLFunction -> {
this.conjugate()
}
is APLMatrix -> {
this.conjugate()
}
is APLScalar -> {
this.conjugate()
}
is APLString -> {
this.conjugate()
}
else -> {
throw APLError.unknownType(this)
}
}
}

fun APLFunction.conjugate(): APLValue {
throw APLError("Cannot conjugate Function")
}

fun APLMatrix.conjugate(): APLMatrix {
return map { it.conjugate() }
}

fun APLScalar.conjugate(): APLScalar {
return APLScalar(value.conjugate)
}

fun APLString.conjugate(): APLValue {
throw APLError("Cannot conjugate String")
}

+ 0
- 41
src/main/kotlin/com/martmists/kapt/functions/direction.kt View File

@@ -1,41 +0,0 @@
package com.martmists.kapt.functions

import com.martmists.kapt.types.*
import kscience.kmath.operations.r


fun APLValue.direction(): APLValue {
return when (this) {
is APLFunction -> {
this.direction()
}
is APLMatrix -> {
this.direction()
}
is APLScalar -> {
this.direction()
}
is APLString -> {
this.direction()
}
else -> {
throw APLError.unknownType(this)
}
}
}

fun APLFunction.direction(): APLValue {
throw APLError("Cannot direction Function")
}

fun APLMatrix.direction(): APLMatrix {
return map { it.direction() }
}

fun APLScalar.direction(): APLScalar {
return APLScalar(value.div(value.r))
}

fun APLString.direction(): APLValue {
throw APLError("Cannot direction String")
}

src/main/kotlin/com/martmists/kapt/functions/minus.kt → src/main/kotlin/com/martmists/kapt/functions/dyadic/minus.kt View File

@@ -1,7 +1,12 @@
package com.martmists.kapt.functions
package com.martmists.kapt.functions.dyadic

import com.martmists.kapt.APLError
import com.martmists.kapt.types.*


/**
* Minus
*/
operator fun APLValue.minus(other: APLValue): APLValue {
return when (this) {
is APLFunction -> {
@@ -13,7 +18,7 @@ operator fun APLValue.minus(other: APLValue): APLValue {
is APLScalar -> {
this - other
}
is APLString -> {
is APLChar -> {
this - other
}
else -> {
@@ -26,7 +31,7 @@ operator fun APLFunction.minus(other: APLValue): APLValue {
throw APLError("Cannot subtract Function and ${other.javaClass.name}")
}

operator fun APLMatrix.minus(other: APLValue): APLMatrix {
operator fun APLMatrix.minus(other: APLValue): APLValue {
if (other is APLScalar) {
return map { it - other }
}
@@ -66,6 +71,6 @@ operator fun APLScalar.minus(other: APLValue): APLValue {
throw APLError("Cannot subtract Scalar and ${other.javaClass.name}")
}

operator fun APLString.minus(other: APLValue): APLValue {
throw APLError("Cannot subtract String and ${other.javaClass.name}")
operator fun APLChar.minus(other: APLValue): APLValue {
throw APLError("Cannot subtract Char and ${other.javaClass.name}")
}

src/main/kotlin/com/martmists/kapt/functions/plus.kt → src/main/kotlin/com/martmists/kapt/functions/dyadic/plus.kt View File

@@ -1,7 +1,12 @@
package com.martmists.kapt.functions
package com.martmists.kapt.functions.dyadic

import com.martmists.kapt.APLError
import com.martmists.kapt.types.*


/**
* Plus
*/
operator fun APLValue.plus(other: APLValue): APLValue {
return when (this) {
is APLFunction -> {
@@ -13,7 +18,7 @@ operator fun APLValue.plus(other: APLValue): APLValue {
is APLScalar -> {
this + other
}
is APLString -> {
is APLChar -> {
this + other
}
else -> {
@@ -26,7 +31,7 @@ operator fun APLFunction.plus(other: APLValue): APLValue {
throw APLError("Cannot sum Function and ${other.javaClass.name}")
}

operator fun APLMatrix.plus(other: APLValue): APLMatrix {
operator fun APLMatrix.plus(other: APLValue): APLValue {
if (other is APLScalar) {
return map { it + other }
}
@@ -66,6 +71,6 @@ operator fun APLScalar.plus(other: APLValue): APLValue {
throw APLError("Cannot sum Scalar and ${other.javaClass.name}")
}

operator fun APLString.plus(other: APLValue): APLValue {
throw APLError("Cannot sum String and ${other.javaClass.name}")
operator fun APLChar.plus(other: APLValue): APLValue {
throw APLError("Cannot sum Char and ${other.javaClass.name}")
}

src/main/kotlin/com/martmists/kapt/functions/reshape.kt → src/main/kotlin/com/martmists/kapt/functions/dyadic/rho.kt View File

@@ -1,13 +1,14 @@
package com.martmists.kapt.functions
package com.martmists.kapt.functions.dyadic

import com.martmists.kapt.APLError
import com.martmists.kapt.types.*
import kscience.kmath.structures.Buffer
import kscience.kmath.structures.toList

fun APLValue.reshape(other: APLValue): APLValue {
// IntelliJ Ultimate Edition
// at Jetbrains.com
/**
* Reshape
*/
fun APLValue.rho(other: APLValue): APLValue {
if (other !is APLMatrix) {
throw APLError("Cannot reshape to non-matrix")
}
@@ -18,16 +19,16 @@ fun APLValue.reshape(other: APLValue): APLValue {

return when (this) {
is APLFunction -> {
this.reshape(other)
this.rho(other)
}
is APLMatrix -> {
this.reshape(other)
this.rho(other)
}
is APLScalar -> {
this.reshape(other)
this.rho(other)
}
is APLString -> {
this.reshape(other)
is APLChar -> {
this.rho(other)
}
else -> {
throw APLError.unknownType(this)
@@ -35,18 +36,18 @@ fun APLValue.reshape(other: APLValue): APLValue {
}
}

fun APLFunction.reshape(other: APLMatrix): APLValue {
fun APLFunction.rho(other: APLMatrix): APLValue {
throw APLError("Cannot reshape Function")
}

fun APLMatrix.reshape(other: APLMatrix): APLMatrix {
fun APLMatrix.rho(other: APLMatrix): APLValue {
if (other.shape.size != 1) {
throw APLError("Cannot reshape Matrix to 2D shape")
}
if (size == 1) {
if (matrix.buffer[0] is APLString) {
matrix.buffer[0].reshape(other)
if (matrix.buffer[0] is APLChar) {
matrix.buffer[0].rho(other)
}
}

@@ -55,15 +56,10 @@ fun APLMatrix.reshape(other: APLMatrix): APLMatrix {
return APLMatrix(shape, Buffer.boxing(other.shape.reduce { acc, i -> acc * i }) { matrix.buffer[it % this.size] })
}

fun APLScalar.reshape(other: APLMatrix): APLValue {
fun APLScalar.rho(other: APLMatrix): APLValue {
throw APLError("Cannot reshape Scalar")
}

fun APLString.reshape(other: APLMatrix): APLValue {
val shape = other.matrix.buffer.toList().map { (it as APLScalar).asNatural() }.toIntArray()
// return APLMatrix(
// shape,
// Buffer.boxing(other.size) { }
// )
TODO()
fun APLChar.rho(other: APLMatrix): APLValue {
throw APLError("Cannot reshape Char")
}

src/main/kotlin/com/martmists/kapt/functions/times.kt → src/main/kotlin/com/martmists/kapt/functions/dyadic/times.kt View File

@@ -1,7 +1,11 @@
package com.martmists.kapt.functions
package com.martmists.kapt.functions.dyadic

import com.martmists.kapt.APLError
import com.martmists.kapt.types.*

/**
* Times
*/
operator fun APLValue.times(other: APLValue): APLValue {
return when (this) {
is APLFunction -> {
@@ -13,7 +17,7 @@ operator fun APLValue.times(other: APLValue): APLValue {
is APLScalar -> {
this * other
}
is APLString -> {
is APLChar -> {
this * other
}
else -> {
@@ -26,7 +30,7 @@ operator fun APLFunction.times(other: APLValue): APLValue {
throw APLError("Cannot multiply Function and ${other.javaClass.name}")
}

operator fun APLMatrix.times(other: APLValue): APLMatrix {
operator fun APLMatrix.times(other: APLValue): APLValue {
if (other is APLScalar) {
return map { it * other }
}
@@ -66,6 +70,6 @@ operator fun APLScalar.times(other: APLValue): APLValue {
throw APLError("Cannot multiply Scalar and ${other.javaClass.name}")
}

operator fun APLString.times(other: APLValue): APLValue {
throw APLError("Cannot multiply String and ${other.javaClass.name}")
operator fun APLChar.times(other: APLValue): APLValue {
throw APLError("Cannot multiply Char and ${other.javaClass.name}")
}

src/main/kotlin/com/martmists/kapt/functions/iota.kt → src/main/kotlin/com/martmists/kapt/functions/monadic/iota.kt View File

@@ -1,13 +1,14 @@
package com.martmists.kapt.functions
package com.martmists.kapt.functions.monadic

import com.martmists.kapt.APLError
import com.martmists.kapt.Quad
import com.martmists.kapt.types.*
import com.martmists.kapt.util.ReversedStrides
import kscience.kmath.operations.Complex
import kscience.kmath.operations.r
import kscience.kmath.structures.*


/**
* Indices
*/
fun APLValue.iota(): APLValue {
return when (this) {
is APLFunction -> {
@@ -19,7 +20,7 @@ fun APLValue.iota(): APLValue {
is APLScalar -> {
this.iota()
}
is APLString -> {
is APLChar -> {
this.iota()
}
else -> {
@@ -32,7 +33,7 @@ fun APLFunction.iota(): APLValue {
throw APLError("Cannot iota Function")
}

fun APLMatrix.iota(): APLMatrix {
fun APLMatrix.iota(): APLValue {
if (size == 1) {
return asScalar().iota()
}
@@ -44,24 +45,23 @@ fun APLMatrix.iota(): APLMatrix {
throw APLError("Cannot iota non-natural Matrix")
}

shape.add(1 + it.asNatural() - Quad.indexOrigin)
shape.add(it.asNatural())
}

val index_shape = listOf(*shape.toTypedArray(), shape.size)
val new_size = index_shape.reduce { acc, i -> acc * i }
val buf = MutableBuffer.boxing(new_size) { APLScalar(0) }

for (index in 0 until new_size) {
val dim_index = index % (index_shape.size-1)
val increase_every = index_shape.subList(dim_index+1, index_shape.size).reduce { acc, i -> acc * i }
val new_val = (((index-dim_index) / increase_every) % index_shape[dim_index]) + Quad.indexOrigin
buf[index] = APLScalar(new_val)
val ind = index_shape.size-1
val increases = (0 until ind).map { index_shape.subList(it+1, index_shape.size).reduce { acc, i -> acc * i } }.toList()
val buf = MutableBuffer.boxing(new_size) { index ->
val dim_index = index % ind
APLScalar((((index-dim_index) / increases[dim_index]) % index_shape[dim_index]) + Quad.indexOrigin)
}


return APLMatrix(index_shape.toIntArray(), buf)
}

fun APLScalar.iota(): APLMatrix {
fun APLScalar.iota(): APLValue {
if (!natural()) {
throw APLError("Cannot iota Complex or Float")
}
@@ -77,6 +77,6 @@ fun APLScalar.iota(): APLMatrix {
return APLMatrix(intArrayOf(elems.size), elems.asBuffer())
}

fun APLString.iota(): APLValue {
throw APLError("Cannot iota String")
fun APLChar.iota(): APLValue {
throw APLError("Cannot iota Char")
}

+ 45
- 0
src/main/kotlin/com/martmists/kapt/functions/monadic/minus.kt View File

@@ -0,0 +1,45 @@
package com.martmists.kapt.functions.monadic

import com.martmists.kapt.APLError
import com.martmists.kapt.types.*
import kscience.kmath.operations.Complex


/**
* Negate
*/
fun APLValue.minus(): APLValue {
return when (this) {
is APLFunction -> {
this.minus()
}
is APLMatrix -> {
this.minus()
}
is APLScalar -> {
this.minus()
}
is APLChar -> {
this.minus()
}
else -> {
throw APLError.unknownType(this)
}
}
}

fun APLFunction.minus(): APLValue {
throw APLError("Cannot negate Function")
}

fun APLMatrix.minus(): APLValue {
return map { it.minus() }
}

fun APLScalar.minus(): APLValue {
return APLScalar(Complex(-value.re, -value.im))
}

fun APLChar.minus(): APLValue {
throw APLError("Cannot negate Char")
}

+ 45
- 0
src/main/kotlin/com/martmists/kapt/functions/monadic/plus.kt View File

@@ -0,0 +1,45 @@
package com.martmists.kapt.functions.monadic

import com.martmists.kapt.APLError
import com.martmists.kapt.types.*
import kscience.kmath.operations.conjugate


/**
* Conjugate
*/
fun APLValue.plus(): APLValue {
return when (this) {
is APLFunction -> {
this.plus()
}
is APLMatrix -> {
this.plus()
}
is APLScalar -> {
this.plus()
}
is APLChar -> {
this.plus()
}
else -> {
throw APLError.unknownType(this)
}
}
}

fun APLFunction.plus(): APLValue {
throw APLError("Cannot conjugate Function")
}

fun APLMatrix.plus(): APLValue {
return map { it.plus() }
}

fun APLScalar.plus(): APLValue {
return APLScalar(value.conjugate)
}

fun APLChar.plus(): APLValue {
throw APLError("Cannot conjugate Char")
}

+ 43
- 0
src/main/kotlin/com/martmists/kapt/functions/monadic/rho.kt View File

@@ -0,0 +1,43 @@
package com.martmists.kapt.functions.monadic

import com.martmists.kapt.APLError
import com.martmists.kapt.types.*

/**
* Shape
*/
fun APLValue.rho(): APLValue {
return when (this) {
is APLFunction -> {
this.rho()
}
is APLMatrix -> {
this.rho()
}
is APLScalar -> {
this.rho()
}
is APLChar -> {
this.rho()
}
else -> {
throw APLError.unknownType(this)
}
}
}

fun APLFunction.rho(): APLValue {
throw APLError("Cannot get shape of Function")
}

fun APLMatrix.rho(): APLValue {
return APLValue.of(shape.toList())
}

fun APLScalar.rho(): APLValue {
throw APLError("Cannot get shape of Char")
}

fun APLChar.rho(): APLValue {
throw APLError("Cannot get shape of Char")
}

+ 44
- 0
src/main/kotlin/com/martmists/kapt/functions/monadic/times.kt View File

@@ -0,0 +1,44 @@
package com.martmists.kapt.functions.monadic

import com.martmists.kapt.APLError
import com.martmists.kapt.types.*
import kscience.kmath.operations.r

/**
* Direction
*/
fun APLValue.times(): APLValue {
return when (this) {
is APLFunction -> {
this.times()
}
is APLMatrix -> {
this.times()
}
is APLScalar -> {
this.times()
}
is APLChar -> {
this.times()
}
else -> {
throw APLError.unknownType(this)
}
}
}

fun APLFunction.times(): APLValue {
throw APLError("Cannot get direction of Function")
}

fun APLMatrix.times(): APLValue {
return map { it.times() }
}

fun APLScalar.times(): APLValue {
return APLScalar(value.div(value.r))
}

fun APLChar.times(): APLValue {
throw APLError("Cannot get direction of Char")
}

+ 0
- 42
src/main/kotlin/com/martmists/kapt/functions/negate.kt View File

@@ -1,42 +0,0 @@
package com.martmists.kapt.functions

import com.martmists.kapt.types.*
import kscience.kmath.operations.Complex
import kscience.kmath.operations.r


fun APLValue.negate(): APLValue {
return when (this) {
is APLFunction -> {
this.negate()
}
is APLMatrix -> {
this.negate()
}
is APLScalar -> {
this.negate()
}
is APLString -> {
this.negate()
}
else -> {
throw APLError.unknownType(this)
}
}
}

fun APLFunction.negate(): APLValue {
throw APLError("Cannot negate Function")
}

fun APLMatrix.negate(): APLMatrix {
return map { it.negate() }
}

fun APLScalar.negate(): APLScalar {
return APLScalar(Complex(-value.re, -value.im))
}

fun APLString.negate(): APLValue {
throw APLError("Cannot negate String")
}

+ 11
- 7
src/main/kotlin/com/martmists/kapt/main.kt View File

@@ -1,14 +1,18 @@
package com.martmists.kapt

import com.martmists.kapt.functions.iota
import com.martmists.kapt.functions.monadic.iota
import com.martmists.kapt.types.APLValue

fun main(args: Array<String>) {
// val value = APLValue.of(listOf(-2, 3, "abcdef", listOf(Complex(10.0, -1.7), Complex(0.0, -1.0)), 8.7))
// print(value)
val values = APLValue.of(listOf(100, 100, 100))
val now = System.nanoTime()
val res = values.iota()
val after = System.nanoTime()
println((after - now)/1000000.0)
// println(value)

val values = APLValue.of(null).iota()
println(values)

// val values = APLValue.of(listOf(100, 100, 100))
// val now = System.nanoTime()
// values.iota()
// val after = System.nanoTime()
// println((after - now)/1000000.0)
}

src/main/kotlin/com/martmists/kapt/types/APLString.kt → src/main/kotlin/com/martmists/kapt/types/APLChar.kt View File

@@ -1,7 +1,7 @@
package com.martmists.kapt.types

class APLString(val value: String) : APLValue {
class APLChar(val value: Char) : APLValue {
override fun toString(): String {
return value
return value.toString()
}
}

+ 2
- 0
src/main/kotlin/com/martmists/kapt/types/APLFunction.kt View File

@@ -1,5 +1,7 @@
package com.martmists.kapt.types

import com.martmists.kapt.APLError

typealias APLMonadic = (omega: APLValue) -> APLValue
typealias APLDyadic = (alpha: APLValue, omega: APLValue) -> APLValue
typealias APLAxisMonadic = (axis: IntArray, omega: APLValue) -> APLValue


+ 15
- 6
src/main/kotlin/com/martmists/kapt/types/APLMatrix.kt View File

@@ -2,6 +2,7 @@ package com.martmists.kapt.types

import com.inamik.text.tables.GridTable
import com.inamik.text.tables.grid.Border
import com.martmists.kapt.APLError
import com.martmists.kapt.util.ReversedStrides
import com.martmists.kapt.ext.*
import kscience.kmath.operations.r
@@ -9,8 +10,8 @@ import kscience.kmath.structures.*
import kotlin.math.roundToInt


class APLMatrix(val shape: IntArray,
buffer: Buffer<out APLValue>
open class APLMatrix(val shape: IntArray,
buffer: Buffer<out APLValue>
) : APLValue {
val matrix = BufferNDStructure(ReversedStrides(shape), buffer)

@@ -68,7 +69,7 @@ class APLMatrix(val shape: IntArray,
operator fun get(indices: APLValue): APLValue {
if (indices is APLScalar) {
if (indices.natural()) {
return this[indices.value.r.roundToInt(), 0]
return indexAxis(0, indices.value.r.roundToInt())
} else {
throw APLError("Cannot index Matrix by Complex or Float")
}
@@ -85,11 +86,19 @@ class APLMatrix(val shape: IntArray,
}

override fun toString(): String {
if (size == 0) {
return ""
}

if (shape.size == 1) {
if (matrix.elements().all { it.second is APLChar }) {
return matrix.elements().map { (it.second as APLChar).value }.joinToString()
}

var grid = GridTable.of(1, shape[0])
val addRules = matrix.elements().any { it.second is APLMatrix }
matrix.elements().map { it.second.toString() }.forEachIndexed { index, s -> grid.put(0, index, grid.cellOf(s.split("\n"), addRules)) }
if (addRules) {
val hasNested = matrix.elements().any { it.second is APLMatrix }
matrix.elements().map { it.second.toString() }.forEachIndexed { index, s -> grid.put(0, index, grid.cellOf(s.split("\n"), hasNested)) }
if (hasNested) {
grid = Border.SINGLE_LINE.apply(grid)
}
return grid.contentToString()


+ 9
- 5
src/main/kotlin/com/martmists/kapt/types/APLValue.kt View File

@@ -8,26 +8,30 @@ import java.lang.IllegalStateException
interface APLValue {
companion object {
@JvmStatic
fun of(value: Any): APLValue {
fun of(value: Any?): APLValue {
if (value == null) {
return APLZilde()
}

if (value is Number) {
return APLScalar(value)
} else if (value is Complex) {
return APLScalar(value)
}

if (value is String) {
return APLString(value)
if (value is Char) {
return APLChar(value)
}

if (value is List<*>) {
return APLMatrix(
intArrayOf(value.size),
Buffer.boxing(value.size) { of(value[it] ?: error("Element is null")) }
Buffer.boxing(value.size) { of(value[it]) }
)
} else if (value is Array<*>) {
return APLMatrix(
intArrayOf(value.size),
Buffer.boxing(value.size) { of(value[it] ?: error("Element is null")) }
Buffer.boxing(value.size) { of(value[it]) }
)
}



+ 7
- 0
src/main/kotlin/com/martmists/kapt/types/APLZilde.kt View File

@@ -0,0 +1,7 @@
package com.martmists.kapt.types

import kscience.kmath.structures.Buffer

class APLZilde : APLMatrix(intArrayOf(0), Buffer.boxing(0) { APLValue.of(null) }) {
override fun toString() = ""
}

+ 1
- 1
src/main/kotlin/com/martmists/kapt/util/ReversedStrides.kt View File

@@ -8,7 +8,7 @@ import kscience.kmath.structures.Strides
*/
class ReversedStrides(override val shape: IntArray) : Strides {
override val linearSize: Int
get() = strides[shape.size]
get() = if (shape.isNotEmpty()) strides[shape.size] else 0

override val strides: List<Int> by lazy {
sequence {


Loading…
Cancel
Save