aerosandbox.numpy#

Submodules#

Package Contents#

Functions#

is_casadi_type(object[, recursive])

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True,

array(array_like[, dtype])

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

concatenate(arrays[, axis])

Join a sequence of arrays along an existing axis. Returns a NumPy array if possible; if not, returns a CasADi array.

stack(arrays[, axis])

Join a sequence of arrays along a new axis. Returns a NumPy array if possible; if not, returns a CasADi array.

hstack(arrays)

vstack(arrays)

dstack(arrays)

length(array)

Returns the length of an 1D-array-like object. An extension of len() with slightly different functionality.

diag(v[, k])

Extract a diagonal or construct a diagonal array.

roll(a, shift[, axis])

Roll array elements along a given axis.

max(a[, axis])

Return the maximum of an array or maximum along an axis.

min(a[, axis])

Return the minimum of an array or minimum along an axis.

reshape(a, newshape[, order])

Gives a new shape to an array without changing its data.

ravel(a[, order])

Returns a contiguous flattened array.

tile(A, reps)

Construct an array by repeating A the number of times given by reps.

zeros_like(a[, dtype, order, subok, shape])

Return an array of zeros with the same shape and type as a given array.

ones_like(a[, dtype, order, subok, shape])

Return an array of ones with the same shape and type as a given array.

empty_like(prototype[, dtype, order, subok, shape])

Return a new array with the same shape and type as a given array.

full_like(a, fill_value[, dtype, order, subok, shape])

Return a full array with the same shape and type as a given array.

assert_equal_shape(arrays)

Assert that all of the given arrays are the same shape. If this is not true, raise a ValueError.

is_casadi_type(object[, recursive])

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True,

sum(x[, axis])

Sum of array elements over a given axis.

mean(x[, axis])

Compute the arithmetic mean along the specified axis.

abs(x)

prod(x[, axis])

Return the product of array elements over a given axis.

where(condition, value_if_true, value_if_false)

Return elements chosen from x or y depending on condition.

is_casadi_type(object[, recursive])

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True,

_make_casadi_types_broadcastable(x1, x2)

add(x1, x2)

multiply(x1, x2)

mod(x1, x2)

Return element-wise remainder of division.

centered_mod(x1, x2)

Return element-wise remainder of division, centered on zero.

is_casadi_type(object[, recursive])

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True,

_centered_mod(x1, x2)

Return element-wise remainder of division, centered on zero.

array(array_like[, dtype])

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

concatenate(arrays[, axis])

Join a sequence of arrays along an existing axis. Returns a NumPy array if possible; if not, returns a CasADi array.

reshape(a, newshape[, order])

Gives a new shape to an array without changing its data.

diff(a[, n, axis, period])

Calculate the n-th discrete difference along the given axis.

gradient(f, *varargs[, axis, edge_order, n, period])

Return the gradient of an N-dimensional array.

trapz(x[, modify_endpoints])

Computes each piece of the approximate integral of x via the trapezoidal method with unit spacing.

is_casadi_type(object[, recursive])

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True,

where(condition, value_if_true, value_if_false)

Return elements chosen from x or y depending on condition.

maximum(x1, x2)

Element-wise maximum of two arrays.

minimum(x1, x2)

Element-wise minimum of two arrays.

array(array_like[, dtype])

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

length(array)

Returns the length of an 1D-array-like object. An extension of len() with slightly different functionality.

finite_difference_coefficients(x[, x0, derivative_degree])

Computes the weights (coefficients) in compact finite differece formulas for any order of derivative

quad(func, a, b[, full_output, variable_of_integration])

solve_ivp(fun, t_span, y0[, method, t_eval, ...])

lotkavolterra_func(t, z)

is_casadi_type(object[, recursive])

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True,

array(array_like[, dtype])

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

zeros_like(a[, dtype, order, subok, shape])

Return an array of zeros with the same shape and type as a given array.

where(condition, value_if_true, value_if_false)

Return elements chosen from x or y depending on condition.

all(a)

Test whether all array elements along a given axis evaluate to True.

any(a)

Test whether any array element along a given axis evaluates to True.

logical_or(x1, x2)

Compute the truth value of x1 OR x2 element-wise.

interp(x, xp, fp[, left, right, period])

One-dimensional linear interpolation, analogous to numpy.interp().

is_data_structured(x_data_coordinates, y_data_structured)

Determines if the shapes of a given dataset are consistent with "structured" (i.e. gridded) data.

interpn(points, values, xi[, method, bounds_error, ...])

Performs multidimensional interpolation on regular grids. Analogue to scipy.interpolate.interpn().

is_casadi_type(object[, recursive])

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True,

dot(a, b[, manual])

Dot product of two arrays.

cross(a, b[, axisa, axisb, axisc, axis, manual])

Return the cross product of two (arrays of) vectors.

transpose(a[, axes])

Reverse or permute the axes of an array; returns the modified array.

is_casadi_type(object[, recursive])

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True,

clip(x, min, max)

Clip a value to a range.

logical_and(x1, x2)

Compute the truth value of x1 AND x2 element-wise.

logical_or(x1, x2)

Compute the truth value of x1 OR x2 element-wise.

logical_not(x)

Compute the truth value of NOT x element-wise.

all(a)

Test whether all array elements along a given axis evaluate to True.

any(a)

Test whether any array element along a given axis evaluates to True.

array(array_like[, dtype])

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

rotation_matrix_2D(angle[, as_array])

Gives the 2D rotation matrix associated with a counterclockwise rotation about an angle.

rotation_matrix_3D(angle, axis[, as_array, ...])

Yields the rotation matrix that corresponds to a rotation by a specified amount about a given axis.

rotation_matrix_from_euler_angles([roll_angle, ...])

Yields the rotation matrix that corresponds to a given Euler angle rotation.

is_valid_rotation_matrix(a[, tol])

Returns a boolean of whether the given matrix satisfies the properties of a rotation matrix.

is_casadi_type(object[, recursive])

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True,

linspace([start, stop, num])

Returns evenly spaced numbers over a specified interval.

cosspace([start, stop, num])

Makes a cosine-spaced vector.

sinspace([start, stop, num, reverse_spacing])

Makes a sine-spaced vector. By default, bunches points near the start.

logspace([start, stop, num])

Return numbers spaced evenly on a log scale.

geomspace([start, stop, num])

Return numbers spaced evenly on a log scale (a geometric progression).

softmax(*args[, softness, hardness])

An element-wise softmax between two or more arrays. Also referred to as the logsumexp() function.

softmin(*args[, softness, hardness])

An element-wise softmin between two or more arrays. Related to the logsumexp() function.

softmax_scalefree(*args[, relative_softness, ...])

softmin_scalefree(*args[, relative_softness, ...])

softplus(x[, beta, threshold])

A smooth approximation of the ReLU function, applied elementwise to an array x.

sigmoid(x[, sigmoid_type, normalization_range])

A sigmoid function. From Wikipedia (https://en.wikipedia.org/wiki/Sigmoid_function):

swish(x[, beta])

A smooth approximation of the ReLU function, applied elementwise to an array x.

blend(switch, value_switch_high, value_switch_low)

Smoothly blends between two values on the basis of some switch function.

degrees(x)

Converts an input x from radians to degrees

radians(x)

Converts an input x from degrees to radians

sind(x)

Returns the sin of an angle x, given in degrees

cosd(x)

Returns the cos of an angle x, given in degrees

tand(x)

Returns the tangent of an angle x, given in degrees

arcsind(x)

Returns the arcsin of an x, in degrees

arccosd(x)

Returns the arccos of an x, in degrees

arctan2d(y, x)

Returns the angle associated with arctan(y, x), in degrees

Attributes#

aerosandbox.numpy.is_casadi_type(object, recursive=True)[source]#

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type.

Parameters:
  • object (Any) – The object to evaluate.

  • recursive (bool) – If the object is a list or tuple, recursively iterate through every subelement. If any of the

  • subelements (at any depth) –

Return type:

bool

Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type.

aerosandbox.numpy.array(array_like, dtype=None)[source]#

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.array.html

aerosandbox.numpy.concatenate(arrays, axis=0)[source]#

Join a sequence of arrays along an existing axis. Returns a NumPy array if possible; if not, returns a CasADi array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html

Parameters:
  • arrays (Sequence) –

  • axis (int) –

aerosandbox.numpy.stack(arrays, axis=0)[source]#

Join a sequence of arrays along a new axis. Returns a NumPy array if possible; if not, returns a CasADi array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.stack.html

Parameters:
  • arrays (Sequence) –

  • axis (int) –

aerosandbox.numpy.hstack(arrays)[source]#
aerosandbox.numpy.vstack(arrays)[source]#
aerosandbox.numpy.dstack(arrays)[source]#
aerosandbox.numpy.length(array)[source]#

Returns the length of an 1D-array-like object. An extension of len() with slightly different functionality. :param array:

Returns:

Return type:

int

aerosandbox.numpy.diag(v, k=0)[source]#

Extract a diagonal or construct a diagonal array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.diag.html

aerosandbox.numpy.roll(a, shift, axis=None)[source]#

Roll array elements along a given axis.

Elements that roll beyond the last position are re-introduced at the first.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.roll.html

Parameters:
  • a (array_like) – Input array.

  • shift (int or tuple of ints) – The number of places by which elements are shifted. If a tuple, then axis must be a tuple of the same size, and each of the given axes is shifted by the corresponding number. If an int while axis is a tuple of ints, then the same value is used for all given axes.

  • axis (int or tuple of ints, optional) – Axis or axes along which elements are shifted. By default, the array is flattened before shifting, after which the original shape is restored.

Returns:

res – Output array, with the same shape as a.

Return type:

ndarray

aerosandbox.numpy.max(a, axis=None)[source]#

Return the maximum of an array or maximum along an axis.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.max.html

aerosandbox.numpy.min(a, axis=None)[source]#

Return the minimum of an array or minimum along an axis.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.min.html

aerosandbox.numpy.reshape(a, newshape, order='C')[source]#

Gives a new shape to an array without changing its data.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.reshape.html

aerosandbox.numpy.ravel(a, order='C')[source]#

Returns a contiguous flattened array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.ravel.html

aerosandbox.numpy.tile(A, reps)[source]#

Construct an array by repeating A the number of times given by reps.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.tile.html

aerosandbox.numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None)[source]#

