0001    // Trigonometric.swift
0002    //
0003    // Copyright (c) 2014–2015 Mattt Thompson (http://mattt.me)
0004    //
0005    // Permission is hereby granted, free of charge, to any person obtaining a copy
0006    // of this software and associated documentation files (the "Software"), to deal
0007    // in the Software without restriction, including without limitation the rights
0008    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0009    // copies of the Software, and to permit persons to whom the Software is
0010    // furnished to do so, subject to the following conditions:
0011    //
0012    // The above copyright notice and this permission notice shall be included in
0013    // all copies or substantial portions of the Software.
0014    //
0015    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0018    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0020    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0021    // THE SOFTWARE.
0022    
0023    import Accelerate
0024    
0025    // MARK: Sine-Cosine
0026    
0027    public func sincos(x: [Float]) -> (sin: [Float], cos: [Float]) {
0028        var sin = [Float](count: x.count, repeatedValue: 0.0)
0029        var cos = [Float](count: x.count, repeatedValue: 0.0)
0030        vvsincosf(&sin, &cos, x, [Int32(x.count)])
0031    
0032        return (sin, cos)
0033    }
0034    
0035    public func sincos(x: [Double]) -> (sin: [Double], cos: [Double]) {
0036        var sin = [Double](count: x.count, repeatedValue: 0.0)
0037        var cos = [Double](count: x.count, repeatedValue: 0.0)
0038        vvsincos(&sin, &cos, x, [Int32(x.count)])
0039    
0040        return (sin, cos)
0041    }
0042    
0043    // MARK: Sine
0044    
0045    public func sin(x: [Float]) -> [Float] {
0046        var results = [Float](count: x.count, repeatedValue: 0.0)
0047        vvsinf(&results, x, [Int32(x.count)])
0048    
0049        return results
0050    }
0051    
0052    public func sin(x: [Double]) -> [Double] {
0053        var results = [Double](count: x.count, repeatedValue: 0.0)
0054        vvsin(&results, x, [Int32(x.count)])
0055    
0056        return results
0057    }
0058    
0059    // MARK: Cosine
0060    
0061    public func cos(x: [Float]) -> [Float] {
0062        var results = [Float](count: x.count, repeatedValue: 0.0)
0063        vvcosf(&results, x, [Int32(x.count)])
0064    
0065        return results
0066    }
0067    
0068    public func cos(x: [Double]) -> [Double] {
0069        var results = [Double](count: x.count, repeatedValue: 0.0)
0070        vvcos(&results, x, [Int32(x.count)])
0071    
0072        return results
0073    }
0074    
0075    // MARK: Tangent
0076    
0077    public func tan(x: [Float]) -> [Float] {
0078        var results = [Float](count: x.count, repeatedValue: 0.0)
0079        vvtanf(&results, x, [Int32(x.count)])
0080    
0081        return results
0082    }
0083    
0084    public func tan(x: [Double]) -> [Double] {
0085        var results = [Double](count: x.count, repeatedValue: 0.0)
0086        vvtan(&results, x, [Int32(x.count)])
0087    
0088        return results
0089    }
0090    
0091    // MARK: Arcsine
0092    
0093    public func asin(x: [Float]) -> [Float] {
0094        var results = [Float](count: x.count, repeatedValue: 0.0)
0095        vvasinf(&results, x, [Int32(x.count)])
0096    
0097        return results
0098    }
0099    
0100    public func asin(x: [Double]) -> [Double] {
0101        var results = [Double](count: x.count, repeatedValue: 0.0)
0102        vvasin(&results, x, [Int32(x.count)])
0103    
0104        return results
0105    }
0106    
0107    // MARK: Arccosine
0108    
0109    public func acos(x: [Float]) -> [Float] {
0110        var results = [Float](count: x.count, repeatedValue: 0.0)
0111        vvacosf(&results, x, [Int32(x.count)])
0112    
0113        return results
0114    }
0115    
0116    public func acos(x: [Double]) -> [Double] {
0117        var results = [Double](count: x.count, repeatedValue: 0.0)
0118        vvacos(&results, x, [Int32(x.count)])
0119    
0120        return results
0121    }
0122    
0123    // MARK: Arctangent
0124    
0125    public func atan(x: [Float]) -> [Float] {
0126        var results = [Float](count: x.count, repeatedValue: 0.0)
0127        vvatanf(&results, x, [Int32(x.count)])
0128    
0129        return results
0130    }
0131    
0132    public func atan(x: [Double]) -> [Double] {
0133        var results = [Double](count: x.count, repeatedValue: 0.0)
0134        vvatan(&results, x, [Int32(x.count)])
0135    
0136        return results
0137    }
0138    
0139    // MARK: -
0140    
0141    // MARK: Radians to Degrees
0142    
0143    func rad2deg(x: [Float]) -> [Float] {
0144        var results = [Float](count: x.count, repeatedValue: 0.0)
0145        let divisor = [Float](count: x.count, repeatedValue: Float(M_PI / 180.0))
0146        vvdivf(&results, x, divisor, [Int32(x.count)])
0147    
0148        return results
0149    }
0150    
0151    func rad2deg(x: [Double]) -> [Double] {
0152        var results = [Double](count: x.count, repeatedValue: 0.0)
0153        let divisor = [Double](count: x.count, repeatedValue: M_PI / 180.0)
0154        vvdiv(&results, x, divisor, [Int32(x.count)])
0155    
0156        return results
0157    }
0158    
0159    // MARK: Degrees to Radians
0160    
0161    func deg2rad(x: [Float]) -> [Float] {
0162        var results = [Float](count: x.count, repeatedValue: 0.0)
0163        let divisor = [Float](count: x.count, repeatedValue: Float(180.0 / M_PI))
0164        vvdivf(&results, x, divisor, [Int32(x.count)])
0165    
0166        return results
0167    }
0168    
0169    func deg2rad(x: [Double]) -> [Double] {
0170        var results = [Double](count: x.count, repeatedValue: 0.0)
0171        let divisor = [Double](count: x.count, repeatedValue: 180.0 / M_PI)
0172        vvdiv(&results, x, divisor, [Int32(x.count)])
0173    
0174        return results
0175    }
0176