Passed
Push — main ( 71dcec...306c22 )
by Shubham
01:53
created

ops::abs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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
    /**
137
     * make a copy of matrix|vector;
138
     * @return matrix|vector
139
     */
140
    public function copy(): matrix|vector {
141
        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...
142
    }
143
144
    /**
145
     * Clip the elements in the matrix to be between given minimum and maximum
146
     * and return a new matrix.
147
     * 
148
     * @param float $min
149
     * @param float $max
150
     * @return matrix
151
     */
152
    public function clip(float $min, float $max): matrix|vector {
153
        if ($this instanceof matrix) {
154
            $ar = self::factory($this->row, $this->col, $this->dtype);
155
        } else {
156
            $ar = self::factory($this->col, $this->dtype);
157
        }
158
        for ($i = 0; $i < $this->ndim; ++$i) {
159
            if ($this->data[$i] > $max) {
160
                $ar->data[$i] = $max;
161
                continue;
162
            }
163
            if ($this->data[$i] < $min) {
164
                $ar->data[$i] = $min;
165
                continue;
166
            }
167
            $ar->data[$i] = $this->data[$i];
168
        }
169
        return $ar;
170
    }
171
172
    /**
173
     * Clip the matrix|vector to be lower bounded by a given minimum.
174
     * @param float $min
175
     * @return matrix
176
     */
177
    public function clipLower(float $min): matrix {
178
        if ($this instanceof matrix) {
179
            $ar = self::factory($this->row, $this->col, $this->dtype);
180
        } else {
181
            $ar = self::factory($this->col, $this->dtype);
182
        }
183
        for ($i = 0; $i < $this->ndim; ++$i) {
184
            if ($this->data[$i] < $min) {
185
                $ar->data[$i] = $min;
186
                continue;
187
            }
188
            $ar->data[$i] = $this->data[$i];
189
        }
190
        return $ar;
191
    }
192
193
    /**
194
     * Clip the matrix|vector to be upper bounded by a given maximum.
195
     *
196
     * @param float $max
197
     * @return matrix
198
     */
199
    public function clipUpper(float $max): matrix|vector {
200
        if ($this instanceof matrix) {
201
            $ar = self::factory($this->row, $this->col, $this->dtype);
202
        } else {
203
            $ar = self::factory($this->col, $this->dtype);
204
        }
205
        for ($i = 0; $i < $this->ndim; ++$i) {
206
            if ($this->data[$i] > $max) {
207
                $ar->data[$i] = $max;
208
                continue;
209
            }
210
            $ar->data[$i] = $this->data[$i];
211
        }
212
        return $ar;
213
    }
214
215
    /**
216
     * return a reshaped data buffer as matrix
217
     * @param int $row
218
     * @param int $col
219
     * @return matrix
220
     */
221
    public function reshape(int $row, int $col): matrix {
222
        if ($this->ndim != $row * $col) {
223
            self::_dimensionaMisMatchErr('given dimenssion is not valid for current bufferData');
224
        }
225
        if ($this instanceof vector) {
226
            $ar = matrix::factory($row, $col, $this->dtype);
227
            $ar->data = $this->data;
228
            return $ar;
229
        }
230
        $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...
231
        $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...
232
        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...
233
    }
234
235
}
236