2 # Module for Bio::PhyloNetwork::muVector
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Gabriel Cardona <gabriel(dot)cardona(at)uib(dot)es>
8 # Copyright Gabriel Cardona
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::PhyloNetwork::muVector - Module to compute with vectors of arbitrary
24 use Bio::PhyloNetwork::muVector;
26 my $vec1=Bio::PhyloNetwork::muVector->new(4);
27 my $vec2=Bio::PhyloNetwork::muVector->new([1,2,3,4]);
28 my $vec3=Bio::PhyloNetwork::muVector->new([10,20,30,40]);
30 my $vec4=$vec3-10*$vec2;
31 if (($vec4 cmp $vec1) == 0) {
32 print "$vec4 is zero\n";
35 my $vec5=Bio::PhyloNetwork::muVector->new([8,2,2,4]);
36 my $vec6=Bio::PhyloNetwork::muVector->new([1,2,3,4]);
38 print "Test poset $vec5 > $vec6: ".$vec5->geq_poset($vec6)."\n";
39 print "Test lex $vec5 > $vec6: ".($vec5 cmp $vec6)."\n";
43 This is a module to work with vectors. It creates
44 vectors of arbitrary length, defines its basic arithmetic operations,
45 its lexicographic ordering and the natural structure of poset.
49 Gabriel Cardona, gabriel(dot)cardona(at)uib(dot)es
53 The rest of the documentation details each of the object methods.
57 package Bio
::PhyloNetwork
::muVector
;
62 use base
qw(Bio::Root::Root);
67 Usage : my $mu = new Bio::PhyloNetwork::muVector();
68 Function: Creates a new Bio::PhyloNetwork::muVector object
69 Returns : Bio::PhyloNetwork::muVector
70 Args : integer or (reference to) an array
72 If given an integer as argument, returns a Bio::PhyloNetwork::muVector
73 object with dimension the integer given and initialized to zero.
74 If it is an anonimous array, then the vector is initialized with the values
75 in the array and with the corresponding dimension.
81 my $self=$pkg->SUPER::new
();
84 #$cont is a number; initialize to a zero-vector
85 for (my $i=0; $i < $cont; $i++) {
90 #$cont points to an array
93 $self->{dim
}=scalar @arr;
106 "*" => \
&scalarproduct
,
107 "<=>" => \
&comparelex
,
108 "cmp" => \
&comparelex
,
119 Usage : my $str=$mu->display()
120 Function: returns an string displaying its contents
124 This function is also overloaded to the "" operator.
130 my @arr=@
{$self->{arr
}};
137 Usage : $mu->add($mu2)
138 Function: returns the sum of $mu and $mu2
139 Returns : Bio::PhyloNetwork::muVector
140 Args : Bio::PhyloNetwork::muVector
142 This function is also overloaded to the + operator.
149 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
152 for (my $i=0; $i<$dim; $i++) {
153 $sum[$i]=$v1->[$i]+$v2->[$i];
155 my $result=Bio
::PhyloNetwork
::muVector
->new(\
@sum);
162 Usage : $mu->substract($mu2)
163 Function: returns the difference of $mu and $mu2
164 Returns : Bio::PhyloNetwork::muVector
165 Args : Bio::PhyloNetwork::muVector
167 This function is also overloaded to the - operator.
174 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
177 for (my $i=0; $i<$dim; $i++) {
178 $sum[$i]=$v1->{arr
}->[$i]-$v2->{arr
}->[$i];
180 my $result=Bio
::PhyloNetwork
::muVector
->new(\
@sum);
186 Title : scalarproduct
187 Usage : $mu->scalarproduct($ct)
188 Function: returns the scalar product of $ct and $mu
189 Returns : Bio::PhyloNetwork::muVector
192 This function is also overloaded to the * operator.
197 my ($v1,$num,$swapped)=@_;
201 for (my $i=0; $i<$dim; $i++) {
202 $sum[$i]=$num*$v1->{arr
}->[$i];
204 my $result=Bio
::PhyloNetwork
::muVector
->new(\
@sum);
212 Usage : $mu1->comparelex($mu2)
213 Function: compares $mu and $mu2 w.r.t. the lexicographic ordering
214 Returns : scalar (-1 if $mu1<$mu2, 0 if $mu1=$mu2, 1 if $mu1>$mu2)
215 Args : Bio::PhyloNetwork::muVector
217 This function is also overloaded to the E<lt>=E<gt> and cmp operator.
224 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
226 for (my $i=0; $i<$dim; $i++) {
227 return -1 if $v1->{arr
}->[$i] < $v2->{arr
}->[$i];
228 return 1 if $v1->{arr
}->[$i] > $v2->{arr
}->[$i];
236 Usage : $mu1->geq_poset($mu2)
237 Function: compares $mu and $mu2 w.r.t. the natural partial ordering
238 Returns : boolean (1 if $mu >= $mu2, 0 otherwise)
239 Args : Bio::PhyloNetwork::muVector
246 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
248 for (my $i=0; $i<$dim; $i++) {
249 return 0 unless $v1->[$i] >= $v2->[$i];
257 Usage : $mu->is_positive()
258 Function: tests if all components of $mu are positive (or zero)
268 for (my $i=0; $i<$dim; $i++) {
269 return 0 unless $v1->[$i] >= 0;
277 Usage : $mu1->hamming($mu2)
278 Function: returns the Hamming distance between $mu1 and $mu2
280 Args : Bio::PhyloNetwork::muVector
287 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
290 for (my $i=0; $i<$dim; $i++) {
291 $w++ unless $v1->[$i] == $v2->[$i];
299 Usage : $mu1->manhattan($mu2)
300 Function: returns the Manhattan distance between $mu1 and $mu2
302 Args : Bio::PhyloNetwork::muVector
309 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
312 for (my $i=0; $i<$dim; $i++) {
313 $w+= abs($v1->[$i] - $v2->[$i]);