🎲
數學
  • 🎲數學
    • ✅待辦
    • 🗒️範本
  • 🚧有序組
  • 🚧邏輯
    • 🚧命題╱statement
    • 🚧述詞╱predicate
  • 📗集合
    • 🚧宇集╱universal set
    • 🚧補集╱complement
    • 🚧關係╱relation
    • 🔰二元關係
      • 🔰二元關係屬性
        • 🔸反身性╱reflexivity
        • 🔸對稱性╱symmetry
        • 🔸反對稱╱antisymmetry
        • 🔸遞移性╱transitivity
        • 🔸全序性╱totality
      • 🔰全序╱total ordering
      • 🚧良序╱well ordering
    • 🚧集合間關係
      • 🚧包含關係:A ⊆ B
  • 🚧數系
    • 🔰自然數 ℕ
      • 🚧自然數的良序性
    • 🚧有理數 ℚ
      • 🔰阿基米德性質
    • 🚧實數 ℝ
      • 🚧建造實數系
        • 🚧實數定義
          • 🔰戴德金分割
          • 🔰(-∞,r) 是戴德金分割
          • 🔰實數系定義
        • 🔰實數順序:a ≤ b
        • 🚧實數加法
          • 🔰定義實數加法 a + b
          • 🔰零元素 𝟘
          • 🔰加法反元素 -a
          • 🔰加法性質
        • 🚧實數乘法
          • 🚧非負實數乘法
          • 🚧乘法單位元素 𝕝
          • 🚧非負實數運算性質
    • 🔰複數 ℂ
      • 🔰複數表示法
      • 🔸共軛複數
      • 🔸複數長度
      • ✖️複數乘法
    • 🔰四元數 ℍ
      • 🔰四元數表示法
      • 🚧共軛四元數
      • 🚧四元數長度
      • 🔸四元數倒數
      • ➗四元數運算
        • ➕四元數加法
        • 🚧四元數乘法
        • ❗四元數除法
        • 🚧四元數內積
        • 🚧四元數外積
        • 🔰四元數旋轉
  • 📙代數
    • 🔰運算 (operation)
    • 🔰二元運算
      • 🔸封閉性
      • 🔸結合律
      • 🔸交換律
      • 🔸單位元素
      • 🔸反元素
    • 🔰代數系統
    • 🔰Group╱群
      • 🔰群的性質
      • 🔰交換群
    • 🔰Ring╱環
      • 🔸identity╱乘法單位元素
      • 🔸unit & 乘法反元素
      • 🔰交換環
      • 🔰commutative ring with identity
      • 🔰integral domain
      • 🔰skew field
    • 🚧Field╱體
      • 🔰有序體 (ordered field)
  • 📙線性代數
    • 🔰向量
      • 🔸向量長度
      • 🔸單位向量
      • 🔸垂直向量
        • 🔸法向量
      • 🔸平行向量
        • 🔸切向量
      • ⭐向量運算
        • ➕向量加法
        • ✖️向量係數積
        • ➗向量除法
          • 🔰射影座標
        • ✖️內積
          • 🔰內積的矩陣表示法
          • 🔰內積的餘弦定理
        • ✖️外積
          • 🔰平面外積
          • 🔰空間外積
            • 🔰空間外積矩陣
        • ✖️三重積
          • 🔰純量三重積
          • 🔰向量三重積
      • 🔰射影向量
        • 🔰射影向量的矩陣表示法
      • 🔰向量分解
        • 🔰向量垂直分解
          • 🔰向量垂直分解矩陣
        • 🔰圓弧插值法
    • 🔰線性組合
    • 🔰線性獨立
    • 🔰向量空間
      • 🔸基底
        • 🔸座標
        • 🔰正交基底
          • 🔸正交矩陣
      • 🔰線性變換
        • 🔰R³ 中的旋轉
        • 🔰變換法向量
    • 🔰矩陣
      • #️矩陣符號
      • 🔯矩陣公式表
      • 🔰行向量、列向量
      • ⭐矩陣運算
        • 🚧轉置矩陣
        • ➕矩陣加法
        • ✖️矩陣係數積
        • ⭐矩陣乘法
          • 🔸單位方陣
          • 🚧反方陣
          • 🔰行向量 ⨉ 列向量
          • 🔰「分割式」乘法
          • ⭐「表格疊加」法
          • 🔰「分組式」乘法
          • 🔰「塊狀」乘法
      • 🔰旋轉矩陣
        • 🔰繞軸旋轉矩陣
  • 📗幾何
    • 📗射影幾何
  • 🛠️工具
    • 📱GGB
      • 🔰矩陣
        • 🔰輸入矩陣
        • 🔰矩陣乘法
      • 💡祕技
      • 🛠️自製工具
    • 📱Desmos
      • 💡小技巧
      • 🎹快速鍵
      • 🔰物件屬性
        • 🔸標籤 (Label)
      • 🔰儲存格類型
        • 📔筆記 (Note)
      • 🔰數學式
        • 🔰序列 (List)
        • 🔰函數
          • 🔵atan2(y, x)
        • 🔰矩陣
          • 🔰矩陣乘法
    • 🛠️TeX
      • 🔰vector
      • 🔰matrix
      • 🔰table
      • 🔰decoration
      • 🔰layout
      • 🔰parentheses
      • 🔰style
        • 📘color names
  • 📥待整理
    • √(6+2√(7+3√(8+...))) = 4
    • x²+y²−z<0 且 x+y+z<3 體積
    • x³ - 3x² + 3x + 7 = 0
    • 雙曲線上兩點的最短距離
    • 拋物線切線所截最小面積
