|
| 1 | +package com.thoughtworks.compute |
| 2 | + |
| 3 | +import scala.annotation.tailrec |
| 4 | + |
| 5 | +/** |
| 6 | + * @author 杨博 (Yang Bo) |
| 7 | + */ |
| 8 | +object NDimensionalAffineTransform { |
| 9 | + |
| 10 | + def preConcatenate(matrix01: Array[Double], matrix12: Array[Double], length0: Int): Array[Double] = { |
| 11 | + val length1 = matrix01.length / (length0 + 1) |
| 12 | + val length2 = matrix12.length / (length1 + 1) |
| 13 | + val matrix02 = Array.ofDim[Double]((length0 + 1) * length2) |
| 14 | + concatenate(matrix01, matrix12, matrix02, length0, length1, length2) |
| 15 | + matrix02 |
| 16 | + } |
| 17 | + |
| 18 | + def concatenate(matrix01: Array[Double], |
| 19 | + matrix12: Array[Double], |
| 20 | + matrix02: Array[Double], |
| 21 | + length0: Int, |
| 22 | + length1: Int, |
| 23 | + length2: Int): Unit = { |
| 24 | + |
| 25 | + @tailrec |
| 26 | + def loop2(index2: Int): Unit = { |
| 27 | + if (index2 < length2) { |
| 28 | + @tailrec |
| 29 | + def loop0(index0: Int): Unit = { |
| 30 | + if (index0 < length0) { |
| 31 | + @tailrec |
| 32 | + def loop1(index1: Int, accumulator: Double): Double = { |
| 33 | + if (index1 < length1) { |
| 34 | + loop1(index1 + 1, |
| 35 | + accumulator + |
| 36 | + matrix12(index2 * (length1 + 1) + index1) * |
| 37 | + matrix01(index1 * (length0 + 1) + index0)) |
| 38 | + } else { |
| 39 | + accumulator |
| 40 | + } |
| 41 | + } |
| 42 | + matrix02(index2 * (length0 + 1) + index0) = loop1(0, 0.0) |
| 43 | + |
| 44 | + loop0(index0 + 1) |
| 45 | + } |
| 46 | + } |
| 47 | + loop0(0) |
| 48 | + @tailrec |
| 49 | + def loop1(index1: Int, accumulator: Double): Double = { |
| 50 | + if (index1 < length1) { |
| 51 | + loop1(index1 + 1, |
| 52 | + accumulator + |
| 53 | + matrix12(index2 * (length1 + 1) + index1) * |
| 54 | + matrix01(index1 * (length0 + 1) + length0)) |
| 55 | + } else { |
| 56 | + accumulator |
| 57 | + } |
| 58 | + } |
| 59 | + matrix02(index2 * (length0 + 1) + length0) = loop1(0, matrix12(index2 * (length1 + 1) + length1)) |
| 60 | + loop2(index2 + 1) |
| 61 | + } |
| 62 | + } |
| 63 | + loop2(0) |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + def transform(matrix: Array[Double], source: Array[Double]): Array[Double] = { |
| 68 | + val sourceLength = source.length |
| 69 | + val destination = Array.ofDim[Double](matrix.length / (sourceLength + 1)) |
| 70 | + transform(matrix, source, destination) |
| 71 | + destination |
| 72 | + } |
| 73 | + |
| 74 | + private def transform(matrix: Array[Double], source: Array[Double], destination: Array[Double]): Unit = { |
| 75 | + val sourceLength = source.length |
| 76 | + val destinationLength = destination.length |
| 77 | + if (matrix.length != (sourceLength + 1) * destinationLength) { |
| 78 | + throw new IllegalArgumentException |
| 79 | + } |
| 80 | + @tailrec |
| 81 | + def rowLoop(y: Int): Unit = { |
| 82 | + if (y < destinationLength) { |
| 83 | + @tailrec |
| 84 | + def columnLoop(x: Int, accumulator: Double): Double = { |
| 85 | + if (x < sourceLength) { |
| 86 | + columnLoop(x + 1, accumulator + matrix(y * (sourceLength + 1) + x) * source(x)) |
| 87 | + } else { |
| 88 | + accumulator |
| 89 | + } |
| 90 | + } |
| 91 | + destination(y) = columnLoop(0, matrix(y * (sourceLength + 1) + sourceLength)) |
| 92 | + |
| 93 | + rowLoop(y + 1) |
| 94 | + } |
| 95 | + } |
| 96 | + rowLoop(0) |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments