r68688 - in /branches/upstream/libmath-sparsevector-perl: ./ current/ current/lib/ current/lib/Math/ current/t/

takaki at users.alioth.debian.org takaki at users.alioth.debian.org
Tue Feb 15 04:55:40 UTC 2011


Author: takaki
Date: Tue Feb 15 04:55:31 2011
New Revision: 68688

URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=68688
Log:
[svn-inject] Installing original source of libmath-sparsevector-perl (0.04)

Added:
    branches/upstream/libmath-sparsevector-perl/
    branches/upstream/libmath-sparsevector-perl/current/
    branches/upstream/libmath-sparsevector-perl/current/CHANGES
    branches/upstream/libmath-sparsevector-perl/current/INSTALL.pod
    branches/upstream/libmath-sparsevector-perl/current/MANIFEST
    branches/upstream/libmath-sparsevector-perl/current/META.yml
    branches/upstream/libmath-sparsevector-perl/current/Makefile.PL
    branches/upstream/libmath-sparsevector-perl/current/README
    branches/upstream/libmath-sparsevector-perl/current/lib/
    branches/upstream/libmath-sparsevector-perl/current/lib/Math/
    branches/upstream/libmath-sparsevector-perl/current/lib/Math/SparseVector.pm
    branches/upstream/libmath-sparsevector-perl/current/t/
    branches/upstream/libmath-sparsevector-perl/current/t/Math-SparseVector.t

Added: branches/upstream/libmath-sparsevector-perl/current/CHANGES
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libmath-sparsevector-perl/current/CHANGES?rev=68688&op=file
==============================================================================
--- branches/upstream/libmath-sparsevector-perl/current/CHANGES (added)
+++ branches/upstream/libmath-sparsevector-perl/current/CHANGES Tue Feb 15 04:55:31 2011
@@ -1,0 +1,13 @@
+Revision history for Perl extension Math::SparseVector.
+
+0.04  Mar 24, 2008
+	- relaxed Perl requirement to 5.6.2 from 5.8.5
+	- modifed SparseVector.pm so README is generated from it
+	- modified Makefile.PL to better support META.yml
+
+0.03  June 18, 2006
+	- Fixed typos in documentation
+	- original version; created by h2xs 1.23 with options 
+    -AXc -n Math::SparseVector. Code and documentation used from
+    existing Sparse::Vector v0.03 module, with minimal changes.
+

Added: branches/upstream/libmath-sparsevector-perl/current/INSTALL.pod
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libmath-sparsevector-perl/current/INSTALL.pod?rev=68688&op=file
==============================================================================
--- branches/upstream/libmath-sparsevector-perl/current/INSTALL.pod (added)
+++ branches/upstream/libmath-sparsevector-perl/current/INSTALL.pod Tue Feb 15 04:55:31 2011
@@ -1,0 +1,72 @@
+=head1 NAME
+
+INSTALL Installation instructions for Math::SparseVetor
+
+=head1 DEPENDENCIES
+
+This module has been developed on Linux and Unix platforms, using the 
+Perl programming language. To use this module, you must have Perl (version
+5.6.2 or better recommended) installed on your system. 
+
+=head1 SYNOPSIS
+
+To install this module type the following:
+
+    perl Makefile.PL
+    make
+    make test
+    make install
+
+The exact location of where Math::SparseVector will be installed depends
+on your system configuration.
+
+If you do not have authority to write into system directories, you can
+install Math::SparseVector in a local directory that you own and have 
+permissions to read and write into as follows:
+
+    perl Makefile.PL PREFIX=/YOUR/DIR
+    make
+    make test
+    make install
+
+This will install the module into
+
+    /YOUR/DIR/lib/perl5/site_perl/Math/SparseMatrix.pm
+
+If you install Math::SparseVector in a local directory, you will have to 
+explicitly set your PERL5LIB environment variable to include:
+
+    /YOUR/DIR/lib/perl5/site_perl
+
+if this directory was not already included.
+
+=head1 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
+
+=head1 COPYRIGHT
+
+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.

