|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Np; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Operations |
|
7
|
|
|
* A fast lite & memory efficient Scientific Computing for php |
|
8
|
|
|
* |
|
9
|
|
|
* @package Np |
|
10
|
|
|
* @category Scientific Computing |
|
11
|
|
|
* @author ghost (Shubham Chaudhary) |
|
12
|
|
|
* @email [email protected] |
|
13
|
|
|
* @copyright (c) 2020-2021, Shubham Chaudhary |
|
14
|
|
|
*/ |
|
15
|
|
|
trait ops { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Return the element-wise maximum of two matrices| two vectors. |
|
19
|
|
|
* @param matrix|vector $d |
|
20
|
|
|
* @return matrix|vector |
|
21
|
|
|
*/ |
|
22
|
|
|
public function max(matrix|vector $d): matrix|vector { |
|
23
|
|
|
if ($this instanceof matrix && $d instanceof matrix && $this->checkShape($this, $d)) { |
|
24
|
|
|
$r = self::factory($this->row, $this->col); |
|
25
|
|
|
} elseif ($this instanceof vector && $d instanceof vector && $this->checkShape($this, $d)) { |
|
26
|
|
|
$r = self::factory($this->col); |
|
27
|
|
|
} |
|
28
|
|
|
for ($i = 0; $i < $this->ndim; ++$i) { |
|
29
|
|
|
$r->data[$i] = max($this->data[$i], $d->data[$i]); |
|
30
|
|
|
} |
|
31
|
|
|
return $r; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Return the element-wise minimum of two matrices|two vectors. |
|
36
|
|
|
* @param matrix|vector $d |
|
37
|
|
|
* @return matrix|vector |
|
38
|
|
|
*/ |
|
39
|
|
|
public function min(matrix|vector $d): matrix|vector { |
|
40
|
|
|
if ($this instanceof matrix && $d instanceof matrix && $this->checkShape($this, $d)) { |
|
41
|
|
|
$r = self::factory($this->row, $this->col); |
|
42
|
|
|
} elseif ($this instanceof vector && $d instanceof vector && $this->checkShape($this, $d)) { |
|
43
|
|
|
$r = self::factory($this->col); |
|
44
|
|
|
} |
|
45
|
|
|
for ($i = 0; $i < $this->ndim; ++$i) { |
|
46
|
|
|
$r->data[$i] = max($this->data[$i], $d->data[$i]); |
|
47
|
|
|
} |
|
48
|
|
|
return $r; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Run a function over all of the elements in the matrix|vector. |
|
53
|
|
|
* @param callable $func |
|
54
|
|
|
* @return matrix|vector |
|
55
|
|
|
*/ |
|
56
|
|
|
public function map(callable $func): matrix|vector { |
|
57
|
|
|
if ($this instanceof matrix) { |
|
58
|
|
|
$r = self::factory($this->row, $this->col); |
|
59
|
|
|
} else { |
|
60
|
|
|
$r = self::factory($this->col); |
|
61
|
|
|
} |
|
62
|
|
|
for ($i = 0; $i < $this->ndim; ++$i) { |
|
63
|
|
|
$r->data[$i] = $func($this->data[$i]); |
|
64
|
|
|
} |
|
65
|
|
|
return $r; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function abs(): matrix|vector { |
|
69
|
|
|
return $this->map('abs'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function sqrt(): matrix|vector { |
|
73
|
|
|
return $this->map('sqrt'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function exp(): matrix|vector { |
|
77
|
|
|
return $this->map('exp'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function exp1(): matrix|vector { |
|
81
|
|
|
return $this->map('exp1'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function log(float $b = M_E): matrix|vector { |
|
85
|
|
|
$ar = $this->copy(); |
|
86
|
|
|
for ($i = 0; $i < $ar->ndim; ++$i) { |
|
87
|
|
|
log($ar->data[$i], $b); |
|
88
|
|
|
} |
|
89
|
|
|
return $ar; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function log1p(): matrix|vector { |
|
93
|
|
|
return $this->map('log1p'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function sin(): matrix|vector { |
|
97
|
|
|
return $this->map('sin'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function asin(): matrix|vector { |
|
101
|
|
|
return $this->map('asin'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function cos(): matrix|vector { |
|
105
|
|
|
return $this->map('cos'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function acos(): matrix|vector { |
|
109
|
|
|
return $this->map('acos'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function tan(): matrix|vector { |
|
113
|
|
|
return $this->map('tan'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function atan(): matrix|vector { |
|
117
|
|
|
return $this->map('atan'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function radToDeg(): matrix|vector { |
|
121
|
|
|
return $this->map('rad2deg'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function degToRad(): matrix|vector { |
|
125
|
|
|
return $this->map('deg2rad'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function floor(): matrix|vector { |
|
129
|
|
|
return $this->map('floor'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function ceil(): matrix|vector { |
|
133
|
|
|
return $this->map('ceil'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function free():void { |
|
137
|
|
|
if($this instanceof matrix) { |
|
138
|
|
|
unset($this->row); |
|
139
|
|
|
unset($this->col); |
|
140
|
|
|
unset($this->ndim); |
|
141
|
|
|
unset($this->dtype); |
|
142
|
|
|
unset($this->data); |
|
143
|
|
|
return; |
|
144
|
|
|
} |
|
145
|
|
|
unset($this->col); |
|
146
|
|
|
unset($this->ndim); |
|
147
|
|
|
unset($this->dtype); |
|
148
|
|
|
unset($this->data); |
|
149
|
|
|
return; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* make a copy of matrix|vector; |
|
154
|
|
|
* @return matrix|vector |
|
155
|
|
|
*/ |
|
156
|
|
|
public function copy(): matrix|vector { |
|
157
|
|
|
return clone $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Clip the elements in the matrix to be between given minimum and maximum |
|
162
|
|
|
* and return a new matrix. |
|
163
|
|
|
* |
|
164
|
|
|
* @param float $min |
|
165
|
|
|
* @param float $max |
|
166
|
|
|
* @return matrix |
|
167
|
|
|
*/ |
|
168
|
|
|
public function clip(float $min, float $max): matrix|vector { |
|
169
|
|
|
if ($this instanceof matrix) { |
|
170
|
|
|
$ar = self::factory($this->row, $this->col); |
|
171
|
|
|
} else { |
|
172
|
|
|
$ar = self::factory($this->col); |
|
173
|
|
|
} |
|
174
|
|
|
for ($i = 0; $i < $this->ndim; ++$i) { |
|
175
|
|
|
if ($this->data[$i] > $max) { |
|
176
|
|
|
$ar->data[$i] = $max; |
|
177
|
|
|
continue; |
|
178
|
|
|
} |
|
179
|
|
|
if ($this->data[$i] < $min) { |
|
180
|
|
|
$ar->data[$i] = $min; |
|
181
|
|
|
continue; |
|
182
|
|
|
} |
|
183
|
|
|
$ar->data[$i] = $this->data[$i]; |
|
184
|
|
|
} |
|
185
|
|
|
return $ar; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Clip the matrix|vector to be lower bounded by a given minimum. |
|
190
|
|
|
* @param float $min |
|
191
|
|
|
* @return matrix |
|
192
|
|
|
*/ |
|
193
|
|
|
public function clipLower(float $min): matrix|vector { |
|
194
|
|
|
if ($this instanceof matrix) { |
|
195
|
|
|
$ar = self::factory($this->row, $this->col); |
|
196
|
|
|
} else { |
|
197
|
|
|
$ar = self::factory($this->col); |
|
198
|
|
|
} |
|
199
|
|
|
for ($i = 0; $i < $this->ndim; ++$i) { |
|
200
|
|
|
if ($this->data[$i] < $min) { |
|
201
|
|
|
$ar->data[$i] = $min; |
|
202
|
|
|
continue; |
|
203
|
|
|
} |
|
204
|
|
|
$ar->data[$i] = $this->data[$i]; |
|
205
|
|
|
} |
|
206
|
|
|
return $ar; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Clip the matrix|vector to be upper bounded by a given maximum. |
|
211
|
|
|
* |
|
212
|
|
|
* @param float $max |
|
213
|
|
|
* @return matrix |
|
214
|
|
|
*/ |
|
215
|
|
|
public function clipUpper(float $max): matrix|vector { |
|
216
|
|
|
if ($this instanceof matrix) { |
|
217
|
|
|
$ar = self::factory($this->row, $this->col); |
|
218
|
|
|
} else { |
|
219
|
|
|
$ar = self::factory($this->col); |
|
220
|
|
|
} |
|
221
|
|
|
for ($i = 0; $i < $this->ndim; ++$i) { |
|
222
|
|
|
if ($this->data[$i] > $max) { |
|
223
|
|
|
$ar->data[$i] = $max; |
|
224
|
|
|
continue; |
|
225
|
|
|
} |
|
226
|
|
|
$ar->data[$i] = $this->data[$i]; |
|
227
|
|
|
} |
|
228
|
|
|
return $ar; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* return a reshaped data buffer as matrix |
|
233
|
|
|
* @param int $row |
|
234
|
|
|
* @param int $col |
|
235
|
|
|
* @return matrix |
|
236
|
|
|
*/ |
|
237
|
|
|
public function reshape(int $row, int $col): matrix { |
|
238
|
|
|
if ($this->ndim != $row * $col) { |
|
239
|
|
|
self::_dimensionaMisMatchErr('given dimenssion is not valid for current bufferData'); |
|
240
|
|
|
} |
|
241
|
|
|
if ($this instanceof vector) { |
|
242
|
|
|
$ar = matrix::factory($row, $col); |
|
243
|
|
|
$ar->data = $this->data; |
|
244
|
|
|
return $ar; |
|
245
|
|
|
} |
|
246
|
|
|
$this->row = $row; |
|
|
|
|
|
|
247
|
|
|
$this->col = $col; |
|
|
|
|
|
|
248
|
|
|
return $this; |
|
|
|
|
|
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|