๐Ÿ…ฟ๏ธVector

swift โŸฉ type โŸฉ custom โŸฉ package โŸฉ GeometryKit โŸฉ Vector

// 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)
    }
    
}

Last updated