Skip to content

Commit 68eea41

Browse files
committed
Allow numpy arrays for q in ScatteringFactorTable.lookup.
Doing this in C++ layer is much faster than in a Python loop.
1 parent 1ebb4cb commit 68eea41

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

srrealmodule/wrap_ScatteringFactorTable.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
#include <diffpy/srreal/SFTNeutron.hpp>
2828
#include <diffpy/srreal/SFTElectronNumber.hpp>
2929

30+
#include "srreal_numpy_symbol.hpp"
31+
// numpy/arrayobject.h needs to be included after srreal_numpy_symbol.hpp,
32+
// which defines PY_ARRAY_UNIQUE_SYMBOL. NO_IMPORT_ARRAY indicates
33+
// import_array will be called in the extension module initializer.
34+
#define NO_IMPORT_ARRAY
35+
#include <numpy/arrayobject.h>
36+
37+
3038
#include "srreal_converters.hpp"
3139
#include "srreal_pickling.hpp"
3240

@@ -81,8 +89,10 @@ can be redefined using the setCustomAs method.\n\
8189
\n\
8290
smbl -- string symbol for atom, ion or isotope.\n\
8391
Q -- Q value in inverse Angstroms, by default 0.\n\
92+
Q can be either float or NumPy array.\n\
8493
\n\
85-
Return float. No support for Python override.\n\
94+
Return float or NumPy array of the same shape as Q.\n\
95+
No support for Python override.\n\
8696
";
8797

8898
const char* doc_ScatteringFactorTable__standardLookup = "\
@@ -325,6 +335,22 @@ class ScatteringFactorTableWrap :
325335

326336
}; // class ScatteringFactorTableWrap
327337

338+
object lookupnparray(const ScatteringFactorTable& sftb,
339+
std::string smbl, object& qobj)
340+
{
341+
NumPyArray_DoublePtr aa = extractNumPyDoubleArray(qobj);
342+
NumPyArray_DoublePtr bb = createNumPyDoubleArrayLike(aa.first);
343+
double* src = aa.second;
344+
double* last = aa.second + PyArray_Size(aa.first.ptr());
345+
double* dst = bb.second;
346+
for (; src != last; ++src, ++dst)
347+
{
348+
*dst = sftb.lookup(smbl, *src);
349+
}
350+
return bb.first;
351+
}
352+
353+
328354
} // namespace nswrap_ScatteringFactorTable
329355

330356
// Wrapper definition --------------------------------------------------------
@@ -349,6 +375,9 @@ void wrap_ScatteringFactorTable()
349375
&ScatteringFactorTable::radiationType,
350376
return_value_policy<copy_const_reference>(),
351377
doc_ScatteringFactorTable_radiationType)
378+
.def("lookup",
379+
lookupnparray,
380+
(bp::arg("smbl"), bp::arg("qarray")))
352381
.def("lookup",
353382
&ScatteringFactorTable::lookup,
354383
(bp::arg("smbl"), bp::arg("q")=0.0),

0 commit comments

Comments
 (0)