apple / swift-numerics

Advanced mathematical types and functions for Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Quaternion Math

pashabou opened this issue · comments

Since the Numerics modules cover complex numbers and operations, and you mentioned here (#6) that ShapedArrays are within the scope of the project, would quaternion math and possibly other algebras fall under the scope of Swift Numerics as well?

Use cases include everywhere 3D rotation is used, from games and visualization to simulation and control systems. A well-tested and well-documented implementation would be a big boon for a new math library. Some quick googling shows this is not in the C++ STL or a standard part of NumPy, but it is a standard part of MATLAB and part of Accelerate (maybe better suited as part of an update/rewrite of Accelerate?).

Hi @pashabou -- quaternions are definitely in-scope, this is a reasonable suggestion.

A couple quick notes:

  • a quaternion implementation should wrap the SIMD4 type from the Standard Library
  • we'd probably also want to add an explicit 3d vector type, since quaternions are mostly useful as an abstraction for representing rotations and dilations of 3d vector spaces.
  • there's an interesting discussion about whether or not it's worth having a unit-quaternion type
  • there's an interesting discussion about whether to talk about quaternions or rotations or both (unit quaternions are a double-cover of rotations, so the two things have different notions of equality).

Hi @pashabou and @stephentyrone,

I just raised a PR with an implementation sketch for Quaternions.
I hope it is okay for both of you that I linked the PR against this issue (altough I did not open it).

While the PR already lists all the implementation details, I would like to mention, that I am really liking the implementation of the Complex type and I tried to align Quaternion as much as I could (I even re-used a lot of the comments - sorry).
As both may have similar use cases, I found it reasonable to align them as closly as possible.

I have stripped most of the functionality from my initial implementation sketch down to simply having a PR with only the Quaternion type, relevant properties and its most basic arithmetic. It does not define rotations or vector types. I think it will focus the discussion more on the quaternion definition itself and once we have a consent, to iterate upon it. Though, I intent to provide additional functionality (later).

@markuswntr Thanks!

As a heads-up, I'm a little bit swamped for the next few days, but I'll give your PR a quick skim, then provide more detailed feedback sometime next week.

Hi @stephentyrone!

Thanks for the heads-up. There is no hurry.
And thank you very much for the feedback you already provided.