Skip to content

Commit

Permalink
Merge pull request #4 from jalexcole/Alex_Cole
Browse files Browse the repository at this point in the history
Set and Get work for all indices
  • Loading branch information
jalexcole authored Jan 10, 2021
2 parents c07e9bc + 3cfafdd commit 0ed6501
Show file tree
Hide file tree
Showing 80 changed files with 189 additions and 171 deletions.
Binary file modified build/classes/kotlin/main/MainKt.class
Binary file not shown.
Binary file modified build/classes/kotlin/main/arrayND/ArrayND.class
Binary file not shown.
Binary file modified build/classes/kotlin/main/arrayND/ArrayNDKt.class
Binary file not shown.
Binary file modified build/classes/kotlin/test/ArrayNDTest.class
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileKotlin/build-history.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileKotlin/caches-jvm/jvm/kotlin/proto.tab
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions build/kotlin/compileKotlin/caches-jvm/lookups/counters.tab
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
5
0
7
2
Binary file modified build/kotlin/compileKotlin/caches-jvm/lookups/file-to-id.tab
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileKotlin/caches-jvm/lookups/id-to-file.tab
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileKotlin/caches-jvm/lookups/id-to-file.tab.len
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileKotlin/caches-jvm/lookups/id-to-file.tab_i
Binary file not shown.
Binary file modified build/kotlin/compileKotlin/caches-jvm/lookups/lookups.tab
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileKotlin/caches-jvm/lookups/lookups.tab.len
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileKotlin/caches-jvm/lookups/lookups.tab_i
Binary file not shown.
Binary file modified build/kotlin/compileKotlin/last-build.bin
Binary file not shown.
Binary file modified build/kotlin/compileTestKotlin/build-history.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileTestKotlin/caches-jvm/jvm/kotlin/proto.tab
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileTestKotlin/caches-jvm/jvm/kotlin/proto.tab_i
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1
0
3
1
Binary file modified build/kotlin/compileTestKotlin/caches-jvm/lookups/file-to-id.tab
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileTestKotlin/caches-jvm/lookups/id-to-file.tab
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileTestKotlin/caches-jvm/lookups/lookups.tab
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/kotlin/compileTestKotlin/caches-jvm/lookups/lookups.tab_i
Binary file not shown.
Binary file modified build/kotlin/compileTestKotlin/last-build.bin
Binary file not shown.
Binary file modified build/libs/numeric-0.1.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion build/reports/tests/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ <h2>Classes</h2>
<input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/>
</label>
</div>Generated by
<a href="http://www.gradle.org">Gradle 6.6.1</a> at Jan 9, 2021 9:05:15 PM</p>
<a href="http://www.gradle.org">Gradle 6.7</a> at Jan 9, 2021, 10:40:58 PM</p>
</div>
</div>
</body>
Expand Down
342 changes: 178 additions & 164 deletions src/main/kotlin/arrayND/ArrayND.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,189 +5,203 @@ import kotlin.collections.ArrayList
import kotlin.math.pow



