nd::checkDimensions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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