小程序三方平台开发: 解析小程序开发的未来趋势和机遇
1040
2022-10-14
Upsurge 数学实用程序库
Upsurge
Upsurge is a math utilities library. It provides support for linear operations on vectors and matrices, and slicing of higher-dimensional tensors. It relies on Accelerate, which is a framework that provides high-performance functions for matrix math, digital signal processing, and image manipulation by harnessing SIMD instructions available in modern CPUs.
Upsurge is a fork of Surge which was abandoned for a while. Upsurge supports tensors and has better support for matrices and arrays. It provides a custom ValueArray class as an alternative to Swift's built-in Array. It being a class instead of a struct means that you can manage when and if it gets copied, making memory management more explicit. This also allows defining the += operator to mean addition instead of concatenation.
Features
Tensor and tensor slicing: tensor.asMatrix(1, 1, 0...4, 0...4) Matrix and matrix operations: let result = A * B′ ValueArrays with explicit copying and numeric operators: let result = A • B Accelerate functions: let conv = convolution(signal: signal, kernel: kernel)
Installation
Upsurge supports both CocoaPods (pod 'Upsurge') and Carthage (github "aleph7/Upsurge").
Usage
Arrays and vector operations
All of Upsurge's linear (1-dimensional) operations can be performed on anything that conforms to LinearType. Swift's built-in arrays and array slices conform to LinearType, of course. But Upsurge also defines the ValueArray class to store a one-dimensional collection of values. ValueArray is very similar to Swift's Array but it is optimized to reduce unnecessary memory allocation. These are the most important differences:
Its instances have a fixed size defined on creation. When you create a ValueArray you can define a capacity var a = ValueArray
Creating arrays
Create a ValueArray with specific literal elements when you know ahead of time what the contents are, and you don't need to add more elements at a later time:
let a: ValueArray = [1.0, 3.0, 5.0, 7.0]
Create a ValueArray with a capacity and then fill it in when you are loading the contents from an external source or have a very large array:
let a = ValueArray
Finally there is a way of initializing both the capacity and the count of a ValueArray. You should rarely need this but it's there for when you are doing operations on existing arrays using low-level APIs that take pointers:
func operation(a: ValueArray
Vector arithmetic
You can perform operations on ValueArray in an intuitive manner:
let a: ValueArray = [1.0, 3.0, 5.0, 7.0]let b: ValueArray = [2.0, 4.0, 6.0, 8.0]let addition = a + b // [3.0, 7.0, 11.0, 15.0]let product = a • b // 100.0
Matrix operations
import Upsurgelet A = Matrix
Tensors
The Tensor class makes it easy to manipulate multi-dimensional data. You can easily slice or flatten a tensor to get matrices and vectors that you can operate on.
License
Upsurge is available under the MIT license. See the LICENSE file for more info.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~