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

nd   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getMemory() 0 7 2
A _dtypeErr() 0 2 1
A _ndFloat() 0 2 1
A _ndDouble() 0 2 1
A _dimensionaMisMatchErr() 0 2 1
A __construct() 0 15 4
A _ndInt() 0 2 1
A _err() 0 2 1
A time() 0 5 2
A _invalidArgument() 0 2 1
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