Added: branches/upstream/libmath-sparsevector-perl/current/MANIFEST
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libmath-sparsevector-perl/current/MANIFEST?rev=68688&op=file
==============================================================================
--- branches/upstream/libmath-sparsevector-perl/current/MANIFEST (added)
+++ branches/upstream/libmath-sparsevector-perl/current/MANIFEST Tue Feb 15 04:55:31 2011
@@ -1,0 +1,8 @@
+CHANGES
+INSTALL.pod
+lib/Math/SparseVector.pm
+Makefile.PL
+MANIFEST			This list of files
+META.yml
+README
+t/Math-SparseVector.t

Added: branches/upstream/libmath-sparsevector-perl/current/META.yml
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libmath-sparsevector-perl/current/META.yml?rev=68688&op=file
==============================================================================
--- branches/upstream/libmath-sparsevector-perl/current/META.yml (added)
+++ branches/upstream/libmath-sparsevector-perl/current/META.yml Tue Feb 15 04:55:31 2011
@@ -1,0 +1,10 @@
+# http://module-build.sourceforge.net/META-spec.html
+#XXXXXXX This is a prototype!!!  It will change in the future!!! XXXXX#
+name:         Math-SparseVector
+version:      0.04
+version_from: lib/Math/SparseVector.pm
+installdirs:  site
+requires:
+
+distribution_type: module
+generated_by: ExtUtils::MakeMaker version 6.17

