PONS, Protocol-Oriented Number System in Pure Swift.
https://github.com/dankogai/swift-pons
In addition to Complex, it includes
- BigInt
- Rational
- Protocols that allows you to extend all types that all at once.
func fact<T:POInteger>(n:T)->T { // teach all integer types how to factorial
return n < 2 ? 1 : (2...n).reduce(1, combine:*)
}
The last one is important because in PONS, even Complex is implemented via protocol extensions that can be extended in that manner. Also note in swift-complex, Complex<Rational<Int>>
was just in theory. But with PONS that is in reality.
Complex numbers in Swift.
import Complex // needed only if you build module and load it, like "make repl"
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.
- Generic! (Since version 0.3.0.
Int
support introduced in 0.5.0)- Complex numbers are
Complex<T>
whereT
is a type of.re
and.im
that conforms to theArithmeticType
protocol. - In addition to basic arithmetic operations like
+
,-
,*
,/
andabs()
,Complex<T:RealType>
getslibm
functions likeexp()
,log()
,sin()
,cos()
.
- Complex numbers are
- 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 also provided as properties:
z.real
forreal(z)
z.imag
forimag(z)
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)
- In addition to
pow()
, it comes with the**
and=~
operators. See complex/exops.swift for details.
Just add complex/complex.swift to it.
To use **
and =~
operators, add complex/exops.swift as well.
Browse all the playgrounds and the project via Complex.xcworkspace
.
Have fun with playgrounds that is a part of this git repo. While OSX.playground is pretty detaild with many pages, iOS.playground and tvOS.playground are maily for testing purpose.
When you use it, make sure you turn on the left pane (it's off right after you pulled since UI settings are .gitignore
d). As you see above, this playground consists of multiple pages and sources.
Just drop complex/complex.swift to Sources
. In git *.playground/Sources/complex.swift
is a symlink thereto.
To use **
and =~
operators, add complex/exops.swift as well.
git clone https://github.com/dankogai/swift-complex.git
cd swift-complex
make repl
git clone https://github.com/dankogai/swift-complex.git
cd swift-complex
make SWIFTPATH=${YOUR_SWIFT_PATH} repl # ${YOUR_SWIFT_PATH}=~/swift/usr/bin in my case
git clone https://github.com/dankogai/swift-complex.git
cd swift-complex
make test
git clone https://github.com/dankogai/swift-complex.git
cd swift-complex
make SWIFTPATH=${YOUR_SWIFT_PATH} test # ${YOUR_SWIFT_PATH}=~/swift/usr/bin in my case
Swift 2.0 or better, OS X or Linux to build. iOS and tvOS are supported (so far as I see via playground)
For Swift 1.x and below see the swift-1.x branch (which is no longer maintained).