All Projects → dankogai → Swift Complex

dankogai / Swift Complex

Licence: mit
Complex numbers in Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swift Complex

bcmath-extended
Extends php BCMath lib for missing functions like floor, ceil, round, abs, min, max, rand for big numbers. Also wraps existing BCMath functions.
Stars: ✭ 59 (-10.61%)
Mutual labels:  math, complex-numbers
Numphp
Mathematical PHP library for scientific computing
Stars: ✭ 120 (+81.82%)
Mutual labels:  math, complex-numbers
Algebra
means completeness and balancing, from the Arabic word الجبر
Stars: ✭ 92 (+39.39%)
Mutual labels:  math, complex-numbers
Mathjs
An extensive math library for JavaScript and Node.js
Stars: ✭ 11,861 (+17871.21%)
Mutual labels:  math, complex-numbers
Swift Numerics
Numerical APIs for Swift
Stars: ✭ 1,052 (+1493.94%)
Mutual labels:  math, complex-numbers
mathcore
Advanced .NET math library (.NET Standard).
Stars: ✭ 24 (-63.64%)
Mutual labels:  math, complex-numbers
Calc
C-style arbitrary precision calculator
Stars: ✭ 127 (+92.42%)
Mutual labels:  math, complex-numbers
java.math.expression.parser
java math expression parser is faster than JEP
Stars: ✭ 25 (-62.12%)
Mutual labels:  math, complex-numbers
Grassmann.jl
⟨Leibniz-Grassmann-Clifford⟩ differential geometric algebra / multivector simplicial complex
Stars: ✭ 289 (+337.88%)
Mutual labels:  math, complex-numbers
Rich Text Editor
Math editor (http://digabi.github.io/rich-text-editor/)
Stars: ✭ 45 (-31.82%)
Mutual labels:  math
Roadmap
Stars: ✭ 57 (-13.64%)
Mutual labels:  math
Jglm
Java OpenGL Mathematics Library
Stars: ✭ 44 (-33.33%)
Mutual labels:  math
Time
🕰 Type-safe time calculations in Swift
Stars: ✭ 1,032 (+1463.64%)
Mutual labels:  generics
Lgenerics
Generic algorithms and data structures for Lazarus/Free Pascal
Stars: ✭ 59 (-10.61%)
Mutual labels:  generics
Mvvm Example
iOS protocol-oriented MVVM examples
Stars: ✭ 45 (-31.82%)
Mutual labels:  generics
Gena
Generic pseudo-templated containers for C. Written entirely in C89 with design inspired by the C++ STL. /// DOCS ARE SLIGHTLY OUTDATED, PROJECT IS STABLE AND STILL IN ACTIVE DEVELOPMENT
Stars: ✭ 61 (-7.58%)
Mutual labels:  generics
Prosemirror Math
Schema and plugins for "first-class" math support in ProseMirror!
Stars: ✭ 43 (-34.85%)
Mutual labels:  math
One Liner
Constraint-based generics
Stars: ✭ 41 (-37.88%)
Mutual labels:  generics
Numericannex
Numeric facilities for Swift
Stars: ✭ 63 (-4.55%)
Mutual labels:  math
String Calc
PHP calculator library for mathematical terms (expressions) passed as strings
Stars: ✭ 60 (-9.09%)
Mutual labels:  math

Swift 5 MIT LiCENSE build status

CAVEAT: Swift Numerics vs. this module

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) is Complex(+infinity, 0.0), not Complex(-infinity, nan) like many other platforms.

swift-complex

Complex numbers in Swift and Swift Package Manager.

Synopsis

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)

Description

complex.swift implements all the functionality of std::complex in c++11, arguably more intuitively.

like C++11

  • Protocol-Oriented * Complex numbers are Complex<R> where R is a type of .real and .imag that conforms to the ComplexElement protocol or GaussianIntElement protocol.
    • In addition to basic arithmetic operations like +, -, *, / and abs(), Complex<T:RealType> gets libm functions like exp(), log(), sin(), cos().

unlike C++11

  • Instead of defining the constant i, Double and Complex have a property .i which returns self * Complex(0,1) so it does not pollute the identifier i, too popularly used for iteration to make it a constant.
  • Following functions are provided as compouted properties:
    • z.abs for abs(z)
    • z.arg for arg(z)
    • z.norm for norm(z)
    • z.conj for conj(z)
    • z.proj for proj(z)
  • Construct a complex number via polar notation as:
    • Complex(abs:magnitude, arg:argument)

Usage

build

$ git clone https://github.com/dankogai/swift-complex.git
$ cd swift-complex # the following assumes your $PWD is here
$ swift build

REPL

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

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.

iOS and Swift Playground

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).

From Your SwiftPM-Managed Projects

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!

Prerequisite

Swift 5 or better, OS X or Linux to build.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].