1
# $Id: muVector.pm 14716 2008-06-11 06:48:28Z heikki $
3
# Module for Bio::PhyloNetwork::muVector
5
# Cared for by Gabriel Cardona <gabriel(dot)cardona(at)uib(dot)es>
7
# Copyright Gabriel Cardona
9
# You may distribute this module under the same terms as perl itself
11
# POD documentation - main docs before the code
15
Bio::PhyloNetwork::muVector - Module to compute with vectors of arbitrary
23
use Bio::PhyloNetwork::muVector;
25
my $vec1=Bio::PhyloNetwork::muVector->new(4);
26
my $vec2=Bio::PhyloNetwork::muVector->new([1,2,3,4]);
27
my $vec3=Bio::PhyloNetwork::muVector->new([10,20,30,40]);
29
my $vec4=$vec3-10*$vec2;
30
if (($vec4 cmp $vec1) == 0) {
31
print "$vec4 is zero\n";
34
my $vec5=Bio::PhyloNetwork::muVector->new([8,2,2,4]);
35
my $vec6=Bio::PhyloNetwork::muVector->new([1,2,3,4]);
37
print "Test poset $vec5 > $vec6: ".$vec5->geq_poset($vec6)."\n";
38
print "Test lex $vec5 > $vec6: ".($vec5 cmp $vec6)."\n";
42
This is a module to work with vectors. It creates
43
vectors of arbitrary length, defines its basic arithmetic operations,
44
its lexicographic ordering and the natural structure of poset.
48
Gabriel Cardona, gabriel(dot)cardona(at)uib(dot)es
52
The rest of the documentation details each of the object methods.
56
package Bio::PhyloNetwork::muVector;
61
use base qw(Bio::Root::Root);
66
Usage : my $mu = new Bio::PhyloNetwork::muVector();
67
Function: Creates a new Bio::PhyloNetwork::muVector object
68
Returns : Bio::PhyloNetwork::muVector
69
Args : integer or (reference to) an array
71
If given an integer as argument, returns a Bio::PhyloNetwork::muVector
72
object with dimension the integer given and initialized to zero.
73
If it is an anonimous array, then the vector is initialized with the values
74
in the array and with the corresponding dimension.
80
my $self=$pkg->SUPER::new();
83
#$cont is a number; initialize to a zero-vector
84
for (my $i=0; $i < $cont; $i++) {
89
#$cont points to an array
92
$self->{dim}=scalar @arr;
105
"*" => \&scalarproduct,
106
"<=>" => \&comparelex,
107
"cmp" => \&comparelex,
118
Usage : my $str=$mu->display()
119
Function: returns an string displaying its contents
123
This function is also overloaded to the "" operator.
129
my @arr=@{$self->{arr}};
136
Usage : $mu->add($mu2)
137
Function: returns the sum of $mu and $mu2
138
Returns : Bio::PhyloNetwork::muVector
139
Args : Bio::PhyloNetwork::muVector
141
This function is also overloaded to the + operator.
148
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
151
for (my $i=0; $i<$dim; $i++) {
152
$sum[$i]=$v1->[$i]+$v2->[$i];
154
my $result=Bio::PhyloNetwork::muVector->new(\@sum);
161
Usage : $mu->substract($mu2)
162
Function: returns the difference of $mu and $mu2
163
Returns : Bio::PhyloNetwork::muVector
164
Args : Bio::PhyloNetwork::muVector
166
This function is also overloaded to the - operator.
173
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
176
for (my $i=0; $i<$dim; $i++) {
177
$sum[$i]=$v1->{arr}->[$i]-$v2->{arr}->[$i];
179
my $result=Bio::PhyloNetwork::muVector->new(\@sum);
185
Title : scalarproduct
186
Usage : $mu->scalarproduct($ct)
187
Function: returns the scalar product of $ct and $mu
188
Returns : Bio::PhyloNetwork::muVector
191
This function is also overloaded to the * operator.
196
my ($v1,$num,$swapped)=@_;
200
for (my $i=0; $i<$dim; $i++) {
201
$sum[$i]=$num*$v1->{arr}->[$i];
203
my $result=Bio::PhyloNetwork::muVector->new(\@sum);
211
Usage : $mu1->comparelex($mu2)
212
Function: compares $mu and $mu2 w.r.t. the lexicographic ordering
213
Returns : scalar (-1 if $mu1<$mu2, 0 if $mu1=$mu2, 1 if $mu1>$mu2)
214
Args : Bio::PhyloNetwork::muVector
216
This function is also overloaded to the E<lt>=E<gt> and cmp operator.
223
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
225
for (my $i=0; $i<$dim; $i++) {
226
return -1 if $v1->{arr}->[$i] < $v2->{arr}->[$i];
227
return 1 if $v1->{arr}->[$i] > $v2->{arr}->[$i];
235
Usage : $mu1->geq_poset($mu2)
236
Function: compares $mu and $mu2 w.r.t. the natural partial ordering
237
Returns : boolean (1 if $mu >= $mu2, 0 otherwise)
238
Args : Bio::PhyloNetwork::muVector
245
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
247
for (my $i=0; $i<$dim; $i++) {
248
return 0 unless $v1->[$i] >= $v2->[$i];
256
Usage : $mu->is_positive()
257
Function: tests if all components of $mu are positive (or zero)
267
for (my $i=0; $i<$dim; $i++) {
268
return 0 unless $v1->[$i] >= 0;
276
Usage : $mu1->hamming($mu2)
277
Function: returns the Hamming distance between $mu1 and $mu2
279
Args : Bio::PhyloNetwork::muVector
286
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
289
for (my $i=0; $i<$dim; $i++) {
290
$w++ unless $v1->[$i] == $v2->[$i];
298
Usage : $mu1->manhattan($mu2)
299
Function: returns the Manhattan distance between $mu1 and $mu2
301
Args : Bio::PhyloNetwork::muVector
308
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
311
for (my $i=0; $i<$dim; $i++) {
312
$w+= abs($v1->[$i] - $v2->[$i]);