# Array¶

vector stands for “$$N$$ dimensional array”. It’s a mix of programming language and math language; in math, this is called an $$N$$ dimensional vector.

The full docs can be found below but I’ll touch on some highlights:

• operators. All arithmetic operators and a host of comparison operators work element-wise
• easy initializers (ones, zeros, zeros_like, linspace, etc)
• simple math. Math that applies to each element.
• nice indexing, including accessing multiple elements at once (goodbye for loops!).

Found in swix/vector/*

Note

* is an element-wise multiplication. *! is used for a dot product or matrix multiplication. Not like matlab!

## Docs¶

### vector class¶

Yes, an actual class (or struct).

class vector.vector[source]
copy()[source]

Copies the contents of an array.

Return type: vector. An exact copy of the array.
>>> assert(x.copy() ~== x)


np.copy

count = 'Int'[source]

Number of elements.

grid = '[Double]'[source]

The Array this vector depends on.

indexing(idx)[source]

Grabs elements using an vector, Range<Int> or integers. Includes negative indexing like Python.

Warning

Assumes an index is either wholly positive or negative; indexes like array(-1, 0, 1) aren’t supported.

Use x["all"] to grab all elements from this array.

It’s also possible to use boolean indexing. The boolean indexs are assumed to have a length the same as the array being indexed.

Parameters: idx (Int, Range, vector, String) – The values to get the index from.
>>> var x = arange(10)
>>> assert(x[1] == 1)
>>> assert(x[0..<2] ~== array(0, 1))
>>> assert(x[array(0, 1)] ~== array(0, 1))
>>> assert(x[-1] == 9)
>>> assert(x["all"] ~== arange(10))
>>> assert(x[x < 3] ~== array(0, 1, 2))


np.indexing

max()[source]

Finds the maximum value.

Return type: Double. The maximum value in the array.
>>> assert(array(1, 2, 3, 4).max() == 4)


np.max

mean()[source]

Finds the average value.

Return type: Double. The mean value in the array.
>>> assert(array(1, 2, 3, 4).mean() == 2.5)


np.mean

min()[source]

Finds the minimum value.

Return type: Double. The minimum value in the array.
>>> assert(array(1, 2, 3, 4).min() == 1)


np.min

n = 'Int'[source]

Number of elements this array contains

reshape(shape)[source]

Reshapes the matrix to the specified size. The number of elements must remain constant, however, if a value of -1 is passed a new size is calculated to keep the number of elements the same.

Parameters: shape ((Int, Int)) – The size of the new matrix. matrix. The vector reshaped as a matrix.
>>> var x = arange(2*2).reshape((2,2))
>>> var y = x.reshape((2,-1))
>>> print(y)
# prints [0 1; 2 3]
>>> print(x)
# prints [0 1; 2 3]

reverse()[source]

Reverses the array in place.

sort()[source]

Sorts the array in-place.

### Initing¶

This is a file, not an actual class.

class vector.initing[source]
arange(max, min=0, x=True)[source]

Makes an array of integers.

Parameters: max (Int) – How high should the array index up to? min (Int) – Where should the indexing start? x (Bool) – Should the array be exclusive?
>>> assert(arange(2) ~== array(0, 1))
>>> assert(arange(4, 6) ~== array(4, 5))
>>> assert(arange(2, x:true) ~== array(0, 1, 2))


np.arange

array(numbers)[source]

Makes an array out of a list of numbers.

Parameters: numbers (Double...) – A list of numbers to make an array.

Note

Unoptimized. I assume this is only being used in test code.

>>> print(array(0, 1, 2, 3, 4))
# prints array([0.000, 1.000, 2.000, 3.000, 4.000])

asarray(x)[source]

Convert the input to an vector.

Parameters: x ([Double], Range) – A native Swift array. vector. The native Swift array converted to an vector.
>>> assert(asarray(0..<2) ~== array(0, 1))
>>> assert(asarray([0, 1, 2]) ~== array(0, 1, 2))

copy(x)[source]

Copies the contents of an array.

Parameters: x (vector) – The array to copy.

Returns vector.copy()

>>> assert(copy(x) ~== x.copy())


linspace(min, max, num=50)[source]

Makes a series of values linearly interpolated between two poitns.

Parameters: min (Double.) – The minimum value. max (Double.) – The maximum value. num (Int) – How many elements?
>>> assert(linspace(0, 1, num:3) ~== array(0.0, 0.5, 1.0))


np.linspace

ones(N)[source]

Makes an array of ones.

Parameters: N (Int) – The length of the array.
>>> assert(ones(3) ~== array(1, 1, 1))


np.ones

ones_like(x)[source]

Makes an array like another array.

Parameters: x (vector) – The array to imitate.
>>> var x = array(3, 8)
>>> assert(ones_like(x) ~== array(1, 1)


np.ones_like

rand(N, distro='uniform')[source]

Makes an array of uniform random variables.

Parameters: N (Int) – The size of the array. distro (String) – The type of distribution. Assumed to be either "uniform" or "normal" vector. Random integers normally distributed.

randn(N, mean=0.0, sigma=1.0)[source]

Makes an array of normally distributed random variables.

Parameters: N (Int) – The size of the array. mean (Double) – The mean of this distribution. sigma (Double) – The standard deviation of this distribution. vector. Random integers that are normally distributed.

np.random.randn

randperm(N)[source]

Shuffle an array of (typically) indices.

Parameters: N (Int) – The size of the array. Values included are between 0 and N-1. vector
seed(N)[source]

Sets the seed for a psuedo-random number generation. This uses the global variable SWIX_SEED defined in numbers.swift. This global variable SWIX_SEED updates every time a call to rand is called (which happens through randn, etc).

Parameters: N (Int) – The seed. If seed is not called, this defaults to 42.
zeros(N)[source]

Makes an array of 0’s.

Parameters: N (Int) – The length of the array.
>>> assert(zeros(3) ~== array(0, 0, 0))


np.zeros

zeros_like(x)[source]

Makes an array like another array.

Parameters: x (vector) – The array to imitate.
>>> var x = array(3, 8)
>>> assert(zeros_like(x) ~== array(0, 0)


np.zeros_like

### Helper functions¶

class vector.helper_functions[source]
argmax(x)[source]

The location of the maximum.

Parameters: x (vector) – An array. Double. The location of the maximum value.
argmin(x)[source]

The location of the minimum.

Parameters: x (vector) – An array. Double. The location of the maximum value.
argsort(x)[source]

Sort the array but using indices.

Parameters: x (vector) – An array. vector
>>> var x = array(1, 4, 2, 6, 7)
>>> assert(argsort(x) ~== array(0, 2, 1, 3, 4))

argwhere(idx)[source]

Sees where a condition exists.

Parameters: idx (vector) – An array of 0’s and 1’s (analagous to true and false). vector. Returns the indices where idx has non-zero elements.
clip(a, a_min, a_max)[source]

Only keep values between a_min and a_max; set the rest to 0. Performs the following operation:

$$(x_i < a_{min}) \lor (x_i > a_{max}) \implies x_i = 0$$

Similar to Numeric’s clip.

Parameters: a (vector) – The array to clip. a_min (Double) – The minimum value. a_max (Double) – The maximum value. vector
concat(x, y)[source]

Concatenates two arrays.

Parameters: x (vector) – The first array to stack. y (vector) – The second array to stack. vector. Equivalent to concatenate((x, y)) in NumPy or [x y] in Matlab.
>>> var x = array(0, 1)
>>> var y = array(2, 3)
>>> assert(concat(x, y) ~== arange(4))


count_nonzero(x)[source]

Counts the nonzero elements.

Parameters: x (vector) – The array to be counted. Double. The number of nonzero elements.

np.count_nonzero

cumprod(x)[source]

Find the cumulative product of every element in this array.

Parameters: x (vector) – The cumulative product of elements in this array. vector
>>> assert(cumprod(arange(4)+1) ~== array(1, 2, 6, 24))

delete(x, idx)[source]

Deletes elements.

Parameters: x (vector) – The original array. idx (vector) – The indices to remove. vector. The array with the specified indices deleted.
in1d(x, y)[source]

Sees if elements from the first array are in the second and returns the indices.

Parameters: x (vector) – The array to index from. y (vector) – The array to check to see if values of x are in this array. vector. An array of true/false (well, 1/0).
>>> var x = array(1, 2, 3, 4, 5)
>>> var y = array(4, 5, 6)
>>> assert(in1d(x, y) ~== array(0, 0, 0, 1, 1))


np.in1d

intersection(x, y)[source]

Find elements that are both arrays.

Parameters: x (vector) – The first array. y (vector) – The second array. The sorted array of unique elements that are in both arrays.
>>> var x = array(1, 2, 3, 4)
>>> var y = array(5, 3, 1, 0)
>>> assert(intersection(x, y) ~== array(1, 3))


np.intersect1d

logical_and(x, y)[source]

Find the logical AND of x and y.

Parameters: x (vector) – An array of truth values; assumed to take values of 0 and 1. y (vector) – An array of truth values; assumed to take values of 0 and 1. vector

np.logical_and

logical_not(x)[source]

Find the logical NOT of x and y.

Parameters: x (vector) – An array of truth values; assumed to take values of 0 and 1. vector

np.logical_not

logical_or(x, y)[source]

Find the logical OR of x and y.

Parameters: x (vector) – An array of truth values; assumed to take values of 0 and 1. y (vector) – An array of truth values; assumed to take values of 0 and 1. vector

np.logical_or

logical_xor(x, y)[source]

Find the logical XOR of x and y.

Parameters: x (vector) – An array of truth values; assumed to take values of 0 and 1. y (vector) – An array of truth values; assumed to take values of 0 and 1. vector

np.logical_xor

norm(x, ord=2)[source]

Finds the norm of an array.

Parameters: x (vector) – An array. ord (Int or $$\pm$$ inf) – Indicates the specific type of norm. Double. A specific norm of the array. Either the $$\ell_0$$, $$\ell_1$$, $$\ell_2$$ or $$\ell_\infty$$ norm.

Note

This is a direct copy of np.linalg.norm. See NumPy’s documentation for info about appropiate values for ord.

println(x, prefix='array([', postfix='])', newline='\n', format='%.3f', seperator=', ', printAllElements=False)[source]

Prints an array.

Note

Can be called with either print(x) or println(x)

Parameters: x (vector) – Prints that matrix.

Prints the vector with the above optional formatters. They are all of type String.

prod(x)[source]

Find the product of every element in this array.

Parameters: x (vector) – Find the product of every element in this array Double
>>> assert(prod(arange(4)+1) == 24)

repeat(x, N, axis=0)[source]

Repeats an array.

Warning

Must be called with repeat(x, N:3). repeat is a keyword in Swift and those backticks must be used.

Parameters: x (vector) – The array to repeat. N (Int) – How many times to repeat the array. axis (Int) – Assumed to be “vector” or “elements”.
>>> var x = array(0, 1)
>>> assert(repeat(x, 2, axis:0) ~== array(0, 1, 0, 1))
>>> assert(repeat(x, 2, axis:1) ~== array(0, 0, 1, 1))


np.repeat

reverse(x)[source]

Reverses an array.

Parameters: x (vector) – The array to be reversed. The reveresed vector.
shuffle(x)[source]

Randomly shuffle an array.

Parameters: x (vector) – The array to shuffle. The array shuffled. Not in place!

sort(x)[source]

Sorts an array.

Parameters: x (vector) – Array to be sorted. vector. The sorted array.

union(x, y)[source]

Finds unique elements that are in either array.

Parameters: x (vector) – The first array of elements. y (vector) – The second array of elements. The unique list of elements that in either array.
>>> var x = array(1, 2, 3, 4)
>>> var y = array(2, 3, 4, 5)
>>> assert(union(x, y) ~== array(1, 2, 3, 4, 5))

unique(x)[source]

Find the unique elements of an array. Returns the unique elements sorted.

Parameters: x (vector) – The input array. vector. The unqiue element.
>>> x = array(1, 1, 2, 3, 4, 4, 5, 6)
>>> assert(unique(x) ~== array(1, 2, 3, 4, 5, 6))


### Operators¶

This is a file, not an actual class.

class vector.operators[source]
assignment_operator(lhs, rhs)[source]

Assign select values of an array to a value.

Parameters: lhs (vector) – The vector to assign to. rhs (Double) – The value to fill the array with.

Note

Callable through x[0..<2] <- 4

>>> var x = arange(4)
>>> # assign select values!
>>> x[0..<2] <- 4
>>> assert(x ~== array(4, 4, 2, 3)

elementwise_operators(lhs, rhs)[source]

Element-wise operators.

Parameters: lhs (vector, Double) – Left hand side. rhs (vector, Double) – Right hand side.

The operators + - * / % += -= *= /= < > <= >= == !== all work element wise and act similar to scalars.

Note

Callable through the natural operators (+, -, etc).

Note

== and !== see when two arrays are exactly equal for the incredibly percise doubles. I recommend using abs(x-y)<1e-9.

np.operators

equality(lhs, rhs)[source]

Seeing if two arrays are about equal.

Parameters: lhs (vector, Double) – Left hand side. rhs (vector, Double) – Right hand side.

Sees if two arrays are approximately equal.

The comparison operators are all implemented through OpenCV and rely on the array being continuous in memory, which relies on having small input arrays. It will print (but not assert) if this is not the case.

Note

Callable through the ~== operator.

>>> assert(array(0, 1) ~== array(0, 1+1e-10))

logical_operators(lhs, rhs)[source]

Logical operators.

Parameters: lhs (vector) – An array of true/false (meaning 0 or 1) rhs (vector) – An array of true/false (meaning 0 or 1) vector. The logical operator between the two elements.

Currently only logical AND and logical OR are implemented through && and || respectively.

pow(lhs, rhs)[source]

The power operator.

Parameters: lhs (vector, Double, vector) – The vector rhs (Double, vector, vector.) – The power. vector, pow(lhs, rhs).

Note

callable with x^y.

Note

Optimized for lhs:vector, rhs=2. If close(2, rhs) == True, prints message but no assert.

>>> assert(array(1, 4) ~== array(1, 2)^2)


### Simple math¶

This is a file, not an actual class.

class vector.simple_math[source]

The following are detailed docs on various functions, but the following functions work like expected:

• min, max, mean, std, variance
• sign, abs, norm, pow, sqrt
• sum, cumsum,
• rand, randn
• sin, cos, tan
• round, floor, ceil
abs(x)[source]

Finds the absolute value of an array.

Parameters: x (vector) – An array. vector. The absolute value of the array
apply_function(function, x)[source]

Apply a function to every element of an array.

Parameters: function (String, Double->Double) – Assumed to be one of “abs”, “sign”, “floor” or “cumsum” or a simple Double->Double function. x (vector) – An array. A new array with the function applied to every element. Optimized for string case.

Note

Unoptimized for function type Double->Double. I tried to optimize with Grand Central Dispatch’s dispatch_apply, but no luck.

ceil(x)[source]

Ceil’s each element.

Parameters: x (vector) – An array. vector. Performs the function sin on each element.
>>> assert(ceil(array(0.4, 0.6)) ~== array(1, 1))

cos(x)[source]

Finds the cos of an array.

Parameters: x (vector) – An array. vector. Performs the function cos on each element.
cumsum(x)[source]

Finds the cumulative sum of an array.

Parameters: x (vector) – An array. vector. The cumulative sum of the array.
>>> assert(cumsum(arange(5)) ~== array(0, 1, 3, 6))

exp(x)[source]

Finds the exponent of each element; mathematically it finds $$\exp(x)$$

Parameters: x (vector) – The input array vector.
exp2(x)[source]

Finds $$2^x$$ for some array x.

Parameters: x (vector) – 2 to the power of this array. vector. $$2^x$$
expm1(x)[source]

Estimate $$\exp(x)-1$$. Useful for when $$exp(1) << 1$$.

Parameters: x (vector) – The array to calculate the exponent of. vector

np.expm1

floor(x)[source]

Floors each element.

Parameters: x (vector) – An array. vector. Performs the function floor on each element.
>>> assert(floor(array(0.4, 0.6)) ~== array(0, 0))

log(x)[source]

Finds the log base e of an array.

Parameters: x (vector) – An array. vector. Performs the function log on each element.
log10(x)[source]

Finds the log base 10 of an array.

Parameters: x (vector) – An array. vector. Performs the function log on each element.
log2(x)[source]

Finds the log base 2 of an array.

Parameters: x (vector) – An array. vector. Performs the function log on each element.
max(x, y=None)[source]

Finds the maximum element.

If only one array specified, it only finds the maximum element of that array. If two arrays specficied, it finds the maximum element wise.

>>> assert(max(array(0, 1), array(1, 0)) ~== array(1, 1))

Parameters: x (vector) – An array. y (vector) – Another array. Double, vector. The maximum value of the array or the maximum element from both arrays.
mean(x)[source]

Finds the mean of an array.

Parameters: x (vector) – An array. Double. The average of all the components or $$\sum x_i / N$$
min(x, y=None)[source]

Finds the maximum element.

If only one array specified, it only finds the maximum element of that array. If two arrays specficied, it finds the maximum element wise.

>>> assert(max(array(0, 1), array(1, 0)) ~== array(0, 0))

Parameters: x (vector) – An array. y (vector) – Another array. Double, vector. The maximum value of the array or the maximum element from both arrays.
pow(x, power)[source]

The power of a {Double, vector} to {Double, vector}

Parameters: x (vector, Double, vector) – An array. power (Double, vector, Double) – The power vector. Performs the same action as the operator ^. Raises each element to a power.

Note

Also callable through x^y

>>> assert(pow(array(1, 2, 3), 2) ~== array(1, 4, 9))

remainer(x1, x2)[source]

Finds the element wise remainder after dividing the two arrays.

>>> assert(array(1.1, 1.2, 1.3) % ones(3) ~== array(0.1, 0.2, 0.3))

Parameters: x1 (vector, Double, vector) – The top divisor. x2 (Double, vector, vector) – The bottom divisor. vector
round(x, decimals=0)[source]

Rounds each element.

Parameters: x (vector) – An array. decimals (Int) – The number of decimals kept. vector. Performs the function round on each element.
>>> assert(round(array(0.4, 0.6)) ~== array(0, 1))
>>> assert(round(array(0.43, 0.418), decimals:2) ~== array(0.43, 0.42))

sign(x)[source]

Finds the sign an array.

Parameters: x (vector) – An array. The sign of the array centered at 0.
>>> assert(sign(array(-1, 0.1, 1)) ~== array(-1, 1, 1))

sin(x)[source]

Finds the sin of an array.

Parameters: x (vector) – An array. vector. Performs the function sin on each element.
sqrt(x)[source]

Takes the sqrt of each element.

Parameters: x (vector) – An array. vector. Performs the function sqrt on each element.
std(x)[source]

Finds the standard deviation of an array.

Parameters: x (vector) – An array. Double. The standard deviation of the array.
sum(x)[source]

Finds the sum of an array.

Parameters: x (vector) – An array. Double. The sum of all the elements in x. Returns the result from the following operation: $$\sum x_i$$
>>> assert(sum(array(1, 2, 3)) == 6)

tan(x)[source]

Finds the tan of an array.

Parameters: x (vector) – An array. vector. Performs the function tan on each element.
variance(x)[source]

Finds the variance of an array.

Parameters: x (vector) – An array. Double. The variance of the array.

Variance

### Complex math¶

This is a file, not an actual class.

class vector.complex_math[source]
fft(x)[source]

Takes the discrete Fourier Transform.

Parameters: x (vector) – An array to perform the fft on. vector, vector. The real and imaginary components of the Fourier transform.
>>> x = arange(10)
>>> assert(x ~== ifft(fft(x)))


Warning

I had to do some weird dividing; possibly related to dimension?

fftconvolve(x, kernel)[source]

Convolve x with a kernel.

Parameters: x (vector) – The base signal. kernel (vector) – The convolution kernel. x convolved with kernel through the Fourier transform. That is, if $$y = x \otimes k$$ then $$F(y) = F(x) \cdot F(k)$$

Warning

There are bugs in this function. My simple tests with a delta function failed, possibly do to the funky divide in fft/ifft.

Warning

The number of elements in kernel is assumed to be less than the number of elements in x.

ifft(y_real, y_imag)[source]
Parameters: y_real (vector) – The real part of fft(x). y_imag (vector) – The imaginary part of fft(x) vector. The inverse Fourier transform of $$y_{real} + j \cdot y_{imag}$$