Powered by GitBook
On this page

Was this helpful?

  1. 數系

四元數 ℍ

Quaternions: ring of real quaternions ╱🚧 under construction

Previous複數乘法Next四元數表示法

Last updated 1 year ago

Was this helpful?

⟩ ⟩ 四元數 ℍ

四元數 (H,+,⋅)(\mathbb{H},+,\cdot)(H,+,⋅) 是在四維 R4\mathbb{R}^4R4 上定義,並擁有以下性質:

  • i2=j2=k2=ijk=−1\Bbb{i}^2=\Bbb{j}^2=\Bbb{k}^2= \Bbb{ijk}=-1i2=j2=k2=ijk=−1

其中: 1=(1,0,0,0)\mathbb{1}=(1,0,0,0)1=(1,0,0,0), i=(0,1,0,0)\mathbb{i}=(0,1,0,0)i=(0,1,0,0), j=(0,0,1,0)\mathbb{j}=(0,0, 1,0)j=(0,0,1,0), k=(0,0,0,1)\mathbb{k}=(0, 0, 0,1)k=(0,0,0,1)

  • 四元數表示法

  • 共軛四元數

  • 四元數長度

  • 四元數運算

四元數 (H,+,⋅)(\mathbb{H},+,\cdot)(H,+,⋅)

四元數的優點就是:

  • 複數 ℂ

// 2023.03.04 - 06:50 (+) Quaternion, n.asQuaternion
// 2023.03.01 - 13:30 (/) isEqualTo -> equal, Number.EPSILON -> 1e-10
//                    (-) areEqualNumbers()
//                    (+) .round()
// 2023.01.20 - 16:45 (+) deg
// 2023.01.20 - 11:47 (+) .clamp(), .isEqualTo (*first recorded)
// -----------------------------------------------------

const { Random } = require('./Random.js');
const { sqrt, abs } = Math;
const {log} = console;

// deg
const deg = Math.PI / 180;

