Upsurge

多维 Swift 数学。(Multi-dimensional Swift math)

  • Owner: alejandro-isaza/Upsurge
  • Platform: Mac
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

Upsurge(热潮)

Upsurge 实现了多维数据结构和操作。 它为 Swift 带来了类似 numpy 的操作。

Upsurge 不再支持 DSP 和其他线性运算,请使用 Surge。 Surge 和 Upsurge 在一起很好玩。

特性

  • 张量和张量切片: tensor.asMatrix(1, 1, 0...4, 0...4)
  • 矩阵和矩阵运算: let result = A * B′
  • 具有显式复制和数值运算符的ValueArrays: let result = A • B

安装

Upsurge 支持 CocoaPods(pod 'Upsurge')和 Carthage(github "aleph7/Upsurge")。对于 macOS 应用程序,您可以使用 Swift 软件包管理器通过添加适当的描述到您的 Package.swift 文件来安装 Upsurge。

import PackageDescription
let package = Package(
    name: "YOUR_PROJECT_NAME",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/aleph7/Upsurge.git", Version(0,8,.max)),
    ]
)

用法

数组和向量运算

Upsurge 的所有线性(一维)操作都可以在符合 LinearType 的任何对象上执行。 Swift 的内置数组和数组切片当然符合 LinearType。但是 Upsurge 还定义了 ValueArray 类来存储一维值集合。 ValueArray与 Swift 的 Array 非常相似,但经过优化以减少不必要的内存分配。这些是最重要的区别:

  • 其实例具有在创建时定义的固定大小。创建 ValueArray 时,可以定义容量 var a = ValueArray<double>(capacity: 100),然后将元素附加到该容量。或者,您可以使用特定元素 var a 创建它:ValueArray=[1.0, 2.0, 3.0],但是之后便不能再添加任何元素。
  • 这是一个类。这意味着创建新变量将仅创建引用,而修改引用也将修改原始引用。例如,执行 var a:ValueArray = [1, 2, 3]; var b=a,然后 b[0]=5 将导致 a 为 [5, 2, 3]。如果要创建副本,则需要执行 var b = ValueArray(a) 或 var b = a.copy()。
  • 您可以通过执行 var a = ValueArray<double>(capacity: n) 或 var a = ValueArray<double>(count: n) 来创建未初始化的 ValueArray。当您自己填充阵列时,这非常有用。但是,如果要初始化所有值,也可以使用 var a = ValueArray(count:n,repeatedValue: 0.0)。

创建数组

当您提前知道内容是什么,并且以后不需要添加更多元素时,请使用特定的文字元素创建一个 ValueArray:

let a: ValueArray = [1.0, 3.0, 5.0, 7.0]

创建具有容量的 ValueArray,然后在从外部源加载内容或具有非常大的数组时将其填充:

let a = ValueArray<Double>(capacity: 100)
for v in intputSource {
    a.append(v)
}

最后,有一种方法可以初始化ValueArray的容量和计数。 您应该很少需要它,但是当您使用带有指针的低级API在现有数组上执行操作时就可以使用它:

func operation(a: ValueArray<Double>) {
    let N = a.count
    let b = ValueArray<Double>(count: N)
    // ...
}

向量算术

您可以以直观的方式对 ValueArray 执行操作:

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

矩阵运算

import Upsurge
let A = Matrix<Double>([
    [1,  1],
    [1, -1]
])
let C = Matrix<Double>([
    [3],
    [1]
])
// find B such that A*B=C
let B = inv(A) * C // [2.0, 1.0]′
// Verify result
let r = A*B - C    // zero

平铺(Tiling)

可以通过重复一维ValueArray或二维矩阵mxn次来形成块矩阵。

import Upsurge
let a = ValueArray = [1.0, 2.0]
// Tile source array 2 times in each directon,
// returning a 2X4 block matrix
let A = a.tile(2, 2)
let B = Matrix<Double>([
    [1.0,  2.0],
    [3.0,  4.0]
)]
// Tile source matrix 2 times in each directon,
// returning a 4x4 block matrix
let r = B.tile(2, 2)

张量

Tensor 类使操作多维数据变得容易。 您可以轻松地将张量切片或展平,以获得可以对其进行操作的矩阵和向量。

许可

Upsurge 在 MIT 许可下可以使用。有关更多信息,请参见 LICENSE 文件。

(The first version translated by vz on 2020.07.15)

Main metrics

Overview
Name With Owneralejandro-isaza/Upsurge
Primary LanguageSwift
Program languageSwift (Language Count: 3)
PlatformMac
License:MIT License
所有者活动
Created At2015-09-24 00:41:59
Pushed At2019-05-14 14:30:33
Last Commit At2019-05-14 16:30:32
Release Count24
Last Release Name0.11.0 (Posted on )
First Release Name0.1.0 (Posted on )
用户参与
Stargazers Count183
Watchers Count11
Fork Count42
Commits Count275
Has Issues Enabled
Issues Count49
Issue Open Count18
Pull Requests Count25
Pull Requests Open Count0
Pull Requests Close Count28
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Upsurge

Upsurge implements multi-dimensional data structures and operations. It brings numpy-like operations to Swift.

Upsurge no longer supports DSP and other linear operations, please use Surge for that. Surge and Upsurge play nice together.

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

Installation

Upsurge supports both CocoaPods (pod 'Upsurge') and Carthage (github "aleph7/Upsurge"). For macOS apps you can use the Swift Package Manager to install Upsurge by adding the proper description to your Package.swift file:

import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/aleph7/Upsurge.git", Version(0,8,.max)),
    ]
)

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<Double>(capacity: 100) and then append elements up to that capacity. Or you can create it with specific elements var a: ValueArray = [1.0, 2.0, 3.0] but then you can't add any more elements after.
  • It is a class. That means that creating a new variable will only create a reference and modifying the reference will also modify the original. For instance doing var a: ValueArray = [1, 2, 3]; var b = a and then b[0] = 5 will result in a being [5, 2, 3]. If you want to create a copy you need to do var b = ValueArray(a) or var b = a.copy().
  • You can create an uninitialized ValueArray by doing var a = ValueArray<Double>(capacity: n) or var a = ValueArray<Doube>(count: n). This is good for when you are going to fill up the array yourself. But you can also use var a = ValueArray(count: n, repeatedValue: 0.0) if you do want to initialize all the values.

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<Double>(capacity: 100)
for v in intputSource {
    a.append(v)
}

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<Double>) {
    let N = a.count
    let b = ValueArray<Double>(count: N)
    // ...
}

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 Upsurge

let A = Matrix<Double>([
    [1,  1],
    [1, -1]
])
let C = Matrix<Double>([
    [3],
    [1]
])

// find B such that A*B=C
let B = inv(A) * C // [2.0, 1.0]′

// Verify result
let r = A*B - C    // zero   

Tiling

A block Matrix can be formed by repeating a 1-D ValueArray or 2-D Matrix mxn times.

import Upsurge

let a = ValueArray = [1.0, 2.0]
// Tile source array 2 times in each directon,
// returning a 2X4 block matrix
let A = a.tile(2, 2)

let B = Matrix<Double>([
    [1.0,  2.0],
    [3.0,  4.0]
)]
// Tile source matrix 2 times in each directon,
// returning a 4x4 block matrix
let r = B.tile(2, 2)

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.