IDL Operators
Parentheses
Parentheses are used to group expressions and to enclose function parameter lists.
;Parentheses enclose function argument lists.
SIN(ANG * PI/180.)
;Parentheses specify order of operator evaluation.
(A + 5)/B
Square Brackets
Square brackets are used to create arrays and to enclose array subscripts.
;Use brackets when assigning elements to an array.
ARRAY = [1, 2, 3, 4, 5]
ARRAY = [1, array_1, 3, array_2, 5]
;Brackets enclose subscripts.
ARRAY[X, Y]
ARRAY(X, Y) : works in version prior to version 5.0
Mathematical Operators
There are seven basic IDL mathematical operators, described below.
Assignment
A = 32
Compound Assignment Operators ( +=, -=, etc. )
A = A + 100 A += 100
Addition
;Store the sum of 3 and 6 in B. B = 3 + 6
;Store the string value of "John Doe" in B.
B = 'John' + ' ' + 'Doe'
Subtraction and Negation
;Store the value of 5 subtracted from 9 in C. C = 9 - 5
;Change the sign of C. C = -C
Multiplication
; Store the product of 2 and 5 in variable C: C = 2 * 5
Division
; Store the result of 10.0 divided by 3.2 in variable D: D = 10.0/3.2
Exponentiation
The caret (^) is the exponentiation operator. A^B is equal to A raised to the B power.
For real numbers, A^B is evaluated as follows:
If A is a real number and B is of integer type, repeated multiplication is applied.
If both A and B are real (non-integer), the formula AB = eBlnA is evaluated.
A0 is defined as 1.
For complex numbers, A^B is evalutated as follows. The complex number A can be represented as A = a + ib, where a is the real part, and ib is the imaginary part. In polar form, we can represent the complex number as A = reiq = r cosq + ir sinq, where r cosq is the real part, and ir sinq is the imaginary part:
If A is complex and B is real, the formula AB = (reiq)B = rB (cosBq + isinBq) is evaluated.
If A is real and B is complex, the formula AB = eBlnA is evaluated.
If both A and B are complex, the formula AB = eBlnA is evaluated, and the natural logarithm is computed to be ln(A) = ln(reiq) = ln(r) + iq.
Modulo
;Assign the value of 9 modulo 5 (4) to A. A = 9 MOD 5
;Compute angle modulo 2p.
A =(ANGLE + B) MOD (2 * !PI)
Increment/Decrement
Increment and decrement operators can be used, along with a variable, as standalone statements
A++ or ++A
A-- or --A
Increment/Decrement Expressions
B = 27
A = B++
In contrast, after executing the following statements, both A and B have a value of 26:
B = 27
A = --B
Minimum and Maximum Operators
The Minimum Operator
The "less than" sign (<) is the IDL minimum operator. The value of "A < B" is equal to the smaller of A or B. For example:
;Set A equal to 3.
A = 5 < 3
;Set A equal to -6.
A = 5 < (-6)
;Syntax Error. IDL attempts to perform a subtraction operation if
;the "-6" is not enclosed in parentheses.
A = 5 < -6
;Set all points in array ARR that are larger than 100 to 100.
ARR = ARR < 100
;Set X to the smallest of the three operands.
X = X0 < X1 < X2
For complex numbers the absolute value (or modulus) is used to determine which value is smaller. If both values have the same magnitude then the first value is returned.
For example:
; Set A equal to 1+2i, since ABS(1+2i) is less than ABS(2-4i)
A = COMPLEX(1,2) < COMPLEX(2,-4)
; Set A equal to 1-2i, since ABS(1-2i) equals ABS(-2+i)
A = COMPLEX(1,-2) < COMPLEX(-2,1)
The Maximum Operator
The "greater than" sign (>) is the IDL maximum operator. "A > B" is equal to the larger of A or B. For example:
;'>' is used to avoid taking the log of zero or negative numbers.
C = ALOG(D > 1E - 6)
;Plot positive points only. Negative points are plotted as zero.
PLOT, ARR > 0
For complex numbers the absolute value (or modulus) is used to determine which value is larger. If both values have the same magnitude then the first value is returned. For example:
; Set A equal to 2-4i, since ABS(2-4i) is greater than ABS(1+2i)
A = COMPLEX(1,2) > COMPLEX(2,-4)
; Set A equal to 1-2i, since ABS(1-2i) equals ABS(-2+i)
A = COMPLEX(1,-2) > COMPLEX(-2,1)
Matrix Multiplication
IDL has two operators used to multiply arrays and matrices.
The # Operator
The # operator computes array elements by multiplying the columns of the first array by the rows of the second array. The second array must have the same number of columns as the first array has rows. The resulting array has the same number of columns as the first array and the same number of rows as the second array.
syntax: M x N # P x M = P x N ( multiple col by row )
The ## Operator (normal matrix multiplication)
The ## operator does what is commonly referred to as matrix multiplication. It computes array elements by multiplying the rows of the first array by the columns of the second array. The second array must have the same number of rows as the first array has columns. The resulting array has the same number of rows as the first array and the same number of columns as the second array.
For an example illustrating the difference between the two, see Multiplying Arrays.
syntax: M x N ## N x P = M x P ( multiple row by col )
Array Concatenation
The square brackets are used as array concatenation operators.
The expression [A,B] is an array formed by concatenating A and B, which can be scalars or arrays, along the first dimension.
The second and third dimensions can be concatenated by nesting the bracket levels;
[[1,2],[3,4]] is a 2-element by 2-element array with the first row containing 1 and 2 and the second row containing 3 and 4. Operands must have compatible dimensions; all dimensions must be equal except the dimension that is to be concatenated, e.g., [2,INTARR(2,2)] are incompatible. Examples:
;Define C as three-point vector.
C = [-1, 1, -1]
;Add 12 to the end of C.
C = [C, 12]
;Insert 12 at the beginning of C.
C = [12, C]
;Plot ARR2 appended to ARR1.
PLOT, [ARR1, ARR2]
;Define a 3x3 matrix.
KER = [[1,2,1], [2,4,2], [1,2,1]]
Logical Operators
There are three logical operators in IDL: &&, ||, and ~.
&& (and)
The logical && operator performs the logical short-circuiting "and" operation on two scalars or one-element arrays, returning 1 if both operands are true and 0 if either operand is false.
|| (or)
The logical || operator performs the logical short-circuiting "or" operation on two scalars or one-element arrays, returning 1 if either of the operands is true and 0 if both are false.
~ (not)
The logical ~ operator performs the logical "not" operation on a scalar or array operand. If the operand is a scalar, it returns scalar 1 if the operand is false or scalar 0 if the operand is true. If the operand is an array, it returns an array containing a 1 for each element of the operand array that is false, and a 0 for each element that is true.
Note
Programmers familiar with the C programming language, and the many languages that share its syntax, may expect ~ to perform bitwise negation (1's complement), and for ! to be used for logical negation. This is not the case in IDL: ! is used to reference system variables, the NOT operator performs bitwise negation, and ~ performs logical negation.
When is an Operand True?
When evaluated by a logical operator, an expression is considered to be "true" under the following conditions:
For numerical operands, if the value is non-zero.
For string operands, if the value is non-null.
For heap variables (pointers and object references), if the value is non-null.
Logical Operator Examples
Results of relational expressions can be combined into more complex expressions using the logical operators. Some examples of relational and logical expressions are as follows:
;True if A is between 25 and 50. If A is an array, then the result
;is an array of zeros and ones.
(A LE 50) && (A GE 25)
;True if A is less than 25 or greater than 50. This is the inverse
;of the first.
(A GT 50) || (A LT 25)
Bitwise Operators
AND
NOT
OR
XOR
Relational Operators
The IDL relational operators can be used to test the relationship between two arguments. The six relational operators are described in the following table:
EQ
NE
GE
GT
LE
LT
In IDL, the value "true" is represented by the following:
Any odd, nonzero value for byte, integer, and longword data types
Any nonzero value for single, double-precision, and the real part of a complex number (the
imaginary part is ignored)
Any non-null string
Using Relational Operators with Arrays
Relational operators can be applied to arrays, and the resulting array of ones and zeroes can be used as an operand. For example, the expression, ARR * (ARR LE 100) is an array equal to ARR except that all points greater than 100 have been reduced to zero. The expression (ARR LE 100) is an array that contains a 1 where the corresponding element of ARR is less than or equal to 100, and zero otherwise. For example, to print the number of positive elements in the array ARR:
PRINT,TOTAL(ARR GT 0)
Using Relational Operators with Infinity and NaN Values
On Windows and Solaris x86 platforms, using relational operators with the values infinity or NaN (Not a Number) causes an "illegal operand" error. The FINITE function's INFINITY and NAN keywords can be used to perform comparisons involving infinity and NaN values. For more information, see FINITE and Special Floating-Point Values.
Conditional Expression
The conditional expression-written with the ternary operator ?:-has the lowest precedence of all the operators. It provides a way to write simple constructions of the IF...THEN...ELSE statement in expression form. In the following example, Z receives the larger of the values contained by A and B:
IF (A GT B) THEN Z = A ELSE Z = B
This statement can be written more concisely using a conditional expression:
Z = (A GT B) ? A : B
The general form of a conditional expression is:
expr1 ? expr2 : expr3
No comments:
Post a Comment