Swift 5 MIT LiCENSE build status

警告:Swift Numerics 与本模块

随着 apple/swift-numerics 对 Swift 中复数的支持,Swift 对复数的支持终于 正式发布。您应该考虑使用 NumericsComplexModule 而不是本模块。我自己在任何可能的地方都切换到了 swift-numerics。但是仍然有一些原因导致您可能想要使用本模块。

swift-complex

Swift 和 Swift 包管理器中的复数。

概要

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 实现了 c++11 中的 std::complex 的所有功能,而且可以说是更加直观。

像 C++11 一样

不像 C++11

用法

构建

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

REPL

简单地

$ swift run --repl

$ scripts/run-repl.sh

$ swift build && swift -I.build/debug -L.build/debug -lComplex

并在您的 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 项目是故意从存储库中排除的,因为它应该通过 swift package generate-xcodeproj 生成。为方便起见,您可以

$ scripts/prep-xcode

然后工作区会打开,并在顶部显示 Playground。该 playground 被编写为手册。

iOS 和 Swift Playground

不幸的是,Swift 包管理器不支持 iOS。更糟糕的是,Swift Playgrounds 不支持模块。

幸运的是,Playgrounds 允许您在 Sources 目录下包含 Swift 源代码。只需运行

$ scripts/ios-prep.sh

这样就完成了。iOS/Complex.playground 现在可以在 macOS 上的 Xcode 和 Playgrounds 以及 iOS 上的 Playgrounds 上运行(好吧,它应该是在 iPadOS 上,但仍然标记为 iOS)。

来自您 SwiftPM 管理的项目

将以下内容添加到 dependencies 部分

.package(
  url: "https://github.com/dankogai/swift-complex.git", from: "5.0.0"
)

并将以下内容添加到 .target 参数

.target(
  name: "YourSwiftyPackage",
  dependencies: ["Complex"])

现在您要做的就是

import Complex

在您的代码中。尽情享受吧!

先决条件

Swift 5 或更高版本,OS X 或 Linux 用于构建。