Skip to content

Commit d6bf9ef

Browse files
committed
Relaxed reduce signature
1 parent 010b528 commit d6bf9ef

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/Matrix.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,17 @@ public function map(callable $fn) : self
567567
* Reduce the matrix down to a scalar.
568568
*
569569
* @param callable $fn
570-
* @param float $initial
571-
* @return float
570+
* @param mixed $initial
571+
* @throws \InvalidArgumentException
572+
* @return int|float
572573
*/
573-
public function reduce(callable $fn, float $initial = 0.) : float
574+
public function reduce(callable $fn, $initial = 0)
574575
{
576+
if (!is_int($initial) and !is_float($initial)) {
577+
throw new InvalidArgumentException('Initial value must'
578+
. ' be an integer or float.');
579+
}
580+
575581
$carry = $initial;
576582

577583
foreach ($this->a as $row) {
@@ -610,12 +616,14 @@ public function transpose() : self
610616
*/
611617
public function inverse() : self
612618
{
613-
$b = $this->augmentRight(self::identity($this->m))->rref();
619+
$b = $this->augmentRight(self::identity($this->m));
620+
621+
$b = $b->rref()->asArray();
614622

615623
$c = [];
616624

617-
for ($i = 0; $i < $this->n; $i++) {
618-
$c[] = array_slice($b[$i], $this->n);
625+
foreach ($b as $row) {
626+
$c[] = array_slice($row, $this->n);
619627
}
620628

621629
return self::quick($c);
@@ -931,7 +939,7 @@ public function add($b) : self
931939
return $this->addScalar($b);
932940
}
933941

934-
throw new InvalidArgumentException('Cannot sdd matrix to a'
942+
throw new InvalidArgumentException('Cannot add matrix to a'
935943
. ' ' . gettype($b) . '.');
936944
}
937945

src/Tensor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public function map(callable $fn);
3838
* Reduce the tensor down to a scalar.
3939
*
4040
* @param callable $fn
41-
* @param float $initial
42-
* @return float
41+
* @param mixed $initial
42+
* @return int|float
4343
*/
44-
public function reduce(callable $fn, float $initial = 0.) : float;
44+
public function reduce(callable $fn, $initial = 0);
4545

4646
/**
4747
* A universal function to multiply this tensor with another tensor

src/Vector.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,17 @@ public function map(callable $fn) : self
381381
* Reduce the vector down to a scalar.
382382
*
383383
* @param callable $fn
384-
* @param float $initial
385-
* @return float
384+
* @param mixed $initial
385+
* @throws \InvalidArgumentException
386+
* @return int|float
386387
*/
387-
public function reduce(callable $fn, float $initial = 0.) : float
388+
public function reduce(callable $fn, $initial = 0)
388389
{
390+
if (!is_int($initial) and !is_float($initial)) {
391+
throw new InvalidArgumentException('Initial value must'
392+
. ' be an integer or float.');
393+
}
394+
389395
return array_reduce($this->a, $fn, $initial);
390396
}
391397

0 commit comments

Comments
 (0)