Passed
Push — main ( 595812...39c26d )
by Shubham
01:50
created

nd::_dimensionaMisMatchErr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
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, $ndim, $dtype;
31
    public static $_time = null, $_mem = null;
32
33
    protected function __construct(int $size, int $dtype = self::FLOAT) {
34
        $this->ndim = $size;
35
        $this->dtype = $dtype;
36
        switch ($dtype) {
37
            case self::FLOAT:
38
                $this->data = self::_ndFloat($this->ndim);
39
                break;
40
            case self::DOUBLE:
41
                $this->data = self::_ndDouble($this->ndim);
42
                break;
43
            case self::INT:
44
                $this->data = self::_ndInt($this->ndim);
45
                break;
46
            default :
47
                throw new dtypeException('given dtype is not supported by Np');
48
        }
49
    }
50
51
    protected static function _ndFloat(int $size) {
52
        return \FFI::cast('float *', \FFI::new("float[$size]"));
53
    }
54
55
    protected static function _ndDouble(int $size) {
56
        return \FFI::cast('double *', \FFI::new("double[$size]"));
57
    }
58
59
    protected static function _ndInt(int $size) {
60
        return \FFI::cast('int *', \FFI::new("int[$size]"));
61
    }
62
63
    protected static function _err($msg): runtimeException {
64
        throw new runtimeException($msg);
65
    }
66
67
    protected static function _invalidArgument($argument): invalidArgumentException {
68
        throw new invalidArgumentException($argument);
69
    }
70
    
71
    protected static function _dtypeErr($msg) : dtypeException {
72
        throw new dtypeException($msg);
73
    }
74
    
75
    protected static function _dimensionaMisMatchErr($msg) :dimensionalityMismatch {
76
        throw new dimensionalityMismatch($msg);
77
    }
78
79
    /**
80
     * set Timer, get total time 
81
     */
82
    public static function time() {
83
        if (is_null(self::$_time)) {
84
            self::$_time = microtime(true);
85
        } else {
86
            echo 'Time-Consumed:- ' . (microtime(true) - self::$_time) . PHP_EOL;
87
        }
88
    }
89
90
    /**
91
     * set memory dog, get total memory
92
     */
93
    public static function getMemory() {
94
        if (is_null(self::$_mem)) {
95
            self::$_mem = memory_get_usage();
96
        } else {
97
            $memory = memory_get_usage() - self::$_mem;
98
            $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
99
            echo round($memory / pow(1024, ($i = floor(log($memory, 1024)))), 2) . $unit[$i] . PHP_EOL;
100
        }
101
    }
102
103
}
104