|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Np\core; |
|
6
|
|
|
|
|
7
|
|
|
use Np\exceptions\{ |
|
8
|
|
|
dtypeException, |
|
9
|
|
|
invalidArgumentException, |
|
10
|
|
|
runtimeException, |
|
11
|
|
|
dimensionalityMismatch |
|
12
|
|
|
}; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* ND |
|
16
|
|
|
* A fast lite memory efficient Scientific Computing for php |
|
17
|
|
|
* |
|
18
|
|
|
* @package NumPhp |
|
19
|
|
|
* @category Scientific Computing |
|
20
|
|
|
* @author ghost (Shubham Chaudhary) |
|
21
|
|
|
* @email [email protected] |
|
22
|
|
|
* @copyright (c) 2020-2021, Shubham Chaudhary |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
class nd { |
|
26
|
|
|
|
|
27
|
|
|
const TWO_PI = 2. * M_PI, EPSILON = 1e-8; |
|
28
|
|
|
const FLOAT = 1, DOUBLE = 2, INT = 3; |
|
29
|
|
|
|
|
30
|
|
|
public $data; |
|
31
|
|
|
protected $_time = null, $_mem = null; |
|
32
|
|
|
|
|
33
|
|
|
public function checkDimensions(\Np\matrix|\Np\vector $Obj1, \Np\matrix $Obj2) { |
|
34
|
|
|
if($Obj1->col == $Obj2->row){ |
|
35
|
|
|
return true; |
|
36
|
|
|
} |
|
37
|
|
|
self::_dimensionaMisMatchErr('Mismatch Dimensions of given Objects! Obj-A col & Obj-B row amount need to be the same!'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function checkDtype(\Np\matrix|\Np\vector $Obj1, \Np\matrix|\Np\vector $Obj2){ |
|
41
|
|
|
if($Obj1->dtype == $Obj2->dtype) { |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
self::_dtypeErr('mismatch data type of given Np\Objects!'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function checkShape(\Np\matrix|\Np\vector $Obj1, \Np\matrix|\Np\vector $Obj2) { |
|
48
|
|
|
if ($Obj1 instanceof \Np\vector && $Obj2 instanceof \Np\vector) { |
|
49
|
|
|
if ($Obj1->col == $Obj2->col) { |
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
self::_dimensionaMisMatchErr('mismatch Dimensions of given vectors!'); |
|
53
|
|
|
} elseif ($Obj1 instanceof \Np\vector && $Obj2 instanceof \Np\matrix) { |
|
54
|
|
|
if ($Obj1->col == $Obj2->col) { |
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
self::_dimensionaMisMatchErr('mismatch Dimensions of given vectors & matrix!'); |
|
58
|
|
|
} else { |
|
59
|
|
|
if ($Obj1->row == $Obj2->row || $Obj1->col == $Obj2->col) { |
|
60
|
|
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
self::_dimensionaMisMatchErr('mismatch Dimensions of given matrix!'); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function __construct(public int $ndim, public int $dtype = self::FLOAT) { |
|
67
|
|
|
$this->getMemory(); |
|
68
|
|
|
$this->time(); |
|
69
|
|
|
switch ($this->dtype) { |
|
70
|
|
|
case self::FLOAT: |
|
71
|
|
|
$this->data = self::_ndFloat($this->ndim); |
|
72
|
|
|
break; |
|
73
|
|
|
case self::DOUBLE: |
|
74
|
|
|
$this->data = self::_ndDouble($this->ndim); |
|
75
|
|
|
break; |
|
76
|
|
|
case self::INT: |
|
77
|
|
|
$this->data = self::_ndInt($this->ndim); |
|
78
|
|
|
break; |
|
79
|
|
|
default : |
|
80
|
|
|
throw new dtypeException('given dtype is not supported by Np'); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected static function _ndFloat(int $size) { |
|
85
|
|
|
return \FFI::cast('float *', \FFI::new("float[$size]")); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected static function _ndDouble(int $size) { |
|
89
|
|
|
return \FFI::cast('double *', \FFI::new("double[$size]")); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected static function _ndInt(int $size) { |
|
93
|
|
|
return \FFI::cast('int *', \FFI::new("int[$size]")); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected static function _err($msg): runtimeException { |
|
97
|
|
|
throw new runtimeException($msg); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected static function _invalidArgument($argument): invalidArgumentException { |
|
101
|
|
|
throw new invalidArgumentException($argument); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected static function _dtypeErr($msg) : dtypeException { |
|
105
|
|
|
throw new dtypeException($msg); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected static function _dimensionaMisMatchErr($msg) :dimensionalityMismatch { |
|
109
|
|
|
throw new dimensionalityMismatch($msg); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* set Timer, get total time |
|
114
|
|
|
*/ |
|
115
|
|
|
public function time() { |
|
116
|
|
|
if (is_null($this->_time)) { |
|
117
|
|
|
$this->_time = microtime(true); |
|
118
|
|
|
} else { |
|
119
|
|
|
echo 'Time-Consumed:- ' . (microtime(true) - $this->_time) . PHP_EOL; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* set memory dog, get total memory |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getMemory() { |
|
127
|
|
|
if (is_null($this->_mem)) { |
|
128
|
|
|
$this->_mem = memory_get_usage(); |
|
129
|
|
|
} else { |
|
130
|
|
|
$memory = memory_get_usage() - $this->_mem; |
|
131
|
|
|
$unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb']; |
|
132
|
|
|
echo round($memory / pow(1024, ($i = floor(log($memory, 1024)))), 2) . $unit[$i] . PHP_EOL; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|