1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Np; |
6
|
|
|
|
7
|
|
|
use Np\core\{nd,blas,lapack}; |
8
|
|
|
use Np\exceptions\invalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Vector (rank-1) |
12
|
|
|
* |
13
|
|
|
* @package Np |
14
|
|
|
* @category Scientific Library for PHP |
15
|
|
|
* @author ghost (Shubham Chaudhary) |
16
|
|
|
* @email [email protected] |
17
|
|
|
* @copyright (c) 2020-2021, Shubham Chaudhary |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class vector extends nd { |
21
|
|
|
use ops,linAlgb\linAlg; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Factory method to build a new vector. |
25
|
|
|
* |
26
|
|
|
* @param int $col |
27
|
|
|
* @param int $dtype |
28
|
|
|
* @return vector |
29
|
|
|
*/ |
30
|
|
|
public static function factory(int $col, int $dtype = self::DOUBLE): vector { |
31
|
|
|
return new self($col, $dtype); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Build a new vector from a php array. |
36
|
|
|
* |
37
|
|
|
* @param array $data |
38
|
|
|
* @return vector |
39
|
|
|
*/ |
40
|
|
|
public static function ar(array $data): vector { |
41
|
|
|
if (is_array($data) && !is_array($data[0])) { |
42
|
|
|
$ar = self::factory(count($data)); |
43
|
|
|
$ar->setData($data); |
44
|
|
|
return $ar; |
45
|
|
|
} else { |
46
|
|
|
self::_err('data must be of same dimensions'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return vector with random values |
52
|
|
|
* @param int $col |
53
|
|
|
* @return vector |
54
|
|
|
*/ |
55
|
|
|
public static function randn(int $col): vector { |
56
|
|
|
$ar = self::factory($col); |
57
|
|
|
$max = getrandmax(); |
58
|
|
|
for ($i = 0; $i < $ar->col; ++$i) { |
59
|
|
|
$ar->data[$i] = rand() / $max; |
60
|
|
|
} |
61
|
|
|
return $ar; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return vector with uniform values |
66
|
|
|
* @param int $col |
67
|
|
|
* @return vector |
68
|
|
|
*/ |
69
|
|
|
public static function uniform(int $col): vector { |
70
|
|
|
$ar = self::factory($col); |
71
|
|
|
$max = getrandmax(); |
72
|
|
|
for ($i = 0; $i < $col; ++$i) { |
73
|
|
|
$ar->data[$i] = rand(-$max, $max) / $max; |
74
|
|
|
} |
75
|
|
|
return $ar; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Build a vector of zeros with n elements. |
80
|
|
|
* |
81
|
|
|
* @param int $col |
82
|
|
|
* @return vector |
83
|
|
|
*/ |
84
|
|
|
public static function zeros(int $col): vector { |
85
|
|
|
$ar = self::factory($col); |
86
|
|
|
for ($i = 0; $i < $col; ++$i) { |
87
|
|
|
$ar->data[$i] = 0; |
88
|
|
|
} |
89
|
|
|
return $ar; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* create one like vector |
94
|
|
|
* |
95
|
|
|
* @param int $col |
96
|
|
|
* @return vector |
97
|
|
|
*/ |
98
|
|
|
public static function ones(int $col): vector { |
99
|
|
|
$ar = self::factory($col); |
100
|
|
|
for ($i = 0; $i < $col; ++$i) { |
101
|
|
|
$ar->data[$i] = 1; |
102
|
|
|
} |
103
|
|
|
return $ar; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* create a null like vector |
108
|
|
|
* @param int $col |
109
|
|
|
* @return vector |
110
|
|
|
*/ |
111
|
|
|
public static function null(int $col): vector { |
112
|
|
|
$ar = self::factory($col); |
113
|
|
|
for ($i = 0; $i < $col; ++$i) { |
114
|
|
|
$ar->data[$i] = null; |
115
|
|
|
} |
116
|
|
|
return $ar; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* create a vector with given scalar value |
121
|
|
|
* @param int $col |
122
|
|
|
* @param int|float|double $val |
123
|
|
|
* @return vector |
124
|
|
|
*/ |
125
|
|
|
public static function full(int $col, int|float $val): vector { |
126
|
|
|
$ar = self::factory($col); |
127
|
|
|
for ($i = 0; $i < $col; ++$i) { |
128
|
|
|
$ar->data[$i] = $val; |
129
|
|
|
} |
130
|
|
|
return $ar; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return evenly spaced values within a given interval. |
135
|
|
|
* |
136
|
|
|
* @param int|float $start |
137
|
|
|
* @param int|float $end |
138
|
|
|
* @param int|float $interval |
139
|
|
|
* @return vector |
140
|
|
|
*/ |
141
|
|
|
public static function range(int|float $start, int|float $end, int|float $interval = 1): vector { |
142
|
|
|
return self::ar(range($start, $end, $interval)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Return a Gaussian random vector with mean 0 |
147
|
|
|
* and unit variance. |
148
|
|
|
* |
149
|
|
|
* @param int $n |
150
|
|
|
* @return self |
151
|
|
|
*/ |
152
|
|
|
public static function gaussian(int $n): vector { |
153
|
|
|
$max = getrandmax(); |
154
|
|
|
$a = []; |
155
|
|
|
while (count($a) < $n) { |
156
|
|
|
$r = sqrt(-2.0 * log(rand() / $max)); |
157
|
|
|
$phi = rand() / $max * (2. * M_PI); |
158
|
|
|
$a[] = $r * sin($phi); |
159
|
|
|
$a[] = $r * cos($phi); |
160
|
|
|
} |
161
|
|
|
if (count($a) > $n) { |
162
|
|
|
$a = array_slice($a, 0, $n); |
163
|
|
|
} |
164
|
|
|
return self::ar($a); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Generate a vector with n elements from a Poisson distribution. |
169
|
|
|
* |
170
|
|
|
* @param int $n |
171
|
|
|
* @param float $lambda |
172
|
|
|
* @return vector |
173
|
|
|
*/ |
174
|
|
|
public static function poisson(int $n, float $lambda = 1.0): vector { |
175
|
|
|
$max = getrandmax(); |
176
|
|
|
$l = exp(-$lambda); |
177
|
|
|
$a = new self($n); |
178
|
|
|
for ($i = 0; $i < $n; ++$i) { |
179
|
|
|
$k = 0; |
180
|
|
|
$p = 1.0; |
181
|
|
|
while ($p > $l) { |
182
|
|
|
++$k; |
183
|
|
|
$p *= rand() / $max; |
184
|
|
|
} |
185
|
|
|
$a->data[$i] = $k - 1; |
186
|
|
|
} |
187
|
|
|
return $a; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Return a vector of n evenly spaced numbers between minimum and maximum. |
192
|
|
|
* |
193
|
|
|
* @param float $min |
194
|
|
|
* @param float $max |
195
|
|
|
* @param int $n |
196
|
|
|
* @throws invalidArgumentException |
197
|
|
|
* @return vector |
198
|
|
|
*/ |
199
|
|
|
public static function linspace(float $min, float $max, int $n): vector { |
200
|
|
|
if ($min > $max) { |
201
|
|
|
throw new invalidArgumentException('Minimum must be less than maximum.'); |
202
|
|
|
} |
203
|
|
|
if ($n < 2) { |
204
|
|
|
throw new invalidArgumentException('Number of elements must be greater than 1.'); |
205
|
|
|
} |
206
|
|
|
$k = $n - 1; |
207
|
|
|
$interval = abs($max - $min) / $k; |
208
|
|
|
$a = [$min]; |
209
|
|
|
while (count($a) < $k) { |
210
|
|
|
$a[] = end($a) + $interval; |
211
|
|
|
} |
212
|
|
|
$a[] = $max; |
213
|
|
|
return self::ar($a); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Return the index of the minimum element in the vector. |
218
|
|
|
* |
219
|
|
|
* @return int |
220
|
|
|
*/ |
221
|
|
|
public function argMin(): int { |
222
|
|
|
return blas::min($this); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Return the index of the maximum element in the vector. |
227
|
|
|
* |
228
|
|
|
* @return int |
229
|
|
|
*/ |
230
|
|
|
public function argMax(): int { |
231
|
|
|
return blas::max($this); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* The sum of the vector. |
236
|
|
|
* @return float |
237
|
|
|
*/ |
238
|
|
|
public function sum(): float { |
239
|
|
|
return blas::asum($this); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Return the product of the vector. |
244
|
|
|
* @return int|float |
245
|
|
|
*/ |
246
|
|
|
public function product(): float { |
247
|
|
|
$r = 1.0; |
248
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
249
|
|
|
$r *= $this->data[$i]; |
250
|
|
|
} |
251
|
|
|
return $r; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Compute the vector-matrix dot product of this vector and matrix . |
256
|
|
|
* @param \Np\matrix $m |
257
|
|
|
* @return vector |
258
|
|
|
*/ |
259
|
|
|
public function dotMatrix(\Np\matrix $m): vector { |
260
|
|
|
if ($this->checkDtype($this, $m)) { |
261
|
|
|
$mvr = self::factory($this->col); |
262
|
|
|
core\blas::gemv($m, $this, $mvr); |
263
|
|
|
return $mvr; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* |
269
|
|
|
* @param int|float|matrix|vector $d |
270
|
|
|
* @return matrix|vector |
271
|
|
|
*/ |
272
|
|
|
public function divide(int|float|matrix|vector $d): matrix|vector { |
273
|
|
|
if ($d instanceof matrix) { |
274
|
|
|
return $this->divideMatrix($d); |
275
|
|
|
} |
276
|
|
|
if ($d instanceof self) { |
277
|
|
|
return $this->divideVector($d); |
278
|
|
|
} |
279
|
|
|
return $this->divideScalar($d); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* |
284
|
|
|
* @param \Np\matrix $m |
285
|
|
|
* @return matrix |
286
|
|
|
*/ |
287
|
|
|
protected function divideMatrix(\Np\matrix $m): matrix { |
288
|
|
|
if ($this->checkShape($this, $m)) { |
289
|
|
|
$vr = matrix::factory($m->row, $m->col); |
290
|
|
|
for ($i = 0; $i < $m->row; ++$i) { |
291
|
|
|
for ($j = 0; $j < $m->col; ++$j) { |
292
|
|
|
$vr->data[$i * $m->col + $j] = $this->data[$j] / $m->data[$i * $m->col + $j]; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
return $vr; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* |
301
|
|
|
* @param vector $v |
302
|
|
|
* @return vector |
303
|
|
|
*/ |
304
|
|
|
protected function divideVector(vector $v): vector { |
305
|
|
|
if ($this->checkShape($this, $v)) { |
306
|
|
|
$vr = self::factory($this->col); |
307
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
308
|
|
|
$vr->data[$i] = $this->data[$i] / $v->data[$i]; |
309
|
|
|
} |
310
|
|
|
return $vr; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* |
316
|
|
|
* @param int|float $s |
317
|
|
|
* @return vector |
318
|
|
|
*/ |
319
|
|
|
protected function divideScalar(int|float $s): vector { |
320
|
|
|
$vr = self::factory($this->col); |
321
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
322
|
|
|
$vr->data[$i] = $this->data[$i] / $s; |
323
|
|
|
} |
324
|
|
|
return $vr; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* |
329
|
|
|
* @param int|float|matrix|vector $d |
330
|
|
|
* @return matrix|vector |
331
|
|
|
*/ |
332
|
|
|
public function multiply(int|float|matrix|vector $d): matrix|vector { |
333
|
|
|
if ($d instanceof matrix) { |
334
|
|
|
return $this->multiplyMatrix($d); |
335
|
|
|
} |
336
|
|
|
if ($d instanceof self) { |
337
|
|
|
return $this->multiplyVector($d); |
338
|
|
|
} |
339
|
|
|
return $this->multiplyScalar($d); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* |
344
|
|
|
* @param \Np\matrix $m |
345
|
|
|
* @return matrix |
346
|
|
|
*/ |
347
|
|
|
protected function multiplyMatrix(\Np\matrix $m): matrix { |
348
|
|
|
if ($this->checkShape($this, $m)) { |
349
|
|
|
$vr = matrix::factory($m->row, $m->col); |
350
|
|
|
for ($i = 0; $i < $m->row; ++$i) { |
351
|
|
|
for ($j = 0; $j < $m->col; ++$j) { |
352
|
|
|
$vr->data[$i * $m->col + $j] = $this->data[$j] * $m->data[$i * $m->col + $j]; |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
return $vr; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* |
361
|
|
|
* @param \Np\vector $vector |
362
|
|
|
* @return vector |
363
|
|
|
*/ |
364
|
|
|
protected function multiplyVector(\Np\vector $vector): vector { |
365
|
|
|
if ($this->checkShape($this, $vector)) { |
366
|
|
|
$vr = self::factory($this->col); |
367
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
368
|
|
|
$vr->data[$i] = $this->data[$i] * $vector->data[$i]; |
369
|
|
|
} |
370
|
|
|
return $vr; |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* |
376
|
|
|
* @param int|float $s |
377
|
|
|
* @return vector |
378
|
|
|
*/ |
379
|
|
|
protected function multiplyScalar(int|float $s): vector { |
380
|
|
|
$vr = $this->copy(); |
381
|
|
|
blas::scale($s, $vr); |
382
|
|
|
return $vr; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* |
387
|
|
|
* @param int|float|matrix|vector $d |
388
|
|
|
* @return matrix|vector |
389
|
|
|
*/ |
390
|
|
|
public function add(int|float|matrix|vector $d): matrix|vector { |
391
|
|
|
if ($d instanceof matrix) { |
392
|
|
|
return $this->addMatrix($d); |
393
|
|
|
} |
394
|
|
|
if ($d instanceof self) { |
395
|
|
|
return $this->addVector($d); |
396
|
|
|
} |
397
|
|
|
return $this->addScalar($d); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* |
402
|
|
|
* @param \Np\matrix $m |
403
|
|
|
* @return matrix |
404
|
|
|
*/ |
405
|
|
|
protected function addMatrix(\Np\matrix $m): matrix { |
406
|
|
|
if ($this->checkShape($this, $m)) { |
407
|
|
|
$vr = matrix::factory($m->row, $m->col); |
408
|
|
|
for ($i = 0; $i < $m->row; ++$i) { |
409
|
|
|
for ($j = 0; $j < $m->col; ++$j) { |
410
|
|
|
$vr->data[$i * $m->col + $j] = $this->data[$j] + $m->data[$i * $m->col + $j]; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
return $vr; |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* |
419
|
|
|
* @param \Np\vector $vector |
420
|
|
|
* @return vector |
421
|
|
|
*/ |
422
|
|
|
protected function addVector(\Np\vector $vector): vector { |
423
|
|
|
if ($this->checkShape($this, $vector)) { |
424
|
|
|
$vr = self::factory($this->col); |
425
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
426
|
|
|
$vr->data[$i] = $this->data[$i] + $vector->data[$i]; |
427
|
|
|
} |
428
|
|
|
return $vr; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* |
434
|
|
|
* @param int|float $s |
435
|
|
|
* @return vector |
436
|
|
|
*/ |
437
|
|
|
protected function addScalar(int|float $s): vector { |
438
|
|
|
$vr = $this->copy(); |
439
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
440
|
|
|
$vr->data[$i] += $s; |
441
|
|
|
} |
442
|
|
|
return $vr; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* |
447
|
|
|
* @param int|float|\Np\matrix|\Np\vector $d |
448
|
|
|
* @return matrix|vector |
449
|
|
|
*/ |
450
|
|
|
public function pow(int|float|\Np\matrix|\Np\vector $d): matrix|vector { |
451
|
|
|
if ($d instanceof matrix) { |
452
|
|
|
return $this->powMatrix($d); |
453
|
|
|
} |
454
|
|
|
if ($d instanceof vector) { |
455
|
|
|
return $this->powVector($d); |
456
|
|
|
} |
457
|
|
|
return $this->powScalar($d); |
458
|
|
|
|
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* |
463
|
|
|
* @param \Np\matrix $m |
464
|
|
|
* @return matrix |
465
|
|
|
*/ |
466
|
|
|
protected function powMatrix(\Np\matrix $m): matrix { |
467
|
|
|
if ($this->checkDimensions($this, $m)) { |
468
|
|
|
$ar = matrix::factory($m->row, $m->col); |
469
|
|
|
for ($i = 0; $i < $m->row; ++$i) { |
470
|
|
|
for ($j = 0; $j < $m->col; ++$j) { |
471
|
|
|
$ar->data[$i * $m->col + $j] = $m->data[$i * $m->col + $j] ** $this->data[$j]; |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
return $ar; |
475
|
|
|
} |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* |
480
|
|
|
* @param \Np\vector $vector |
481
|
|
|
* @return vector |
482
|
|
|
*/ |
483
|
|
|
protected function powVector(\Np\vector $vector): vector { |
484
|
|
|
if ($this->checkShape($this, $vector)) { |
485
|
|
|
$vr = self::factory($this->col); |
486
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
487
|
|
|
$vr->data[$i] = $this->data[$i] ** $vector->data[$i]; |
488
|
|
|
} |
489
|
|
|
return $vr; |
490
|
|
|
} |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* |
495
|
|
|
* @param int|float $s |
496
|
|
|
* @return vector |
497
|
|
|
*/ |
498
|
|
|
protected function powScalar(int|float $s): vector { |
499
|
|
|
$v = $this->copy(); |
500
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
501
|
|
|
$v->data[$i] = $v->data[$i] ** $s; |
502
|
|
|
} |
503
|
|
|
return $v; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* |
508
|
|
|
* @param int|float|\Np\matrix|\Np\vector $d |
509
|
|
|
* @return matrix|vector |
510
|
|
|
*/ |
511
|
|
|
public function mod(int|float|\Np\matrix|\Np\vector $d): matrix|vector { |
512
|
|
|
if ($d instanceof matrix) { |
513
|
|
|
return $this->powMatrix($d); |
514
|
|
|
} |
515
|
|
|
if ($d instanceof vector) { |
516
|
|
|
return $this->powVector($d); |
517
|
|
|
} |
518
|
|
|
return $this->powScalar($d); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* |
523
|
|
|
* @param \Np\matrix $m |
524
|
|
|
* @return matrix |
525
|
|
|
*/ |
526
|
|
|
protected function modMatrix(\Np\matrix $m): matrix { |
527
|
|
|
if ($this->checkDimensions($this, $m)) { |
528
|
|
|
$ar = matrix::factory($m->row, $m->col); |
529
|
|
|
for ($i = 0; $i < $m->row; ++$i) { |
530
|
|
|
for ($j = 0; $j < $m->col; ++$j) { |
531
|
|
|
$ar->data[$i * $m->col + $j] = $m->data[$i * $m->col + $j] % $this->data[$j]; |
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
return $ar; |
535
|
|
|
} |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* |
540
|
|
|
* @param \Np\vector $vector |
541
|
|
|
* @return vector |
542
|
|
|
*/ |
543
|
|
|
protected function modVector(\Np\vector $vector): vector { |
544
|
|
|
if ($this->checkShape($this, $vector)) { |
545
|
|
|
$vr = self::factory($this->col); |
546
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
547
|
|
|
$vr->data[$i] = $this->data[$i] % $vector->data[$i]; |
548
|
|
|
} |
549
|
|
|
return $vr; |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* |
555
|
|
|
* @param int|float $s |
556
|
|
|
*/ |
557
|
|
|
protected function modScalar(int|float $s) { |
558
|
|
|
$v = $this->copy(); |
559
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
560
|
|
|
$v->data[$i] = $v->data[$i] % $s; |
561
|
|
|
} |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* |
566
|
|
|
* @param int|float|matrix|vector $d |
567
|
|
|
* @return matrix|vector |
568
|
|
|
*/ |
569
|
|
|
public function subtract(int|float|matrix|vector $d): matrix|vector { |
570
|
|
|
if ($d instanceof matrix) { |
571
|
|
|
return $this->subtractMatrix($d); |
572
|
|
|
} |
573
|
|
|
if ($d instanceof self) { |
574
|
|
|
return $this->subtractVector($d); |
575
|
|
|
} |
576
|
|
|
return $this->substractScalar($d); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* |
581
|
|
|
* @param \Np\matrix $m |
582
|
|
|
* @return matrix |
583
|
|
|
*/ |
584
|
|
|
protected function subtractMatrix(\Np\matrix $m): matrix { |
585
|
|
|
if ($this->checkShape($this, $m)) { |
586
|
|
|
$vr = matrix::factory($m->row, $m->col); |
587
|
|
|
for ($i = 0; $i < $m->row; ++$i) { |
588
|
|
|
for ($j = 0; $j < $m->col; ++$j) { |
589
|
|
|
$vr->data[$i * $m->col + $j] = $this->data[$j] - $m->data[$i * $m->col + $j]; |
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
return $vr; |
593
|
|
|
} |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* |
598
|
|
|
* @param \Np\vector $vector |
599
|
|
|
* @return vector |
600
|
|
|
*/ |
601
|
|
|
protected function subtractVector(\Np\vector $vector): vector { |
602
|
|
|
if ($this->checkShape($this, $vector) ) { |
603
|
|
|
$vr = self::factory($this->col); |
604
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
605
|
|
|
$vr->data[$i] = $this->data[$i] - $vector->data[$i]; |
606
|
|
|
} |
607
|
|
|
return $vr; |
608
|
|
|
} |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* |
613
|
|
|
* @param \Np\vector $scalar |
614
|
|
|
* @return \Np\vector |
615
|
|
|
*/ |
616
|
|
|
protected function substractScalar(int|float $scalar): vector { |
617
|
|
|
$vr = self::factory($this->col); |
618
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
619
|
|
|
$vr->data[$i] = $this->data[$i] - $scalar; |
620
|
|
|
} |
621
|
|
|
return $vr; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* |
626
|
|
|
* @param \Np\vector $v |
627
|
|
|
* @param int $stride |
628
|
|
|
* @return vector |
629
|
|
|
*/ |
630
|
|
|
public function convolve(\Np\vector $v, int $stride = 1): vector { |
631
|
|
|
return convolve::conv1D($this, $v, $stride); |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
public function max() { |
635
|
|
|
return $this->data[blas::max($this)]; |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
public function min() { |
639
|
|
|
$this->data[blas::min($this)]; |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Return the inner product of two vectors. |
644
|
|
|
* |
645
|
|
|
* @param \Np\vector $vector |
646
|
|
|
* |
647
|
|
|
*/ |
648
|
|
|
public function inner(\Np\vector $vector) { |
649
|
|
|
return $this->dotVector($vector); |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Calculate the L1 norm of the vector. |
654
|
|
|
* @return float |
655
|
|
|
*/ |
656
|
|
|
public function normL1(): float { |
657
|
|
|
return $this->abs()->sum(); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
public function normL2() { |
661
|
|
|
return sqrt($this->square()->sum()); |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
public function normMax() { |
665
|
|
|
return $this->abs()->max(); |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
public function normP(float $p = 2.5) { |
669
|
|
|
if ($p <= 0.0) { |
670
|
|
|
self::_invalidArgument('P must be greater than 0.0 !'); |
671
|
|
|
} |
672
|
|
|
return $this->abs()->powScalar($p)->sum() ** (1.0 / $p); |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Return the reciprocal of the vector element-wise. |
677
|
|
|
* |
678
|
|
|
* @return self |
679
|
|
|
*/ |
680
|
|
|
public function reciprocal(): vector { |
681
|
|
|
return self::ones($this->col)->divideVector($this); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* |
686
|
|
|
* @return int|float |
687
|
|
|
*/ |
688
|
|
|
public function mean():int|float { |
689
|
|
|
return $this->sum()/ $this->col; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* |
694
|
|
|
* @return int|float |
695
|
|
|
*/ |
696
|
|
|
public function median():int|float { |
697
|
|
|
$mid = intdiv($this->col, 2); |
698
|
|
|
|
699
|
|
|
$a = $this->copy()->sort(); |
700
|
|
|
if ($this->col % 2 === 1) { |
701
|
|
|
$median = $a->data[$mid]; |
702
|
|
|
} else { |
703
|
|
|
$median = ($a->data[$mid - 1] + $a->data[$mid]) / 2.; |
704
|
|
|
} |
705
|
|
|
return $median; |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
public function variance($mean = null) |
709
|
|
|
{ |
710
|
|
|
if (is_null($mean)) { |
711
|
|
|
$mean = $this->mean(); |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
$sd = $this->substractScalar($mean)->square()->sum(); |
715
|
|
|
|
716
|
|
|
return $sd / $this->col; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* |
721
|
|
|
* @return vector |
722
|
|
|
*/ |
723
|
|
|
public function square(): vector { |
724
|
|
|
return $this->multiplyVector($this); |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
/** |
728
|
|
|
* sort the vector |
729
|
|
|
* @param string $type i or d |
730
|
|
|
* |
731
|
|
|
*/ |
732
|
|
|
public function sort($type = 'i') { |
733
|
|
|
lapack::sort($this, $type); |
734
|
|
|
return $this; |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* set data to vector |
739
|
|
|
* @param int|float|array $data |
740
|
|
|
*/ |
741
|
|
|
public function setData(int|float|array $data) { |
742
|
|
|
if (is_array($data) && !is_array($data[0])) { |
743
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
744
|
|
|
$this->data[$i] = $data[$i]; |
745
|
|
|
} |
746
|
|
|
} |
747
|
|
|
if (is_numeric($data)) { |
748
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
749
|
|
|
$this->data[$i] = $data; |
750
|
|
|
} |
751
|
|
|
} |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
/** |
755
|
|
|
* get the size of vector |
756
|
|
|
* @return int |
757
|
|
|
*/ |
758
|
|
|
public function getSize(): int { |
759
|
|
|
return $this->col; |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
public function getDtype() { |
763
|
|
|
return $this->dtype; |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
public function asArray() { |
767
|
|
|
$ar = array_fill(0, $this->col, null); |
768
|
|
|
for ($i = 0; $i < $this->col; ++$i) { |
769
|
|
|
$ar[$i] = $this->data[$i]; |
770
|
|
|
} |
771
|
|
|
return $ar; |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
public function printVector() { |
775
|
|
|
echo __CLASS__ . PHP_EOL; |
776
|
|
|
for ($j = 0; $j < $this->col; ++$j) { |
777
|
|
|
printf('%lf ', $this->data[$j]); |
778
|
|
|
} |
779
|
|
|
echo PHP_EOL; |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
public function __toString() { |
783
|
|
|
return (string) $this->printVector(); |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
protected function __construct(public int $col, int $dtype = self::DOUBLE) { |
787
|
|
|
if ($this->col < 1) { |
788
|
|
|
throw new invalidArgumentException('* To create Numphp/Vector col must be greater than 0!, Op Failed! * '); |
789
|
|
|
} |
790
|
|
|
parent::__construct($this->col, $dtype); |
791
|
|
|
return $this; |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
} |
795
|
|
|
|