PluginProbe
TablePress – Tables in WordPress made easy / 3.0
TablePress – Tables in WordPress made easy v3.0
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 / Multiplication.php

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

87 lines 2.5 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\Builder;
7 use TablePress\Matrix\Exception;
8 use Throwable;
9
10 class Multiplication extends Operator
11 {
12 /**
13 * Execute the multiplication
14 *
15 * @param mixed $value The matrix or numeric value to multiply the current base value by
16 * @throws Exception If the provided argument is not appropriate for the operation
17 * @return $this The operation object, allowing multiple multiplications to be chained
18 **/
19 public function execute($value, string $type = 'multiplication'): Operator
20 {
21 if (is_array($value)) {
22 $value = new Matrix($value);
23 }
24
25 if (is_object($value) && ($value instanceof Matrix)) {
26 return $this->multiplyMatrix($value, $type);
27 } elseif (is_numeric($value)) {
28 return $this->multiplyScalar($value, $type);
29 }
30
31 throw new Exception("Invalid argument for $type");
32 }
33
34 /**
35 * Execute the multiplication for a scalar
36 *
37 * @param mixed $value The numeric value to multiply with the current base value
38 * @return $this The operation object, allowing multiple mutiplications to be chained
39 **/
40 protected function multiplyScalar($value, string $type = 'multiplication'): Operator
41 {
42 try {
43 for ($row = 0; $row < $this->rows; ++$row) {
44 for ($column = 0; $column < $this->columns; ++$column) {
45 $this->matrix[$row][$column] *= $value;
46 }
47 }
48 } catch (Throwable $e) {
49 throw new Exception("Invalid argument for $type");
50 }
51
52 return $this;
53 }
54
55 /**
56 * Execute the multiplication for a matrix
57 *
58 * @param TablePress\Matrix $value The numeric value to multiply with the current base value
59 * @return $this The operation object, allowing multiple mutiplications to be chained
60 * @throws Exception If the provided argument is not appropriate for the operation
61 **/
62 protected function multiplyMatrix(TablePress\Matrix $value, string $type = 'multiplication'): Operator
63 {
64 $this->validateReflectingDimensions($value);
65
66 $newRows = $this->rows;
67 $newColumns = $value->columns;
68 $matrix = Builder::createFilledMatrix(0, $newRows, $newColumns)
69 ->toArray();
70 try {
71 for ($row = 0; $row < $newRows; ++$row) {
72 for ($column = 0; $column < $newColumns; ++$column) {
73 $columnData = $value->getColumns($column + 1)->toArray();
74 foreach ($this->matrix[$row] as $key => $valueData) {
75 $matrix[$row][$column] += $valueData * $columnData[$key][0];
76 }
77 }
78 }
79 } catch (Throwable $e) {
80 throw new Exception("Invalid argument for $type");
81 }
82 $this->matrix = $matrix;
83
84 return $this;
85 }
86 }
87