Skip to content

Commit 89a80e2

Browse files
committed
add mult test from Math::Quaternion, order the code like Wikipedia page
1 parent 2d68350 commit 89a80e2

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

lib/PDL/Graphics/TriD/Quaternion.pm

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,26 @@ sub to_vrmlrot {
4848
return [(map {$_/sin($d)} @{$this}[1..3]),2*$d];
4949
}
5050

51-
# Yuck
5251
sub multiply {
53-
my($this,$with) = @_;
54-
return PDL::Graphics::TriD::Quaternion->new(
55-
$this->[0] * $with->[0] -
56-
$this->[1] * $with->[1] -
57-
$this->[2] * $with->[2] -
58-
$this->[3] * $with->[3],
59-
$this->[2] * $with->[3] -
60-
$this->[3] * $with->[2] +
61-
$this->[0] * $with->[1] +
62-
$this->[1] * $with->[0],
63-
$this->[3] * $with->[1] -
64-
$this->[1] * $with->[3] +
65-
$this->[0] * $with->[2] +
66-
$this->[2] * $with->[0],
67-
$this->[1] * $with->[2] -
68-
$this->[2] * $with->[1] +
69-
$this->[0] * $with->[3] +
70-
$this->[3] * $with->[0],
71-
);
52+
my($this,$with) = @_;
53+
return PDL::Graphics::TriD::Quaternion->new(
54+
$this->[0] * $with->[0]
55+
- $this->[1] * $with->[1]
56+
- $this->[2] * $with->[2]
57+
- $this->[3] * $with->[3],
58+
$this->[0] * $with->[1]
59+
+ $this->[1] * $with->[0]
60+
+ $this->[2] * $with->[3]
61+
- $this->[3] * $with->[2],
62+
$this->[0] * $with->[2]
63+
- $this->[1] * $with->[3]
64+
+ $this->[2] * $with->[0]
65+
+ $this->[3] * $with->[1],
66+
$this->[0] * $with->[3]
67+
+ $this->[1] * $with->[2]
68+
- $this->[2] * $with->[1]
69+
+ $this->[3] * $with->[0],
70+
);
7271
}
7372

7473
sub multiply_scalar {

t/quaternion.t

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ is_qua $q, [0,0,0,1];
1818

1919
is_qua +PDL::Graphics::TriD::Quaternion->new(0,0,0,2)->normalise, [0,0,0,1];
2020

21+
my ($q1, $q2) = map PDL::Graphics::TriD::Quaternion->new(@$_), [1,2,3,4], [5,6,7,8];
22+
23+
is_qua $q1->multiply($q2), [
24+
5-12-21-32, # $a0*$b0 - $a1*$b1 - $a2*$b2 - $a3*$b3,
25+
6+10+24-28, # $a0*$b1 + $b0*$a1 + $a2*$b3 - $a3*$b2,
26+
7+15+24-16, # $a0*$b2 + $b0*$a2 + $a3*$b1 - $a1*$b3,
27+
8+20+14-18, # $a0*$b3 + $b0*$a3 + $a1*$b2 - $a2*$b1
28+
];
29+
2130
done_testing;

0 commit comments

Comments
 (0)