Skip to content

Commit

Permalink
Update Scale operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 26, 2020
1 parent 1702597 commit 85db341
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,9 @@ public function run(Operation ...$operations)
public function scale(
float $lowerBound,
float $upperBound,
?float $wantedLowerBound = null,
?float $wantedUpperBound = null,
?float $base = null
float $wantedLowerBound = 0.0,
float $wantedUpperBound = 1.0,
float $base = 0.0
): CollectionInterface {
return $this->run(new Scale($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base));
}
Expand Down
10 changes: 3 additions & 7 deletions src/Contract/Operation/Scaleable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ interface Scaleable
/**
* Scale/normalize values.
*
* @param ?float $wantedLowerBound
* @param ?float $wantedUpperBound
* @param ?float $base
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function scale(
float $lowerBound,
float $upperBound,
?float $wantedLowerBound = null,
?float $wantedUpperBound = null,
?float $base = null
float $wantedLowerBound = 0.0,
float $wantedUpperBound = 1.0,
float $base = 0.0
): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Unpackable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
interface Unpackable
{
/**
* @return \loophp\collection\Contract\Collection<TKey, T>
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function unpack(): Collection;
}
21 changes: 12 additions & 9 deletions src/Operation/Scale.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,33 @@

use const INF;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
final class Scale extends AbstractOperation implements Operation
{
public function __construct(
float $lowerBound,
float $upperBound,
?float $wantedLowerBound = null,
?float $wantedUpperBound = null,
?float $base = null
float $wantedLowerBound = 0.0,
float $wantedUpperBound = 1.0,
float $base = 0.0
) {
$wantedLowerBound = $wantedLowerBound ?? (null === $base ? 0.0 : 1.0);
$wantedUpperBound = $wantedUpperBound ?? ($base ?? 1.0);
$wantedLowerBound = (0.0 === $wantedLowerBound) ? (0.0 === $base ? 0.0 : 1.0) : $wantedLowerBound; // phpcs:ignore
$wantedUpperBound = (1.0 === $wantedUpperBound) ? (0.0 === $base ? 1.0 : $base) : $wantedUpperBound; // phpcs:ignore

$this->storage['mapper'] = new Map(
/**
* @param float|int $v
*/
static function ($v) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): float { // phpcs:ignore
$mx = null === $base ?
$mx = 0.0 === $base ?
($v - $lowerBound) / ($upperBound - $lowerBound) :
log($v - $lowerBound, $base) / log($upperBound - $lowerBound, $base);

if ($mx === -INF) {
$mx = 0;
}
$mx = $mx === -INF ? 0 : $mx;

return $wantedLowerBound + $mx * ($wantedUpperBound - $wantedLowerBound);
}
Expand Down

0 comments on commit 85db341

Please sign in to comment.