Skip to content

Commit

Permalink
Enhance arrayFromVector as per review
Browse files Browse the repository at this point in the history
  • Loading branch information
r-owen committed Oct 20, 2017
1 parent afc9a74 commit 18183c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 4 additions & 3 deletions include/astshim/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ enum class DataType {
/**
Reshape a vector as a 2-dimensional array that shares the same memory
@warning You must hold onto the original vector until you are done
with the returned array, else the array will be corrupted.
To convert a vector of coefficients to an array of coefficients
for PolyMap or ChebyMap, call this with nAxes = nPoints / width,
where width is the number of elements in each coefficient:
Expand All @@ -88,6 +85,10 @@ width = nOut + 2 for forward coefficients, nIn + 2 for inverse coefficients.
@param[in] nAxes Number of axes per point
@return 2-dimensional array with dimensions (nPts, nAxes)
@throws std::runtime_error if vec length is not a multiple of nAxes
@warning You must hold onto the original vector until you are done
with the returned array, else the array will be corrupted.
(However, the Python version returns a copy, to avoid memory issues.)
@{
*/
ConstArray2D arrayFromVector(std::vector<double> const &vec, int nAxes);
Expand Down
7 changes: 6 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ class TestObject(ObjectTestCase):
def test_arrayFromVector(self):
nAxes = 3
nValues = 5
np.random.seed(1)
dataVec = np.random.rand(nAxes * nValues)
desiredDataArr = dataVec.copy()
desiredDataArr.shape = (nAxes, nValues)
dataArr = ast.arrayFromVector(vec=dataVec, nAxes=nAxes)
assert_equal(dataArr, desiredDataArr)
# make sure dataArr is a deep copy

dataArr2 = ast.arrayFromVector(vec=list(dataVec), nAxes=nAxes)
assert_equal(dataArr2, desiredDataArr)

# make sure dataArr is a deep copy; changing dataVec should not change dataArr
dataVec[0] += 10
assert_equal(dataArr, desiredDataArr)

Expand Down

0 comments on commit 18183c3

Please sign in to comment.