3 # Module for Bio::PhyloNetwork::muVector
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Gabriel Cardona <gabriel(dot)cardona(at)uib(dot)es>
9 # Copyright Gabriel Cardona
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
17 Bio::PhyloNetwork::muVector - Module to compute with vectors of arbitrary
25 use Bio::PhyloNetwork::muVector;
27 my $vec1=Bio::PhyloNetwork::muVector->new(4);
28 my $vec2=Bio::PhyloNetwork::muVector->new([1,2,3,4]);
29 my $vec3=Bio::PhyloNetwork::muVector->new([10,20,30,40]);
31 my $vec4=$vec3-10*$vec2;
32 if (($vec4 cmp $vec1) == 0) {
33 print "$vec4 is zero\n";
36 my $vec5=Bio::PhyloNetwork::muVector->new([8,2,2,4]);
37 my $vec6=Bio::PhyloNetwork::muVector->new([1,2,3,4]);
39 print "Test poset $vec5 > $vec6: ".$vec5->geq_poset($vec6)."\n";
40 print "Test lex $vec5 > $vec6: ".($vec5 cmp $vec6)."\n";
44 This is a module to work with vectors. It creates
45 vectors of arbitrary length, defines its basic arithmetic operations,
46 its lexicographic ordering and the natural structure of poset.
50 Gabriel Cardona, gabriel(dot)cardona(at)uib(dot)es
54 The rest of the documentation details each of the object methods.
58 package Bio
::PhyloNetwork
::muVector
;
63 use base
qw(Bio::Root::Root);
68 Usage : my $mu = new Bio::PhyloNetwork::muVector();
69 Function: Creates a new Bio::PhyloNetwork::muVector object
70 Returns : Bio::PhyloNetwork::muVector
71 Args : integer or (reference to) an array
73 If given an integer as argument, returns a Bio::PhyloNetwork::muVector
74 object with dimension the integer given and initialized to zero.
75 If it is an anonimous array, then the vector is initialized with the values
76 in the array and with the corresponding dimension.
82 my $self=$pkg->SUPER::new
();
85 #$cont is a number; initialize to a zero-vector
86 for (my $i=0; $i < $cont; $i++) {
91 #$cont points to an array
94 $self->{dim
}=scalar @arr;
107 "*" => \
&scalarproduct
,
108 "<=>" => \
&comparelex
,
109 "cmp" => \
&comparelex
,
120 Usage : my $str=$mu->display()
121 Function: returns an string displaying its contents
125 This function is also overloaded to the "" operator.
131 my @arr=@
{$self->{arr
}};
138 Usage : $mu->add($mu2)
139 Function: returns the sum of $mu and $mu2
140 Returns : Bio::PhyloNetwork::muVector
141 Args : Bio::PhyloNetwork::muVector
143 This function is also overloaded to the + operator.
150 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
153 for (my $i=0; $i<$dim; $i++) {
154 $sum[$i]=$v1->[$i]+$v2->[$i];
156 my $result=Bio
::PhyloNetwork
::muVector
->new(\
@sum);
163 Usage : $mu->substract($mu2)
164 Function: returns the difference of $mu and $mu2
165 Returns : Bio::PhyloNetwork::muVector
166 Args : Bio::PhyloNetwork::muVector
168 This function is also overloaded to the - operator.
175 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
178 for (my $i=0; $i<$dim; $i++) {
179 $sum[$i]=$v1->{arr
}->[$i]-$v2->{arr
}->[$i];
181 my $result=Bio
::PhyloNetwork
::muVector
->new(\
@sum);
187 Title : scalarproduct
188 Usage : $mu->scalarproduct($ct)
189 Function: returns the scalar product of $ct and $mu
190 Returns : Bio::PhyloNetwork::muVector
193 This function is also overloaded to the * operator.
198 my ($v1,$num,$swapped)=@_;
202 for (my $i=0; $i<$dim; $i++) {
203 $sum[$i]=$num*$v1->{arr
}->[$i];
205 my $result=Bio
::PhyloNetwork
::muVector
->new(\
@sum);
213 Usage : $mu1->comparelex($mu2)
214 Function: compares $mu and $mu2 w.r.t. the lexicographic ordering
215 Returns : scalar (-1 if $mu1<$mu2, 0 if $mu1=$mu2, 1 if $mu1>$mu2)
216 Args : Bio::PhyloNetwork::muVector
218 This function is also overloaded to the E<lt>=E<gt> and cmp operator.
225 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
227 for (my $i=0; $i<$dim; $i++) {
228 return -1 if $v1->{arr
}->[$i] < $v2->{arr
}->[$i];
229 return 1 if $v1->{arr
}->[$i] > $v2->{arr
}->[$i];
237 Usage : $mu1->geq_poset($mu2)
238 Function: compares $mu and $mu2 w.r.t. the natural partial ordering
239 Returns : boolean (1 if $mu >= $mu2, 0 otherwise)
240 Args : Bio::PhyloNetwork::muVector
247 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
249 for (my $i=0; $i<$dim; $i++) {
250 return 0 unless $v1->[$i] >= $v2->[$i];
258 Usage : $mu->is_positive()
259 Function: tests if all components of $mu are positive (or zero)
269 for (my $i=0; $i<$dim; $i++) {
270 return 0 unless $v1->[$i] >= 0;
278 Usage : $mu1->hamming($mu2)
279 Function: returns the Hamming distance between $mu1 and $mu2
281 Args : Bio::PhyloNetwork::muVector
288 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
291 for (my $i=0; $i<$dim; $i++) {
292 $w++ unless $v1->[$i] == $v2->[$i];
300 Usage : $mu1->manhattan($mu2)
301 Function: returns the Manhattan distance between $mu1 and $mu2
303 Args : Bio::PhyloNetwork::muVector
310 $v1->throw("Vectors not the same size") unless ($v1->{dim
} == $v2->{dim
});
313 for (my $i=0; $i<$dim; $i++) {
314 $w+= abs($v1->[$i] - $v2->[$i]);