{"id":16942809,"url":"https://github.com/dankogai/swift-complex","last_synced_at":"2025-07-13T13:34:24.683Z","repository":{"id":17846707,"uuid":"20763259","full_name":"dankogai/swift-complex","owner":"dankogai","description":"Complex numbers in Swift","archived":false,"fork":false,"pushed_at":"2020-11-10T02:23:17.000Z","size":1538,"stargazers_count":70,"open_issues_count":0,"forks_count":8,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-10-14T21:13:05.467Z","etag":null,"topics":["complex-numbers","generics","math","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dankogai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-12T10:20:05.000Z","updated_at":"2024-07-15T07:04:13.000Z","dependencies_parsed_at":"2022-09-01T01:11:42.982Z","dependency_job_id":null,"html_url":"https://github.com/dankogai/swift-complex","commit_stats":null,"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankogai%2Fswift-complex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankogai%2Fswift-complex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankogai%2Fswift-complex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankogai%2Fswift-complex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dankogai","download_url":"https://codeload.github.com/dankogai/swift-complex/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221829629,"owners_count":16887648,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["complex-numbers","generics","math","swift"],"created_at":"2024-10-13T21:13:02.578Z","updated_at":"2024-10-28T12:53:52.643Z","avatar_url":"https://github.com/dankogai.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Swift 5](https://img.shields.io/badge/swift-5-blue.svg)](https://swift.org)\n[![MIT LiCENSE](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n[![build status](https://secure.travis-ci.org/dankogai/swift-complex.png)](http://travis-ci.org/dankogai/swift-complex)\n\n# CAVEAT: Swift Numerics vs. this module\n\nWith [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.\n\n* `swift-numerics` relies 100% on swift package manager.  You cannot use it on Swift Playgrounds.\n* `ComplexModule` may be too swifty on some respects.\n  * `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. \n\n[apple/swift-numerics]: https://github.com/apple/swift-numerics\n[official at last]: https://swift.org/blog/numerics/\n[point at infinity]: https://en.wikipedia.org/wiki/Point_at_infinity\n\n# swift-complex\n\nComplex numbers in Swift and Swift Package Manager.\n\n## Synopsis\n\n````swift\nimport Complex\nlet z0 = 1.0 + 1.0.i    // (1.0+1.0.i)\nlet z1 = 1.0 - 1.0.i    // (1.0-1.0.i)\nz0.conj // (1.0-1.0.i)\nz0.i    // (-1.0+1.0.i)\nz0.norm // 2\nz0 + z1 // (2.0+0.0.i)\nz0 - z1 // (0.0+2.0.i)\nz0 * z1 // (2.0+0.0.i)\nz0 / z1 // (0.0+1.0.i)\n````\n\n## Description\n\ncomplex.swift implements all the functionality of [std::complex in c++11], arguably more intuitively. \n\n[std::complex in c++11]: http://www.cplusplus.com/reference/complex/\n\n### like C++11\n\n* Protocol-Oriented  * Complex numbers are `Complex\u003cR\u003e` where `R` is a type of `.real` and `.imag` that conforms to the `ComplexElement` protocol or `GaussianIntElement` protocol.\n  * In addition to basic arithmetic operations like `+`, `-`, `*`, `/` and `abs()`, `Complex\u003cT:RealType\u003e` gets `libm` functions like `exp()`, `log()`, `sin()`, `cos()`.\n\n### unlike C++11\n\n* 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.\n* Following functions are provided as compouted properties:\n  * `z.abs` for `abs(z)`\n  * `z.arg` for `arg(z)`\n  * `z.norm` for `norm(z)`\n  * `z.conj` for `conj(z)`\n  * `z.proj` for `proj(z)`\n* Construct a complex number via polar notation as:\n  * `Complex(abs:magnitude, arg:argument)`\n\n## Usage\n\n### build\n\n```sh\n$ git clone https://github.com/dankogai/swift-complex.git\n$ cd swift-complex # the following assumes your $PWD is here\n$ swift build\n```\n\n### REPL\n\nSimply\n\n```sh\n$ swift run --repl\n```\n\nor\n\n```sh\n$ scripts/run-repl.sh\n```\n\nor\n\n```sh\n$ swift build \u0026\u0026 swift -I.build/debug -L.build/debug -lComplex\n\n```\n\nand in your repl,\n\n```sh\nWelcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.\n  1\u003e import Complex\n  2\u003e Complex.sqrt(1.i)\n$R0: Complex.Complex\u003cDouble\u003e = {\n  real = 0.70710678118654757\n  imag = 0.70710678118654757\n}\n````\n\n### Xcode\n\nXcode project is deliberately excluded from the repository because it should be generated via `swift package generate-xcodeproj` . For convenience, you can\n\n```\n$ scripts/prep-xcode\n```\n\nAnd the Workspace opens up for you with Playground on top.  The playground is written as a manual.\n\n### iOS and Swift Playground\n\nUnfortunately Swift Package Manager does not support iOS.  To make matters worse Swift Playgrounds does not support modules.\n\nFortunately Playgrounds allow you to include swift source codes under `Sources` directory.  Just run:\n\n```shell\n$ scripts/ios-prep.sh\n```\n\nand 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).\n\n![](img/playground.png)\n\n### From Your SwiftPM-Managed Projects\n\nAdd the following to the `dependencies` section:\n\n```swift\n.package(\n  url: \"https://github.com/dankogai/swift-complex.git\", from: \"5.0.0\"\n)\n```\n\nand the following to the `.target` argument:\n\n```swift\n.target(\n  name: \"YourSwiftyPackage\",\n  dependencies: [\"Complex\"])\n```\n\nNow all you have to do is:\n\n```swift\nimport Complex\n```\n\nin your code.  Enjoy!\n\n### Prerequisite\n\nSwift 5 or better, OS X or Linux to build.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdankogai%2Fswift-complex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdankogai%2Fswift-complex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdankogai%2Fswift-complex/lists"}