February 2002
| IDL | Python Equivalent |
|---|---|
| .run |
import reload(module) to recompile/re-execute |
| @"filename" | execfile("filename") |
| exit | |
| up-arrow (cmd recall) | up-arrow |
| <,> (clipping) | Currently no operator equivalent. The functional equivalents are: |
| 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. |
| ^ | ** |
| and | and |
| not | not |
| or | or |
| xor | <None> |
| 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) |
| (array ufuncs) | ||
| eq | == or equal() | |
| ge | >= or greater_equal() | |
| gt | > or greater() | |
| le | <= or less_equal() | |
| lt | < or less() | |
| ne | != or not_equal() | |
| The operator versions only work in Python 2.1. Use the functional form for Python 2.0 |
NOTE: Order of indices is opposite! For a 2-dim array:
For a given dimension,slices may be specified
| 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) |
| . | i:-j |
| . | empty arrays and slices are permitted (e.g. a[0:0] is an array of length 0) |
| . | -i (indexing from end of array) |
| . | ... (fill in unspecified dimensions) |
| . | NewAxis (add new axis to array shape as part nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;of subscript to match shapes) |
NOTE: Slicing generally does not create a new array, but instead creates a virtual array (non-contiguous 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 does not generate a new array in memory.
| IDL | numarray |
| newarr = arrayvar(indexarr) | newarr = arrayvar[indexarr] |
| arrayvar(indexarr) = valuearr | arrayvar[indexarr] = valuearr |
| fltarr() | array(seq,[type]) to create from existing sequence |
| dblarr() | zeros(shape,[type]) to create 0 filled array |
| complexarr() | ones(shape,[type]) to create 1 filled array |
| intarr() | |
| longarr() | |
| bytarr() | |
| make_array() | |
| strarr() | lists and tuples are more appropriate (chararray do exist to handle large fixed length strings) |
| findgen() | arrayrange(size, [type]) (aka arange() ) |
| dindgen() | |
| indgen() | |
| lindgen() | |
| sindgen() | no direct equivalent, but trivial to duplicate. |
| replicate() | repeat() (more general) |
| byte(arrayvar) | arrayvar.astype() |
| fix(arrayvar) | |
| long(arrayvar) | |
| float(arrayvar) | |
| double(arrayvar) | |
| complex(arrayvar) |
| reform() | reshape() [also arrayvar.flat() and ravel(), or changing the shape attribute] |
| sort() | argsort() [i.e.,indices needed to sort] |
| arrayvar(sort(arrayvar)) | sort(arrayvar) [i.e., sorted array] |
| max() | max() [ufunc] (also argmax()) |
| min() | min() [ufunc] (also argmin()) |
| where() | nonzero() |
| 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() |
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 types | Python numarray types |
| byte | Int8 |
| UInt8 | |
| int | Int16 |
| long | Int32 |
| float | Float32 |
| double | Float64 |
| complex | Complex64 Complex128 |
| [Python ufuncts] | |
| 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() |
| Other Python ufuncs: | |
| 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: |
| reduce() | apply operator to all elements in array (add.reduce equivalent to IDL total()) |
|
| accumulate() | generate new array whose elements equal the operation applied to the first n elements of the input array (add.accumulate is equivalent to an integrate function) |
|
| outer() | produce all combinations of results (multiply.outer(x,y) is equivalent to an outer product) |
| fft() | fft() [FFT module] (not available yet) |
| convol() | (not available yet) |
| randomu() | random(), uniform() [RandomArray (not available yet)] |
| randomn() | RNG module (not available yet) |
| execute() | exec() |
| n_elements() | len() |
| n_params() | closest equivalent is len(*args). Ability to supply default values for parameters replaces some uses of n_params(). |
| size() | shape(), arrayvar.type() |
| wait | time.sleep() |
Last updated: November 5 2002