1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
# Author: Chao-Kuei Hung
# For more info, including license, please see doc/index.html
package Vector;
# Mathematical Vector
use strict;
use Carp;
use vars qw(@ISA);
@ISA = qw();
my (%generated);
BEGIN {
my ($functemplate) = q{
sub {
my ($self, $that) = @_;
my ($r) = bless [], ref($self);
my ($i);
if (ref $that) {
croak "dimension mismatch (", $#$self+1, " vs ",
$#$that+1, ") in <OP>" unless $#$self == $#$that;
for ($i=0; $i<=$#$self; ++$i) {
$r->[$i] = $self->[$i] <OP> $that->[$i];
}
} else {
for ($i=0; $i<=$#$self; ++$i) {
$r->[$i] = $self->[$i] <OP> $that;
}
}
return $r;
}
};
my (%functab) = (
add => '+',
sbt => '-',
mul => '*',
div => '/',
);
my ($name, $op);
while (($name, $op) = each %functab) {
my ($t) = $functemplate;
$t =~ s/<OP>/$op/g;
$generated{$name} = eval $t;
}
}
# see perldoc overload, especially the "MAGIC AUTOGENERATION" section
use overload
'=' => '_clone',
'""' => 'stringify',
'+' => $generated{add},
'-' => $generated{sbt},
'neg'=> 'negate',
'*' => $generated{mul},
'/' => $generated{div},
'fallback' => undef
;
sub pw_mul { return $generated{mul}->(@_); }
sub pw_div { return $generated{div}->(@_); }
# Different from "Perl Cookbook", chap 13.6, p.461 "cloning objects"
# See Randal Schwartz's "Constructing Objects" at
# http://www.stonehenge.com/merlyn/UnixReview/col52.html
# (search for "three camps")
sub new {
my ($proto, @data) = @_;
my ($class) = ref $proto || $proto;
# if (ref $data[0] eq "Vector") {
if (ref $proto) {
return bless [ @$proto ], $class;
} else {
return bless [@data], $class;
}
}
# Copy constructor is very tricky. It is _not_ called until
# just before a mutator is applied to one of the reference
# variables sharing the same copy. See perldoc overload,
# especially the "Copy Constructor" section.
sub _clone {
my ($a, $b, $switch) = @_;
print STDERR "Vector::_clone : switch is undef!\n"
unless defined $switch;
# print STDERR $switch ? "+" : "-"; # always prints "-"
return $switch ? bless([@$a],"Vector") : bless([@$b],"Vector");
}
sub stringify {
my ($self) = @_;
my ($r) = sprintf "[ %8g", $self->[0];
foreach (@{$self}[1..$#$self]) {
$r .= sprintf(", %8g", $_);
}
return $r . " ]";
}
sub negate {
my ($self) = @_;
return bless [map { -$_ } @$self], ref $self;
}
sub x { return $_[0]->[0]; }
sub y { return $_[0]->[1]; }
sub z { return $_[0]->[2]; }
sub dot {
# dot product
my ($t) = $_[0]->pw_mul($_[1]);
my ($s, $i);
for ($i=0; $i<=$#$t; ++$i) {
$s += $t->[$i];
}
return $s;
}
sub norm {
my ($self) = @_;
return sqrt($self->dot($self));
}
sub angle_cos {
my ($self, $b) = @_;
return $self->dot($b)/$self->norm()/$b->norm();
}
sub cob {
# change of basis
my ($self, $b) = @_;
die unless ($#$b == $#$self and $#$b == $#{$b->[0]});
my ($r) = $self->new();
map { $_ = 0; } @$r;
my ($i);
for ($i=0; $i<=$#$self; ++$i) {
$r += $b->[$i]->pw_mul($self->[$i]);
}
return $r;
}
if ($0 =~ /Vector.pm$/) {
# being tested as a stand-alone program, so run test code.
my ($p, $q, $r);
$p = Vector->new(4,-3);
$q = Vector->new(5,12);
print $p+$q, ",", $p-$q, "\n";
$r = $p;
$r += $q;
$q = $q->pw_div(2);
print $p, ",", $q, ",", $r, ",", $p->pw_mul(3), ",", -$p, "\n";
}
1;
|