#=============================================== # Vector #=============================================== package Vector; use Exporter; use Math::Vector::Real; use Math::MatrixReal; @ISA = qw(Exporter Math::Vector::Real Math::MatrixReal); #公開したいサブルーチン #@EXPORT = qw(V); #our $VERSION = '0.17'; use strict; use Sci qw($todeg $torad); #my $u = V(3, 3, 0); #$p = $u * $v; # dot product #$f = 1.4 * $u + $v; # scalar product and vector addition #$c = $u x $v; # cross product, only defined for 3D vectors #+ * / #- (both unary and binary) #x (cross product for 3D vectors) #+= -= *= /= x= #== != #"" (stringfication) #abs (returns the norm) #atan2 (returns the angle between two vectors) #use Math::MatrixReal; #my $a = Math::MatrixReal->new_random(5, 5); #my $matrix = Math::MatrixReal->new_from_cols( [ [1,2], [3,4] ] ); #my $matrix = Math::MatrixReal->new_from_rows( [ [1,2], [3,4] ] ); #my $b = $a->new_random(10, 30, { symmetric=>1, bounded_by=>[-1,1] }); #my $c = $b * $a ** 3; #my $d = $b->new_from_rows( [ [ 5, 3 ,4], [3, 4, 5], [ 2, 4, 1 ] ] ); #print $a; #my $row = ($a * $b)->row(3); #my $col = (5*$c)->col(2); #my $transpose = ~$c; #my $transpose = $c->transpose; #my $inverse = $a->inverse; #my $inverse = 1/$a; #my $inverse = $a ** -1; #my $determinant= $a->det; #sub V { bless [@_] } #sub new { # my $class = shift; # bless [@_], $class #} sub new { my ($module) = @_; my $this = {}; bless $this, $module; #$v = Math::Vector::Real->new(@components) return $this; } sub DESTROY { my $this = shift; } sub zero { my ($dim) = @_; return Math::Vector::Real->zero($dim); } sub unit { my ($v) = @_; #$v / abs($v) = $v->versor(); return $v / abs($v); } sub UnitVectors { #V(@components) = Math::Vector::Real->new(@components) my $e1 = V(1.0, 0.0, 0.0); my $e2 = V(0.0, 1.0, 0.0); my $e3 = V(0.0, 0.0, 1.0); return ($e1, $e2, $e3); } sub Angle { my ($pa, $pb, $UseDeg) = @_; my $a2 = &InnerProduct(@$pa, @$pa); my $b2 = &InnerProduct(@$pb, @$pb); my $ab = &InnerProduct(@$pa, @$pb); my $cosQ = $ab / sqrt($a2 * $b2); return Sci::acos($cosQ) if(!$UseDeg); return Sci::acos($cosQ) * $todeg; } sub OuterProduct { my (@a) = @_; my $dim = 3; return ($a[1]*$a[2+$dim]-$a[2]*$a[1+$dim], $a[2]*$a[0+$dim]-$a[0]*$a[2+$dim], $a[0]*$a[1+$dim]-$a[1]*$a[0+$dim]); } sub InnerProduct { my (@a) = @_; my $dim = scalar @a / 2; #print "dim=$dim\n"; my $c = 0.0; for(my $i = 0 ; $i < $dim ; $i++) { $c += $a[$i] * $a[$i+$dim]; } return $c; } sub Difference { my (@a) = @_; my $dim = scalar @a / 2; #print "dim=$dim\n"; my @c; for(my $i = 0 ; $i < $dim ; $i++) { $c[$i] = $a[$i] - $a[$i+$dim]; } return @c; } 1;