Often functions are defined in a piecewise manner using one or more conditions.  For example, a job may pay $10 per hour for the first 40 hours per week and $15 per hour for any additional hours.


IF expressions are a convenient way to express such conditions in Derive.  For example, if h hours are worked during a week, the above pay schedule can be entered as


       IF(h <= 40, 10h, 400 + 15·(h - 40))


When this IF expression is simplified and if Derive determines that the condition h <=40 is true using the current value or domain declaration of h, the second argument 10·h is simplified and returned.  If Derive determines that the condition is false, the third argument 400 + 15·(h - 40) is simplified and returned.  For example, if h is assigned a value of 50, this IF expression simplifies to 550.  Or if h is not assigned a value but is declared less than 30, it simplifies to 10h.


If Derive cannot determine whether the condition is true or false, the entire IF expression is returned.


Note that an IF expression can be used as a subexpression within another expression.  For example, adding a $100 bonus to the above pay schedule can be entered as


       100 + IF(h <= 40, 10h, 400 + 15·(h - 40))


A pair of expressions separated by one of the relational operators (=, /=, <, <=, >, or >=) is a relation (see Entering Equations and Relations).  When a relation is simplified, its two sides are simplified independently and Derive makes no attempt to determine the truth or falsehood of the relation.  However, when a relation appears as the first clause of an IF expression and the IF expression is simplified, Derive treats the relation as a condition and actively tries to determine its truth value.


The general form of the IF expression is


IF (test, then, else, unknown)


where the test clause is a condition, and the then, else, and unknown clauses are expressions.  If the test clause is not a relation or a logical combination of relations (see Boolean Test ClausesBoolean_Test_Clauses), it is treated as the condition test=0.


When an IF expression is simplified, Derive evaluates the test clause to determine its truth value.  If true, the then clause is simplified and returned as the value of the IF expression.  If false, the else clause is simplified and returned.  If the truth value cannot be determined, the unknown clause is simplified and returned.  Note that whichever clause is returned, the other two clauses are not simplified.


If the unknown clause is omitted and the truth value of the test clause cannot be determined, the entire IF expression is returned.  This is often informative, and subsequent assignments or domain declarations might enable you to resimplify the IF expression to a decisive result.  Alternatively, ? is often used as the unknown clause since it yields a more compact result when the truth value of the test clause cannot be determined.  ? is the expression signifying a quantity of unknown phase and magnitude.


If the else clause is also omitted from the IF expression, a default value of ? is assumed for it.  Finally, if the then clause is also omitted, IF returns 1 if the argument evaluates to true, otherwise it returns 0.  When used in conjunction with the SUM function, this is useful for counting the elements of a vector or sequence that satisfy some condition.  For example,


       SUM(IF(PRIME(n)), n, 1, 100)


simplifies to 25, the number of primes between 1 and 100.


An expression of the form


ITERATE (IF (test, x, u), x, x0)


can be used to search for a number or expression that satisfies the search condition test.  Given x, u generates the next number or expression to test.  For example,


       ITERATE(IF(PRIME(n) AND PRIME(n+2), n, n+2), n, 100001)


simplifies to 100151, the leading element of the first prime pair larger than 100000.  (Prime pairs are consecutive primes that differ by 2.)



IF expressions can be used on the right side of a function definition.  For example, most Derive functions are defined for complex as well as real values of their arguments, and many functions return complex results for some real values of their arguments.  However, in an educational setting devoted to the real domain, some instructors may prefer that functions return ? when an argument or a result is not real.  Using the following filter function, more restrictive variants can be defined that impose this constraint:


       REAL_ONLY(x) := IF(IM(x) = 0, x)


Note that the else clause of the IF expression is omitted so that ? is automatically returned if Derive can determine that IM(x) is not 0.


As examples of using REAL_ONLY, you can define:


       REAL_LN(x) := REAL_ONLY(LN(x))


       REAL_ABS(x) := ABS(REAL_ONLY(x))


LN(x) is not real-valued if x is negative.  ABS(x) is real-valued even if x is not real.  These two facts account for the different placement of REAL_ONLY in the above definitions.



Since zero can be approached from either the positive or negative direction, or for that matter any direction in the complex plane, zero does not have a definite sign.  Thus, SIGN(0) simplifies to plus-or-minus 1 (see Complex Variable FunctionsComplex_Variable_Functions).



For additional examples of IF expressions, inspect the definitions in the files VectorMatrixFunctions.mthI._DEZ and NumericalApproximation.mthY5C_1X.


Other Programming in DERIVEProgramming 

Created with the Personal Edition of HelpNDoc: Easy EBook and documentation generator