0

I'm trying to implement some example as I am planning to explore ojAlgo for optimization purposes. My question is really simple.

In Java I can easily write:

PrimitiveDenseStore Q = PrimitiveDenseStore.FACTORY.rows(new double[][]{{2.0,0.0}, {0.0, 2.0}});

I tried to do the same thing it kotlin:

val Q: Array<DoubleArray> = arrayOf(DoubleArray(2.0, 0.0), DoubleArray(2.0, 0.0))
var tmpQ = PrimitiveDenseStore.FACTORY.rows(Q)

but it seems that .rows cannot be called with the argument I gave.

Maybe I doing something stupid but I would appreciate the help.

Thank you.

1 Answers1

0

DoubleArray constructor takes array size as a first argument, that's why your construction is invalid. The analogue of double[][] in Kotlin is Array<DoubleArray>, that's right, but it should be constructed like this:

val Q: Array<DoubleArray> = arrayOf(doubleArrayOf(2.0, 0.0), doubleArrayOf(2.0, 0.0))

UPDATE:

Looks like rows function takes double[]... source as params, so in Kotlin you can use spread operator:

val Q: Array<DoubleArray> = arrayOf(doubleArrayOf(2.0, 0.0), doubleArrayOf(2.0, 0.0))
var tmpQ = PrimitiveDenseStore.FACTORY.rows(*Q)
Kirill Bubochkin
  • 5,868
  • 2
  • 31
  • 50
  • Thanks. I tried that but still it seems that: `PrimitiveDenseStore.FACTORY.rows(Q)` with the line you wrote is not allowed as an argument. – Eli Wilner Jun 15 '18 at 20:06
  • Not an error but using InelliJ, the .rows() in `PrimitiveDenseStore.FACTORY.rows` is underline with red saying : "Non of the following functions can be called with the argument supplied" – Eli Wilner Jun 15 '18 at 20:13
  • And what are the "following functions"? :) – Kirill Bubochkin Jun 15 '18 at 20:14
  • `rows(vararg (MutableList..List?)) defined in org.ojalgo.matrix.store.PhysicalStore.Factory rows(vararg Array<(out) Number!>!) defined in org.ojalgo.matrix.store.PhysicalStore.Factory rows(vararg DoubleArray!) defined in org.ojalgo.matrix.store.PhysicalStore.Factory rows(vararg Access1D<*>!) defined in org.ojalgo.matrix.store.PhysicalStore.Factory` – Eli Wilner Jun 15 '18 at 20:28
  • So why would he accept the Java's double[][] and not Kotlin's?? – Eli Wilner Jun 15 '18 at 20:29