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

ops::free()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 10
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 12
rs 9.9332
1
<?php
2
3
namespace Np;
4
5
/**
6
 * Operations
7
 * A fast lite & memory efficient Scientific Computing for php
8
 * 
9
 * @package   NP
10
 * @category  Scientific Computing
11
 * @author    ghost (Shubham Chaudhary)
12
 * @email     [email protected]
13
 * @copyright (c) 2020-2021, Shubham Chaudhary
14
 */
15
trait ops {
16
17
    /**
18
     * Return the element-wise maximum of two matrices| two vectors.
19
     * @param matrix|vector $d
20
     * @return matrix|vector
21
     */
22
    public function max(matrix|vector $d): matrix|vector {
23
        if ($this instanceof matrix && $d instanceof matrix && $this->checkShape($this, $d) && $this->checkDtype($this, $d)) {
24
            $r = self::factory($this->row, $this->col, $this->dtype);
25
        } elseif ($this instanceof vector && $d instanceof vector && $this->checkShape($this, $d) && $this->checkDtype($this, $d)) {
26
            $r = self::factory($this->col, $this->dtype);
27
        }
28
        for ($i = 0; $i < $this->ndim; ++$i) {
29
            $r->data[$i] = max($this->data[$i], $d->data[$i]);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $r does not seem to be defined for all execution paths leading up to this point.
Loading history...
30
        }
31
        return $r;
32
    }
33
34
    /**
35
     * Return the element-wise minimum of two matrices|two vectors.
36
     * @param matrix|vector $d
37
     * @return matrix|vector
38
     */
39
    public function min(matrix|vector $d): matrix|vector {
40
        if ($this instanceof matrix && $d instanceof matrix && $this->checkShape($this, $d) && $this->checkDtype($this, $d)) {
41
            $r = self::factory($this->row, $this->col, $this->dtype);
42
        } elseif ($this instanceof vector && $d instanceof vector && $this->checkShape($this, $d) && $this->checkDtype($this, $d)) {
43
            $r = self::factory($this->col, $this->dtype);
44
        }
45
        for ($i = 0; $i < $this->ndim; ++$i) {
46
            $r->data[$i] = max($this->data[$i], $d->data[$i]);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $r does not seem to be defined for all execution paths leading up to this point.
Loading history...
47
        }
48
        return $r;
49
    }
50
51
    /**
52
     * Run a function over all of the elements in the matrix|vector. 
53
     * @param callable $func
54
     * @return matrix|vector
55
     */
56
    public function map(callable $func): matrix|vector {
57
        if ($this instanceof matrix) {
58
            $r = self::factory($this->row, $this->col, $this->dtype);
59
        } else {
60
            $r = self::factory($this->col, $this->dtype);
61
        }
62
        for ($i = 0; $i < $this->ndim; ++$i) {
63
            $r->data[$i] = $func($this->data[$i]);
64
        }
65
        return $r;
66
    }
67
68
    public function abs(): matrix|vector {
69
        return $this->map('abs');
70
    }
71
72
    public function sqrt(): matrix|vector {
73
        return $this->map('sqrt');
74
    }
75
76
    public function exp(): matrix|vector {
77
        return $this->map('exp');
78
    }
79
80
    public function exp1(): matrix|vector {
81
        return $this->map('exp1');
82
    }
83
84
    public function log(float $b = M_E): matrix|vector {
85
        $ar = $this->copy();
86
        for ($i = 0; $i < $ar->ndim; ++$i) {
87
            log($ar->data[$i], $b);
88
        }
89
        return $ar;
90
    }
91
92
    public function log1p(): matrix {
93
        return $this->map('log1p');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('log1p') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
94
    }
95
96
    public function sin(): matrix {
97
        return $this->map('sin');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('sin') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
98
    }
99
100
    public function asin(): matrix {
101
        return $this->map('asin');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('asin') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
102
    }
103
104
    public function cos(): matrix {
105
        return $this->map('cos');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('cos') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
106
    }
107
108
    public function acos(): matrix {
109
        return $this->map('acos');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('acos') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
110
    }
111
112
    public function tan(): matrix {
113
        return $this->map('tan');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('tan') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
114
    }
115
116
    public function atan(): matrix {
117
        return $this->map('atan');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('atan') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
118
    }
119
120
    public function radToDeg(): matrix {
121
        return $this->map('rad2deg');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('rad2deg') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
122
    }
123
124
    public function degToRad(): matrix {
125
        return $this->map('deg2rad');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('deg2rad') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
126
    }
127
128
    public function floor(): matrix {
129
        return $this->map('floor');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('floor') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
130
    }
131
132
    public function ceil(): matrix {
133
        return $this->map('ceil');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map('ceil') could return the type Np\vector which is incompatible with the type-hinted return Np\matrix. Consider adding an additional type-check to rule them out.
Loading history...
134
    }
135
    
136
    public function free():void {
137
        if($this instanceof matrix) {
138
            unset($this->row);
139
            unset($this->col);
140
            unset($this->ndim);
141
            unset($this->data);
142
            return;
143
        }
144
        unset($this->col);
145
        unset($this->ndim);
146
        unset($this->data);
147
        return;
148
    }
149
    /**
150
     * make a copy of matrix|vector;
151
     * @return matrix|vector
152
     */
153
    public function copy(): matrix|vector {
154
        return clone $this;
1 ignored issue
show
Bug Best Practice introduced by
The expression return clone $this returns the type Np\ops which includes types incompatible with the type-hinted return Np\matrix|Np\vector.
Loading history...
155
    }
156
157
    /**
158
     * Clip the elements in the matrix to be between given minimum and maximum
159
     * and return a new matrix.
160
     * 
161
     * @param float $min
162
     * @param float $max
163
     * @return matrix
164
     */
165
    public function clip(float $min, float $max): matrix|vector {
166
        if ($this instanceof matrix) {
167
            $ar = self::factory($this->row, $this->col, $this->dtype);
168
        } else {
169
            $ar = self::factory($this->col, $this->dtype);
170
        }
171
        for ($i = 0; $i < $this->ndim; ++$i) {
172
            if ($this->data[$i] > $max) {
173
                $ar->data[$i] = $max;
174
                continue;
175
            }
176
            if ($this->data[$i] < $min) {
177
                $ar->data[$i] = $min;
178
                continue;
179
            }
180
            $ar->data[$i] = $this->data[$i];
181
        }
182
        return $ar;
183
    }
184
185
    /**
186
     * Clip the matrix|vector to be lower bounded by a given minimum.
187
     * @param float $min
188
     * @return matrix
189
     */
190
    public function clipLower(float $min): matrix {
191
        if ($this instanceof matrix) {
192
            $ar = self::factory($this->row, $this->col, $this->dtype);
193
        } else {
194
            $ar = self::factory($this->col, $this->dtype);
195
        }
196
        for ($i = 0; $i < $this->ndim; ++$i) {
197
            if ($this->data[$i] < $min) {
198
                $ar->data[$i] = $min;
199
                continue;
200
            }
201
            $ar->data[$i] = $this->data[$i];
202
        }
203
        return $ar;
204
    }
205
206
    /**
207
     * Clip the matrix|vector to be upper bounded by a given maximum.
208
     *
209
     * @param float $max
210
     * @return matrix
211
     */
212
    public function clipUpper(float $max): matrix|vector {
213
        if ($this instanceof matrix) {
214
            $ar = self::factory($this->row, $this->col, $this->dtype);
215
        } else {
216
            $ar = self::factory($this->col, $this->dtype);
217
        }
218
        for ($i = 0; $i < $this->ndim; ++$i) {
219
            if ($this->data[$i] > $max) {
220
                $ar->data[$i] = $max;
221
                continue;
222
            }
223
            $ar->data[$i] = $this->data[$i];
224
        }
225
        return $ar;
226
    }
227
228
    /**
229
     * return a reshaped data buffer as matrix
230
     * @param int $row
231
     * @param int $col
232
     * @return matrix
233
     */
234
    public function reshape(int $row, int $col): matrix {
235
        if ($this->ndim != $row * $col) {
236
            self::_dimensionaMisMatchErr('given dimenssion is not valid for current bufferData');
237
        }
238
        if ($this instanceof vector) {
239
            $ar = matrix::factory($row, $col, $this->dtype);
240
            $ar->data = $this->data;
241
            return $ar;
242
        }
243
        $this->row = $row;
0 ignored issues
show
Bug Best Practice introduced by
The property row does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
244
        $this->col = $col;
0 ignored issues
show
Bug Best Practice introduced by
The property col does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
245
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Np\ops which includes types incompatible with the type-hinted return Np\matrix.
Loading history...
246
    }
247
248
}
249