Avatar
↓で遊べたけど誤差が・・・ import Numerics extension Complex { static func *(lhs: Self, rhs: RealType) -> Self { return Self(lhs.real * rhs, lhs.imaginary * rhs) } static func *(lhs: RealType, rhs: Self) -> Self { return Self(lhs * rhs.real, lhs * rhs.imaginary) } } extension Complex where RealType: ElementaryFunctions { static func sqrt(_ z: Self) -> Self { let length = z.length let phase = z.phase return .sqrt(length) * Self(.cos(phase / 2), .sin(phase / 2)) } } let a: Complex<Double> = -1 let i: Complex<Double> = .sqrt(a) print(i) (6.123233995736766e-17, 1.0)
8:17 AM
お、すでに提案されてる。 https://github.com/apple/swift-numerics/issues/2
All of the usual ElementaryFunctions operations should be defined for Complex types. Implementation details may be found in Kahan&#39;s &quot;Branch Cuts for Complex Elementary Functions; o...
8:19 AM
というか、初期の実装から外しただけか。