Added: branches/upstream/libmath-sparsevector-perl/current/Makefile.PL
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libmath-sparsevector-perl/current/Makefile.PL?rev=68688&op=file
==============================================================================
--- branches/upstream/libmath-sparsevector-perl/current/Makefile.PL (added)
+++ branches/upstream/libmath-sparsevector-perl/current/Makefile.PL Tue Feb 15 04:55:31 2011
@@ -1,0 +1,21 @@
+use 5.006;
+use ExtUtils::MakeMaker;
+
+# See lib/ExtUtils/MakeMaker.pm for details of how to influence
+# the contents of the Makefile that is written.
+
+WriteMakefile(
+    NAME              => 'Math::SparseVector',
+    VERSION_FROM      => 'lib/Math/SparseVector.pm', # finds $VERSION
+    PREREQ_PM         => {}, # e.g., Module::Name => 1.1
+    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
+      (ABSTRACT_FROM  => 'lib/Math/SparseVector.pm', # retrieve abstract from module
+       AUTHOR         => 'Amruta Purandare <amruta at cs.pitt.edu>; Ted Pedersen <tpederse at d.umn.edu>; 
+				Mahesh Joshi <maheshj at cmu.edu>') : ()),
+   # allows make dist to create .tar.gz with correct name/version
+   'dist'             => {'COMPRESS' => 'gzip -9f', 'SUFFIX' => 'gz'},
+   # allows for automatic creation of META.yml
+   ($ExtUtils::MakeMaker::VERSION ge '6.30_00'?
+   ('LICENSE' => 'gpl', ) : ()),
+
+);

Added: branches/upstream/libmath-sparsevector-perl/current/README
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libmath-sparsevector-perl/current/README?rev=68688&op=file
==============================================================================
--- branches/upstream/libmath-sparsevector-perl/current/README (added)
+++ branches/upstream/libmath-sparsevector-perl/current/README Tue Feb 15 04:55:31 2011
@@ -1,0 +1,202 @@
+NAME
+    Math::SparseVector - Supports sparse vector operations such as 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.
+
+MODULE HISTORY
+    This module is the successor to Sparse::Vector, which was re-cast into
+    this new namespace in order to introduce another module
+    Math::SparseMatrix, which makes use of this module.
+
+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 use this module, you must insert the following line in your Perl
+        program before using any of the supported methods.
+
+            use Math::SparseVector;
+
+    2. Creating a Math::SparseVector Object
+        The following line creates a new object of Math::SparseVector class
+        referred with the name 'spvec'.
+
+            $spvec=Math::SparseVector->new;
+
+        The newly created 'spvec' vector will be initially empty.
+
+    3. Using Methods
+        Now you can use any of the following methods on this 'spvec'
+        Math::SparseVector object.
+
+        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
+        print() method except the vector is written to a string that is
+        returned instead of displaying 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.
+
+                     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. 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.
+

Added: branches/upstream/libmath-sparsevector-perl/current/lib/Math/SparseVector.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libmath-sparsevector-perl/current/lib/Math/SparseVector.pm?rev=68688&op=file
==============================================================================
--- branches/upstream/libmath-sparsevector-perl/current/lib/Math/SparseVector.pm (added)
+++ branches/upstream/libmath-sparsevector-perl/current/lib/Math/SparseVector.pm Tue Feb 15 04:55:31 2011
@@ -1,0 +1,498 @@
+package Math::SparseVector;
+
+use 5.006;
+use strict;
+use warnings;
+
+require Exporter;
+
+our @ISA = qw(Exporter);
+
+# Items to export into callers namespace by default. Note: do not export
+# names by default without a very good reason. Use EXPORT_OK instead.
+# Do not simply export all your public functions/methods/constants.
+
+# This allows declaration    use Math::SparseVector ':all';
+# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
+# will save memory.
+our %EXPORT_TAGS = ( 'all' => [ qw(
+    
+) ] );
+
+our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
+
+our @EXPORT = qw(
+    
+);
+
+our $VERSION = '0.04';
+
+use overload
+    '++' => 'incr',
+    '+' => 'add',
+    'fallback' => undef;
+
+# sparse vector contructor 
+# creates an empty sparse vector
+sub new 
+{
+    my $class = shift;
+    my $self = {};
+    bless($self,$class);
+    return $self;
+}
+
+# sets value at given index
+sub set
+{
+    my $self = shift;
+    my $key = shift;
+    my $value = shift;
+    if(!defined $key || !defined $value)
+    {
+        print STDERR "Usage: vector->set(key,value)\n";
+        exit;
+    }
+    if($value==0)
+    {
+        print STDERR "Can not store 0 in the Math::SparseVector.\n";
+        exit;
+    }
+    $self->{$key} = $value;
+}
+
+# returns value at given index
+sub get
+{
+    my $self = shift;
+    my $key = shift;
+    if(!defined $key)
+    {
+        print STDERR "Usage: vector->get(key)\n";
+        exit;
+    }
+    if(defined $self->{$key})
+    {
+        return $self->{$key};
+    }
+    return 0;
+}
+
+# returns indices of non-zero values in sorted order
+sub keys
+{
+    my $self = shift;
+    my @indices = keys %{$self};
+    my @sorted = sort {$a <=> $b} @indices;
+    return @sorted;
+}
+
+# returns 1 if the vector is empty
+sub isnull
+{
+    my $self = shift;
+    my @indices = $self->keys;
+    if(scalar(@indices)==0)
+    {
+        return 1;
+    }
+    return 0;
+}
+
+# prints sparse vector
+sub print
+{
+    my $self = shift;
+    foreach my $ind ($self->keys)
+    {
+        print "$ind " . $self->get($ind) . " ";
+    }
+    print "\n";
+}
+
+# returns the equivalent string form
+sub stringify
+{
+    my $self = shift;
+    my $str="";
+        foreach my $ind ($self->keys)
+        {
+                $str.= "$ind " . $self->get($ind) . " ";
+        }
+    chop $str;
+    return $str;
+}
+
+# increments value at given index
+sub incr 
+{
+    my $self = shift;
+    my $key = shift;
+    if(!defined $key)
+    {
+        print STDERR "Usage: vector->incr(key)\n";
+        exit;
+    }
+    $self->{$key}++;
+}
+
+# adds 2 sparse vectors
+sub add
+{
+    my $self = shift;
+    my $v2 = shift;
+    if(!defined $v2)
+    {
+        print STDERR "Usage: v1->add(v2)\n";
+        exit;
+    }
+    foreach my $key ($v2->keys)
+    {
+        if(defined $self->{$key})
+        {
+            $self->{$key}+=$v2->get($key);
+        }
+        else
+        {
+            $self->{$key}=$v2->get($key);
+        }
+    }
+}
+
+# returns the norm 
+sub norm
+{
+    my $self = shift;
+    my $sum = 0;
+    foreach my $key ($self->keys)
+    {
+        my $value = $self->{$key};
+        $sum += $value ** 2;
+    }
+    return sqrt $sum;
+}
+
+# normalizes given sparse vector
+sub normalize
+{
+    my $self = shift;
+    my $vnorm = $self->norm;
+    if($vnorm != 0)
+    {
+        $self->div($vnorm);
+    }
+}
+
+sub dot
+{
+    my $self = shift;
+    my $v2 = shift;
+    if(!defined $v2)
+    {
+        print STDERR "Usage: v1->dot(v2)\n";
+        exit;
+    }
+    my $dotprod = 0;
+
+    # optimize to do lesser comparisons by looping on
+    # the smaller vector
+    if (scalar($v2->keys) < scalar($self->keys)) {
+        # v2 is smaller
+        foreach my $key ($v2->keys)
+        {
+            if(defined $self->{$key})
+            {
+                $dotprod += $v2->get($key) * $self->{$key};
+            }
+        }
+    } else {
+        # self is smaller or equal to v2
+        foreach my $key ($self->keys)
+        {
+            if(defined $v2->{$key})
+            {
+                $dotprod += $v2->get($key) * $self->{$key};
+            }
+        }
+    }
+
+    return $dotprod;
+}
+
+# divides each vector entry by a given divisor
+sub div
+{
+    my $self = shift;
+    my $divisor = shift;
+    if(!defined $divisor)
+    {
+        print STDERR "Usage: v1->div(DIVISOR)\n";
+        exit;
+    }
+    if($divisor==0)
+    {
+        print STDERR "Divisor 0 not allowed in Math::SparseVector::div().\n";
+        exit;
+    }
+    foreach my $key ($self->keys)
+    {
+        $self->{$key}/=$divisor;
+    }
+}
+
+# adds a given sparse vector to a binary sparse vector
+sub binadd
+{
+    my $v1 = shift;
+    my $v2 = shift;
+
+    if(!defined $v2)
+    {
+        print STDERR "Usage: v1->binadd(v2)\n";
+        exit;
+    }
+
+    foreach my $key ($v2->keys)
+    {
+        $v1->{$key}=1;
+    }
+}
+
+# deallocates all the vector entries
+sub free
+{
+    my $self = shift;
+    %{$self}=();
+    undef %{$self};
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Math::SparseVector - Supports sparse vector operations such as 
+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. 
+
+=head1 MODULE HISTORY
+
+This module is the successor to Sparse::Vector, which was re-cast 
+into this new namespace in order to introduce another module 
+Math::SparseMatrix, which makes use of this module. 
+
+=head1 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;
+
+=head1 USAGE NOTES
+
+=over 
+
+=item 1. Loading Math::SparseVector Module
+
+To use this module, you must insert the following line in your Perl program
+before using any of the supported methods.
+
+    use Math::SparseVector;
+
+=item 2. Creating a Math::SparseVector Object
+
+The following line creates a new object of Math::SparseVector class referred 
+with the name 'spvec'.
+
+    $spvec=Math::SparseVector->new;
+
+The newly created 'spvec' vector will be initially empty.
+
+=item 3. Using Methods
+
+Now you can use any of the following methods on this 'spvec' Math::SparseVector
+object.
+
+=over
+
+=item 1. set(i,n) - Sets the value at index i to n
+     
+         # equivalent to $spvec{12}=5;
+         $spvec->set(12,5); 
+
+=item 2. get(i)    - Returns the value at index i
+        
+         # equivalent to $value=$spvec{12};
+         $value = $spvec->get(12); 
+
+=item 3. keys()    - Returns the indices of all non-zero values in the vector
+
+         # equivalent to @keys=sort {$a <=> $b} keys %spvec;
+         @indices = $spvec->keys;
+
+=item 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"; }
+
+=item 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;
+
+=item 6. stringify() - Returns the vector in a string form. Same as print() method except the vector is written to a string that is returned instead of displaying onto stdout
+
+         # the below will do exactly same as $spvec->print;
+         $string=$spvec->stringify;
+         print "$string\n";
+
+=item 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.
+
+         After      $v1->add($v2); 
+         v1 = (2, 1, , 8, 8, , 5, , 10) and v2 remains same
+
+=item 8. v1->binadd(v2) - Binary equivalent of v2 is added into v1. 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).
+
+=item 9. incr(i)   - Increments the value at index i
+
+         # is similar to $spvec{12}++;
+         $spvec->incr(12);
+
+=item 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)
+
+=item 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
+
+=item 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
+
+=item 13. free()   - Deallocates all entries and makes the vector empty
+
+         $spvec->free;
+         will set spvec to null vector ()
+
+=back
+=back
+
+=head1 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
+
+=head1 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.
+
+=cut