Return an array of zeros with the same shape and type as a given array.

aerosandbox.numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None)[source]#

Return an array of ones with the same shape and type as a given array.

aerosandbox.numpy.empty_like(prototype, dtype=None, order='K', subok=True, shape=None)[source]#

Return a new array with the same shape and type as a given array.

aerosandbox.numpy.full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None)[source]#

Return a full array with the same shape and type as a given array.

aerosandbox.numpy.assert_equal_shape(arrays)[source]#

Assert that all of the given arrays are the same shape. If this is not true, raise a ValueError.

Args: arrays: The arrays to be evaluated.

Can be provided as a:

  • List, in which case a generic ValueError is thrown

  • Dictionary consisting of name:array pairs for key:value, in which case the names are given in the ValueError.

Returns: None. Throws an error if leng

Parameters:

arrays (Union[List[numpy.ndarray], Dict[str, numpy.ndarray]]) –

Return type:

None

aerosandbox.numpy.is_casadi_type(object, recursive=True)[source]#

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type.

Parameters:
  • object (Any) – The object to evaluate.

  • recursive (bool) – If the object is a list or tuple, recursively iterate through every subelement. If any of the

  • subelements (at any depth) –

Return type:

bool

Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type.

aerosandbox.numpy.sum(x, axis=None)[source]#

Sum of array elements over a given axis.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.sum.html

Parameters:

axis (int) –

aerosandbox.numpy.mean(x, axis=None)[source]#

Compute the arithmetic mean along the specified axis.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.mean.html

Parameters:

axis (int) –

aerosandbox.numpy.abs(x)[source]#
aerosandbox.numpy.prod(x, axis=None)[source]#

Return the product of array elements over a given axis.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.prod.html

Parameters:

axis (int) –

aerosandbox.numpy.where(condition, value_if_true, value_if_false)[source]#

Return elements chosen from x or y depending on condition.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.where.html

aerosandbox.numpy.is_casadi_type(object, recursive=True)[source]#

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type.

Parameters:
  • object (Any) – The object to evaluate.

  • recursive (bool) – If the object is a list or tuple, recursively iterate through every subelement. If any of the

  • subelements (at any depth) –

Return type:

bool

Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type.

aerosandbox.numpy._make_casadi_types_broadcastable(x1, x2)[source]#
aerosandbox.numpy.add(x1, x2)[source]#
aerosandbox.numpy.multiply(x1, x2)[source]#
aerosandbox.numpy.mod(x1, x2)[source]#

Return element-wise remainder of division.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.mod.html

aerosandbox.numpy.centered_mod(x1, x2)[source]#

Return element-wise remainder of division, centered on zero.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.mod.html

aerosandbox.numpy.is_casadi_type(object, recursive=True)[source]#

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type.

Parameters:
  • object (Any) – The object to evaluate.

  • recursive (bool) – If the object is a list or tuple, recursively iterate through every subelement. If any of the

  • subelements (at any depth) –

Return type:

bool

Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type.

aerosandbox.numpy._centered_mod(x1, x2)#

Return element-wise remainder of division, centered on zero.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.mod.html

aerosandbox.numpy.array(array_like, dtype=None)[source]#

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.array.html

aerosandbox.numpy.concatenate(arrays, axis=0)[source]#

Join a sequence of arrays along an existing axis. Returns a NumPy array if possible; if not, returns a CasADi array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html

Parameters:
  • arrays (Sequence) –

  • axis (int) –

aerosandbox.numpy.reshape(a, newshape, order='C')[source]#

Gives a new shape to an array without changing its data.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.reshape.html

aerosandbox.numpy.diff(a, n=1, axis=-1, period=None)[source]#

Calculate the n-th discrete difference along the given axis.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.diff.html

Adds one new argument, period, which is the period of the data. If provided, the difference is taken assuming the data “wraps around” at the period (i.e., modulo the period). For example: >>> diff([345, 355, 5, 15], period=360) >>> [10, 10, 10, 10]

aerosandbox.numpy.gradient(f, *varargs, axis=None, edge_order=1, n=1, period=None)[source]#

Return the gradient of an N-dimensional array.

The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.gradient.html

