Skip to content

[AutoDiff] [stdlib] Add 'zeroTangentVector' property to 'Differentiable'. #26828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
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
29 changes: 27 additions & 2 deletions stdlib/public/core/AutoDiff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,30 @@ public extension VectorProtocol where VectorSpaceScalar : SignedNumeric {
/// A type that mathematically represents a differentiable manifold whose
/// tangent spaces are finite-dimensional.
public protocol Differentiable {
/// A type representing a differentiable value’s derivatives.
///
/// Mathematically, this is equivalent to the tangent bundle of the
/// differentiable manifold represented by the differentiable type.
associatedtype TangentVector: Differentiable & AdditiveArithmetic
where TangentVector.TangentVector == TangentVector

/// Moves `self` along the value space towards the given tangent vector. In
/// Riemannian geometry (mathematics), this represents an exponential map.
/// Moves `self` along the given direction. In Riemannian geometry, this is
/// equivalent to exponential map, which moves `self` on the geodesic surface
/// along the given tangent vector.
mutating func move(along direction: TangentVector)

/// A tangent vector such that `move(along: zeroTangentVector)` will not
/// modify `self`.
/// - Note: `zeroTangentVector` can be `TangentVector.zero` in most cases,
/// but types whose tangent vectors depend on instance properties of `self`
/// need to provide a different implementation. For example, the tangent
/// vector of an `Array` depends on the array’s `count`.
@available(*, deprecated, message: """
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be deprecated because protocol requirements cannot be unavailable, and deprecated is the only way to produce a warning.

`zeroTangentVector` derivation has not been implemented; do not use \
this property
""")
var zeroTangentVector: TangentVector { get }

@available(*, deprecated,
message: "'AllDifferentiableVariables' is now equal to 'Self' and will be removed")
typealias AllDifferentiableVariables = Self
Expand All @@ -175,6 +192,14 @@ public extension Differentiable {
get { return self }
set { self = newValue }
}

// This is a temporary solution that allows us to add `zeroTangentVector`
// without implementing derived conformances. This property is marked
// unavailable because it will produce incorrect results when tangent vectors
// depend on instance properties of `self`.
// FIXME: Implement derived conformance and remove this default
// implementation.
var zeroTangentVector: TangentVector { .zero }
}

public extension Differentiable where TangentVector == Self {
Expand Down