With apple/swift-numerics complex number support on swift is official at last. You should consider using ComplexModule
of Numerics
instead of this. I am switching to swift-numerics
myself whereever I can. But there are still a few things that make you want to use this module in spite of that.
swift-numerics
relies 100% on swift package manager. You cannot use it on Swift Playgrounds.ComplexModule
may be too swifty on some respects.ComplexModule
adopts point at infinity. While this is mathmatically more correct, technically it may cause unexpected results because real operation on complex numbers is no longer isomorphic to real operations on real numbers. For instance,Complex(-1.0, 0.0) / Complex(0.0, 0.0)
isComplex(+infinity, 0.0)
, notComplex(-infinity, nan)
like many other platforms.
Complex numbers in Swift and Swift Package Manager.
import Complex
let z0 = 1.0 + 1.0.i // (1.0+1.0.i)
let z1 = 1.0 - 1.0.i // (1.0-1.0.i)
z0.conj // (1.0-1.0.i)
z0.i // (-1.0+1.0.i)
z0.norm // 2
z0 + z1 // (2.0+0.0.i)
z0 - z1 // (0.0+2.0.i)
z0 * z1 // (2.0+0.0.i)
z0 / z1 // (0.0+1.0.i)
complex.swift implements all the functionality of std::complex in c++11, arguably more intuitively.
- Protocol-Oriented * Complex numbers are
Complex<R>
whereR
is a type of.real
and.imag
that conforms to theComplexElement
protocol orGaussianIntElement
protocol.- In addition to basic arithmetic operations like
+
,-
,*
,/
andabs()
,Complex<T:RealType>
getslibm
functions likeexp()
,log()
,sin()
,cos()
.
- In addition to basic arithmetic operations like
- Instead of defining the constant
i
,Double
andComplex
have a property.i
which returnsself * Complex(0,1)
so it does not pollute the identifieri
, too popularly used for iteration to make it a constant. - Following functions are provided as compouted properties:
z.abs
forabs(z)
z.arg
forarg(z)
z.norm
fornorm(z)
z.conj
forconj(z)
z.proj
forproj(z)
- Construct a complex number via polar notation as:
Complex(abs:magnitude, arg:argument)
$ git clone https://github.com/dankogai/swift-complex.git
$ cd swift-complex # the following assumes your $PWD is here
$ swift build
Simply
$ swift run --repl
or
$ scripts/run-repl.sh
or
$ swift build && swift -I.build/debug -L.build/debug -lComplex
and in your repl,
Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
1> import Complex
2> Complex.sqrt(1.i)
$R0: Complex.Complex<Double> = {
real = 0.70710678118654757
imag = 0.70710678118654757
}
Xcode project is deliberately excluded from the repository because it should be generated via swift package generate-xcodeproj
. For convenience, you can
$ scripts/prep-xcode
And the Workspace opens up for you with Playground on top. The playground is written as a manual.
Unfortunately Swift Package Manager does not support iOS. To make matters worse Swift Playgrounds does not support modules.
Fortunately Playgrounds allow you to include swift source codes under Sources
directory. Just run:
$ scripts/ios-prep.sh
and you are all set. iOS/Complex.playground
now runs on Xcode and Playgrounds on macOS, and Playgrounds on iOS (Well, it is supposed to iPadOS but it is still labeled iOS).
Add the following to the dependencies
section:
.package(
url: "https://github.com/dankogai/swift-complex.git", from: "5.0.0"
)
and the following to the .target
argument:
.target(
name: "YourSwiftyPackage",
dependencies: ["Complex"])
Now all you have to do is:
import Complex
in your code. Enjoy!
Swift 5 or better, OS X or Linux to build.