owned this note
owned this note
Published
Linked with GitHub
# Upsurge <-> Numpy API dictionary
A quick and easy guide for [Upsurge](https://github.com/aleph7/Upsurge).
Please feel free to add/edit!
## Creating variables
### Upsurge
```swift
let zeros = ValueArray<Float>(count: 10, repeatedValue: 0.0)
// init with a Swift array
let x: ValueArray<Float> = [1, 2, 3]
// init uninitialized memory
let z = ValueArray<Float>(count: 3)
let X = Matrix<Float>(rows: 5, columns: 10)
```
### Numpy
```python
x, y = np.zeros((N,), dtype=np.float32), np.zeros((N,), np.float32)
X = np.zeros((5, 10), dtype=np.float32)
```
## Table - constructing vector/matrix
| Upsurge | Numpy | Note |
| -------- | -------- | -------- |
| `[1, 2, 3]` | `np.array([1, 2, 3])` | array literal|
| N/A | `np.array([[1, 2], [3, 4]])` | matrix literal|| `x[2..<4]` | `x[2:4]` |slice of an array|
| `X[2..<4, 0..]` | `X[2:4]` | slice of a matrix (along row)|
| `X[0.., 2..<4]` | `X[:, 2:4]`| slice of a matrix (along column) |
| `y = x` | `y = x` | assign by reference|
| `y = ValueArray(x)`, `y = x.copy()` | `y = np.array(x)` | assign by copying|
| `y = x[2..<4]` |`y = x[2:4]`| assign by referencing a slice|
| `y = ValueArray(x[2..<4])` |`y = np.array(x[2:4])`| assign by copying a slice|
| `Array(x)` | `x.to_list()` |to bulit-in list (array)|
||`x.astype(np.float32)`|data type conversion|
## Table - properties/retrieve values
| Upsurge | Numpy | Note |
| -------- | -------- | -------- |
| `x.count` | `len(x)` | length of an 1D array |
| `X.dimensions` (returns a list)| `X.shape`, `np.shape(X)` | shape of a matrix|
| `x.first` | `x[0]` | the first element of an array|
| `x.last` | `x[-1]` | the last element of an array|
| `x[n]`| `x[n]` | access an element by index |
|||sort|
||`np.argmax(x)`|get the index of maximum element|
## Table - editing vector/matrix
| Upsurge | Numpy | Note |
| -------- | -------- | -------- |
| `x.tile(2, 3)`| `np.tile(x, (2, 3))` | tiling |
| | `np.concatenate((a, b), 1)`, `hstack((x, y))` | concatenating two vectors|
| | `np.flatten(x)` | flatten the shape|
||`np.reshape(x, new_shape)`| reshape an array/matrix/tensor |
## Table - arithmetic and linear algebra
| Upsurge | Numpy | Note |
| -------- | -------- | -------- |
| `x • y`, `dot(x, y)` | `np.dot(x, y)` | dot product of two vectors |
| `x * y` | `x * y` | element-wise multiplication (array) |
| | `X * Y`| element-wise multiplication (matrices)|
| `X * Y` | `X.dot(Y)` | matrix multiplication |
| `x + y` |`x + y`|element-wise addition|
| `x - y` |`x - y`|element-wise subtraction|
| `x / y`|`x / y`|element-wise division|
| N/A |`x ** y`|element-wise exponent|
| `log(x)`, `??` (for matrix) |`np.log(x)`, `np.log(X)`|element-wise logarithm|
||`np.log2(x)`|log with base 2|
||`np.log10(x)`|log with base 10|
| `max(x)`,`??`(for matrix)|`np.maximum(x, 10)`, `np.maximum(X, 10)`|element-wise maximum|
| `min(x)` |`np.minimum(x, 10)`|element-wise minimum|
| `X'` | `X.T`, `X.transpose()` | transpose of a matrix |
| `inv(X)`| `np.linalg.inv(X)` | inverse of a matrix|
| | | convolution|
| `sum(x)` | `np.sum(x, axis=None)` | sum over array|
| `max(x)` | `np.max(x, axis=None)`| max of an array|
| `min(x)` | `np.min(x, axis=None)`| min of an array|
| `mean(x)` | `np.mean(x, axis=None)`| average of an array|
| `std(x)` | `np.std(x, axis=None)` | standard deviation of an array|
| | | |
| | | |
## Examples
```
```