open class ArrayND {
var dataND: ArrayList<Double> = arrayListOf()
var shape: Array<Int> = arrayOf<Int>()



constructor(){

}

constructor (ndArray: Array<Double>, shape: Array<Int>){
dataND = arrayListOf(*ndArray)
this.shape = shape
}

constructor(ndArray: Array<Double>) {
dataND = arrayListOf(*ndArray)
shape = arrayOf(ndArray.size)
}

constructor(ndArray: ArrayList<Double>){
dataND = ndArray
shape = arrayOf(ndArray.size)
}

@JvmName("getShape1")
fun getShape(): Array<Int> {
return shape
}
private fun inBounds(shape: Array<Int>, indices: Array<Int>): Boolean {
if (shape.size != indices.size) return false
return shape.zip(indices).all { (s, i) -> s >= i }
}
var dataND: ArrayList<Double> = arrayListOf()
var shape: Array<Int> = arrayOf<Int>()
var size: Int = dataND.size

constructor() {

private fun calculateIndex(indices: Array<Int>): Int {
var index = -1
if (inBounds(shape, indices)){
when (indices.size) {
1 ->{
return indices[0]
}
2 -> {
val i = indices[0]
val j = indices[1]
return i * shape[0] + j
}
3 -> {
val i = indices[0]
val j = indices[1]
val k = indices[2]

return i * shape[0] * shape[1] + j * shape[1] + k
}
else -> return -1
}
}

constructor (ndArray: Array<Double>, shape: Array<Int>) {
dataND = arrayListOf(*ndArray)
this.shape = shape
}

constructor(ndArray: Array<Double>) {
dataND = arrayListOf(*ndArray)
shape = arrayOf(ndArray.size)
}

constructor(ndArray: ArrayList<Double>) {
dataND = ndArray
shape = arrayOf(ndArray.size)
}

operator fun get(vararg indices: Int): ArrayND {
val index = calculateIndex(indices.toTypedArray())
return ArrayND(arrayOf(dataND[index]))
}
operator fun get(indices: Array<Int>): ArrayND {
val index = calculateIndex(indices)
return ArrayND(arrayOf(dataND[index]))
}

operator fun set(vararg indices: Int, value: Double) {
val index = calculateIndex(indices.toTypedArray())
dataND[index] = value
}

operator fun set(indices: Array<Int>, value: Double) {
val index = calculateIndex(indices)
dataND[index] = value
}

@JvmName("getShape1")
fun getShape(): Array<Int> {
return shape
}

private fun inBounds(shape: Array<Int>, indices: Array<Int>): Boolean {
if (shape.size != indices.size) return false
return shape.zip(indices).all { (s, i) -> s >= i }
}

private fun calculateIndex(indices: Array<Int>): Int {
var index = -1
if (inBounds(shape, indices)) when (indices.size) {
1 -> {
return indices[0]
}
2 -> {
val i = indices[0]
val j = indices[1]
return i * shape[0] + j
}
3 -> {
val i = indices[0]
val j = indices[1]
val k = indices[2]

return i * shape[0] * shape[1] + j * shape[1] + k
}

in 4 .. Int.MAX_VALUE -> {
var index = 0
for (i in 0 until shape.size - 1) {
for (j in i until shape.size - 1) {
index += indices[i] * shape[j]
}
}
return -1
index += indices.last()


return index
}
else -> return -1
}
return -1

fun single() = dataND.single()

fun reshape(newShape: Array<Int>): ArrayND {
var count = 1
for (i in newShape) {
count *= i
}
if (count == dataND.size) {
shape = newShape
return ArrayND(dataND.toTypedArray(), newShape)
} else {
println("ValueError: cannot reshape array of size ${dataND.size} into shape ${Arrays.toString(shape)}")
return ArrayND()
}


}

fun single() = dataND.single()

fun reshape(newShape: Array<Int>): ArrayND {
var count = 1
for (i in newShape) {
count *= i
}
if (count == dataND.size) {
shape = newShape
return ArrayND(dataND.toTypedArray(), newShape)
} else {
println("ValueError: cannot reshape array of size ${dataND.size} into shape ${Arrays.toString(shape)}")
return ArrayND()
}

private fun add(b: ArrayND): ArrayND {
return if (shape.contentEquals(b.getShape())){
val x = arrayListOf<Double>()
for(i in 0 until dataND.size){
x.add(dataND[i] + b.dataND[i])
}
ArrayND(x.toTypedArray(), shape)
} else {
ArrayND(arrayOf<Double>(), arrayOf<Int>())
}
}

operator fun plus(other: ArrayND) = add(other)

operator fun plus(other: Double): ArrayND {
val x = arrayListOf<Double>()
for (i in dataND){
x.add(i + other)
}

private fun add(b: ArrayND): ArrayND {
return if (shape.contentEquals(b.getShape())) {
val x = arrayListOf<Double>()
for (i in 0 until dataND.size) {
x.add(dataND[i] + b.dataND[i])
}
ArrayND(x.toTypedArray(), shape)
} else {
ArrayND(arrayOf<Double>(), arrayOf<Int>())
}
}

operator fun plus(other: ArrayND) = add(other)

operator fun plus(other: Double): ArrayND {
val x = arrayListOf<Double>()
for (i in dataND) {
x.add(i + other)
}
return ArrayND(x.toTypedArray(), this.shape)
}

fun print() {
when (shape.size) {
1 -> {
print("[")
for (i in 0 until dataND.size - 1) {
print("$i, ")
}
return ArrayND(x.toTypedArray(), this.shape)
}

fun print(){
when(shape.size) {
1 -> {
print("[")
for (i in 0 until dataND.size - 1) {
print("$i, ")
}
print("${dataND[dataND.size - 1]}]")
}
2 -> {
print("[")
for(column in 0 until shape[0]) {
if (column != 0) print(" ")
print("[")
for(row in 0 until shape[1]) {
val index = shape[0] * column + row
print(dataND[index])
if (row != shape[1] - 1) print(", ")
}
print("]")
if(column != shape[0] - 1) print(",\n")
}
print("]")
}
3 -> {

}
0 -> {
print("[]")
}
else -> {
TODO("Make print work for all cases")
}
print("${dataND[dataND.size - 1]}]")
}
2 -> {
print("[")
for (column in 0 until shape[0]) {
if (column != 0) print(" ")
print("[")
for (row in 0 until shape[1]) {
val index = shape[0] * column + row
print(dataND[index])
if (row != shape[1] - 1) print(", ")
}
print("]")
if (column != shape[0] - 1) print(",\n")
}
}

operator fun get(vararg args: Int): ArrayND {
val index = calculateIndex(args.toTypedArray())
return ArrayND(arrayOf(dataND[index].toDouble()))
}

operator fun set(vararg indices: Int, value: Double) {
val index = calculateIndex(indices.toTypedArray())
dataND[index] = value
}
print("]")
}
3 -> {

}
0 -> {
print("[]")
}
else -> {
TODO("Make print work for all cases")
}
}
}


}

fun ArrayND.sum(): ArrayND {
/**
* Sum
*
* Takes the sum of every element in the array
*/
var sum = 0.0
for (i in dataND) {
sum += i
}
return ArrayND(arrayOf(sum))
/**
* Sum
*
* Takes the sum of every element in the array
*/
var sum = 0.0
for (i in dataND) {
sum += i
}
return ArrayND(arrayOf(sum))
}

fun ArrayND.pow(value: Double): ArrayND {
val newList = arrayListOf<Double>()
val newShape = shape
for (i in 0 until dataND.size){
newList.add(dataND[i])
}
return ArrayND(newList.toTypedArray(), newShape)
val newList = arrayListOf<Double>()
val newShape = shape
for (i in 0 until dataND.size) {
newList.add(dataND[i])
}
return ArrayND(newList.toTypedArray(), newShape)
}

fun ArrayND.sqrt(value: Double): ArrayND {
val newList = arrayListOf<Double>()
val newShape = shape
for (i in 0 until dataND.size){
newList.add(dataND[i].pow(2.0))
}
return ArrayND(newList.toTypedArray(), newShape)
val newList = arrayListOf<Double>()
val newShape = shape

for (i in 0 until dataND.size) {
newList.add(dataND[i].pow(2.0))
}
return ArrayND(newList.toTypedArray(), newShape)
}






Loading

0 comments on commit 0ed6501

Please sign in to comment.