BlockVector
(class from pyomo.contrib.pynumero.sparse.block_vector
)
- class pyomo.contrib.pynumero.sparse.block_vector.BlockVector(nblocks)[source]
Bases:
BaseBlockVector
,ndarray
Structured vector interface. This interface can be used to perform operations on vectors composed by vectors. For example,
>>> import numpy as np >>> from pyomo.contrib.pynumero.sparse import BlockVector >>> bv = BlockVector(3) >>> v0 = np.ones(3) >>> v1 = v0*2 >>> v2 = np.random.normal(size=4) >>> bv.set_block(0, v0) >>> bv.set_block(1, v1) >>> bv.set_block(2, v2) >>> bv2 = BlockVector(2) >>> bv2.set_block(0, v0) >>> bv2.set_block(1, bv)
- _brow_lengths
1D-Array of size nblocks that specifies the length of each entry in the block vector
- Type:
- _undefined_brows
A set of block indices for which the blocks are still None (i.e., the dimensions have not yet ben set). Operations with BlockVectors require all entries to be different than None.
- Type:
- Parameters:
nblocks (int) – The number of blocks in the BlockVector
Methods
__init__
(nblocks)all
([axis, out, keepdims])Returns True if all elements evaluate to True.
any
([axis, out, keepdims])Returns True if any element evaluate to True.
argmax
([axis, out])Returns the index of the larges element.
argmin
([axis, out])Returns the index of the smallest element.
argpartition
(kth[, axis, kind, order])Returns the indices that would partition this array.
argsort
([axis, kind, order])Returns the indices that would sort this array.
astype
(dtype[, order, casting, subok, copy])Copy of the array, cast to a specified type
block_sizes
([copy])Returns 1D-Array with sizes of individual blocks in this BlockVector
byteswap
([inplace])Swap the bytes of the array elements
choose
(choices[, out, mode])Use an index array to construct a new array from a set of choices.
clip
([min, max, out])Return BlockVector whose values are limited to [min, max].
clone
([value, copy])Returns a copy of this BlockVector
compress
(condition[, axis, out])Return selected slices of each subblock.
conj
()Complex-conjugate all elements.
Complex-conjugate all elements.
copy
([order])Returns a copy of the BlockVector
Returns a copy of the BlockVector structure filled with zeros
copyfrom
(other)Copy entries of other vector into this vector
copyto
(other)Copy entries of this BlockVector into other
cumprod
([axis, dtype, out])Returns the cumulative product of the elements along the given axis.
cumsum
([axis, dtype, out])Returns the cumulative sum of the elements along the given axis.
diagonal
([offset, axis1, axis2])Return specified diagonals.
dot
(other[, out])Returns dot product
dump
(file)Dump a pickle of the array to the specified file.
dumps
()Returns the pickle of the array as a string.
fill
(value)Fills the BlockVector with a scalar value.
flatten
([order])Converts the BlockVector to a NumPy array.
get_block
(key)Access a block.
get_block_size
(ndx)getfield
(dtype[, offset])Returns a field of the given array as a certain type.
is_block_defined
(ndx)item
(*args)Copy an element of an array to a standard Python scalar and return it.
itemset
(*args)max
([axis, out, keepdims])Returns the largest value stored in this BlockVector
mean
([axis, dtype, out, keepdims])Returns the average of all entries in this BlockVector
min
([axis, out, keepdims])Returns the smallest value stored in the vector
newbyteorder
([new_order])nonzero
()Return the indices of the elements that are non-zero.
partition
(kth[, axis, kind, order])Partially sorts the elements in the array in such a way that the value of the element in k-th position is in the position it would be in a sorted array.
pprint
()Prints BlockVector in pretty format
prod
([axis, dtype, out, keepdims])Returns the product of all entries in this BlockVector
put
(indices, values[, mode])Set
a.flat[n] = values[n]
for all n in indices.ravel
([order])Converts the BlockVector into a NumPy array.
repeat
(repeats[, axis])Repeat elements of an array.
reshape
(shape, /, *[, order, copy])Returns an array containing the same data with a new shape.
resize
(new_shape[, refcheck])Change shape and size of array in-place.
round
([decimals, out])Return BlockVector with each element rounded to the given number of decimals
searchsorted
(v[, side, sorter])Find indices where elements of v should be inserted in a to maintain order.
set_block
(key, value)Set a block.
set_blocks
(blocks)Assigns vectors in blocks
setfield
(val, dtype[, offset])Put a value into a specified place in a field defined by a data-type.
setflags
([write, align, uic])Set array flags WRITEABLE, ALIGNED, WRITEBACKIFCOPY, respectively.
sort
([axis, kind, order])Sort an array in-place.
squeeze
([axis])Remove axes of length one from a.
std
([axis, dtype, out, ddof, keepdims])Returns the standard deviation of the BlockVector elements.
sum
([axis, dtype, out, keepdims])Returns the sum of all entries in this BlockVector
swapaxes
(axis1, axis2)Return a view of the array with axis1 and axis2 interchanged.
take
(indices[, axis, out, mode])Return an array formed from the elements of a at the given indices.
toMPIBlockVector
(rank_ownership, mpi_comm[, ...])Creates a parallel MPIBlockVector from this BlockVector
to_device
tobytes
([order])Construct Python bytes containing the raw data bytes in the array.
tofile
(fid[, sep, format])Writes flat version of BlockVector to a file as text or binary (default).
tolist
()Return the BlockVector flattened as a list.
tostring
([order])A compatibility alias for ~ndarray.tobytes, with exactly the same behavior.
trace
([offset, axis1, axis2, dtype, out])Return the sum along diagonals of the array.
transpose
(*axes)Returns a view of the array with axes transposed.
var
([axis, dtype, out, ddof, keepdims])Returns the variance of the BlockVector elements.
view
([dtype][, type])New view of array with the same data.
Attributes
T
View of the transposed array.
base
Base object if memory is from some other object.
Returns the number of blocks in this BlockVector in a tuple.
ctypes
An object to simplify the interaction of the array with the ctypes module.
data
Python buffer object pointing to the start of the array's data.
device
dtype
Data-type of the array's elements.
flags
Information about the memory layout of the array.
flat
A 1-D iterator over the array.
Indicate if this BlockVector has any none entries.
imag
The imaginary part of the array.
itemsize
Length of one array element in bytes.
mT
View of the matrix transposed array.
Returns the number of blocks.
nbytes
Total bytes consumed by the elements of the array.
Returns dimension of this BlockVector
ptp
real
The real part of the array.
Returns total number of elements in this BlockVector
Returns total number of elements in this BlockVector
strides
Tuple of bytes to step in each dimension when traversing an array.
Member Documentation
- astype(dtype, order='K', casting='unsafe', subok=True, copy=True)[source]
Copy of the array, cast to a specified type
- block_sizes(copy=True)[source]
Returns 1D-Array with sizes of individual blocks in this BlockVector
- clip(min=None, max=None, out=None)[source]
Return BlockVector whose values are limited to [min, max]. One of max or min must be given.
- Parameters:
min (scalar_like, optional) – Minimum value. If None, clipping is not performed on lower interval edge.
max (scalar_like, optional) – Maximum value. If None, clipping is not performed on upper interval edge.
- Return type:
- clone(value=None, copy=True)[source]
Returns a copy of this BlockVector
- Parameters:
value (scalar (optional)) – all entries of the cloned vector are set to this value
copy (bool (optional)) – if True makes a deepcopy of each block in this vector. default True
- Return type:
- compress(condition, axis=None, out=None)[source]
Return selected slices of each subblock.
- Parameters:
condition (Array or BlockVector that selects which entries to return.) – Determines to select (evaluate True in condition)
- Return type:
- copyfrom(other)[source]
Copy entries of other vector into this vector
- Parameters:
other (BlockVector or numpy.ndarray) – vector to be copied to this BlockVector
- Return type:
None
- copyto(other)[source]
Copy entries of this BlockVector into other
- Parameters:
other (BlockVector or numpy.ndarray)
- Return type:
None
- cumprod(axis=None, dtype=None, out=None)[source]
Returns the cumulative product of the elements along the given axis.
- cumsum(axis=None, dtype=None, out=None)[source]
Returns the cumulative sum of the elements along the given axis.
- dot(other, out=None)[source]
Returns dot product
- Parameters:
other (ndarray or BlockVector)
- Return type:
- fill(value)[source]
Fills the BlockVector with a scalar value.
- Parameters:
value (scalar) – All elements in the vector will be assigned this value
- Return type:
None
- flatten(order='C')[source]
Converts the BlockVector to a NumPy array. This will also call flatten on the underlying NumPy arrays in the BlockVector.
- Parameters:
order (str: {C, F, A, K}, optional) – See NumPy array documentation.
- Returns:
flat_array – The NumPy array resulting from concatenating all of the blocks
- Return type:
- get_block(key)[source]
Access a block.
- Parameters:
key (int) – This is the block index
- Returns:
block – The block corresponding to the index key.
- Return type:
np.ndarray or BlockVector
- max(axis=None, out=None, keepdims=False)[source]
Returns the largest value stored in this BlockVector
- mean(axis=None, dtype=None, out=None, keepdims=False)[source]
Returns the average of all entries in this BlockVector
- prod(axis=None, dtype=None, out=None, keepdims=False)[source]
Returns the product of all entries in this BlockVector
- ravel(order='C')[source]
Converts the BlockVector into a NumPy array. Note that ravel is also called on all of the NumPy arrays in the BlockVector before concatenating them.
- Parameters:
order (str) – See NumPy documentation.
- Returns:
res
- Return type:
- round(decimals=0, out=None)[source]
Return BlockVector with each element rounded to the given number of decimals
- set_block(key, value)[source]
Set a block. The value can be a NumPy array or another BlockVector.
- Parameters:
key (int) – This is the block index
value – This is the block. It can be a NumPy array or another BlockVector.
- set_blocks(blocks)[source]
Assigns vectors in blocks
- Parameters:
blocks (list) – list of numpy.ndarrays and/or BlockVectors
- Return type:
None
- std(axis=None, dtype=None, out=None, ddof=0, keepdims=False)[source]
Returns the standard deviation of the BlockVector elements.
- sum(axis=None, dtype=None, out=None, keepdims=False)[source]
Returns the sum of all entries in this BlockVector
- toMPIBlockVector(rank_ownership, mpi_comm, assert_correct_owners=False)[source]
Creates a parallel MPIBlockVector from this BlockVector
- Parameters:
rank_ownership (array_like) – Array_like of size nblocks. Each entry defines ownership of each block. There are two types of ownership. Block that are owned by all processor, and blocks owned by a single processor. If a block is owned by all processors then its ownership is -1. Otherwise, if a block is owned by a single processor, then its ownership is equal to the rank of the processor.
mpi_comm (MPI communicator) – An MPI communicator. Tyically MPI.COMM_WORLD
- tofile(fid, sep='', format='%s')[source]
Writes flat version of BlockVector to a file as text or binary (default).
- var(axis=None, dtype=None, out=None, ddof=0, keepdims=False)[source]
Returns the variance of the BlockVector elements.
- property bshape
Returns the number of blocks in this BlockVector in a tuple.
- property has_none
Indicate if this BlockVector has any none entries.
- property nblocks
Returns the number of blocks.
- property ndim
Returns dimension of this BlockVector
- property shape
Returns total number of elements in this BlockVector
- property size
Returns total number of elements in this BlockVector