Added: branches/upstream/libmath-sparsevector-perl/current/t/Math-SparseVector.t
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libmath-sparsevector-perl/current/t/Math-SparseVector.t?rev=68688&op=file
==============================================================================
--- branches/upstream/libmath-sparsevector-perl/current/t/Math-SparseVector.t (added)
+++ branches/upstream/libmath-sparsevector-perl/current/t/Math-SparseVector.t Tue Feb 15 04:55:31 2011
@@ -1,0 +1,197 @@
+# Before `make install' is performed this script should be runnable with
+# `make test'. After `make install' it should work as `perl Math-SparseVector.t'
+
+#########################
+
+use Test::More tests => 20;
+BEGIN { use_ok('Math::SparseVector') };
+
+require_ok('Math::SparseVector');
+
+######################### Instantiate Vector
+
+$vector = Math::SparseVector->new;
+isa_ok($vector, 'Math::SparseVector');
+
+######################### isnull
+
+cmp_ok($vector->isnull, '==', 1, 'test isnull when null');
+
+######################### get/set value 
+
+$vector->set(12,5);
+
+cmp_ok($vector->get(12), '==', 5, 'test get/set');
+
+######################### isnull
+
+cmp_ok($vector->isnull, '==', 0, 'test isnull when not null');
+
+######################### get keys 
+
+$vector->set(100,2);
+$vector->set(20,4);
+ at vector_indices=$vector->keys();
+
+ at expected_keys = (12, 20, 100);
+
+eq_array(\@vector_indices, \@expected_keys);
+
+######################### print 
+
+# tested what $vector->print(); prints
+
+######################### stringify
+
+cmp_ok($vector->stringify, 'eq', "12 5 20 4 100 2", 'test stringify');
+
+######################### incr
+
+$vector->incr(20);
+
+cmp_ok($vector->get(20), '==', 5, 'test incr');
+
+######################### add
+
+# v1 	=     5 2 8 15     12 3 24 6       30 7 50 3 100 9
+# v2 	= 2 5 5 7      9 4      24 7  27 2      50 5       105 3
+
+# v1+v2 = 2 5 5 9 8 15 9 4 12 3 24 13 27 2 30 7 50 8 100 9 105 3
+
+$v1=Math::SparseVector->new;
+$v2=Math::SparseVector->new;
+
+$v1->set(5,2);
+$v1->set(8,15);
+$v1->set(12,3);
+$v1->set(24,6);
+$v1->set(30,7);
+$v1->set(50,3);
+$v1->set(100,9);
+
+$v2->set(2,5);
+$v2->set(5,7);
+$v2->set(9,4);
+$v2->set(24,7);
+$v2->set(27,2);
+$v2->set(50,5);
+$v2->set(105,3);
+
+$v1->add($v2);
+
+# v1 = v1+v2 = 2 5 5 9 8 15 9 4 12 3 24 13 27 2 30 7 50 8 100 9 105 3
+
+cmp_ok($v1->stringify, 'eq', "2 5 5 9 8 15 9 4 12 3 24 13 27 2 30 7 50 8 100 9 105 3", 'test add');
+
+######################### norm
+
+$v3 = Math::SparseVector->new;
+$v3->set(1,3);
+$v3->set(5,5);
+$v3->set(16,3);
+$v3->set(20,4);
+$v3->set(12,4);
+$v3->set(8,5);
+
+# norm = 10
+
+cmp_ok($v3->norm, '==', 10, 'test norm');
+
+######################### normalize
+
+$v3->normalize;
+
+cmp_ok($v3->stringify, 'eq', "1 0.3 5 0.5 8 0.5 12 0.4 16 0.3 20 0.4", 'test normalize');
+
+######################### more add 
+
+# v3 	 = 		1 0.3 5 0.5 8 0.5 12 0.4 16 0.3 20 0.4 
+# vector = 				  12 5          20 5   100 2
+
+$vector->add($v3);
+
+# $vector += $v3
+# $vector =		1 0.3 5 0.5 8 0.5 12 5.4 16 0.3 20 5.4 100 2
+
+cmp_ok($vector->stringify, 'eq', "1 0.3 5 0.5 8 0.5 12 5.4 16 0.3 20 5.4 100 2", 'test add');
+
+cmp_ok($v3->stringify, 'eq', "1 0.3 5 0.5 8 0.5 12 0.4 16 0.3 20 0.4", 'test add');
+
+######################### free
+
+$v1->free;
+$v2->free;
+$v3->free;
+
+cmp_ok($v1->stringify, 'eq', "", 'test free');
+cmp_ok($v2->stringify, 'eq', "", 'test free');
+cmp_ok($v3->stringify, 'eq', "", 'test free');
+
+######################### isnull after free
+
+cmp_ok($v1->isnull, '==', 1, 'test isnull after free');
+
+######################### dot
+
+$v1=Math::SparseVector->new;
+$v2=Math::SparseVector->new;
+
+$v1->set(0,2);
+$v1->set(3,1);
+$v1->set(4,4);
+$v1->set(5,2);
+$v1->set(6,1);
+$v1->set(7,3);
+
+$v2->set(0,2);
+$v2->set(1,1);
+$v2->set(4,2);
+$v2->set(7,1);
+$v2->set(8,2);
+
+# v1 = 		2 0 0 1 4 2 1 3 0
+# v2 = 		2 1 0 0 2 0 0 1 2
+
+# v1.v2 = 	4+8+3 = 15
+
+cmp_ok($v1->dot($v2), '==', 15, 'test dot');
+
+######################### div
+
+# v1   = 		2  0  0    1  4  2    1    3  0
+
+# v1/2 =		1  0  0  0.5  2  1  0.5  1.5  0
+
+$v1->div(2);
+cmp_ok($v1->stringify, 'eq', "0 1 3 0.5 4 2 5 1 6 0.5 7 1.5", 'test div');
+
+######################### binadd
+
+$v1->free;
+$v2->free;
+
+$v1=Math::SparseVector->new;
+$v1->set(5,1);
+$v1->set(10,1);
+$v1->set(15,1);
+$v1->set(22,1);
+$v1->set(27,1);
+$v1->set(34,1);
+
+$v2=Math::SparseVector->new;
+$v2->set(2,4);
+$v2->set(10,5);
+$v2->set(13,3);
+$v2->set(22,6);
+$v2->set(30,7);
+$v2->set(36,1);
+
+# v1 =		    5 1 10 1      15 1 22 1 27 1      34 1
+# v2 =		2 4     10 5 13 3      22 6      30 7      36 1
+
+$v1->binadd($v2);
+
+# v1 =		2 1 5 1 10 1 13 1 15 1 22 1 27 1 30 1 34 1 36 1
+
+cmp_ok($v1->stringify, 'eq', "2 1 5 1 10 1 13 1 15 1 22 1 27 1 30 1 34 1 36 1", 'test binadd');
+




More information about the Pkg-perl-cvs-commits mailing list