Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
37 / 37 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| ExponentTrait | |
100.00% |
37 / 37 |
|
100.00% |
6 / 6 |
12 | |
100.00% |
1 / 1 |
| exp | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| expm1 | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| exp2 | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| power | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| square | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| sqrt | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace SciPhp\NumPhp; |
| 6 | |
| 7 | use SciPhp\Exception\Message; |
| 8 | use Webmozart\Assert\Assert; |
| 9 | |
| 10 | /** |
| 11 | * Exponent methods |
| 12 | */ |
| 13 | trait ExponentTrait |
| 14 | { |
| 15 | /** |
| 16 | * Calculate the exponential of all elements in the input array. |
| 17 | * |
| 18 | * @param \SciPhp\NdArray|array|int|float $m |
| 19 | * @return \SciPhp\NdArray|int|float |
| 20 | * @link http://sciphp.org/numphp.exp Documentation |
| 21 | * @api |
| 22 | */ |
| 23 | final public static function exp($m) |
| 24 | { |
| 25 | if (\is_numeric($m)) { |
| 26 | return exp($m); |
| 27 | } |
| 28 | |
| 29 | static::transform($m, true); |
| 30 | |
| 31 | $func = static function(&$element): void { |
| 32 | $element = exp($element); |
| 33 | }; |
| 34 | |
| 35 | return $m->copy()->walk_recursive($func); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Calculate exp(x) - 1 for all elements in the array. |
| 40 | * |
| 41 | * @param \SciPhp\NdArray|array|int|float $m |
| 42 | * @return \SciPhp\NdArray|int|float |
| 43 | * @link http://sciphp.org/numphp.expm1 Documentation |
| 44 | * @api |
| 45 | */ |
| 46 | final public static function expm1($m) |
| 47 | { |
| 48 | if (\is_numeric($m)) { |
| 49 | return expm1($m); |
| 50 | } |
| 51 | |
| 52 | static::transform($m, true); |
| 53 | |
| 54 | $func = static function(&$element): void { |
| 55 | $element = expm1($element); |
| 56 | }; |
| 57 | |
| 58 | return $m->copy()->walk_recursive($func); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Calculate 2**p for all p in the input array. |
| 63 | * |
| 64 | * @param \SciPhp\NdArray|array|int|float $m |
| 65 | * @return \SciPhp\NdArray|int|float |
| 66 | * @link http://sciphp.org/numphp.exp2 Documentation |
| 67 | * @api |
| 68 | */ |
| 69 | final public static function exp2($m) |
| 70 | { |
| 71 | if (\is_numeric($m)) { |
| 72 | return 2 ** $m; |
| 73 | } |
| 74 | |
| 75 | static::transform($m, true); |
| 76 | |
| 77 | $func = static function(&$element): void { |
| 78 | $element = 2 ** $element; |
| 79 | }; |
| 80 | |
| 81 | return $m->copy()->walk_recursive($func); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Matrix elements raised to powers. |
| 86 | * |
| 87 | * @param float|int|array|\SciPhp\NdArray $matrix |
| 88 | * @param float|int $exponent |
| 89 | * @link http://sciphp.org/numphp.power Documentation |
| 90 | * @since 0.3.0 |
| 91 | * @api |
| 92 | */ |
| 93 | final public static function power($matrix, $exponent) |
| 94 | { |
| 95 | Assert::numeric($exponent); |
| 96 | |
| 97 | if (\is_numeric($matrix)) { |
| 98 | return $matrix ** $exponent; |
| 99 | } |
| 100 | |
| 101 | static::transform($matrix, true); |
| 102 | |
| 103 | $func = static function(&$element) use ($exponent): void { |
| 104 | $element **= $exponent; |
| 105 | }; |
| 106 | |
| 107 | return $matrix->copy()->walk_recursive($func); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * The element-wise square of the input. |
| 112 | * |
| 113 | * @param float|int|array|\SciPhp\NdArray $matrix |
| 114 | * @return float|int|\SciPhp\NdArray |
| 115 | * @link http://sciphp.org/numphp.square Documentation |
| 116 | * @since 0.3.0 |
| 117 | * @api |
| 118 | */ |
| 119 | final public static function square($matrix) |
| 120 | { |
| 121 | if (\is_numeric($matrix)) { |
| 122 | return $matrix ** 2; |
| 123 | } |
| 124 | |
| 125 | static::transform($matrix, true); |
| 126 | |
| 127 | return $matrix->power(2); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * The non-negative square-root of an array, element-wise. |
| 132 | * |
| 133 | * @return \SciPhp\NdArray |
| 134 | * @link http://sciphp.org/ndarray.sqrt Documentation |
| 135 | * @since 0.3.0 |
| 136 | * @api |
| 137 | */ |
| 138 | final public static function sqrt($matrix) |
| 139 | { |
| 140 | if (\is_numeric($matrix)) { |
| 141 | Assert::greaterThanEq( |
| 142 | $matrix, |
| 143 | 0, |
| 144 | Message::ONLY_POSITIVE_NUMBER |
| 145 | ); |
| 146 | |
| 147 | return sqrt($matrix); |
| 148 | } |
| 149 | |
| 150 | static::transform($matrix, true); |
| 151 | |
| 152 | $func = static function(&$element): void { |
| 153 | Assert::greaterThanEq( |
| 154 | $element, |
| 155 | 0, |
| 156 | Message::ONLY_POSITIVE_NUMBER |
| 157 | ); |
| 158 | |
| 159 | $element = sqrt($element); |
| 160 | }; |
| 161 | |
| 162 | return $matrix->copy()->walk_recursive($func); |
| 163 | } |
| 164 | } |