2016-05-30 22:30:45 -07:00
|
|
|
"""
|
2020-01-07 21:31:16 -08:00
|
|
|
Functions for moving between a vector field (list of 3 ndarrays, `[f_x, f_y, f_z]`)
|
|
|
|
and a 1D array representation of that field `[f_x0, f_x1, f_x2,... f_y0,... f_z0,...]`.
|
2017-12-09 18:21:37 -08:00
|
|
|
Vectorized versions of the field use row-major (ie., C-style) ordering.
|
2016-05-30 22:30:45 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
from typing import List
|
|
|
|
import numpy
|
|
|
|
|
2019-11-27 22:59:52 -08:00
|
|
|
from .types import fdfield_t, vfdfield_t
|
2016-05-30 22:30:45 -07:00
|
|
|
|
2019-08-04 13:48:41 -07:00
|
|
|
|
|
|
|
__author__ = 'Jan Petykiewicz'
|
2016-05-30 22:30:45 -07:00
|
|
|
|
|
|
|
|
2019-11-27 22:59:52 -08:00
|
|
|
def vec(f: fdfield_t) -> vfdfield_t:
|
2016-05-30 22:30:45 -07:00
|
|
|
"""
|
|
|
|
Create a 1D ndarray from a 3D vector field which spans a 1-3D region.
|
|
|
|
|
2020-01-07 21:31:16 -08:00
|
|
|
Returns `None` if called with `f=None`.
|
2016-05-30 22:30:45 -07:00
|
|
|
|
2020-01-07 21:31:16 -08:00
|
|
|
Args:
|
|
|
|
f: A vector field, `[f_x, f_y, f_z]` where each `f_` component is a 1- to
|
|
|
|
3-D ndarray (`f_*` should all be the same size). Doesn't fail with `f=None`.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
1D ndarray containing the linearized field (or `None`)
|
2016-05-30 22:30:45 -07:00
|
|
|
"""
|
|
|
|
if numpy.any(numpy.equal(f, None)):
|
|
|
|
return None
|
2019-08-04 13:48:41 -07:00
|
|
|
return numpy.ravel(f, order='C')
|
2016-05-30 22:30:45 -07:00
|
|
|
|
|
|
|
|
2019-11-27 22:59:52 -08:00
|
|
|
def unvec(v: vfdfield_t, shape: numpy.ndarray) -> fdfield_t:
|
2016-05-30 22:30:45 -07:00
|
|
|
"""
|
|
|
|
Perform the inverse of vec(): take a 1D ndarray and output a 3D field
|
2020-01-07 21:31:16 -08:00
|
|
|
of form `[f_x, f_y, f_z]` where each of `f_*` is a len(shape)-dimensional
|
2016-05-30 22:30:45 -07:00
|
|
|
ndarray.
|
|
|
|
|
2020-01-07 21:31:16 -08:00
|
|
|
Returns `None` if called with `v=None`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
v: 1D ndarray representing a 3D vector field of shape shape (or None)
|
|
|
|
shape: shape of the vector field
|
2016-05-30 22:30:45 -07:00
|
|
|
|
2020-01-07 21:31:16 -08:00
|
|
|
Returns:
|
|
|
|
`[f_x, f_y, f_z]` where each `f_` is a `len(shape)` dimensional ndarray (or `None`)
|
2016-05-30 22:30:45 -07:00
|
|
|
"""
|
|
|
|
if numpy.any(numpy.equal(v, None)):
|
|
|
|
return None
|
2019-07-21 22:05:40 -07:00
|
|
|
return v.reshape((3, *shape), order='C')
|
2016-05-30 22:30:45 -07:00
|
|
|
|