> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/custom/package/geometrykit/vector.md).

# Vector

[swift](/ios/swift.md) ⟩ [type](/ios/swift/type.md) ⟩ [custom](/ios/custom.md) ⟩ [package](/ios/custom/package.md) ⟩ [GeometryKit](/ios/custom/package/geometrykit.md) ⟩ Vector&#x20;

{% hint style="info" %}

{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}

```swift
// History:
// • 2024.12.09 - draft
// • 2024.12.11 - version 1.0

// Inheritance Hierarchy
// • MetricSpace -> Vector -> Vector2D -> ComplextNumber

// ---------------------
//      🅿️ Vector
// ---------------------

/// 廣義向量協定
public protocol Vector: Equatable, MetricSpace {
    
    // constants
    
    // zero vector
    static var zero: Self { get }
    
    // ⭐️ 反向量、向量加減法、純量積
    // ---------------------------------------
    
    /// 加法反元素: -v
    static prefix func - (u: Self) -> Self
    
    /// 向量加法：u + v
    static func + (u: Self, v: Self) -> Self
    
    /// 向量減法：u - v
    static func - (u: Self, v: Self) -> Self
    
    /// 純量積：u * a
    /// (a * u: default behavior provided = u * a)
    static func * (u: Self, a: Scalar) -> Self
    
    /// 純量積：u / a
    /// (default behavior provided = u * (1/a))
    static func / (u: Self, a: Scalar) -> Self
}

// -----------------------------------
//      🌀 Vector default behaviors
// -----------------------------------

public extension Vector {
    
    // 純量積：a * u
    static func * (a: Scalar, u: Self) -> Self {
        return u * a
    }
    
    // 純量積：u / a
    static func / (u: Self, a: Scalar) -> Self {
        precondition(a != 0, "⛔ Division by zero is not allowed.")
        return u * (1/a)
    }
    
}
```

{% endtab %}

{% tab title="👥 相關" %}

* [MetricSpace](/ios/custom/package/geometrykit/metricspace.md)：conformed by <mark style="color:purple;">Vector</mark>
  {% endtab %}

{% tab title="📗 參考" %}

* ChatGPT ⟩ 設計 Vector 協定&#x20;
  {% endtab %}
  {% endtabs %}
