Skip to content

Instantly share code, notes, and snippets.

@organic-nailer
Created October 22, 2020 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save organic-nailer/35fb3eb42ae601e464056201e7f70a90 to your computer and use it in GitHub Desktop.
Save organic-nailer/35fb3eb42ae601e464056201e7f70a90 to your computer and use it in GitHub Desktop.
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.plot.MonolithicAwt
import jetbrains.datalore.vis.svg.SvgSvgElement
import jetbrains.datalore.vis.swing.BatikMapperComponent
import jetbrains.datalore.vis.swing.BatikMessageCallback
import jetbrains.letsPlot.geom.geom_line
import jetbrains.letsPlot.ggplot
import jetbrains.letsPlot.intern.toSpec
import jetbrains.letsPlot.label.ggtitle
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.factory.Nd4j
import org.nd4j.linalg.inverse.InvertMatrix
import org.nd4j.linalg.ops.transforms.Transforms
import java.awt.Font
import javax.swing.JFrame
import javax.swing.SwingUtilities
import javax.swing.WindowConstants
private val SVG_COMPONENT_FACTORY_BATIK =
{ svg: SvgSvgElement -> BatikMapperComponent(svg, BATIK_MESSAGE_CALLBACK) }
private val BATIK_MESSAGE_CALLBACK = object : BatikMessageCallback {
override fun handleMessage(message: String) {
println(message)
}
override fun handleException(e: Exception) {
if (e is RuntimeException) {
throw e
}
throw RuntimeException(e)
}
}
private val AWT_EDT_EXECUTOR = { runnable: () -> Unit ->
runnable.invoke()
}
operator fun INDArray.plus(other: INDArray): INDArray = this.add(other)
operator fun INDArray.minus(other: INDArray): INDArray = this.sub(other)
operator fun INDArray.times(other: INDArray): INDArray = this.mmul(other)
operator fun INDArray.plus(other: Number): INDArray = this.add(other)
operator fun INDArray.minus(other: Number): INDArray = this.sub(other)
operator fun INDArray.times(other: Number): INDArray = this.mul(other)
operator fun INDArray.div(other: Number): INDArray = this.div(other)
fun main(args: Array<String>) {
plotSample()
matrixSample()
}
fun matrixSample() {
val a = Nd4j.create(doubleArrayOf(1.0,2.0,3.0,4.0), intArrayOf(2,2))
val b = InvertMatrix.invert(a, false)
println("a = \n$a")
println("b = a^-1 = \n$b")
println("a + b = \n${a + b}")
println("a dot b = \n${a * b}")
}
fun plotSample() {
SwingUtilities.invokeLater {
val xArray = Nd4j.arange(0.0,60.0) / 10.0
val yArray = Transforms.sin(xArray)
val data = mapOf<String, Any>(
"x" to xArray.data().asDouble(),
"y" to yArray.data().asDouble(),
)
// 線グラフをかく
val geom = geom_line() {
x = "x"
y = "y"
}
val p = ggplot(data) + geom + ggtitle("sin")
val plotSpec = p.toSpec()
val plotSize = DoubleVector(1000.0, 1000.0)
val component =
MonolithicAwt.buildPlotFromRawSpecs(plotSpec, plotSize, SVG_COMPONENT_FACTORY_BATIK, AWT_EDT_EXECUTOR) {
for (message in it) {
println("PLOT MESSAGE: $message")
}
}
val frame = JFrame("三角関数")
frame.contentPane.add(component)
frame.font = Font(Font.SANS_SERIF, Font.PLAIN, 10)
frame.defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
frame.pack()
frame.isVisible = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment