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)

See also

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<Int>, 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))

See also

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)

See also

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)

See also

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)

See also

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.
Return type: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))

See also

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<Int>) – A native Swift array.
Return type: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())

See also

vector.copy np.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))

See also

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))

See also

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)

See also

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"
Return type:

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.
Return type:

vector. Random integers that are normally distributed.

See also

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.
Return type: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))

See also

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)

See also

np.zeros_like

Helper functions

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

The location of the maximum.

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

The location of the minimum.

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

Sort the array but using indices.

Parameters:x (vector) – An array.
Return type: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).
Return type: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.
Return type:

vector

concat(x, y)[source]

Concatenates two arrays.

Parameters:
  • x (vector) – The first array to stack.
  • y (vector) – The second array to stack.
Return type:

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.
Return type:Double. The number of nonzero elements.

See also

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.
Return type: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.
Return type:

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.
Return type:

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))

See also

np.in1d

intersection(x, y)[source]

Find elements that are both arrays.

Parameters:
  • x (vector) – The first array.
  • y (vector) – The second array.
Return type:

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))

See also

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.
Return type:

vector

See also

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.
Return type:vector

See also

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.
Return type:

vector

See also

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.
Return type:

vector

See also

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.
Return type:

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.

See also

np.linalg.norm, Norm

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
Return type: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))

See also

np.repeat

reverse(x)[source]

Reverses an array.

Parameters:x (vector) – The array to be reversed.
Return type:The reveresed vector.

See also

vector.reverse

shuffle(x)[source]

Randomly shuffle an array.

Parameters:x (vector) – The array to shuffle.
Return type:The array shuffled. Not in place!
sort(x)[source]

Sorts an array.

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

See also

vector.sort, np.sort

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.
Return type:

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.
Return type: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.

See also

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)
Return type:

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.
Return type:

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.
Return type: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.
Return type:

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.
Return type: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.
Return type:vector. Performs the function cos on each element.
cumsum(x)[source]

Finds the cumulative sum of an array.

Parameters:x (vector) – An array.
Return type: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
Return type:vector.
exp2(x)[source]

Finds \(2^x\) for some array x.

Parameters:x (vector) – 2 to the power of this array.
Return type: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.
Return type:vector

See also

np.expm1

floor(x)[source]

Floors each element.

Parameters:x (vector) – An array.
Return type: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.
Return type:vector. Performs the function log on each element.
log10(x)[source]

Finds the log base 10 of an array.

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

Finds the log base 2 of an array.

Parameters:x (vector) – An array.
Return type: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:
Return type:

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.
Return type: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:
Return type:

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
Return type:

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.
Return type:

vector

round(x, decimals=0)[source]

Rounds each element.

Parameters:
  • x (vector) – An array.
  • decimals (Int) – The number of decimals kept.
Return type:

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.
Return type: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.
Return type:vector. Performs the function sin on each element.
sqrt(x)[source]

Takes the sqrt of each element.

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

Finds the standard deviation of an array.

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

Finds the sum of an array.

Parameters:x (vector) – An array.
Return type: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.
Return type:vector. Performs the function tan on each element.
variance(x)[source]

Finds the variance of an array.

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

See also

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.
Return type: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.
Return type:

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]

Takes the inverse Foureir Transform.

Parameters:
  • y_real (vector) – The real part of fft(x).
  • y_imag (vector) – The imaginary part of fft(x)
Return type:

vector. The inverse Fourier transform of \(y_{real} + j \cdot y_{imag}\)

Warning

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