๐ ฟ๏ธComplexNumber
swift โฉ type โฉ custom โฉ package โฉ GeometryKit โฉ ComplexNumber
// History:
// โข 2024.12.09 - draft
// โข 2024.12.11 - version 1.0
// Protocol Inheritance Hierarchy
// โข MetricSpace -> Vector -> Vector2D -> ComplextNumber
// ---------------------------
// ๐
ฟ๏ธ ComplexNumber
// ---------------------------
/// ๅปฃ็พฉ็ใ่คๆธใๅๅฎ
///
/// ่ฎไธ่ฌๅๅฅ(ๅฆ๏ผCGPoint)ๅฏ้ตๅพช ComplexNumber ๅ่ญฐ๏ผ
/// ่ฎๅฎๅๆๆ่คๆธ็่จ็ฎ่ฝๅ๏ผๅฆ๏ผ่คๆธไนๆณไปฃ่กจๆ่ฝ่็ธฎๆพ๏ผ
///
/// ```swift
/// import CoreGraphics
///
/// // ๐CGSize + ComplexNumber
/// extension CGSize: ComplexNumber {
///
/// public typealias Scalar = CGFloat
///
/// // required properties
/// public var x: CGFloat { width }
/// public var y: CGFloat { height }
///
/// // required initializer
/// public init(x: CGFloat, y: CGFloat) {
/// self.init(width: x, height: y)
/// }
/// }
/// ```
public protocol ComplexNumber: Vector2D {
// default behavior provided by Vector2D
// โข ๅ ๆณๅๅ
็ด : -v
// โข ่คๆธๅ ๆณ๏ผu + v
// โข ่คๆธๆธๆณ๏ผu - v
// โข ็ด้็ฉ๏ผv * a, a * v, v / a
// โญ๏ธ ่คๆธ้็ฎ
/// ่คๆธไนๆณ๏ผu * v
static func * (u: Self, v: Self) -> Self
/// ่คๆธ้คๆณ๏ผu / v
static func / (u: Self, v: Self) -> Self
/// ่คๆธ้คๆณ๏ผa / v
static func / (a: Scalar, v: Self) -> Self
}
// ----------------------------------------------
// ๐ ComplexNumber (default behaviors)
// ----------------------------------------------
public extension ComplexNumber {
// default behavior provided by Vector2D
// โข u + v, u += v
// โข u - v, u -= v
// โข u * a, u *= a
// โข u / a, u /= a
// ่คๆธ้ทๅบฆ๏ผ
// โข normSqaured ( |z|ยฒ = xยฒ + yยฒ )
// โข magnitude ( |z| = โ(xยฒ + yยฒ) )
// โข norm ( = magnitude )
/// ๅ
ฑ่ป่คๆธ๏ผ a - bi
var conjugate: Self {
Self.init(x: x, y: -y)
}
/* ------- u + v, u - v ------- */
/// ่คๆธๅ ๆณ๏ผa + v
@inlinable // ๆธๅฐ้็ฎ้ ป็น่ชฟ็จ็้้ท
static func + (a: Scalar, v: Self) -> Self {
Self.init(x: a + v.x, y: v.y)
}
/// ่คๆธๅ ๆณ๏ผv + a
@inlinable // ๆธๅฐ้็ฎ้ ป็น่ชฟ็จ็้้ท
static func + (v: Self, a: Scalar) -> Self {
Self.init(x: a + v.x, y: v.y)
}
// u += a
static func += (u: inout Self, a: Scalar) {
u = u + a
}
/// ่คๆธๆธๆณ๏ผa - v
@inlinable // ๆธๅฐ้็ฎ้ ป็น่ชฟ็จ็้้ท
static func - (a: Scalar, v: Self) -> Self {
Self.init(x: a - v.x, y: -v.y)
}
/// ่คๆธๆธๆณ๏ผv - a
@inlinable // ๆธๅฐ้็ฎ้ ป็น่ชฟ็จ็้้ท
static func - (v: Self, a: Scalar) -> Self {
Self.init(x: v.x - a, y: v.y)
}
// u -= a
static func -= (u: inout Self, a: Scalar) {
u = u - a
}
/* ------- u * v, u *= v ------- */
// ่คๆธไนๆณ๏ผu * v
// u = a + bi
// v = c + di
// u * v = (ac-bd) + (ad+bc)i
@inlinable // ๆธๅฐ้็ฎ้ ป็น่ชฟ็จ็้้ท
static func * (u: Self, v: Self) -> Self {
let (a,b,c,d) = (u.x, u.y, v.x, v.y)
return Self.init(x: a * c - b * d, y: a * d + b * c)
}
// u *= v
static func *= (u: inout Self, v: Self) {
u = u * v
}
/* ------- u / v, u /= v ------- */
/// ่คๆธ้คๆณ๏ผa / v
@inlinable // ๆธๅฐ้็ฎ้ ป็น่ชฟ็จ็้้ท
static func / (a: Scalar, v: Self) -> Self {
precondition(v.normSquared != 0,
"โ Division by zero is not allowed."
)
return Self.init(x: a, y: 0) / v
}
// ่คๆธ้คๆณ๏ผu / v
// u = a + bi
// v = c + di
// u / v = (a + bi)(c - di) / (cยฒ + dยฒ)
@inlinable // ๆธๅฐ้็ฎ้ ป็น่ชฟ็จ็้้ท
static func / (u: Self, v: Self) -> Self {
precondition(v.normSquared != 0,
"โ Division by zero is not allowed."
)
return (u * v.conjugate) / v.normSquared
}
/// ่คๆธ้คๆณ๏ผu /= v
/// u = u / v
static func /= (u: inout Self, v: Self) {
precondition(v != Self.zero, "โ Division by zero is not allowed.")
u = u / v
}
}
Vector๏ผconformed by ComplexNumber
ChatGPT โฉ ่จญ่จ MetricSpace ๅๅฎ
Last updated