# array_utils.py¶

Utils for dealing with arrays.

openmdao.utils.array_utils.abs_complex(x)[source]

Compute the absolute value of a complex-stepped vector.

Rather than taking a Euclidian norm, simply negate the values that are less than zero.

Parameters
xndarray

Input array.

Returns
ndarray

Complex-step absolute value of the array.

openmdao.utils.array_utils.array_connection_compatible(shape1, shape2)[source]

Return True if the two arrays shapes are compatible.

Array shapes are compatible if the underlying data has the same size and is stored in the same contiguous order for the two shapes.

Parameters
shape1tuple of int

Shape of the first array.

shape2tuple of int

Shape of the second array.

Returns
bool

True if the two shapes are compatible for connection, else False.

openmdao.utils.array_utils.array_viz(arr, prob=None, of=None, wrt=None, stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]

Display the structure of a boolean array in a compact form.

If prob, of, and wrt are supplied, print the name of the response alongside each row and print the names of the design vars, aligned with each column, at the bottom.

Parameters
arrndarray

Array being visualized.

probProblem or None

Problem object.

oflist of str or None

Names of response variables used in derivative calculation.

wrtlist of str or None

Names of design variables used in derivative calculation.

streamfile-like

Stream where output will be written.

openmdao.utils.array_utils.convert_neg(arr, dim)[source]

Convert any negative indices into their positive equivalent.

Parameters
arrndarray

Array having negative indices converted.

dimint

Dimension of the array.

Returns
ndarray

The converted array.

openmdao.utils.array_utils.dv_abs_complex(x, x_deriv)[source]

Apply the complex-step derivative of the absolute value function.

Parameters
xndarray

Input array, used for determining which elements to negate.

x_derivndarray

Incominng partial derivative array, may have one additional dimension.

Returns
ndarray

Absolute value applied to x_deriv.

openmdao.utils.array_utils.evenly_distrib_idxs(num_divisions, arr_size)[source]

Return evenly distributed entries for the given array size.

Given a number of divisions and the size of an array, chop the array up into pieces according to number of divisions, keeping the distribution of entries as even as possible.

Parameters
num_divisionsint

Number of parts to divide the array into.

arr_sizeint

Number of entries in the array.

Returns
tuple

a tuple of (sizes, offsets), where sizes and offsets contain values for all divisions.

openmdao.utils.array_utils.get_input_idx_split(full_idxs, inputs, outputs, use_full_cols, is_total)[source]

Split an array of indices into vec outs + ins into two arrays of indices into outs and ins.

Parameters
full_idxsndarray

Indices into the full array (which could be outs + ins or just ins)

inputsVector

Inputs vector.

outputsVector

Outputs vector.

use_full_colsbool

If True, full idxs are into the full outs + ins vector.

is_totalbool

If True, total derivatives are being computed and wrt vector is the outputs vector.

Returns
list of tuples

Each tuple is of the form (array, idxs).

openmdao.utils.array_utils.sizes2offsets(size_array, dtype=<class 'int'>)[source]

For a given array of sizes, return an array of offsets.

Offsets will be computed using a flattened version of size_array and then reshaped to match the shape of size_array.

Parameters
size_arrayndarray

Array of sizes.

dtypetype

numpy dtype of size_array.

Returns
ndarray

Array of offsets.

openmdao.utils.array_utils.sub2full_indices(all_names, matching_names, sizes, idx_map=())[source]

Return the given indices converted into indices into the full vector.

This routine is used to compute how column indices computed during coloring of a subset of the jacobian map to column indices corresponding to the full jacobian.

Parameters
all_namesordered iter of str

An ordered list of variable names containing all variables of the appropriate type.

matching_namesset of str

Subset of all_names that make up the reduced index set.

sizesndarray of int

Array of variable sizes.

idx_mapdict

Mapping of var name to some subset of its full indices.

Returns
ndarray

Full array indices that map to the provided subset of variables.

openmdao.utils.array_utils.take_nth(rank, size, seq)[source]

Iterate returning every nth value.

Return an iterator over the sequence that returns every nth element of seq based on the given rank within a group of the given size. For example, if size = 2, a rank of 0 returns even indexed elements and a rank of 1 returns odd indexed elements.

Parameters
rankint

MPI rank of this process.

sizeint

Size of the array we’re taking nth entries from.

seqiter

Iterator containing the values being returned.

openmdao.utils.array_utils.tile_sparse_jac(data, rows, cols, nrow, ncol, num_nodes)[source]

Assemble arrays necessary to define a COO sparse jacobian for a vectorized component.

These arrays can also be passed to csc_matrix or csr_matrix to create CSC and CSR sparse matrices.

Parameters
datandarray

Array of values

rowsindex array

Array of row indices.

colsindex array

Array of column indices.

nrowint

Number of rows in sub jacobian.

ncolint

Number of columns in sub jacobian.

num_nodesint

Number of vectorized copies to tile.

Returns
ndarray, ndarray, ndarray

Arrays to define a COO sparse jacobian of size num_nodes*nrow by num_nodes*ncol