Complex numbers and quaternions

(ns complex-quaternions
  (:require [fastmath.complex :as cplx]
            [fastmath.quaternion :as quat]
            [fastmath.dev.codox :as codox]))

Complex numbers

Reference

fastmath.complex

Complex numbers functions.

Complex number is represented as Vec2 type (from clojure2d.math.vector namespace).

To create complex number use complex, vec2 or ->Vec2.

Implementation checks for ##Inf, ##NaN and some of the function distinguish +0.0 and -0.0

-I
I
I-
ONE
TWO
ZERO
abs
  • (abs z)

Absolute value, magnitude

acos
  • (acos z)

acos

acosh
  • (acosh z)

acosh

acot
  • (acot z)

acot

acoth
  • (acoth z)
acsc
  • (acsc z)

acsc

acsch
  • (acsch z)
add
  • (add z1 z2)

Sum of two complex numbers

arg
  • (arg z)

Argument (angle) of the complex number

asec
  • (asec z)

asec

asech
  • (asech z)
asin
  • (asin z)

asin

asinh
  • (asinh z)

asinh

atan
  • (atan z)

atan

atanh
  • (atanh z)

atanh

complex
  • (complex a b)
  • (complex a)
  • (complex)

Create complex number. Represented as Vec2.

conjugate
  • (conjugate z)

Complex conjugate. \(\bar{z}\)

cos
  • (cos z)

cos

cosh
  • (cosh z)

cosh

cot
  • (cot z)

csc

coth
  • (coth z)

coth

csc
  • (csc z)

csc

csch
  • (csch z)

csch

csgn
  • (csgn re im)
  • (csgn z)

Complex sgn.

Returns 0 for 0+0i or calls m/sgn on real part otherwise.

delta-eq
  • (delta-eq q1 q2)
  • (delta-eq q1 q2 accuracy)

Compare complex numbers with given accuracy (10e-6 by default)

div
  • (div z1 z2)

Divide two complex numbers.

exp
  • (exp z)

exp

flip
  • (flip z)

Exchange imaginary and real parts

im
  • (im z)

Imaginary part

imaginary?
  • (imaginary? z)

Is z is a pure imaginary number?

inf?
  • (inf? z)

Is infinite?

log
  • (log z)

log, principal value

logb
  • (logb z b)

log with base b

mult
  • (mult z1 z2)

Multiply two complex numbers.

mult-I
  • (mult-I z)
mult-I-
  • (mult-I- z)
nan?
  • (nan? z)

Is NaN?

neg
  • (neg z)

Negate complex number. \(-z\)

norm
  • (norm z)

Norm (Guass) of the complex number, absolute value squared

pow
  • (pow z1 z2)

Power. \(z_1^{z_2}\)

re
  • (re z)

Real part

real?
  • (real? z)

Is z is a real number?

reciprocal
  • (reciprocal z)

\(\frac{1}{z}\)

scale
  • (scale z v)

Multiply by real number

sec
  • (sec z)

sec

sech
  • (sech z)

sech

sin
  • (sin z)

sin

sinh
  • (sinh z)

sinh

sq
  • (sq z)

Square complex number. \(z^2\)

sqrt
  • (sqrt z)

Sqrt of complex number. \(\sqrt{z}\)

sqrt1z
  • (sqrt1z z)

\(\sqrt{1-z^2}\)

sub
  • (sub z1 z2)

Difference of two complex numbers

tan
  • (tan z)

tan

tanh
  • (tanh z)

tanh

zero?
  • (zero? z)

Is zero?

Quaternions

Reference

fastmath.quaternion

-I
-J
-K
I
J
K
ONE
ZERO
acos
  • (acos q)
acosh
  • (acosh q)
acot
  • (acot q)
acoth
  • (acoth q)
acsc
  • (acsc q)
acsch
  • (acsch q)
add
  • (add q1 q2)

Sum of two quaternions

arg
  • (arg quaternion)

Argument of quaternion, atan2(|vector(q)|, re(q))

asec
  • (asec q)
asech
  • (asech q)
asin
  • (asin q)
asinh
  • (asinh q)
atan
  • (atan q)
atanh
  • (atanh q)
complex->quaternion
  • (complex->quaternion z)

Create quaternion from complex number

conjugate
  • (conjugate quaternion)

Returns conjugate of quaternion

cos
  • (cos q)
cosh
  • (cosh q)
cot
  • (cot q)
coth
  • (coth q)
csc
  • (csc q)
csch
  • (csch q)
delta-eq
  • (delta-eq q1 q2)
  • (delta-eq q1 q2 accuracy)

Compare quaternions with given accuracy (10e-6 by default)

div
  • (div q1 q2)

Divide two quaternions

exp
  • (exp q)
from-angles
  • (from-angles [x y z])
  • (from-angles x y z)

Convert Tait–Bryan angles z-y′-x” to quaternion.

from-euler
  • (from-euler [roll pitch yaw])
  • (from-euler roll pitch yaw)

Convert Euler ZYX (body 3-2-1) representation to quaternion

Input should be 3d vector contating roll (x), pitch (y) and yaw (z) angles, or individual values.

  • roll and yaw should be from [-pi, pi] range
  • pitch should be from [-pi/2, pi/2] range
from-rotation-matrix
  • (from-rotation-matrix m)

Convert rotation 3x3 matrix to a quaternion

im-i
  • (im-i quaternion)

Return i imaginary part

im-j
  • (im-j quaternion)

Return j imaginary part

im-k
  • (im-k quaternion)

Return k imaginary part

imaginary?
  • (imaginary? quaternion)

Is q is a pure imaginary number?

inf?
  • (inf? quaternion)

Is infinitive?

log
  • (log q)
logb
  • (logb quaternion b)

log with base b

mult
  • (mult q1 q2)

Multiply two quaternions.

nan?
  • (nan? quaternion)

Is NaN?

neg
  • (neg quaternion)

Negation of quaternion.

norm
  • (norm quaternion)

Norm of the quaternion, length of the vector

normalize
  • (normalize quaternion)

Normalize quaternion

pow
  • (pow q p)

Quaternion power

qsgn
  • (qsgn re im-i im-j im-k)
  • (qsgn q)

sgn of the quaternion.

Returns 0 for 0+0i+0j+0k or calls m/sgn on real part otherwise.

quaternion
  • (quaternion a b c d)
  • (quaternion scalar [i j k])
  • (quaternion a)

Create quaternion from individual values or scalar and vector parts, reprezented as Vec4.

re
  • (re quaternion)

Returns scalar part of quaternion

real?
  • (real? quaternion)

Is q is a real number?

reciprocal
  • (reciprocal quaternion)
rotate
  • (rotate in rotq)
  • (rotate in angle u)

Rotate 3d in vector around axis u, the same as fastmath.vector/axis-rotate.

rotation-quaternion
  • (rotation-quaternion angle u)

Create rotation quaternion around vector u and angle alpha

scalar
  • (scalar quaternion)

Returns scalar part of quaternion, double

scale
  • (scale quaternion scale)

Scale the quaternion

sec
  • (sec q)
sech
  • (sech q)
sin
  • (sin q)
sinh
  • (sinh q)
slerp
  • (slerp q1 q2 t)

Interpolate quaternions

sq
  • (sq quaternion)

Square of quaternion.

sqrt
  • (sqrt q)
sub
  • (sub q1 q2)

Difference of two quaternions

tan
  • (tan q)
tanh
  • (tanh q)
to-angles
  • (to-angles q)

Convert quaternion to Tait–Bryan angles, z-y′-x”.

to-euler
  • (to-euler q)

Convert quaternion to Euler ZYX (body 3-2-1). Quaternion will be normalized before calculations.

Output will contain roll (x), pitch (y) and yaw (z) angles.

to-rotation-matrix
  • (to-rotation-matrix q)

Convert quaternion to rotation 3x3 matrix

vector
  • (vector quaternion)

Returns vector part of quaternion, Vec3 type

zero?
  • (zero? quaternion)

Is zero?