Numeric
/numarray
Mapping
How do Interactive Data Language (IDL)
commands translate into Numeric
/numarray
functions?
Advertisement: In 2012, I published the book A HandsOn Introduction to Using Python in the Atmospheric and Oceanic Sciences. If you're new to Python, you may find it useful!
Below is a summary mapping between IDL and
numarray
.
Most of the mapping also applies to
Numeric
and NumPy.
The vast majority of content on this page is a copy of the
IDL to numarray mapping
page (5 Nov 2002 version, copied 5 Jun 2004) from the
Space Telescope Science Institute
(STScI, the developers of numarray
).
Since they didn't seem to be adding anything more to the page,
I decided to make a copy (it's in the public domain) and
add additional topics.
NumPy for IDL Users provides a more current
mapping summary than this sheet.
In some cases, there are multiple Python equivalents for a given
IDL construct. I also mention a few MLab
and MA
functions, since both modules are distributed with Numeric
and numarray
.
Some of the descriptions are incomplete.
I have not checked to see whether the descriptions correspond with
the most recent versions of either IDL or numarray
and
there may be some errors.
IDL  Python Equivalent 

.run <filename>  import <module> (.py assumed)reload(module) to recompile/reexecute 
@filename 
execfile(filename) 
exit 
<EOF> (e.g. ctrlD) 
uparrow (cmd recall)  uparrow 
IDL  Python Equivalent 

< , > (clipping) 
Currently no operator equivalent. The functional equivalents are shown in the following examples. 
a < 100. 
choose(greater(a, 100.),(a, 100.)) 
a > 100. 
choose(less(a, 100.),(a, 100.)) or for both
clip(a, min_val, max_val) .
[For python 2.1 or greater, the following should work:
choose(a < 100, (a, 100.)) ]. 
mod 
% 
# (matrix multiply) 
multiply.outer() is equivalent for some applications,
matrixmultiply() for others. 
+ ,  , * , / 
Same 
^ 
** 
Note: The Python Boolean operators (e.g. and, or)
do not operate
elementwise on arrays like they do in IDL.
For elementwise comparison, use the logical_
prefixed
ufuncs
described below.
IDL  Python Equivalent 

and 
and , logical_and() 
not 
not , logical_not() 
or 
or , logical_or() 
xor 
xor , logical_xor() 
IDL  Python Equivalent 

and 
& (bitwise and) 
or 
 (bitwise or) 
xor 
^ (bitwise xor) 
not 
~ (bitwise not) 
ishft(a, 1) 
a << 1 (left shift) 
ishft(a,1) 
a >> 1 (right shift) 
The Python functional forms below are array ufuncs
.
The operator versions only work in Python 2.1.
Use the functional form for Python 2.0
IDL  Python Equivalent 

eq 
== , equal() 
ge 
>= , greater_equal() 
gt 
> , greater() 
le 
<= , less_equal() 
lt 
< , less() 
ne 
!= , not_equal() 
Note: Order of indices in Numeric
/numarray
is opposite of IDL! For a 2dimension array:
a[i,j]
in IDL is equivalent to
a[j,i]
in PythonFor a given dimension, slices may be specified:
IDL  Python Equivalent 

i:j 
i:j (except for different interpretation for
j ) 
i:* 
i: 
*:i 
:i 
a[i,*] 
a[:,i] 
<No IDL equiv.>  i:j:s (stride, take every s 'th element) 
<No IDL equiv.>  i:j 
<No IDL equiv.>  Empty arrays and slices are permitted (e.g. a[0:0] is
an array of length 0) 
<No IDL equiv.>  i (indexing from end of array) 
<No IDL equiv.>  ... (fill in unspecified dimensions) 
<No IDL equiv.>  NewAxis (add new axis to array shape as part of subscript to match shapes) 
Note: Slicing in Python generally does not create a new array, but
instead creates a virtual array (noncontiguous in memory)
that points to the same memory locations as the original
array, e.g. b = a[0:10:2]
followed by
b[0] = 5
will change a[0]
to 5 as well. You must use
b = copy(a[0:10:2])
to get a complete new copy of the data.
This may sound confusing, but it can be very useful if you are working with big arrays and need to limit memory use. Unlike IDL, every new slice in Python does not generate a new array in memory.
IDL  Python Equivalent 

newarr = arrayvar[indexarr] 
newarr = arrayvar[indexarr] 
arrayvar[indexarr] = valuearr 
arrayvar[indexarr] = valuearr 
The syntax of using an index array for specifying subarrays of 1D arrays is currently the same in Python and IDL. For nD cases the syntax is different enough such that a brief description is not possible. Originally IDL used parenthesis instead of square brackets for array indexing.
IDL  Python Equivalent 

fltarr() dblarr() complexarr() intarr() longarr() bytarr() make_array() 
array(seq, [type]) to create
from existing sequence;
zeros(shape, [type]) to create array filled with 0's;
ones(shape, [type]) to create array filled with 1's. 
strarr() 
Lists and tuples are more appropriate
(chararray does exist to handle
large fixed length strings). 
findgen() dindgen() indgen() lindgen() 
arrayrange(size, [type]) ,
a.k.a. arange() , for vectors 
sindgen() 
No direct equivalent, but trivial to duplicate. 
replicate() 
repeat() (more general) 
IDL  Python Equivalent 

byte(arrayvar) fix(arrayvar) long(arrayvar) float(arrayvar) double(arrayvar) complex(arrayvar) 
arrayvar.astype(typecode) 
IDL  Python Equivalent 

reform() 
reshape() Also arrayvar.flat() ,
ravel() , or changing the shape attribute.
The MLab module also has the squeeze
function that removes degenerate dimensions. 
sort() 
argsort() , i.e. indices needed to sort 
arrayvar[sort(arrayvar)] 
sort(arrayvar) ,
i.e. sorted array 
max() 
max() [ufunc]
(also argmax() and
maximum in the MA module). 
min() 
min() [ufunc] (also argmin()
and maximum in the MA module).) 
where() 
nonzero() ; see
this article for details. 
arrayvar[where(condition)] 
compress(condition, arrayvar) 
transpose() 
transpose() 
[a, b] (array concatenation) 
concatenate() 
In general, Python array manipulation functions are far more general and powerful than IDL manipulation functions, but also harder to understand. These include:
choose() take() repeat() resize() 
nonzero() compress() reshape() resize() 
concatenation() transpose() diagonal() identity() 
argmax() searchsorted() sort() argsort() 
The Python "list comprehensions" construct
provides a terse
syntax for creating lists, in conjunction with other
functions/operations. For example:
[len(i) for i in strarr]
will produce the list
[1,3]
if strarr
equals
["a","abc"]
(this is a handy way of creating an iterable
list of lengths).
IDL: If size or shape mismatches, result is coerced to smaller of two and shape of one.
Python: Shape or mismatch results in error if one array not a "subset" of other's shape. If subset, "broadcasting" is used.
IDL  Python Equivalent 

byte  Int8 
UInt8  
int  Int16 
long  Int32 
float  Float32 
double  Float64 
complex  Complex64 Complex128 
The Python functional forms below are array ufuncs
.
IDL  Python Equivalent 

abs() 
absolute() , fabs() 
acos()  arccos() 
alog()  log() 
alog10()  log10() 
asin()  arcsin() 
atan()  arctan() 
atan(y,x)  arctan2() 
ceil()  ceil() 
conj()  conjugate() 
cos()  cos() 
cosh()  cosh() 
exp()  exp() 
floor()  floor() 
imaginary() 
complexvar.imag (.real
for real component) Python 2.2 only;
complexvar.getimag() (complexvar.getreal()
for real) 
invert()  Matrix module 
ishft() 
right_shift() , left_shift() 
round()  ??? 
sin()  sin() 
sinh()  sinh() 
sqrt()  sqrt() 
tan()  tan() 
tanh()  tanh() 
Python ufuncs
, with no exact IDL equivalents:
IDL  Python Equivalent  

Comparison:  
greater() , greater_equal() ,less() , less_equal() ,equal() , not_equal() ,bitwise_and() ,
bitwise_or() , bitwise_xor() ,logical_and() ,
logical_or() , logical_xor() ,add , subtract ,
multiply ,divide , remainder ,power , negative , hypot 

 
For binary ufuncs ,
the following methods exist:  

Other functions:
IDL  Python Equivalent 

fft() 
fft() [FFT module] (not available yet) 
convol() 
Not available yet. 
randomu() 
random() , uniform() .
See module RandomArray . 
randomn() 
RNG module (not available yet) 
reverse(a) (a is
a vector) 
a[::1] 
total() 
sum() 
IDL  Python Equivalent 

execute() 
exec() 
n_elements() 
len() , size() . In Python, the
easiest way to test whether a variable is defined is using
try /except . 
n_params() 
Closest equivalent is len(*args) .
Ability to supply default values for parameters
replaces some uses of n_params() . 
_extra 
**kwargs (see also *args ) 
size() 
shape() , arrayvar.type() 
wait 
time.sleep() 