// ⭐ Number+ext
// -----------------------------------------------------
//   (constants)
// - deg                // degree = PI/180
// -----------------------------------------------------
//   (quaternion)
// 🔸 n.asQuaternion
//
// -----------------------------------------------------
//   (integer)
// 🔸 n.isEven
// 🔸 n.isOdd
// 🔸 n.binary
// 🔸 n.octal
// 🔸 n.hex
// 🔹 n.toBinary({prefix:, pad: {length:, char:}})
// 🔹 n.toOctal({prefix:, pad: {length:, char:})
// 🔹 n.toHex({prefix:, pad: {length:, char:})
// -----------------------------------------------------
// 🔹 .clamp()
// 🔹 .equal()
// 🔹 .round()
//
Object.defineProperties(Number.prototype, {

    // ---------------
    // quaternion
    // ---------------

    // 🔸 n.asQuaternion
    asQuaternion: {
        get() { return new Quaternion(this) },
    },

    // ---------------
    // integer
    // ---------------

    // 🔸 n.isEven
    isEven: {
        get() { return this % 2 === 0 },
    },

    // 🔸 n.isOdd
    isOdd: {
        get() { return Math.abs(this % 2) === 1 },
    },

    // 🔸 n.binary
    binary: {
        get() { return this.toString(2) },
    },

    // 🔸 n.octal
    octal: {
        get() { return this.toString(8) },
    },

    // 🔸 n.hex
    hex: {
        get() { return this.toString(16).toUpperCase() },
    },

    // 🔹 n.toBinary({prefix:, pad: {length:, char:}})
    toBinary: {
        value: formatFunc('0b', 2),
    },

    // 🔹 n.toOctal({prefix:, pad: {length:, char:}})
    toOctal: {
        value: formatFunc('0o', 8),
    },

    // 🔹 n.toHex({prefix:, pad: {length:, char:}})
    toHex: {
        value: formatFunc('0x', 16),
    },

    // ---------------
    // range
    // ---------------

    // 🔹 .clamp(min, max)
    clamp: {
        value: function(min, max){
            if (min > max) [min, max] = [max, min];
            return Math.min(max, Math.max(min, this));
        },
    },

    // 🔹 .equal()
    equal: {
        value: function(y, {threshold=1e-10}={}){
            return abs(this - y) < threshold; 
        },
    },

    // 🔹 .round()
    round: {
        value: function(places){
            return +this.toFixed(places); 
        },
    },

});

// ⭐ helpers
// -----------------------------------------------------

// general format function
function formatFunc(pre, base) {
    return function format({
        prefix = pre,
        pad = { length: 0, char: '0' },
    } = {}) {
        // ⭐ `pad` might be overridden, better destructure again
        const { length = 0, char = '0' } = pad;
        return prefix + 
            this.toString(base).padStart(length, char).toUpperCase();
    }
}

// 2023.03.04 - 07:19 (+) .scalarPart, .vectorPart
// 2023.03.04 - 06:50 (•) -------- merged into "Number+ext" --------
// 2023.03.03 - 17:08 (+) .dot, .cross
// 2023.03.01 - 13:42 (+) .randomInt, .randomFloat, .toString
// 2023.02.24 - 15:33 (+) static members, .conjugate, .norm, .inverse, ...
// 2023.02.24 - 13:35 (•) first draft (by chatGPT)
// -------------------------------------------------

// ⭐ Quaternion
// --------------------------------------------------
// - Quaternion.zero, .one, .i, .j, .k
// - Quaternion.randomInt()
// - Quaternion.randomFloat()
// --------------------------------------------------
// - .scalarPart, vectorPart
// - .conjugate
// - .normSquare
// - .norm
// - .inverse
// --------------------------------------------------
// - .add()            p + q
// - .subtract()       p - q
// - .multiply()       pq
// - .divide()         p/q = p q^(-1)
// - .scale()          kp
// - .dot()            p • q
// - .cross()          p x q
// --------------------------------------------------
// - .distance()       |p-q|
// - .equal()          p == q
//
class Quaternion {

    // i, j, k
    static get zero() { return new Quaternion(0, 0, 0, 0) }
    static get one() { return new Quaternion(1, 0, 0, 0) }
    static get i() { return new Quaternion(0, 1, 0, 0) }
    static get j() { return new Quaternion(0, 0, 1, 0) }
    static get k() { return new Quaternion(0, 0, 0, 1) }

    // init
    constructor(a=0, b=0, c=0, d=0) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    // ----------------------
    //     static methods
    // ----------------------

    // Quaternion.randomInt()
    static randomInt(min, max) {
        return new Quaternion(
            Random.int(min, max),
            Random.int(min, max),
            Random.int(min, max),
            Random.int(min, max),
        )
    }

    // Quaternion.randomFloat()
    static randomFloat(min, max) {
        return new Quaternion(
            Random.float(min, max),
            Random.float(min, max),
            Random.float(min, max),
            Random.float(min, max),
        )
    }


    // ----------------------
    //     getters
    // ----------------------

    // scalarPart
    get scalarPart() {
        return this.a;
    }

    // vectorPart
    get vectorPart() {
        const { b, c, d } = this;
        return new Quaternion(0, b, c, d);
    }

    // conjugate
    get conjugate() {
        const { a, b, c, d } = this;
        return new Quaternion(a, -b, -c, -d);
    }

    // inverse (reciprocal)
    get inverse() {
        const n = this.normSquare;
        return this.conjugate.scale(1 / n);
    }

    // |p|^2 = p • p
    get normSquare() {
        return this.dot(this);
    }

    // norm
    get norm() {
        const { a, b, c, d } = this;
        return sqrt(this.normSquare);
    }

    // ---------------------------
    //   ⭐ + - * / operations 
    // ---------------------------

    add(q) {
        const { a, b, c, d } = this;
        return new Quaternion(
            a + q.a, b + q.b, c + q.c, d + q.d
        );
    }

    subtract(q) {
        const { a, b, c, d } = this;
        return new Quaternion(
            a - q.a, b - q.b, c - q.c, d - q.d
        );
    }

    multiply(q) {
        const { a, b, c, d } = this;
        return new Quaternion(
            a * q.a - b * q.b - c * q.c - d * q.d,
            a * q.b + b * q.a + c * q.d - d * q.c,
            a * q.c - b * q.d + c * q.a + d * q.b,
            a * q.d + b * q.c - c * q.b + d * q.a,
        );
    }

    // p/q = p q^(-1)
    // ❗ note: p q^(-1) !== q^(-1) p
    divide(q) {
        return this.multiply(q.inverse);
    }

    // kq
    scale(k) {
        const { a, b, c, d } = this;
        return new Quaternion(k * a, k * b, k * c, k * d);
    }

    // p • q
    dot(q) {
        const { a, b, c, d } = q;
        return this.a * a + this.b * b + this.c * c + this.d * d;
    }

    // p x q = (p • q) - bar(p) q
    cross(q) {
        return this.dot(q).asQuaternion.subtract(
            this.conjugate.multiply(q)
        );
    }

    // ---------------------------
    //   ⭐ helpers 
    // ---------------------------

    // distance
    distance(q) {
        return this.subtract(q).norm;
    }

    // equal
    equal(q, {threshold=1e-10}={}) {
        return this.distance(q).equal(0, {threshold});
    }

    // toString
    toString(places=3) {
        const { a, b, c, d } = this;
        return `(${a.round(places)},${b.round(places)},${c.round(places)},${d.round(places)})`;
    }
}


// ⭐ CommonJS export
module.exports = {
    Quaternion,
    deg,
};

// export {deg};    // ES module export

通常省略四元數的乘法符號,以避免與向量空間的符號混淆。

四元數沒有「交換律」❗️

是一個 。

可視為 (C,+,⋅)(\mathbb{C},+,\cdot)(C,+,⋅) 的擴充。

可同時處理純量與。( )

它的可以同時處理與。( )

wiki ⟩ ⭐️

MathWorld ⟩

()

3D Game Engine Programming ⟩

replit ⟩

🚧
🔰
內積
乘法
skew field
複數
👉
向量
四元數表示法
👉
乘法
內積
外積
四元數旋轉
Quaternions
Quaternion
Quaternions for Computer Graphics
John Vince (John A.)
Understanding Quaternions
Quaternions (Advanced Methods in Computer Graphics) Part 1
Quaternions
數學
數系
向量空間
自己的乘法
Visualizing Quaternons