Algebraic Statements Are Used to Perform the Basic Mathematical Operations and Functions That Deal with Constants and Map Layers
Total Page:16
File Type:pdf, Size:1020Kb
Introduction Syntax Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data Introduction Map Layer Mathematics Algebraic statements are used to perform the basic mathematical operations and functions that deal with constants and map layers. This is known as Layer Math, or Overlay Math. Algebraic operators and functions are performed on every cell in a specified map layer. If a map layer is multiplied by 1000, then each cell in the input map layer will be multiplied by 1000. If one map layer is subtracted from another, then all common cells are subtracted. Using More Than One Map Layer in an Algebraic Statement All the map layers that appear in an algebraic statement must have the same cell resolution, orientation, and at least one common cell. These parameters can be determined from the Information window of each map layer. The Script Window Operators and functions are applied in the Script window. To open a new Script window select New Script from the Windows menu. Syntax and type conventions Using the Script window interface Syntax Algebraic statements follow the same convention as operation statements in the Script window. They begin by stating the name of the new map layer to be generated. This is followed by an “=” symbol and then an algebraic expression. © Copyright ThinkSpace Inc., 1998, Version 1 UG-ALG-1 Algebraic expressions can contain map names, operators, bracketing, and function names. An algebraic statement must contain at least one map layer name. The basic syntax is: newmap = expression; Algebraic statements can be complex. MFworks can handle complex, bracketed expressions that include multiple functions and operators, such as: map3 = map1 + (5.0 * SIN(map2)) / AVG(map2, 6); OR map4 = map3 * 3.1415 + MAX(map1, map2, map3); Note: Algebraic statements cannot contain map Operations. © Copyright ThinkSpace Inc., 1998, Version 1 UG-ALG-2 Operators Note: In the functions below, a, b, and c represent either a map name or a value. Performing Basic Math on Map Layers Determining Where One Map Layer is Greater Than Another Determining Where Two or More Relational Conditions are True 1) Monadic (one operator followed by one operand) Operator Meaning Example - Negative -a ! or NOT Logical “not” !b or NOT b 2) Dyadic (Math) (one operator separating two operands) Operator Meaning Example * Multiplication a * b ^ Power/Exponent a^b (i.e., ab) / Division a / b (i.e., a ÷ b) % Modulus a % b (i.e., a MOD b) + Addition a + b - Subtraction a - b 3) Dyadic (Relational) (one operator separating two operands) Operator Meaning Example < Less than a < b > Greater than a > b <= Less than or equal to a <= b >= Greater than or equal to a >= b == Equal to a == b != Not Equal to a != b 4) Dyadic (Logical) (one operator separating two operands) Operator Meaning Example | or OR Logical “or” (a != b) | (a > c) & or AND Logical “and” a & !b © Copyright ThinkSpace Inc., 1998, Version 1 UG-ALG-3 Functions Note: In the functions below, x represents either a map name or an expression. Applying Trigonometric Functions to a Map Layer Converting Fixed and Floating Point Data Types Finding the Maximum Non-VOID Value in a Stack of Map Layers Counting the Number of Cells With the Same Value 1) Trigonometric Functions SIN(x) return sine of x (x in radians) COS(x) return cosine of x (x in radians) TAN(x) return tangent of x (x in radians) ARCSIN(x) return arcsine of x ARCCOS(x) return arccosine of x ARCTAN(x) return arctangent of x 2) Logarithmic Functions EXP(x) return ex LOG(x) return log x (base e) LOG10(x) return log x (base 10) 3) Mode Functions FLOAT(x) convert x to a floating point entity TRUNC(x) convert x to an integer entity PREC(x,y) set precision of x to y decimal places 4) List Functions MAX(x1, x2, …) return maximum of list MIN(x1, x2, …) return minimum of list AVG(x1, x2, …) return average value of list MAXNV(x1, x2, …) return maximum of non-VOID values in list MINNV(x1, x2, …) return minimum of non-VOID values in list AVGNV(x1, x2, …) return average value of non-VOID values in list 5) Miscellaneous Functions ABS(x) return absolute value © Copyright ThinkSpace Inc., 1998, Version 1 UG-ALG-4 COUNT(map) return cell count in map Order of Operations Introduction MFworks follows standard order of operations priority logic. This means that expressions are evaluated according to the priority assigned to operators in the expression. You may override the priority system by using brackets, as these have the highest priority. For example, multiplication and division are higher order operators than addition and subtraction: 6 + 5 * 4 = 26 whereas, (6 + 5)*4 = 120 Priority System The priority system is as follows: 1) (highest) ( ) Brackets 2) - (negation), !, NOT Monadic 3) ^ Power/Exponent 4) *, /, % Multiplication, Division, Modulus 5) +, - (minus) Addition, Subtraction 6) <, <=, >, >= Relational 7) ==, != Relational 8) AND, & Boolean “And” 9) (lowest) OR, | Boolean “Or” Operators at Operators that occur at the same level are evaluated from left to right: the Same Level 3 1 2 map1 == map2 <= map3 >= map4 is equivalent to: map1 == ((map2 <= map3) >= map4) 2 4 3 1 map1 * map2 + map3 % (map4 - map5) is equivalent to: (map1 * map2) + (map3 % (map4 - map5)) © Copyright ThinkSpace Inc., 1998, Version 1 UG-ALG-5 Mixed Mode Arithmetic MFworks follows standard mixed mode arithmetic logic when operands of different data types appear in an algebraic expression. When mixing floating point data type and integer data type map layers, the data type of the resultant map layer depends on the order of the map layers and the operators and functions that are used. All Integer/Fixed Point Data Operands If a given operator or function is passed all integer values, the result will be integer values. Using a Floating Point Operand If any operand of an operator or function is a floating point value, then all operands are promoted to floating point data and the result will be a floating point value. The following are exceptions to the rule: 1) All Trigonometric and Logarithmic functions return floating point data. 2) The PREC and FLOAT functions return floating point data. 3) The TRUNC and COUNT functions return integer data. 4) The relational operators (<, <=, >, >=, ==, !=) and the logical operators (AND, OR, NOT, &, |, !) return integer results (1 or 0, where 1 = true and 0 = false). Constants Constants in an expression are considered to be integers unless they contain a decimal point. Using Brackets to Control Mixed Mode Arithmetic The extent of mixed mode arithmetic is bound by brackets. For example: 1. (3/4) + 5.0 is 5.0 not 5.75 and: 2. FLOAT(3/4) + 5.0 is also 5.0, not 5.75 but: 3. (3./4) + 5.0 is 5.75 and: 4. (FLOAT(3)/4) + 5.0 is 5.75 In the first example: (3/4) + 5.0, “(3/4)” is not promoted to floating point arithmetic because of the brackets. There are no decimal places allowed in integer arithmetic, therefore, 3 divided by 4 equals zero. Note: Integer math always truncates the result. © Copyright ThinkSpace Inc., 1998, Version 1 UG-ALG-6 In the second example: FLOAT(3/4) + 5.0, the function FLOAT does not convert 3 divided by 4 into a floating point number, because, by order of operations, the 3 divided by 4 is performed first. FLOAT (0) equals zero. Again the presence of the brackets bound the rules of mixed mode arithmetic. In the third example: (3./4) + 5.0, mixed mode arithmetic is performed. The 4 is promoted to the floating point number 4.0 because a floating point value occurs within the brackets. In the final example: (FLOAT(3)/4) + 5.0, the order of operations makes the FLOAT (3) operation occur before dividing the 3 by 4, therefore, this operation is equivalent to the third example. Algebraic statements may contain up to 50 levels of bracketing. Expressions may be embedded in functions as in other programming languages. VOID Data How VOID is Handled in an Expression VOID is a special data value in MFworks. It is considered to be the absence of data. VOID appears in an expression only when it is part of a number list such as in a relational expression. Any expression that is performed on a cell with the value VOID, will result in VOID. For example: 1) 1 + VOID = VOID 2) SIN (VOID) = VOID 3) MAX (VOID, 1, 3) = VOID Exceptions to the Rule: VOID Always Returns VOID There are a few exceptions where VOID is handled differently: 1) The relational comparison operators “==” (equal) and “!=” (not equal) can be used to compare VOID to VOID cells. These operators will return either “1” or “0”, but never “VOID” (i.e., “VOID” == “VOID” returns “1” while VOID = value returns “0”). The other relational operators (<, <=, >, >=) will return “VOID” if the cell in one of the operand map layers is “VOID” (e.g., newmap = (maplayer1 >= VOID)). This is © Copyright ThinkSpace Inc., 1998, Version 1 UG-ALG-7 because relationship is calculated mathematically and “VOID” has no numerical value. VOID > VOID returns “VOID” VOID <= value returns “VOID” value <= value returns either “1” or “0” 2) The Non-VOID (NV) functions AVGNV, MAXNV, and MINNV ignore VOID cells. These functions will only return “VOID” if all the operand cells being compared are “VOID”. In the case of the average function, AVGNV, “VOID” cells do not contribute to the count of values to average. For example: AVG (VOID, 1, 3) = VOID, MAX (VOID, 1, 3) = VOID, MIN (VOID, 1, 3) = VOID, while AVGNV (VOID, 1, 3) = 2 MAXNV (VOID, 1, 3) = 3 MINNV (VOID, 1, 3) = 1 Expressions Not Containing VOID That Result in VOID Expressions that do not contain VOID may result in VOID if: 1) There is a floating point overflow or underflow (i.e., the number exceeds 1.0E38 or is less than 1.0E-38).