chengkun
2025-09-12 26c5c0296e7c094f9a7ae4a4bb3c975796992eaf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
 
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine;
 
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
 
class ArrayArgumentProcessor
{
    private static ArrayArgumentHelper $arrayArgumentHelper;
 
    public static function processArguments(
        ArrayArgumentHelper $arrayArgumentHelper,
        callable $method,
        mixed ...$arguments
    ): array {
        self::$arrayArgumentHelper = $arrayArgumentHelper;
 
        if (self::$arrayArgumentHelper->hasArrayArgument() === false) {
            return [$method(...$arguments)];
        }
 
        if (self::$arrayArgumentHelper->arrayArguments() === 1) {
            $nthArgument = self::$arrayArgumentHelper->getFirstArrayArgumentNumber();
 
            return self::evaluateNthArgumentAsArray($method, $nthArgument, ...$arguments);
        }
 
        $singleRowVectorIndex = self::$arrayArgumentHelper->getSingleRowVector();
        $singleColumnVectorIndex = self::$arrayArgumentHelper->getSingleColumnVector();
 
        if ($singleRowVectorIndex !== null && $singleColumnVectorIndex !== null) {
            // Basic logic for a single row vector and a single column vector
            return self::evaluateVectorPair($method, $singleRowVectorIndex, $singleColumnVectorIndex, ...$arguments);
        }
 
        $matrixPair = self::$arrayArgumentHelper->getMatrixPair();
        if ($matrixPair !== []) {
            if (
                (self::$arrayArgumentHelper->isVector($matrixPair[0]) === true
                    && self::$arrayArgumentHelper->isVector($matrixPair[1]) === false)
                || (self::$arrayArgumentHelper->isVector($matrixPair[0]) === false
                    && self::$arrayArgumentHelper->isVector($matrixPair[1]) === true)
            ) {
                // Logic for a matrix and a vector (row or column)
                return self::evaluateVectorMatrixPair($method, $matrixPair, ...$arguments);
            }
 
            // Logic for matrix/matrix, column vector/column vector or row vector/row vector
            return self::evaluateMatrixPair($method, $matrixPair, ...$arguments);
        }
 
        // Still need to work out the logic for more than two array arguments,
        // For the moment, we're throwing an Exception when we initialise the ArrayArgumentHelper
        return ['#VALUE!'];
    }
 
    private static function evaluateVectorMatrixPair(callable $method, array $matrixIndexes, mixed ...$arguments): array
    {
        $matrix2 = array_pop($matrixIndexes);
        /** @var array $matrixValues2 */
        $matrixValues2 = $arguments[$matrix2];
        $matrix1 = array_pop($matrixIndexes);
        /** @var array $matrixValues1 */
        $matrixValues1 = $arguments[$matrix1];
 
        $rows = min(array_map([self::$arrayArgumentHelper, 'rowCount'], [$matrix1, $matrix2]));
        $columns = min(array_map([self::$arrayArgumentHelper, 'columnCount'], [$matrix1, $matrix2]));
 
        if ($rows === 1) {
            $rows = max(array_map([self::$arrayArgumentHelper, 'rowCount'], [$matrix1, $matrix2]));
        }
        if ($columns === 1) {
            $columns = max(array_map([self::$arrayArgumentHelper, 'columnCount'], [$matrix1, $matrix2]));
        }
 
        $result = [];
        for ($rowIndex = 0; $rowIndex < $rows; ++$rowIndex) {
            for ($columnIndex = 0; $columnIndex < $columns; ++$columnIndex) {
                $rowIndex1 = self::$arrayArgumentHelper->isRowVector($matrix1) ? 0 : $rowIndex;
                $columnIndex1 = self::$arrayArgumentHelper->isColumnVector($matrix1) ? 0 : $columnIndex;
                $value1 = $matrixValues1[$rowIndex1][$columnIndex1];
                $rowIndex2 = self::$arrayArgumentHelper->isRowVector($matrix2) ? 0 : $rowIndex;
                $columnIndex2 = self::$arrayArgumentHelper->isColumnVector($matrix2) ? 0 : $columnIndex;
                $value2 = $matrixValues2[$rowIndex2][$columnIndex2];
                $arguments[$matrix1] = $value1;
                $arguments[$matrix2] = $value2;
 
                $result[$rowIndex][$columnIndex] = $method(...$arguments);
            }
        }
 
        return $result;
    }
 
    private static function evaluateMatrixPair(callable $method, array $matrixIndexes, mixed ...$arguments): array
    {
        $matrix2 = array_pop($matrixIndexes);
        /** @var array $matrixValues2 */
        $matrixValues2 = $arguments[$matrix2];
        $matrix1 = array_pop($matrixIndexes);
        /** @var array $matrixValues1 */
        $matrixValues1 = $arguments[$matrix1];
 
        $result = [];
        foreach ($matrixValues1 as $rowIndex => $row) {
            foreach ($row as $columnIndex => $value1) {
                if (isset($matrixValues2[$rowIndex][$columnIndex]) === false) {
                    continue;
                }
 
                $value2 = $matrixValues2[$rowIndex][$columnIndex];
                $arguments[$matrix1] = $value1;
                $arguments[$matrix2] = $value2;
 
                $result[$rowIndex][$columnIndex] = $method(...$arguments);
            }
        }
 
        return $result;
    }
 
    private static function evaluateVectorPair(callable $method, int $rowIndex, int $columnIndex, mixed ...$arguments): array
    {
        $rowVector = Functions::flattenArray($arguments[$rowIndex]);
        $columnVector = Functions::flattenArray($arguments[$columnIndex]);
 
        $result = [];
        foreach ($columnVector as $column) {
            $rowResults = [];
            foreach ($rowVector as $row) {
                $arguments[$rowIndex] = $row;
                $arguments[$columnIndex] = $column;
 
                $rowResults[] = $method(...$arguments);
            }
            $result[] = $rowResults;
        }
 
        return $result;
    }
 
    /**
     * Note, offset is from 1 (for the first argument) rather than from 0.
     */
    private static function evaluateNthArgumentAsArray(callable $method, int $nthArgument, mixed ...$arguments): array
    {
        $values = array_slice($arguments, $nthArgument - 1, 1);
        /** @var array $values */
        $values = array_pop($values);
 
        $result = [];
        foreach ($values as $value) {
            $arguments[$nthArgument - 1] = $value;
            $result[] = $method(...$arguments);
        }
 
        return $result;
    }
}