Vector products: Difference between revisions

({{header|Liberty BASIC}})
Line 718:
6
[-267, 204, -3]</pre>
 
=={{header|Perl}}==
<lang Perl>package Vector;
use List::Util 'sum';
use List::MoreUtils 'pairwise';
 
sub new { shift; bless [@_] }
 
use overload (
'""' => sub { "(@{+shift})" },
'&' => sub { sum pairwise { $a * $b } @{+shift}, @{+shift} },
'^' => sub {
my @a = @{+shift};
my @b = @{+shift};
bless [ $a[1]*$b[2] - $a[2]*$b[1],
$a[2]*$b[0] - $a[0]*$b[2],
$a[0]*$b[1] - $a[1]*$b[0] ]
},
);
 
package main;
my $a = Vector->new(3, 4, 5);
my $b = Vector->new(4, 3, 5);
my $c = Vector->new(-5, -12, -13);
 
print "a = $a b = $b c = $c\n";
print "$a . $b = ", $a & $b, "\n";
print "$a x $b = ", $a ^ $b, "\n";
print "$a . ($b x $c) = ", $a & ($b ^ $c), "\n";
print "$a x ($b x $c) = ", $a ^ ($b ^ $c), "\n";</lang>
 
Output: <pre>a = (3 4 5) b = (4 3 5) c = (-5 -12 -13)
(3 4 5) . (4 3 5) = 49
(3 4 5) x (4 3 5) = (5 5 -7)
(3 4 5) . ((4 3 5) x (-5 -12 -13)) = 6
(3 4 5) x ((4 3 5) x (-5 -12 -13)) = (-267 204 -3)</pre>
 
=={{header|Perl 6}}==
Anonymous user