Parameters:
  • f – The array-like object to take the gradient of.

  • *varargs – The spacing between the points of f. If a scalar, the spacing is assumed to be uniform in all dimensions. If an array, the array must have the same shape as f.

  • axis – The axis along which the difference is taken. If None, the gradient is taken for all axes.

  • edge_order – The order of the error at the boundaries. 1 means first order, 2 means second order.

  • n – This is a new argument (not in NumPy) that specifies the order of the derivative to take. 1 is the first derivative (default), 2 is the second derivative. Doing np.gradient(f, n=2) results in less discretization error than doing np.gradient(np.gradient(f)).

  • period – The period of the data. If provided, the gradient is taken assuming the data “wraps around” at the period (i.e., modulo the period). See aerosandbox.numpy.diff() for more information.

Returns: The gradient of f.

aerosandbox.numpy.trapz(x, modify_endpoints=False)[source]#

Computes each piece of the approximate integral of x via the trapezoidal method with unit spacing. Can be viewed as the opposite of diff().

Parameters:

x – The vector-like object (1D np.ndarray, cas.MX) to be integrated.

Returns: A vector of length N-1 with each piece corresponding to the mean value of the function on the interval

starting at index i.

aerosandbox.numpy.is_casadi_type(object, recursive=True)[source]#

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type.

Parameters:
  • object (Any) – The object to evaluate.

  • recursive (bool) – If the object is a list or tuple, recursively iterate through every subelement. If any of the

  • subelements (at any depth) –

Return type:

bool

Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type.

aerosandbox.numpy.where(condition, value_if_true, value_if_false)[source]#

Return elements chosen from x or y depending on condition.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.where.html

aerosandbox.numpy.maximum(x1, x2)[source]#

Element-wise maximum of two arrays.

Note: not differentiable at the crossover point, will cause issues if you try to optimize across a crossover.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.maximum.html

aerosandbox.numpy.minimum(x1, x2)[source]#

Element-wise minimum of two arrays.

Note: not differentiable at the crossover point, will cause issues if you try to optimize across a crossover.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.minimum.html

aerosandbox.numpy.array(array_like, dtype=None)[source]#

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.array.html

aerosandbox.numpy.length(array)[source]#

Returns the length of an 1D-array-like object. An extension of len() with slightly different functionality. :param array:

Returns:

Return type:

int

aerosandbox.numpy.finite_difference_coefficients(x, x0=0, derivative_degree=1)[source]#

Computes the weights (coefficients) in compact finite differece formulas for any order of derivative and to any order of accuracy on one-dimensional grids with arbitrary spacing.

(Wording above is taken from the paper below, as are docstrings for parameters.)

Modified from an implementation of:

Fornberg, Bengt, “Generation of Finite Difference Formulas on Arbitrarily Spaced Grids”. Oct. 1988. Mathematics of Computation, Volume 51, Number 184, pages 699-706.

PDF: https://www.ams.org/journals/mcom/1988-51-184/S0025-5718-1988-0935077-0/S0025-5718-1988-0935077-0.pdf

More detail: https://en.wikipedia.org/wiki/Finite_difference_coefficient

Parameters:
  • derivative_degree (int) – The degree of the derivative that you are interested in obtaining. (denoted “M” in the

  • paper)

  • x (numpy.ndarray) – The grid points (not necessarily uniform or in order) that you want to obtain weights for. You must

  • in (provide at least as many grid points as the degree of the derivative that you're interested) –

    The order of accuracy of your derivative depends in part on the number of grid points that you provide. Specifically:

    order_of_accuracy = n_grid_points - derivative_degree

    (This is in general; can be higher in special cases.)

    For example, if you’re evaluating a second derivative and you provide three grid points, you’ll have a first-order-accurate answer.

    (x is denoted “alpha” in the paper)

  • 1. (plus) –

    The order of accuracy of your derivative depends in part on the number of grid points that you provide. Specifically:

    order_of_accuracy = n_grid_points - derivative_degree

    (This is in general; can be higher in special cases.)

    For example, if you’re evaluating a second derivative and you provide three grid points, you’ll have a first-order-accurate answer.

    (x is denoted “alpha” in the paper)

  • x0 (float) – The location that you are interested in obtaining a derivative at. This need not be on a grid point.

Return type:

numpy.ndarray

Complexity is O(derivative_degree * len(x) ^ 2)

Returns: A 1D ndarray corresponding to the coefficients that should be placed on each grid point. In other words, the approximate derivative at x0 is the dot product of coefficients and the function values at each of the grid points x.

aerosandbox.numpy.quad(func, a, b, full_output=False, variable_of_integration=None)[source]#
Parameters:
  • func (Union[Callable, casadi.MX]) –

  • a (float) –

  • b (float) –

  • full_output (bool) –

  • variable_of_integration (casadi.MX) –

Return type:

Union[Tuple[float, float], Tuple[float, float, dict]]

aerosandbox.numpy.solve_ivp(fun, t_span, y0, method='RK45', t_eval=None, dense_output=False, events=None, vectorized=False, args=None, t_variable=None, y_variables=None, **options)[source]#
Parameters:
  • fun (Union[Callable, casadi.MX]) –

  • t_span (Tuple[float, float]) –

  • y0 (Union[aerosandbox.numpy.ndarray, casadi.MX]) –

  • method (str) –

  • t_eval (Union[aerosandbox.numpy.ndarray, casadi.MX]) –

  • dense_output (bool) –

  • events (Union[Callable, List[Callable]]) –

  • vectorized (bool) –

  • args (Optional[Tuple]) –

  • t_variable (casadi.MX) –

  • y_variables (Union[casadi.MX, Tuple[casadi.MX]]) –

aerosandbox.numpy.lotkavolterra_func(t, z)[source]#
aerosandbox.numpy.is_casadi_type(object, recursive=True)[source]#

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type.

Parameters:
  • object (Any) – The object to evaluate.

  • recursive (bool) – If the object is a list or tuple, recursively iterate through every subelement. If any of the

  • subelements (at any depth) –

Return type:

bool

Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type.

aerosandbox.numpy.array(array_like, dtype=None)[source]#

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.array.html

aerosandbox.numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None)[source]#

Return an array of zeros with the same shape and type as a given array.

aerosandbox.numpy.where(condition, value_if_true, value_if_false)[source]#

Return elements chosen from x or y depending on condition.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.where.html

aerosandbox.numpy.all(a)[source]#

Test whether all array elements along a given axis evaluate to True.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.all.html

aerosandbox.numpy.any(a)[source]#

Test whether any array element along a given axis evaluates to True.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.any.html

aerosandbox.numpy.logical_or(x1, x2)[source]#

Compute the truth value of x1 OR x2 element-wise.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.logical_or.html

aerosandbox.numpy.interp(x, xp, fp, left=None, right=None, period=None)[source]#

One-dimensional linear interpolation, analogous to numpy.interp().

Returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.interp.html

Specific notes: xp is assumed to be sorted.

aerosandbox.numpy.is_data_structured(x_data_coordinates, y_data_structured)[source]#

Determines if the shapes of a given dataset are consistent with “structured” (i.e. gridded) data.

For this to evaluate True, the inputs should be:

x_data_coordinates: A tuple or list of 1D ndarrays that represent coordinates along each axis of a N-dimensional hypercube.

y_data_structured: The values of some scalar defined on that N-dimensional hypercube, expressed as an N-dimesional array. In other words, y_data_structured is evaluated at np.meshgrid(*x_data_coordinates, indexing=”ij”).

Returns: Boolean of whether the above description is true.

Parameters:
  • x_data_coordinates (Tuple[numpy.ndarray]) –

  • y_data_structured (numpy.ndarray) –

Return type:

bool

aerosandbox.numpy.interpn(points, values, xi, method='linear', bounds_error=True, fill_value=_onp.nan)[source]#

Performs multidimensional interpolation on regular grids. Analogue to scipy.interpolate.interpn().

See syntax here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interpn.html

Parameters:
  • points (Tuple[numpy.ndarray]) – The points defining the regular grid in n dimensions. Tuple of coordinates of each axis. Shapes (m1,

  • ) (mn,) –

  • ... (mn,) –

  • values (numpy.ndarray) –

  • xi (numpy.ndarray) –

  • method (str) –

Return type:

numpy.ndarray

:param : :type : mn, :param values: The data on the regular grid in n dimensions. Shape (m1, …, mn) :param xi: The coordinates to sample the gridded data at. (…, ndim) :param method: The method of interpolation to perform. one of:

  • “bspline” (Note: differentiable and suitable for optimization - made of piecewise-cubics. For other

applications, other interpolators may be faster. Not monotonicity-preserving - may overshoot.)

  • “linear” (Note: differentiable, but not suitable for use in optimization w/o subgradient treatment due

to C1-discontinuity)

  • “nearest” (Note: NOT differentiable, don’t use in optimization. Fast.)

Parameters:
  • bounds_error – If True, when interpolated values are requested outside of the domain of the input data,

  • False (a ValueError is raised. If) –

  • used. (then fill_value is) –

  • fill_value – If provided, the value to use for points outside of the interpolation domain. If None,

  • extrapolated. (values outside the domain are) –

  • points (Tuple[numpy.ndarray]) –

  • values (numpy.ndarray) –

  • xi (numpy.ndarray) –

  • method (str) –

Return type:

numpy.ndarray

Returns: Interpolated values at input coordinates.

aerosandbox.numpy.is_casadi_type(object, recursive=True)[source]#

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type.

Parameters:
  • object (Any) – The object to evaluate.

  • recursive (bool) – If the object is a list or tuple, recursively iterate through every subelement. If any of the

  • subelements (at any depth) –

Return type:

bool

Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type.

aerosandbox.numpy.dot(a, b, manual=False)[source]#

