Skip to content

Commit 1bef3d8

Browse files
Merge pull request #282 from PolyMathOrg/refactor-quaternions-pt2
Refactor Quaternion Natural Log
2 parents 074d53e + 61f5bd4 commit 1bef3d8

File tree

2 files changed

+82
-23
lines changed

2 files changed

+82
-23
lines changed

src/Math-Quaternion/PMQuaternion.class.st

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,25 +348,21 @@ PMQuaternion >> k [
348348

349349
{ #category : #'mathematical functions' }
350350
PMQuaternion >> ln [
351-
"Answer the receiver natural logarithm"
351+
352+
"Compute the natural logarithm."
352353

353354
| z |
354355
z := self unreal abs.
355-
^z isZero
356-
ifTrue:
357-
[self species
358-
qr: self abs ln
359-
qi: (z arcTan: qr)
360-
qj: 0
361-
qk: 0]
362-
ifFalse:
363-
[| theta |
364-
theta := (z arcTan: qr) / z.
365-
self species
366-
qr: self abs ln
367-
qi: qi * theta
368-
qj: qj * theta
369-
qk: qk * theta]
356+
^ z isZero
357+
ifTrue: [
358+
| vectorPart |
359+
vectorPart := 0 + (z arcTan: qr) i + qj j + qk k.
360+
self abs ln + vectorPart ]
361+
ifFalse: [
362+
| theta vectorPart |
363+
theta := (z arcTan: qr) / z.
364+
vectorPart := theta * (0 + qi i + qj j + qk k).
365+
self abs ln + vectorPart ]
370366
]
371367

372368
{ #category : #'mathematical functions' }

src/Math-Tests-Quaternion/PMQuaternionTest.class.st

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,6 @@ PMQuaternionTest >> testHash [
137137
self assert: 1 k k hash equals: -1 hash
138138
]
139139

140-
{ #category : #running }
141-
PMQuaternionTest >> testLn [
142-
| eps |
143-
eps := 1.0e-6.
144-
self assert: ((1 + 2 i) ln - (1 + 2 j k) ln) abs < eps
145-
]
146-
147140
{ #category : #running }
148141
PMQuaternionTest >> testLog [
149142

@@ -160,6 +153,67 @@ PMQuaternionTest >> testLog [
160153
self assert: (qln qk closeTo: qlg10ln qk)
161154
]
162155

156+
{ #category : #running }
157+
PMQuaternionTest >> testNaturalLogOfQuaternion [
158+
159+
"See https://en.wikipedia.org/wiki/Quaternion#cite_note-Särkkä2007-39 for more detail"
160+
161+
| quaternion naturalLogOfQuaternion expected vectorPartOfQuaternion |
162+
quaternion := 3 + 4 i + 5 j + 6 k.
163+
naturalLogOfQuaternion := quaternion ln.
164+
165+
vectorPartOfQuaternion := PMQuaternion
166+
qr: 0
167+
qi: 4
168+
qj: 5
169+
qk: 6.
170+
expected := 86 sqrt ln
171+
+ (vectorPartOfQuaternion / 77 sqrt * (3 / 86 sqrt) arcCos).
172+
173+
self assert: naturalLogOfQuaternion qr equals: expected qr.
174+
self assert: naturalLogOfQuaternion qi equals: expected qi.
175+
self assert: naturalLogOfQuaternion qj closeTo: expected qj.
176+
self assert: naturalLogOfQuaternion qk equals: expected qk
177+
]
178+
179+
{ #category : #running }
180+
PMQuaternionTest >> testNaturalLogOfQuaternionWithNoVectorPart [
181+
| expected naturalLogOfQuaternion quaternionWithNoVector |
182+
183+
quaternionWithNoVector := 5 + 0 i + 0 j + 0 k.
184+
185+
naturalLogOfQuaternion := quaternionWithNoVector ln.
186+
187+
expected := 5 ln + 0 i + 0 j + 0 k.
188+
self assert: naturalLogOfQuaternion qr equals: expected qr.
189+
self assert: naturalLogOfQuaternion qi equals: 0.
190+
self assert: naturalLogOfQuaternion qj equals: 0.
191+
self assert: naturalLogOfQuaternion qk equals: 0.
192+
]
193+
194+
{ #category : #tests }
195+
PMQuaternionTest >> testNaturalLogOfQuaternionWithOnlyVectorPart [
196+
197+
"See https://en.wikipedia.org/wiki/Quaternion#cite_note-Särkkä2007-39 for more detail"
198+
199+
| quaternion naturalLogOfQuaternion expected expectedVectorPart |
200+
quaternion := 0 + 7 i - 10 j + 23 k.
201+
naturalLogOfQuaternion := quaternion ln.
202+
expectedVectorPart := PMQuaternion
203+
qr: 0
204+
qi: 7
205+
qj: -10
206+
qk: 23.
207+
208+
209+
expected := 678 sqrt ln + ((expectedVectorPart / 678 sqrt) * ((0 / 678 sqrt) arcCos)).
210+
211+
self assert: naturalLogOfQuaternion qr equals: expected qr.
212+
self assert: naturalLogOfQuaternion qi equals: expected qi.
213+
self assert: naturalLogOfQuaternion qj closeTo: expected qj.
214+
self assert: naturalLogOfQuaternion qk equals: expected qk
215+
]
216+
163217
{ #category : #running }
164218
PMQuaternionTest >> testOne [
165219
self assert: PMQuaternion one unreal isZero.
@@ -264,6 +318,15 @@ PMQuaternionTest >> testSinh [
264318
self assert: ((1 + 2 i) sinh - (1 + 2 j k) sinh) abs < eps
265319
]
266320

321+
{ #category : #running }
322+
PMQuaternionTest >> testSquareOfQuaternion [
323+
| q expected |
324+
q := 1 + 2 i - 3 j - 4 k.
325+
expected := -28 + 4 i - 6 j - 8 k.
326+
327+
self assert: q squared equals: expected
328+
]
329+
267330
{ #category : #running }
268331
PMQuaternionTest >> testSubtractPolynomial [
269332
| poly |

0 commit comments

Comments
 (0)