PluginProbe
TablePress – Tables in WordPress made easy / 2.2.5
TablePress – Tables in WordPress made easy v2.2.5
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
tablepress / libraries / vendor / Matrix / Operators / DirectSum.php

DirectSum.php in TablePress – Tables in WordPress made easy 2.2.5, at libraries/vendor/Matrix/Operators/DirectSum.php

65 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\Matrix\Operators;
4
5 use TablePress\Matrix\Matrix;
6 use TablePress\Matrix\Exception;
7
8 class DirectSum extends Operator
9 {
10 /**
11 * Execute the addition
12 *
13 * @param mixed $value The matrix or numeric value to add to the current base value
14 * @return $this The operation object, allowing multiple additions to be chained
15 * @throws Exception If the provided argument is not appropriate for the operation
16 */
17 public function execute($value): Operator
18 {
19 if (is_array($value)) {
20 $value = new Matrix($value);
21 }
22
23 if ($value instanceof Matrix) {
24 return $this->directSumMatrix($value);
25 }
26
27 throw new Exception('Invalid argument for addition');
28 }
29
30 /**
31 * Execute the direct sum for a matrix
32 *
33 * @param TablePress\Matrix $value The numeric value to concatenate/direct sum with the current base value
34 * @return $this The operation object, allowing multiple additions to be chained
35 **/
36 private function directSumMatrix($value): Operator
37 {
38 $originalColumnCount = count($this->matrix[0]);
39 $originalRowCount = count($this->matrix);
40 $valColumnCount = $value->columns;
41 $valRowCount = $value->rows;
42 $value = $value->toArray();
43
44 for ($row = 0; $row < $this->rows; ++$row) {
45 $this->matrix[$row] = array_merge($this->matrix[$row], array_fill(0, $valColumnCount, 0));
46 }
47
48 $this->matrix = array_merge(
49 $this->matrix,
50 array_fill(0, $valRowCount, array_fill(0, $originalColumnCount, 0))
51 );
52
53 for ($row = $originalRowCount; $row < $originalRowCount + $valRowCount; ++$row) {
54 array_splice(
55 $this->matrix[$row],
56 $originalColumnCount,
57 $valColumnCount,
58 $value[$row - $originalRowCount]
59 );
60 }
61
62 return $this;
63 }
64 }
65