Dot product of two arrays.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.dot.html

aerosandbox.numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None, manual=False)[source]#

Return the cross product of two (arrays of) vectors.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.cross.html

aerosandbox.numpy.transpose(a, axes=None)[source]#

Reverse or permute the axes of an array; returns the modified array.

For an array a with two axes, transpose(a) gives the matrix transpose.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.transpose.html

aerosandbox.numpy.is_casadi_type(object, recursive=True)[source]#

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type.

Parameters:
  • object (Any) – The object to evaluate.

  • recursive (bool) – If the object is a list or tuple, recursively iterate through every subelement. If any of the

  • subelements (at any depth) –

Return type:

bool

Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type.

aerosandbox.numpy.clip(x, min, max)[source]#

Clip a value to a range. :param x: Value to clip. :param min: Minimum value to clip to. :param max: Maximum value to clip to.

Returns:

aerosandbox.numpy.logical_and(x1, x2)[source]#

Compute the truth value of x1 AND x2 element-wise.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.logical_and.html

aerosandbox.numpy.logical_or(x1, x2)[source]#

Compute the truth value of x1 OR x2 element-wise.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.logical_or.html

aerosandbox.numpy.logical_not(x)[source]#

Compute the truth value of NOT x element-wise.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.logical_not.html

aerosandbox.numpy.all(a)[source]#

Test whether all array elements along a given axis evaluate to True.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.all.html

aerosandbox.numpy.any(a)[source]#

Test whether any array element along a given axis evaluates to True.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.any.html

aerosandbox.numpy.array(array_like, dtype=None)[source]#

Initializes a new array. Creates a NumPy array if possible; if not, creates a CasADi array.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.array.html

aerosandbox.numpy.rotation_matrix_2D(angle, as_array=True)[source]#

Gives the 2D rotation matrix associated with a counterclockwise rotation about an angle. :param angle: Angle by which to rotate. Given in radians. :param as_array: Determines whether to return an array-like or just a simple list of lists.

Returns: The 2D rotation matrix

Parameters:

as_array (bool) –

aerosandbox.numpy.rotation_matrix_3D(angle, axis, as_array=True, axis_already_normalized=False)[source]#

Yields the rotation matrix that corresponds to a rotation by a specified amount about a given axis.

An implementation of https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle

Parameters:
  • angle (Union[float, numpy.ndarray]) – The angle to rotate by. [radians]

  • rule. (Direction of rotation corresponds to the right-hand) –

  • vectorized. (Can be) –

  • axis (Union[numpy.ndarray, List, str]) – The axis to rotate about. [ndarray]

  • x-components (Can be vectorized; be sure axis[0] yields all the) –

  • etc.

  • as_array (bool) –

    boolean, returns a 3x3 array-like if True, and a list-of-lists otherwise.

    If you are intending to use this function vectorized, it is recommended you flag this False. (Or test before proceeding.)

  • axis_already_normalized (bool) – boolean, skips axis normalization for speed if you flag this true.

Returns:

The rotation matrix, with type according to the parameter as_array.

aerosandbox.numpy.rotation_matrix_from_euler_angles(roll_angle=0, pitch_angle=0, yaw_angle=0, as_array=True)[source]#

Yields the rotation matrix that corresponds to a given Euler angle rotation.

Note: This uses the standard (yaw, pitch, roll) Euler angle rotation, where: * First, a rotation about x is applied (roll) * Second, a rotation about y is applied (pitch) * Third, a rotation about z is applied (yaw)

In other words: R = R_z(yaw) @ R_y(pitch) @ R_x(roll).

Note: To use this, pre-multiply your vector to go from body axes to earth axes.
Example:
>>> vector_earth = rotation_matrix_from_euler_angles(np.pi / 4, np.pi / 4, np.pi / 4) @ vector_body

See notes: http://planning.cs.uiuc.edu/node102.html

Parameters:
  • roll_angle (Union[float, numpy.ndarray]) – The roll angle, which is a rotation about the x-axis. [radians]

  • pitch_angle (Union[float, numpy.ndarray]) – The pitch angle, which is a rotation about the y-axis. [radians]

  • yaw_angle (Union[float, numpy.ndarray]) – The yaw angle, which is a rotation about the z-axis. [radians]

  • as_array (bool) – If True, returns a 3x3 array-like. If False, returns a list-of-lists.

Returns:

aerosandbox.numpy.is_valid_rotation_matrix(a, tol=1e-09)[source]#

Returns a boolean of whether the given matrix satisfies the properties of a rotation matrix.

Specifically, tests for:
  • Volume-preserving

  • Handedness of output reference frame

  • Orthogonality of output reference frame

Parameters:
  • a (numpy.ndarray) – The array-like to be tested

  • tol – A tolerance to use for truthiness; accounts for floating-point error.

Return type:

bool

Returns: A boolean of whether the array-like is a valid rotation matrix.

aerosandbox.numpy.is_casadi_type(object, recursive=True)[source]#

Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type.

