-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- major code refactor, merging ejml and jblas project - AllInit granularity improve
- Loading branch information
Showing
23 changed files
with
943 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
src/main/kotlin/main.kt → ...kotlin/com/dedztbh/kuantum/common/main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/kotlin/util.kt → ...kotlin/com/dedztbh/kuantum/common/util.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package com.dedztbh.kuantum.common | ||
|
||
import java.io.BufferedReader | ||
import java.util.* | ||
import kotlin.math.pow | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.dedztbh.kuantum.ejml | ||
|
||
import com.dedztbh.kuantum.common.main | ||
|
||
/** | ||
* Created by DEDZTBH on 2020/10/11. | ||
* Project KuantumCircuitSim | ||
*/ | ||
|
||
fun main(args: Array<String>) = main(args, "ejml") |
112 changes: 112 additions & 0 deletions
112
src/main/kotlin/com/dedztbh/kuantum/ejml/matrix/CMatrixIO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.dedztbh.kuantum.ejml.matrix | ||
|
||
import com.github.doyaaaaaken.kotlincsv.client.CsvReader | ||
import com.github.doyaaaaaken.kotlincsv.client.CsvWriter | ||
import org.ejml.UtilEjml | ||
import org.ejml.data.MatrixSparse | ||
import org.ejml.data.MatrixType | ||
import org.ejml.ops.MatrixIO | ||
import java.io.* | ||
import java.text.DecimalFormat | ||
|
||
/** | ||
* Created by DEDZTBH on 2020/09/27. | ||
* Project KuantumCircuitSim | ||
*/ | ||
|
||
fun getMatrixType(mat: CMatrix): String { | ||
return if (mat.type == MatrixType.UNSPECIFIED) { | ||
mat.javaClass.simpleName | ||
} else { | ||
mat.type.name | ||
} | ||
} | ||
|
||
fun printTypeSize(out: PrintStream, mat: CMatrix) { | ||
if (mat is MatrixSparse) { | ||
val m = mat as MatrixSparse | ||
out.println( | ||
"Type = " + getMatrixType(mat) + " , rows = " + mat.getNumRows() + | ||
" , cols = " + mat.getNumCols() + " , nz_length = " + m.nonZeroLength | ||
) | ||
} else { | ||
out.println("Type = " + getMatrixType(mat) + " , rows = " + mat.getNumRows() + " , cols = " + mat.getNumCols()) | ||
} | ||
} | ||
|
||
fun padSpace(builder: java.lang.StringBuilder, length: Int): String { | ||
builder.delete(0, builder.length) | ||
for (i in 0 until length) { | ||
builder.append(' ') | ||
} | ||
return builder.toString() | ||
} | ||
|
||
fun saveCsv(A: CMatrix, fileName: String) = | ||
CsvWriter().writeAll(A.data.asList().windowed(A.rowStride, A.rowStride), fileName) | ||
|
||
|
||
fun loadCsv(fileName: String): CMatrix = | ||
CMatrix(CsvReader().open(fileName) { | ||
readAllAsSequence().map { | ||
DoubleArray(it.size) { i -> it[i].toDouble() } | ||
}.toMutableList().toTypedArray() | ||
}) | ||
|
||
fun <T> saveBin(A: T, fileName: String) { | ||
val fileStream = FileOutputStream(fileName) | ||
val stream = ObjectOutputStream(fileStream) | ||
try { | ||
stream.writeObject(A) | ||
stream.flush() | ||
} finally { | ||
// clean up | ||
fileStream.use { | ||
stream.close() | ||
} | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T> loadBin(fileName: String): T { | ||
val fileStream = FileInputStream(fileName) | ||
val stream = ObjectInputStream(fileStream) | ||
val ret: T | ||
try { | ||
ret = stream.readObject() as T | ||
if (stream.available() != 0) { | ||
throw RuntimeException("File not completely read?") | ||
} | ||
} catch (e: ClassNotFoundException) { | ||
throw RuntimeException(e) | ||
} | ||
stream.close() | ||
return ret | ||
} | ||
|
||
fun CMatrix.printFancy2( | ||
allssket: List<String>, | ||
out: PrintStream = System.out, | ||
length: Int = MatrixIO.DEFAULT_LENGTH, | ||
) { | ||
// printTypeSize(out, this) | ||
val format = DecimalFormat("#") | ||
val builder = StringBuilder(length) | ||
val cols = numCols | ||
val c = CNum() | ||
var i = 0 | ||
for (y in 0 until numRows) { | ||
for (x in 0 until cols) { | ||
get(y, x, c) | ||
var real = UtilEjml.fancyString(c.real, format, length, 4) | ||
var img = UtilEjml.fancyString(c.imaginary, format, length, 4) | ||
real += padSpace(builder, length - real.length) | ||
img = img + "i" + padSpace(builder, length - img.length) | ||
out.print("${allssket[i++]}: $real + $img") | ||
if (x < numCols - 1) { | ||
out.print(" , ") | ||
} | ||
} | ||
out.println() | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/matrix/const.kt → .../com/dedztbh/kuantum/ejml/matrix/const.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package matrix | ||
package com.dedztbh.kuantum.ejml.matrix | ||
|
||
/** | ||
* Created by DEDZTBH on 2020/09/25. | ||
|
3 changes: 1 addition & 2 deletions
3
src/main/kotlin/matrix/op.kt → ...lin/com/dedztbh/kuantum/ejml/matrix/op.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
package matrix | ||
|
||
package com.dedztbh.kuantum.ejml.matrix | ||
|
||
/** | ||
* Created by DEDZTBH on 2020/09/22. | ||
|
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/matrix/type.kt → ...n/com/dedztbh/kuantum/ejml/matrix/type.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.