kotlinconvex-optimizationojalgo

ojAlog - ConvexSolver in Kotlin: 2d Array


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.


Solution

  • 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)