Parameters:
  • object (Any) – The object to evaluate.

  • recursive (bool) – If the object is a list or tuple, recursively iterate through every subelement. If any of the

  • subelements (at any depth) –

Return type:

bool

Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type.

aerosandbox.numpy.linspace(start=0.0, stop=1.0, num=50)[source]#

Returns evenly spaced numbers over a specified interval.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.linspace.html

Parameters:
  • start (float) –

  • stop (float) –

  • num (int) –

aerosandbox.numpy.cosspace(start=0.0, stop=1.0, num=50)[source]#

Makes a cosine-spaced vector.

Cosine spacing is useful because these correspond to Chebyshev nodes: https://en.wikipedia.org/wiki/Chebyshev_nodes

To learn more about cosine spacing, see this: https://youtu.be/VSvsVgGbN7I

Parameters:
  • start (float) – Value to start at.

  • end – Value to end at.

  • num (int) – Number of points in the vector.

  • stop (float) –

aerosandbox.numpy.sinspace(start=0.0, stop=1.0, num=50, reverse_spacing=False)[source]#

Makes a sine-spaced vector. By default, bunches points near the start.

Sine spacing is exactly identical to half of a cosine-spaced distrubution, in terms of relative separations.

To learn more about sine spacing and cosine spacing, see this: https://youtu.be/VSvsVgGbN7I

Parameters:
  • start (float) – Value to start at.

  • end – Value to end at.

  • num (int) – Number of points in the vector.

  • reverse_spacing (bool) – Does negative-sine spacing. In other words, if this is True, the points will be bunched near

  • start. (the stop rather than at the) –

  • stop (float) –

Points are bunched up near the start of the interval by default. To reverse this, use the reverse_spacing parameter.

aerosandbox.numpy.logspace(start=0.0, stop=1.0, num=50)[source]#

Return numbers spaced evenly on a log scale.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.logspace.html

Parameters:
  • start (float) –

  • stop (float) –

  • num (int) –

aerosandbox.numpy.geomspace(start=1.0, stop=10.0, num=50)[source]#

Return numbers spaced evenly on a log scale (a geometric progression).

This is similar to logspace, but with endpoints specified directly.

See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.geomspace.html

Parameters:
  • start (float) –

  • stop (float) –

  • num (int) –

aerosandbox.numpy.softmax(*args, softness=None, hardness=None)[source]#

An element-wise softmax between two or more arrays. Also referred to as the logsumexp() function.

Useful for optimization because it’s differentiable and preserves convexity!

Great writeup by John D Cook here:

https://www.johndcook.com/soft_maximum.pdf

Notes: Can provide either hardness or softness, not both. These are the inverse of each other. If neither is provided, hardness is set to 1.

Parameters:
  • *args (Union[float, aerosandbox.numpy.ndarray]) – Provide any number of arguments as values to take the softmax of.

  • hardness (float) – Hardness parameter. Higher values make this closer to max(x1, x2).

  • softness (float) –

    Softness parameter. (Inverse of hardness.) Lower values make this closer to max(x1, x2).

    • Setting softness is particularly useful, because it has the same units as each of the function’s

    inputs. For example, if you’re taking the softmax of two values that are lengths in units of meters, then softness is also in units of meters. In this case, softness has the rough meaning of “an amount of discrepancy between the input values that would be considered physically significant”.

Returns:

Soft maximum of the supplied values.

Return type:

Union[float, aerosandbox.numpy.ndarray]

aerosandbox.numpy.softmin(*args, softness=None, hardness=None)[source]#

An element-wise softmin between two or more arrays. Related to the logsumexp() function.

Useful for optimization because it’s differentiable and preserves convexity!

Great writeup by John D Cook here:

https://www.johndcook.com/soft_maximum.pdf

Notes: Can provide either hardness or softness, not both. These are the inverse of each other. If neither is provided, hardness is set to 1.

Parameters:
  • *args (Union[float, aerosandbox.numpy.ndarray]) – Provide any number of arguments as values to take the softmin of.

  • hardness (float) – Hardness parameter. Higher values make this closer to min(x1, x2).

  • softness (float) –

    Softness parameter. (Inverse of hardness.) Lower values make this closer to min(x1, x2).

    • Setting softness is particularly useful, because it has the same units as each of the function’s

    inputs. For example, if you’re taking the softmin of two values that are lengths in units of meters, then softness is also in units of meters. In this case, softness has the rough meaning of “an amount of discrepancy between the input values that would be considered physically significant”.

Returns:

Soft minimum of the supplied values.

Return type:

Union[float, aerosandbox.numpy.ndarray]

aerosandbox.numpy.softmax_scalefree(*args, relative_softness=None, relative_hardness=None)[source]#
Parameters:
  • args (Union[float, aerosandbox.numpy.ndarray]) –

  • relative_softness (float) –

  • relative_hardness (float) –

Return type:

