nws_tools.
nw_zip
(ntw)[source]¶Convert the upper triangular portion of a real symmetric matrix to a vector and vice versa
Parameters: | ntw : NumPy 1d/2d/3d array
|
---|---|
Returns: | nws : NumPy 1d/2d/3d array
|
See also
Notes
Note, that this routine does NOT consider diagonal elements, i.e., only off-diagonal entries will be stored/reconstructed. By design, entries are assumed to be ordered column-wise. Note further, that a symmetric N-by-N matrix contains K = N * (N - 1) / 2 upper triangular elements. Thus, if N = 3 then K = 3 so that any symmetric 3-by-3 array can either represent a symmetric matrix or a set of three 3-element vectors representing upper triangular entries of three different symmetric matrices. In this case, the routine always assumes that the input represents a symmetric matrix and prints a warning message.
Examples
Consider the symmetric matrix mat
>>> mat
array([[ 0., 1., 2., 3.],
[ 1., 0., 4., 5.],
[ 2., 4., 0., 6.],
[ 3., 5., 6., 0.]])
Using nw_zip to compress mat yields the array vec
>>> import nws_tools as nwt
>>> vec = nwt.nw_zip(mat)
>>> vec
array([ 1., 2., 3., 4., 5., 6.])
Now reconstruct mat from vec
>>> nwt.nw_zip(vec)
array([[ 0., 1., 2., 3.],
[ 1., 0., 4., 5.],
[ 2., 4., 0., 6.],
[ 3., 5., 6., 0.]])
Consider a second symmetric matrix mat2
>>> mat2
array([[ 0., 7., 8., 9.],
[ 7., 0., 10., 11.],
[ 8., 10., 0., 12.],
[ 9., 11., 12., 0.]])
Now, collect mat and mat2 in a tensor and use nw_zip to compress it
>>> mats = np.zeros((4,4,2))
>>> mats[:,:,0] = mat
>>> mats[:,:,1] = mat2
>>> vecs = nw_zip(mats)
>>> vecs
array([[ 1., 7.],
[ 2., 8.],
[ 3., 9.],
[ 4., 10.],
[ 5., 11.],
[ 6., 12.]])
Uncompressing vecs yields mats again
>>> (mats == nw_zip(vecs)).min()
True