Skip to content

Commit

Permalink
Sort indices for SparseVector
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 25, 2024
1 parent 1a0f175 commit 38923c0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/SparseVector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public static function fromDense($value)

public static function fromMap($map, $dimensions)
{
// okay to update in-place since parameter is not a reference
ksort($map);
$indices = [];
$values = [];
// no need to sort since binary format is not supported
foreach ($map as $i => $v) {
$fv = floatval($v);
if ($fv != 0) {
Expand Down
5 changes: 4 additions & 1 deletion tests/SparseVectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public function testFromDense()

public function testFromMap()
{
$embedding = SparseVector::fromMap([2 => 2, 4 => 3, 0 => 1, 3 => 0], 6);
$map = [2 => 2, 4 => 3, 0 => 1, 3 => 0];
$embedding = SparseVector::fromMap($map, 6);
$this->assertEquals([1, 0, 2, 0, 3, 0], $embedding->toArray());
$this->assertEquals([0, 2, 4], $embedding->indices());
$this->assertEquals([2, 4, 0, 3], array_keys($map));
}

public function testFromString()
Expand Down

0 comments on commit 38923c0

Please sign in to comment.