Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Add Complex Numbers #129

Merged
merged 33 commits into from
Aug 2, 2019
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ee905cf
Initial implementation of Complex numbers.
bartchr808 May 21, 2019
9c2d11b
WIP: too strict conformances.
bartchr808 May 21, 2019
346b9b7
Force complex numbers to be differentiable always due to crash.
bartchr808 May 21, 2019
d5e78cf
Loosen constraints on complex number.
bartchr808 May 22, 2019
de575fe
Move T Tangent constraint.
bartchr808 May 22, 2019
683366d
Cleanup comments and commented out code.
bartchr808 May 22, 2019
01efbd9
Flip from inlinable to usableFromInline.
bartchr808 May 23, 2019
23870cb
Remove trailing whitespace, add comment to make init differentiable.
bartchr808 May 23, 2019
fdd1c50
Move around tests and file for complex.
bartchr808 May 23, 2019
3e1d56e
Add license and modify spacing.
bartchr808 May 23, 2019
1e5758a
public -> internal.
bartchr808 May 23, 2019
e58f400
Fix tests for running with XCTest.
bartchr808 May 23, 2019
9e62906
Merge branch 'master' into complex-numbers2
bartchr808 May 23, 2019
4ac97e3
Add target to package for third_party.
bartchr808 May 24, 2019
3350c21
WIP: see if tests pass while figuring out target issue.
bartchr808 May 24, 2019
85b43bb
WIP: see if tests pass while local toolchain is old.
bartchr808 May 24, 2019
46e68c9
Fix package.swift
bartchr808 May 24, 2019
dc4930c
Fix typo in conjugate test.
bartchr808 May 24, 2019
e4b7106
Fix colon spacing and Linux testing static var.
bartchr808 May 24, 2019
ae53238
Format and License fix.
bartchr808 May 24, 2019
09c53d0
Spacing and license update.
bartchr808 May 25, 2019
1856a2f
Fix line wrapping.
bartchr808 May 25, 2019
90bff29
Clean up documentation, slight code cleanup
bartchr808 May 28, 2019
a7d88bb
Pull more multiplication code out to helper func.
bartchr808 May 28, 2019
57bca41
Update licensing and mention Autograd.
bartchr808 May 30, 2019
40522c6
Merge branch 'master' into complex-numbers2
bartchr808 May 30, 2019
4e2053a
Update third party LICENSE file with tensorflow apache license.
bartchr808 May 30, 2019
80cab5c
WIP: add extra tests.
bartchr808 Jun 3, 2019
b5cf071
Merge branch 'complex-numbers2' of github.com:tensorflow/swift-apis i…
bartchr808 Jun 3, 2019
1b4c33a
Merge branch 'master' into complex-numbers2
bartchr808 Jun 3, 2019
33b178f
Add vjpInit test.
bartchr808 Jun 3, 2019
59989bd
Merge branch 'master' into complex-numbers2
bartchr808 Aug 2, 2019
4e01fed
Add another init test and fix test typos.
bartchr808 Aug 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Flip from inlinable to usableFromInline.
  • Loading branch information
bartchr808 committed May 23, 2019
commit 01efbd9b7641caf362d47521e3f51e800d78dbbc
87 changes: 47 additions & 40 deletions Sources/TensorFlow/Core/Complex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ public struct Complex<T : FloatingPoint> {
public var real: T
public var imaginary: T

@differentiable(where T : Differentiable)
public init(real: T = 0, imaginary: T = 0) {
self.real = real
self.imaginary = imaginary
self.real = real
self.imaginary = imaginary
}
}

Expand All @@ -14,43 +15,43 @@ extension Complex : Differentiable where T : Differentiable {
}

extension Complex {
@inlinable
public static var i: Complex {
return Complex(real: 0, imaginary: 1)
}

@inlinable
public var isFinite: Bool {
return real.isFinite && imaginary.isFinite
}

@inlinable
public var isInfinite: Bool {
return real.isInfinite || imaginary.isInfinite
}

@inlinable
public var isNaN: Bool {
return (real.isNaN && !imaginary.isInfinite) ||
(imaginary.isNaN && !real.isInfinite)
}

@inlinable
public var isZero: Bool {
return real.isZero && imaginary.isZero
}
}

extension Complex : ExpressibleByIntegerLiteral {
@inlinable
public init(integerLiteral value: Int) {
self.real = T(value)
self.imaginary = 0
}
}

