Programmer Guide > Expressions and Operators > Structure of Expressions
  

Structure of Expressions
Expressions may contain operands with different structures, just as they may contain operands with different types. Structure conversion is independent of type conversion. An expression will yield an array result if any of its operands is an array as shown in Expression Results:
 
Table 3-5: Expression Results
Operands
Result
Scalar : Scalar
Scalar
Array : Array
Array
Scalar : Array
Array
Array : Scalar
Array
Eight functions exist to create arrays of the eight types: BYTARR, INTARR, I32ARR, LONARR, FLTARR, DBLARR, COMPLEXARR, DCOMPLEXARR, and STRARR. The dimensions of the desired array are the parameters to these functions. The result of FLTARR(5) is a floating point array with one dimension, a vector, with five elements initialized to zero. FLTARR(50, 100) is a two-dimensional array, a matrix, with 50 columns and 100 rows.
The number of elements and the structure of an array-valued expression is the same as the number of elements and the structure of the smaller of its array operands. For example, adding a 50-point array to a 100-point array gives a 50-point array, the last 50 points of the larger array are ignored. Array operations are performed point-by-point without regard to individual dimensions. An operation involving a scalar and an array always yields an array of identical dimensions. When two arrays of equal size (number of elements) but different structure are operands, the result is of the same structure as the first operand.
For example:
FLTARR(4) + FLTARR(1, 4) 
yields FLTARR(4).
In the above example, a row vector is added to a column vector and a row vector is obtained because the operands are the same size causing the result to take the structure of the first operand.
Here are some examples of expressions involving arrays:
; Result is an array in which each element is equal to the same 
; element in ARR plus 1. The result has the same dimensions as 
; ARR. If ARR is byte or integer the result is of integer type, 
; otherwise the result is the same type as ARR.
result = ARR + 1
; Result is an array obtained by summing two arrays.
result = ARR1 + ARR2
; Result is an array in which each element is set to twice the 
; smaller of either the corresponding element of ARR or 100.
result = (ARR < 100) * 2
; Result is an array in which each element is equal to the 
; exponential of the same element of ARR divided by 10.
result = EXP(ARR / 10.)
In the inefficient example above, each point in ARR is multiplied by 3 and then divided by the largest element of ARR. (The MAX function returns the largest element of its array argument.) This way of writing the statement requires that each element of ARR be operated on twice. If (3./MAX(ARR)) is evaluated with one division and the result then multiplied by each point in ARR the process requires approximately half the time.
; This is an inefficient way of writing the following line:
result = ARR * 3. / MAX(ARR)
; The more efficient way.
result = ARR * (3. / MAX(ARR))

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.