これが簡単な方法かどうかはわかりませんが、ネストされたタプルを「2D」配列に変換するためのコードを以下にいくつか含めました。
まず、タプルのサイズを取得し、タプルを に変換するためのボイラー プレートが必要です[Array[Array[Double]]。私が使用した一連の手順は次のとおりです。
- タプルの行数と列数を計算する
- ネストされたタプルを 1 行の配列に変換します
- 元のタプルのサイズに基づいて配列を再形成します。
そのためのコードは次のとおりです。
object Matrix {
    /**
     * Returns the size of a series of nested tuples. 
     */
    def productSize(t: Product): (Int, Int) = {
        val a = t.productArity
        val one = t.productElement(0)
        if (one.isInstanceOf[Product]) {
            val b = one.asInstanceOf[Product].productArity
            (a,  b)
        }
        else {
            (1, a)
        }
    }
    /**
     * Flattens out a nested tuple and returns the contents as an iterator. 
     */
    def flattenProduct(t: Product): Iterator[Any] = t.productIterator.flatMap {
        case p: Product => flattenProduct(p)
        case x => Iterator(x)
    }
    /**
     * Convert a nested tuple to a flattened row-oriented array.
     * Usage is:
     * {{{
     *  val t = ((1, 2, 3), (4, 5, 6))
     *  val a = Matrix.toArray(t)
     *  // a: Array[Double] = Array(1, 2, 3, 4, 5, 6)
     * }}}
     *
     * @param t The tuple to convert to an array
     */
    def toArray(t: Product): Array[Double] = flattenProduct(t).map(v =>
        v match {
            case c: Char => c.toDouble
            case b: Byte => b.toDouble
            case sh: Short => sh.toDouble
            case i: Int => i.toDouble
            case l: Long => l.toDouble
            case f: Float => f.toDouble
            case d: Double => d
            case s: String => s.toDouble
            case _ => Double.NaN
        }
    ).toArray[Double]
    def rowArrayTo2DArray[@specialized(Int, Long, Float, Double) A: Numeric](m: Int, n: Int,
            rowArray: Array[A]) = {
        require(rowArray.size == m * n)
        val numeric = implicitly[Numeric[A]]
        val newArray = Array.ofDim[Double](m, n)
        for (i <- 0 until m; j <- 0 until n) {
            val idx = i * n + j
            newArray(i)(j) = numeric.toDouble(rowArray(idx))
        }
        newArray
    }
    /**
     * Factory method for turning tuples into 2D arrays
     */
    def apply(data: Product): Array[Array[Double]] = {
        def size = productSize(data)
        def array = toArray(data)
        rowArrayTo2DArray(size._1, size._2, array)
    }
}
これを使用するには、次のようにします。
val a  = Matrix((1, 2, 3))
// a: Array[Array[Double]] = Array(Array(1.0, 2.0, 3.0))
val b = Matrix(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
// b: Array[Array[Double]] = Array(Array(1.0, 2.0, 3.0), 
//                                 Array(4.0, 5.0, 6.0), 
//                                 Array(7.0, 8.0, 9.0))
val c = Matrix((1L, 2F, "3"))    // Correctly handles mixed types
// c: Array[Array[Double]] = Array(Array(1.0, 2.0, 3.0))
val d = Matrix((1L, 2F, new java.util.Date())) // Non-numeric types convert to NaN
// d: Array[Array[Double]] = Array(Array(1.0, 2.0, NaN))
または、必要な配列のサイズと値の 1D 配列を使用して直接 rowArrayTo2DArray を呼び出すことができる場合:
val e = Matrix.rowArrayTo2DArray(1, 3, Array(1, 2, 3))
// e: Array[Array[Double]] = Array(Array(1.0, 2.0, 3.0))
val f = Matrix.rowArrayTo2DArray(3, 1, Array(1, 2, 3))
// f: Array[Array[Double]] = Array(Array(1.0), Array(2.0), Array(3.0))
val g = Matrix.rowArrayTo2DArray(3, 3, Array(1, 2, 3, 4, 5, 6, 7, 8, 9))
// g: Array[Array[Double]] = Array(Array(1.0, 2.0, 3.0), 
//                                 Array(4.0, 5.0, 6.0), 
//                                 Array(7.0, 8.0, 9.0))