|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Accelerate |
| 14 | + |
| 15 | +extension vDSP { |
| 16 | + public enum IntegrationRule { |
| 17 | + case runningSum |
| 18 | + case simpson |
| 19 | + case trapezoidal |
| 20 | + } |
| 21 | + |
| 22 | + /// Integrates source vector using specified rule, single-precision. |
| 23 | + /// |
| 24 | + /// - Parameter vector: The vector to integrate. |
| 25 | + /// - Parameter rule: The integration rule. |
| 26 | + /// - Parameter stepSize: The integration step size (weighting factor for running sum). |
| 27 | + /// - Returns: The integration result. |
| 28 | + @inline(__always) |
| 29 | + @available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) |
| 30 | + public static func integrate<U>(_ vector: U, |
| 31 | + using rule: IntegrationRule, |
| 32 | + stepSize: Float = 1) -> [Float] |
| 33 | + where |
| 34 | + U: _ContiguousCollection, |
| 35 | + U.Element == Float { |
| 36 | + |
| 37 | + let result = Array<Float>(unsafeUninitializedCapacity: vector.count) { |
| 38 | + buffer, initializedCount in |
| 39 | + |
| 40 | + integrate(vector, |
| 41 | + using: rule, |
| 42 | + result: &buffer) |
| 43 | + |
| 44 | + initializedCount = vector.count |
| 45 | + } |
| 46 | + |
| 47 | + return result |
| 48 | + } |
| 49 | + |
| 50 | + /// Integrates source vector using specified rule, single-precision. |
| 51 | + /// |
| 52 | + /// - Parameter vector: The vector to integrate. |
| 53 | + /// - Parameter rule: The integration rule. |
| 54 | + /// - Parameter stepSize: The integration step size (weighting factor for running sum). |
| 55 | + /// - Parameter result: The destination vector to receive the result. |
| 56 | + @inline(__always) |
| 57 | + @available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) |
| 58 | + public static func integrate<U, V>(_ vector: U, |
| 59 | + using rule: IntegrationRule, |
| 60 | + stepSize: Float = 1, |
| 61 | + result: inout V) |
| 62 | + where |
| 63 | + U: _ContiguousCollection, |
| 64 | + V: _MutableContiguousCollection, |
| 65 | + U.Element == Float, V.Element == Float { |
| 66 | + |
| 67 | + let n = vDSP_Length(min(vector.count, |
| 68 | + result.count)) |
| 69 | + |
| 70 | + result.withUnsafeMutableBufferPointer { output in |
| 71 | + vector.withUnsafeBufferPointer { input in |
| 72 | + switch rule { |
| 73 | + case .runningSum: |
| 74 | + vDSP_vrsum(input.baseAddress!, 1, |
| 75 | + [stepSize], |
| 76 | + output.baseAddress!, 1, |
| 77 | + n) |
| 78 | + case .simpson: |
| 79 | + vDSP_vsimps(input.baseAddress!, 1, |
| 80 | + [stepSize], |
| 81 | + output.baseAddress!, 1, |
| 82 | + n) |
| 83 | + case .trapezoidal: |
| 84 | + vDSP_vtrapz(input.baseAddress!, 1, |
| 85 | + [stepSize], |
| 86 | + output.baseAddress!, 1, |
| 87 | + n) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// Integrates source vector using specified rule, double-precision. |
| 94 | + /// |
| 95 | + /// - Parameter vector: The vector to integrate. |
| 96 | + /// - Parameter rule: The integration rule. |
| 97 | + /// - Parameter stepSize: The integration step size (weighting factor for running sum). |
| 98 | + /// - Returns: The integration result. |
| 99 | + @inline(__always) |
| 100 | + @available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) |
| 101 | + public static func integrate<U>(_ vector: U, |
| 102 | + using rule: IntegrationRule, |
| 103 | + stepSize: Double = 1) -> [Double] |
| 104 | + where |
| 105 | + U: _ContiguousCollection, |
| 106 | + U.Element == Double { |
| 107 | + |
| 108 | + let result = Array<Double>(unsafeUninitializedCapacity: vector.count) { |
| 109 | + buffer, initializedCount in |
| 110 | + |
| 111 | + integrate(vector, |
| 112 | + using: rule, |
| 113 | + result: &buffer) |
| 114 | + |
| 115 | + initializedCount = vector.count |
| 116 | + } |
| 117 | + |
| 118 | + return result |
| 119 | + } |
| 120 | + |
| 121 | + /// Integrates source vector using specified rule, double-precision. |
| 122 | + /// |
| 123 | + /// - Parameter vector: The vector to integrate. |
| 124 | + /// - Parameter rule: The integration rule. |
| 125 | + /// - Parameter stepSize: The integration step size (weighting factor for running sum). |
| 126 | + /// - Parameter result: The destination vector to receive the result. |
| 127 | + @inline(__always) |
| 128 | + @available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *) |
| 129 | + public static func integrate<U, V>(_ vector: U, |
| 130 | + using rule: IntegrationRule, |
| 131 | + stepSize: Double = 1, |
| 132 | + result: inout V) |
| 133 | + where |
| 134 | + U: _ContiguousCollection, |
| 135 | + V: _MutableContiguousCollection, |
| 136 | + U.Element == Double, V.Element == Double { |
| 137 | + |
| 138 | + let n = vDSP_Length(min(vector.count, |
| 139 | + result.count)) |
| 140 | + |
| 141 | + result.withUnsafeMutableBufferPointer { output in |
| 142 | + vector.withUnsafeBufferPointer { input in |
| 143 | + switch rule { |
| 144 | + case .runningSum: |
| 145 | + vDSP_vrsumD(input.baseAddress!, 1, |
| 146 | + [stepSize], |
| 147 | + output.baseAddress!, 1, |
| 148 | + n) |
| 149 | + case .simpson: |
| 150 | + vDSP_vsimpsD(input.baseAddress!, 1, |
| 151 | + [stepSize], |
| 152 | + output.baseAddress!, 1, |
| 153 | + n) |
| 154 | + case .trapezoidal: |
| 155 | + vDSP_vtrapzD(input.baseAddress!, 1, |
| 156 | + [stepSize], |
| 157 | + output.baseAddress!, 1, |
| 158 | + n) |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments