Math::...Vector(3pm)
π Math::SparseVector π
okay so bestie, welcome to the manual page for Math::SparseVector, straight out of the Perl User Contributed Documentation π
. If you've ever wanted a vector that only bothers to remember the stuff that isn't zero, this is the moment. She does setting a value in a vector, reading a value at a given index, obtaining all indices, addition and dot product of two sparse vectors, and vector normalization. iconic behavior, no cap π
π MODULE HISTORY π
so like, this module is the successor to Sparse::Vector β it got re-cast into this shiny new namespace so that another module, Math::SparseMatrix, could exist and make use of it. glow-up era fr fr β¨
π¦ SYNOPSIS π¦
use Math::SparseVector;
# creating an empty sparse vector object
$spvec=Math::SparseVector->new;
# sets the value at index 12 to 5
$spvec->set(12,5);
# returns value at index 12
$value = $spvec->get(12);
# returns the indices of non-zero values in sorted order
@indices = $spvec->keys;
# returns 1 if the vector is empty and has no keys
if($spvec->isnull) {
print "vector is null.\n";
}
else {
print "vector is not null.\n";
}
# print sparse vector to stdout
$spvec->print;
# returns the string form of sparse vector
# same as print except the string is returned
# rather than displaying on stdout
$spvec->stringify;
# adds sparse vectors v1, v2 and stores
# result into v1
$v1->add($v2);
# adds binary equivalent of v2 to v1
$v1->binadd($v2);
# binary equivalnet treats all non-zero values
# as 1s
# increments the value at index 12
$spvec->incr(12);
# divides each vector entry by a given divisor 4
$spvec->div(4);
# returns norm of the vector
$spvec_norm = $spvec->norm;
# normalizes a sparse vector
$spvec->normalize;
# returns dot product of the 2 vectors
$dotprod = $v1->dot($v2);
# deallocates all entries
$spvec->free;
πΈ USAGE NOTES πΈ
1. Loading Math::SparseVector Module
to even touch this module you gotta slide this line into your Perl program before you use any of the supported methods:
use Math::SparseVector;
2. Creating a Math::SparseVector Object
this next line creates a brand new object of the Math::SparseVector class, and we're gonna refer to her as 'spvec':
$spvec=Math::SparseVector->new;
and when she's born, 'spvec' is initially empty. a clean slate, a blank canvas, main character energy π«Ά
3. Using Methods
now you can run any of these methods on your 'spvec' Math::SparseVector object. let's gooo π
1. set(i,n) β Sets the value at index i to n
# equivalent to $spvec{12}=5;
$spvec->set(12,5);
2. get(i) β Returns the value at index i
# equivalent to $value=$spvec{12};
$value = $spvec->get(12);
3. keys() β Returns the indices of all non-zero values in the vector
# equivalent to @keys=sort {$a <=> $b} keys %spvec;
@indices = $spvec->keys;
4. isnull() β Returns 1 if the vector is empty and has no keys
# similar to
# if(scalar(keys %spvec)==0) {print "vector is null.\n";}
if($spvec->isnull) { print "vector is null.\n"; }
5. print() β Prints the sparse vector to stdout
output will show a list of space separated 'index value' pairs for each non-zero 'value' in the vector.
# similar to
# foreach $ind (sort {$a<=>$b} keys %spvec)
# { print "$ind " . $spvec{$ind} . " "; }
$spvec->print;
6. stringify() β Returns the vector in a string form
same as the print() method except the vector is written to a string that gets returned instead of being displayed onto stdout.
# the below will do exactly same as $spvec->print;
$string=$spvec->stringify;
print "$string\n";
7. v1->add(v2) β Adds contents of v2 to vector v1
similar to v1+=v2
$v1->add($v2);
if v1 = (2, , , 5, 8, , , , 1) & v2 = ( , 1, , 3, , , 5, , 9)
where blanks show the 0 values that are not stored in Math::SparseVector.
then after $v1->add($v2);:
v1 = (2, 1, , 8, 8, , 5, , 10) and v2 remains same π
8. v1->binadd(v2) β Binary equivalent of v2 is added into v1
the binary equivalent of a vector is obtained by setting all non-zero values to 1s.
if v1 = (1, , , 1, 1, , , , 1) & v2 = ( , 1, , 1, , , 1, , 1)
then, after v1->binadd(v2), v1 will be (1, 1, , 1, 1, , 1, , 1).
if v1 = (1, , , 1, 1, , , , 1) & v2 = ( , 1, , 3, , , 5, , 9)
v1->binadd(v2); will set v1 to (1, 1, , 1, 1, , 1, , 1).
9. incr(i) β Increments the value at index i
# is similar to $spvec{12}++;
$spvec->incr(12);
10. div(n) β Divides each vector entry by a given divisor n
$spvec->div(4);
if spvec = (2, , , 5, 8, , , , 1) then $spvec->div(4) will set spvec to (0.5, , , 1.25, 2, , , , 0.25)
11. norm() β Returns the norm of a given vector
$spvec_norm = $spvec->norm;
if spvec = (2, , , 5, 8, , , , 1) $spvec->norm will return the value
= sqrt(2^2 + 5^2 + 8^2 + 1)
= sqrt(4 + 25 + 64 + 1)
= 9.69536
12. v1->dot(v2) β Returns the dot product of two vectors
$dotprod = $v1->dot($v2);
if v1 = (2, , , 5, 8, , , , 1) & v2 = ( , 1, , 3, , , 5, , 9)
v1->dot(v2) returns
5*3 + 1*9 = 15 + 9 = 24
13. free() β Deallocates all entries and makes the vector empty
$spvec->free;
will set spvec to null vector ()
π AUTHORS π
- Amruta Purandare, University of Pittsburgh β
amruta at cs.pitt.edu - Ted Pedersen, University of Minnesota, Duluth β
tpederse at d.umn.edu - Mahesh Joshi, Carnegie-Mellon University β
maheshj at cmu.edu
π COPYRIGHT AND LICENSE π
Copyright (c) 2006-2008, Amruta Purandare, Ted Pedersen, Mahesh Joshi
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to
The Free Software Foundation, Inc.,
59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
β οΈ okay real talk bestie, that "WITHOUT ANY WARRANTY" thing is not a vibe, it's the actual legal text β no promises of merchantability, no promises of fitness for a particular purpose. read the GPL for the full tea π
and that's the whole page, mwah π this was Math::SparseVector for perl v5.26.1, dated 2018-03-30, served fresh from the Perl User Contributed Documentation. go build something cute with your sparse vectors, you ate π
β¨
π¦ where this page comes from
- debian bookworm: libmath-sparsevector-perl 0.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmath-sparsevector-perl 0.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libmath-sparsevector-perl 0.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmath-sparsevector-perl 0.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms