Skip to content

Commit

Permalink
quat: rename Quat to Number
Browse files Browse the repository at this point in the history
This reduces stutter and parallels the convention in the proposed dual
and hyperdual packages.
  • Loading branch information
kortschak committed Dec 3, 2018
1 parent b53e0d9 commit ec146a9
Show file tree
Hide file tree
Showing 16 changed files with 406 additions and 406 deletions.
2 changes: 1 addition & 1 deletion num/quat/abs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package quat
import "math"

// Abs returns the absolute value (also called the modulus) of q.
func Abs(q Quat) float64 {
func Abs(q Number) float64 {
// Special cases.
switch {
case IsInf(q):
Expand Down
12 changes: 6 additions & 6 deletions num/quat/abs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
)

var absTests = []struct {
q Quat
q Number
want float64
}{
{q: Quat{}, want: 0},
{q: Number{}, want: 0},
{q: NaN(), want: nan},
{q: Inf(), want: inf},
{q: Quat{Real: 1, Imag: 1, Jmag: 1, Kmag: 1}, want: 2},
{q: Quat{Real: -1, Imag: 1, Jmag: -1, Kmag: 1}, want: 2},
{q: Quat{Real: 1, Imag: 2, Jmag: 3, Kmag: 4}, want: math.Sqrt(1 + 4 + 9 + 16)},
{q: Quat{Real: -1, Imag: -2, Jmag: -3, Kmag: -4}, want: math.Sqrt(1 + 4 + 9 + 16)},
{q: Number{Real: 1, Imag: 1, Jmag: 1, Kmag: 1}, want: 2},
{q: Number{Real: -1, Imag: 1, Jmag: -1, Kmag: 1}, want: 2},
{q: Number{Real: 1, Imag: 2, Jmag: 3, Kmag: 4}, want: math.Sqrt(1 + 4 + 9 + 16)},
{q: Number{Real: -1, Imag: -2, Jmag: -3, Kmag: -4}, want: math.Sqrt(1 + 4 + 9 + 16)},
}

func TestAbs(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions num/quat/conj.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
package quat

// Conj returns the quaternion conjugate of q.
func Conj(q Quat) Quat {
return Quat{Real: q.Real, Imag: -q.Imag, Jmag: -q.Jmag, Kmag: -q.Kmag}
func Conj(q Number) Number {
return Number{Real: q.Real, Imag: -q.Imag, Jmag: -q.Jmag, Kmag: -q.Kmag}
}

// Inv returns the quaternion inverse of q.
func Inv(q Quat) Quat {
func Inv(q Number) Number {
a := Abs(q)
return Scale(1/(a*a), Conj(q))
}
20 changes: 10 additions & 10 deletions num/quat/conj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
)

var invTests = []struct {
q Quat
q Number
wantNaN bool
}{
{q: Quat{Real: 1, Imag: 1, Jmag: 1, Kmag: 1}},
{q: Quat{Real: 3, Imag: -1, Jmag: 5, Kmag: -40}},
{q: Quat{Real: 1e6, Imag: -1e5, Jmag: 4, Kmag: -10}},
{q: Quat{Real: 0, Imag: 1, Jmag: 1, Kmag: 1}},
{q: Quat{Real: 1, Imag: 0, Jmag: 1, Kmag: 1}},
{q: Quat{Real: 1, Imag: 1, Jmag: 0, Kmag: 1}},
{q: Quat{Real: 1, Imag: 1, Jmag: 1, Kmag: 0}},
{q: Quat{}, wantNaN: true},
{q: Number{Real: 1, Imag: 1, Jmag: 1, Kmag: 1}},
{q: Number{Real: 3, Imag: -1, Jmag: 5, Kmag: -40}},
{q: Number{Real: 1e6, Imag: -1e5, Jmag: 4, Kmag: -10}},
{q: Number{Real: 0, Imag: 1, Jmag: 1, Kmag: 1}},
{q: Number{Real: 1, Imag: 0, Jmag: 1, Kmag: 1}},
{q: Number{Real: 1, Imag: 1, Jmag: 0, Kmag: 1}},
{q: Number{Real: 1, Imag: 1, Jmag: 1, Kmag: 0}},
{q: Number{}, wantNaN: true},
}

func TestInv(t *testing.T) {
Expand All @@ -35,7 +35,7 @@ func TestInv(t *testing.T) {
continue
}
if !(floats.EqualWithinAbsOrRel(got.Real, 1, tol, tol) && floats.EqualWithinAbsOrRel(Abs(got), 1, tol, tol)) {
t.Errorf("unexpected result for Mul(%v, Inv(%[1]v)): got:%v want:%v", test.q, got, Quat{Real: 1})
t.Errorf("unexpected result for Mul(%v, Inv(%[1]v)): got:%v want:%v", test.q, got, Number{Real: 1})
}
}
}
14 changes: 7 additions & 7 deletions num/quat/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package quat
import "math"

// Exp returns e**q, the base-e exponential of q.
func Exp(q Quat) Quat {
func Exp(q Number) Number {
w, uv := split(q)
if uv == zero {
return lift(math.Exp(w))
Expand All @@ -23,7 +23,7 @@ func Exp(q Quat) Quat {
}

// Log returns the natural logarithm of q.
func Log(q Quat) Quat {
func Log(q Number) Number {
w, uv := split(q)
if uv == zero {
return lift(math.Log(w))
Expand All @@ -37,15 +37,15 @@ func Log(q Quat) Quat {
// Pow(0, ±0) returns 1+0i+0j+0k
// Pow(0, c) for real(c)<0 returns Inf+0i+0j+0k if imag(c), jmag(c), kmag(c) are zero,
// otherwise Inf+Inf i+Inf j+Inf k.
func Pow(q, r Quat) Quat {
func Pow(q, r Number) Number {
if q == zero {
w, uv := split(r)
switch {
case w == 0:
return Quat{Real: 1}
return Number{Real: 1}
case w < 0:
if uv == zero {
return Quat{Real: math.Inf(1)}
return Number{Real: math.Inf(1)}
}
return Inf()
case w > 0:
Expand All @@ -56,9 +56,9 @@ func Pow(q, r Quat) Quat {
}

// Sqrt returns the square root of q.
func Sqrt(q Quat) Quat {
func Sqrt(q Number) Number {
if q == zero {
return zero
}
return Pow(q, Quat{Real: 0.5})
return Pow(q, Number{Real: 0.5})
}

0 comments on commit ec146a9

Please sign in to comment.