Union[float, aerosandbox.numpy.ndarray]

aerosandbox.numpy.softmin_scalefree(*args, relative_softness=None, relative_hardness=None)[source]#
Parameters:
  • args (Union[float, aerosandbox.numpy.ndarray]) –

  • relative_softness (float) –

  • relative_hardness (float) –

Return type:

Union[float, aerosandbox.numpy.ndarray]

aerosandbox.numpy.softplus(x, beta=1, threshold=40)[source]#

A smooth approximation of the ReLU function, applied elementwise to an array x.

Softplus(x) = 1/beta * log(1 + exp(beta * x))

Often used as an activation function in neural networks.

Parameters:
  • x (Union[float, aerosandbox.numpy.ndarray]) – The input

  • beta – A parameter that controls the “softness” of the function. Higher values of beta make the function approach ReLU.

  • threshold – Values above this threshold are approximated as linear.

Returns: The value of the softplus function.

aerosandbox.numpy.sigmoid(x, sigmoid_type='tanh', normalization_range=(0, 1))[source]#
A sigmoid function. From Wikipedia (https://en.wikipedia.org/wiki/Sigmoid_function):

A sigmoid function is a mathematical function having a characteristic “S”-shaped curve or sigmoid curve.

Parameters:
  • x – The input

  • sigmoid_type (str) – Type of sigmoid function to use [str]. Can be one of: * “tanh” or “logistic” (same thing) * “arctan” * “polynomial”

  • normalization_type

    Range in which to normalize the sigmoid, shorthanded here in the documentation as “N”. This parameter is given as a two-element tuple (min, max).

    After normalization:
    >>> sigmoid(-Inf) == normalization_range[0]
    >>> sigmoid(Inf) == normalization_range[1]
    
    • In the special case of N = (0, 1):
      >>> sigmoid(-Inf) == 0
      >>> sigmoid(Inf) == 1
      >>> sigmoid(0) == 0.5
      >>> d(sigmoid)/dx at x=0 == 0.5
      
    • In the special case of N = (-1, 1):
      >>> sigmoid(-Inf) == -1
      >>> sigmoid(Inf) == 1
      >>> sigmoid(0) == 0
      >>> d(sigmoid)/dx at x=0 == 1
      

  • normalization_range (Tuple[Union[float, int], Union[float, int]]) –

Returns: The value of the sigmoid.

aerosandbox.numpy.swish(x, beta=1)[source]#

A smooth approximation of the ReLU function, applied elementwise to an array x.

Swish(x) = x / (1 + exp(-beta * x)) = x * logistic(x) = x * (0.5 + 0.5 * tanh(x/2))

Often used as an activation function in neural networks.

Parameters:
  • x – The input

  • beta – A parameter that controls the “softness” of the function. Higher values of beta make the function approach ReLU.

Returns: The value of the swish function.

aerosandbox.numpy.blend(switch, value_switch_high, value_switch_low)[source]#

Smoothly blends between two values on the basis of some switch function.

This function is similar in usage to numpy.where (documented here: https://numpy.org/doc/stable/reference/generated/numpy.where.html) , except that instead of using a boolean as to switch between the two values, a float is used to smoothly transition between the two in a differentiable manner.

Before using this function, be sure to understand the difference between this and smoothmax(), and choose the correct one.

Parameters:
  • switch (float) – A value that acts as a “switch” between the two values [float]. If switch is -Inf, value_switch_low is returned. If switch is Inf, value_switch_high is returned. If switch is 0, the mean of value_switch_low and value_switch_high is returned. If switch is 1, the return value is roughly (0.88 * value_switch_high + 0.12 * value_switch_low). If switch is -1, the return value is roughly (0.88 * value_switch_low + 0.12 * value_switch_high).

  • value_switch_high – Value to be returned when switch is high. Can be a float or an array.

  • value_switch_low – Value to be returned when switch is low. Can be a float or an array.

Returns: A value that is a blend between value_switch_low and value_switch_high, with the weighting dependent

on the value of the ‘switch’ parameter.

aerosandbox.numpy._deg2rad[source]#
aerosandbox.numpy._rad2deg[source]#
aerosandbox.numpy.degrees(x)[source]#

Converts an input x from radians to degrees

aerosandbox.numpy.radians(x)[source]#

Converts an input x from degrees to radians

aerosandbox.numpy.sind(x)[source]#

Returns the sin of an angle x, given in degrees

aerosandbox.numpy.cosd(x)[source]#

Returns the cos of an angle x, given in degrees

aerosandbox.numpy.tand(x)[source]#

Returns the tangent of an angle x, given in degrees

aerosandbox.numpy.arcsind(x)[source]#

Returns the arcsin of an x, in degrees

aerosandbox.numpy.arccosd(x)[source]#

Returns the arccos of an x, in degrees

aerosandbox.numpy.arctan2d(y, x)[source]#

Returns the angle associated with arctan(y, x), in degrees