Passed
Push — main ( 306c22...682a0c )
by Shubham
01:38
created

nd::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 1
rs 10
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 function __destruct() {
85
    }
86
87
    protected static function _ndFloat(int $size) {
88
        return \FFI::cast('float *', \FFI::new("float[$size]"));
89
    }
90
91
    protected static function _ndDouble(int $size) {
92
        return \FFI::cast('double *', \FFI::new("double[$size]"));
93
    }
94
95
    protected static function _ndInt(int $size) {
96
        return \FFI::cast('int *', \FFI::new("int[$size]"));
97
    }
98
99
    protected static function _err($msg): runtimeException {
100
        throw new runtimeException($msg);
101
    }
102
103
    protected static function _invalidArgument($argument): invalidArgumentException {
104
        throw new invalidArgumentException($argument);
105
    }
106
    
107
    protected static function _dtypeErr($msg) : dtypeException {
108
        throw new dtypeException($msg);
109
    }
110
    
111
    protected static function _dimensionaMisMatchErr($msg) :dimensionalityMismatch {
112
        throw new dimensionalityMismatch($msg);
113
    }
114
115
    /**
116
     * set Timer, get total time 
117
     */
118
    public function time() {
119
        if (is_null($this->_time)) {
1 ignored issue
show
introduced by
The condition is_null($this->_time) is always false.
Loading history...
120
            $this->_time = microtime(true);
121
        } else {
122
            echo 'Time-Consumed:- ' . (microtime(true) - $this->_time) . PHP_EOL;
123
        }
124
    }
125
126
    /**
127
     * set memory dog, get total memory
128
     */
129
    public function getMemory() {
130
        if (is_null($this->_mem)) {
1 ignored issue
show
introduced by
The condition is_null($this->_mem) is always false.
Loading history...
131
            $this->_mem = memory_get_usage();
132
        } else {
133
            $memory = memory_get_usage() - $this->_mem;
134
            $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
135
            echo round($memory / pow(1024, ($i = floor(log($memory, 1024)))), 2) . $unit[$i] . PHP_EOL;
136
        }
137
    }
138
139
}
140