extension Complex : CustomStringConvertible {
@inlinable
public var description: String {
return real.isNaN && real.sign == .minus
? imaginary.sign == .minus
Expand All @@ -63,36 +64,36 @@ extension Complex : CustomStringConvertible {
}

extension Complex : Equatable {
@inlinable
public static func == (lhs: Complex, rhs: Complex) -> Bool {
return lhs.real == rhs.real && lhs.imaginary == rhs.imaginary
}
}

extension Complex : AdditiveArithmetic {
@inlinable
@differentiable(vjp: _vjpAdd(lhs:rhs:) where T : Differentiable)
public static func + (lhs: Complex, rhs: Complex) -> Complex {
var lhs = lhs
lhs += rhs
return lhs
}

@inlinable
public static func += (lhs: inout Complex, rhs: Complex) {
lhs.real += rhs.real
lhs.imaginary += rhs.imaginary
}

@inlinable
@differentiable(vjp: _vjpSubtract(lhs:rhs:) where T : Differentiable)
public static func - (lhs: Complex, rhs: Complex) -> Complex {
var lhs = lhs
lhs -= rhs
return lhs
}

@inlinable
public static func -= (lhs: inout Complex, rhs: Complex) {
lhs.real -= rhs.real
lhs.imaginary -= rhs.imaginary
Expand All @@ -106,7 +107,7 @@ extension Complex : Numeric {
self.imaginary = 0
}

@inlinable
@differentiable(vjp: _vjpMultiply(lhs:rhs:) where T : Differentiable)
public static func * (lhs: Complex, rhs: Complex) -> Complex {
var a = lhs.real, b = lhs.imaginary, c = rhs.real, d = rhs.imaginary
Expand Down Expand Up @@ -148,12 +149,12 @@ extension Complex : Numeric {
return Complex(real: x, imaginary: y)
}

@inlinable
public static func *= (lhs: inout Complex, rhs: Complex) {
lhs = lhs * rhs
}

@inlinable
public var magnitude: T {
var x = abs(real)
var y = abs(imaginary)
Expand All @@ -167,21 +168,21 @@ extension Complex : Numeric {
}

extension Complex : SignedNumeric {
@inlinable
@differentiable(vjp: _vjpNegate where T : Differentiable)
public static prefix func - (operand: Complex) -> Complex {
return Complex(real: -operand.real, imaginary: -operand.imaginary)
}

@inlinable
public mutating func negate() {
real.negate()
imaginary.negate()
}
}

extension Complex {
@inlinable
@differentiable(vjp: _vjpDivide(lhs:rhs:) where T : Differentiable)
public static func / (lhs: Complex, rhs: Complex) -> Complex {
var a = lhs.real, b = lhs.imaginary, c = rhs.real, d = rhs.imaginary
Expand Down Expand Up @@ -217,50 +218,51 @@ extension Complex {
return Complex(real: x, imaginary: y)
}

@inlinable
public static func /= (lhs: inout Complex, rhs: Complex) {
lhs = lhs / rhs
}
}

extension Complex {
@inlinable

@differentiable(vjp: _vjpComplexConjugate where T : Differentiable)
public func complexConjugate() -> Complex {
return Complex(real: real, imaginary: -imaginary)
}
}

@inlinable
public func abs<T>(_ z: Complex<T>) -> Complex<T> {
return Complex(real: z.magnitude)
}

extension Complex {
@inlinable
@differentiable(vjp: _vjpAdding(real:) where T : Differentiable, T.TangentVector == T)
public func adding(real: T) -> Complex {
var c = self
c.real += real
return c
}

@inlinable
@differentiable(vjp: _vjpSubtracting(real:) where T : Differentiable, T.TangentVector == T)
public func subtracting(real: T) -> Complex {
var c = self
c.real -= real
return c
}

@inlinable
@differentiable(vjp: _vjpAdding(imaginary:) where T : Differentiable, T.TangentVector == T)
public func adding(imaginary: T) -> Complex {
var c = self
c.imaginary += imaginary
return c
}

@inlinable
@differentiable(vjp: _vjpSubtracting(imaginary:) where T : Differentiable, T.TangentVector == T)
public func subtracting(imaginary: T) -> Complex {
var c = self
Expand All @@ -270,54 +272,59 @@ extension Complex {
}

extension Complex where T : Differentiable {
@inlinable
@usableFromInline
static func _vjpAdd(lhs: Complex, rhs: Complex)
-> (Complex, (Complex) -> (Complex, Complex)) {
return (lhs * rhs, { v in (v, v) })
return (lhs + rhs, { v in (v, v) })
}

@inlinable
@usableFromInline
static func _vjpSubtract(lhs: Complex, rhs: Complex)
-> (Complex, (Complex) -> (Complex, Complex)) {
return (lhs * rhs, { v in (v, -v) })
return (lhs - rhs, { v in (v, -v) })
}

@inlinable
@usableFromInline
static func _vjpMultiply(lhs: Complex, rhs: Complex)
-> (Complex, (Complex) -> (Complex, Complex)) {
return (lhs * rhs, { v in (rhs * v, lhs * v) })
}

@inlinable
@usableFromInline
static func _vjpDivide(lhs: Complex, rhs: Complex)
-> (Complex, (Complex) -> (Complex, Complex)) {
return (lhs * rhs, { v in (v / rhs, -lhs / (rhs * rhs) * v) })
return (lhs / rhs, { v in (v / rhs, -lhs / (rhs * rhs) * v) })
}

@inlinable
@usableFromInline
static func _vjpNegate(operand: Complex)
-> (Complex, (Complex) -> Complex) {
return (-operand, { v in -v})
return (-operand, { -$0 })
}

@usableFromInline
func _vjpComplexConjugate() -> (Complex, (Complex) -> Complex) {
return (complexConjugate(), { v in v.complexConjugate() })
}
}

extension Complex where T : Differentiable, T.TangentVector == T {
@inlinable
@usableFromInline
func _vjpAdding(real: T) -> (Complex, (Complex) -> (Complex, T)) {
return (self.adding(real: real), { ($0, $0.real) })
}

@inlinable
@usableFromInline
func _vjpSubtracting(real: T) -> (Complex, (Complex) -> (Complex, T)) {
return (self.subtracting(real: real), { ($0, -$0.real) })
}

@inlinable
@usableFromInline
func _vjpAdding(imaginary: T) -> (Complex, (Complex) -> (Complex, T)) {
return (self.adding(real: real), { ($0, $0.imaginary) })
}

@inlinable
@usableFromInline
func _vjpSubtracting(imaginary: T) -> (Complex, (Complex) -> (Complex, T)) {
return (self.subtracting(real: real), { ($0, -$0.imaginary) })
}
Expand Down