Previous: Kinds of Declarations, Up: Declarations

6.6.3 Functions for Declarations

Calc has a set of functions for accessing the current declarations in a convenient manner. These functions return 1 if the argument can be shown to have the specified property, or 0 if the argument can be shown not to have that property; otherwise they are left unevaluated. These functions are suitable for use with rewrite rules (see Conditional Rewrite Rules) or programming constructs (see Conditionals in Macros). They can be entered only using algebraic notation. See Logical Operations, for functions that perform other tests not related to declarations.

For example, ‘dint(17)’ returns 1 because 17 is an integer, as do ‘dint(n)’ and ‘dint(2 n - 3)’ if n has been declared int, but ‘dint(2.5)’ and ‘dint(n + 0.5)’ return 0. Calc consults knowledge of its own built-in functions as well as your own declarations: ‘dint(floor(x))’ returns 1.

The dint function checks if its argument is an integer. The dnatnum function checks if its argument is a natural number, i.e., a nonnegative integer. The dnumint function checks if its argument is numerically an integer, i.e., either an integer or an integer-valued float. Note that these and the other data type functions also accept vectors or matrices composed of suitable elements, and that real infinities ‘inf’ and ‘-inf’ are considered to be integers for the purposes of these functions.

The drat function checks if its argument is rational, i.e., an integer or fraction. Infinities count as rational, but intervals and error forms do not.

The dreal function checks if its argument is real. This includes integers, fractions, floats, real error forms, and intervals.

The dimag function checks if its argument is imaginary, i.e., is mathematically equal to a real number times ‘i’.

The dpos function checks for positive (but nonzero) reals. The dneg function checks for negative reals. The dnonneg function checks for nonnegative reals, i.e., reals greater than or equal to zero. Note that Calc's algebraic simplifications, which are effectively applied to all conditions in rewrite rules, can simplify an expression like ‘x > 0’ to 1 or 0 using dpos. So the actual functions dpos, dneg, and dnonneg are rarely necessary.

The dnonzero function checks that its argument is nonzero. This includes all nonzero real or complex numbers, all intervals that do not include zero, all nonzero modulo forms, vectors all of whose elements are nonzero, and variables or formulas whose values can be deduced to be nonzero. It does not include error forms, since they represent values which could be anything including zero. (This is also the set of objects considered “true” in conditional contexts.)

The deven function returns 1 if its argument is known to be an even integer (or integer-valued float); it returns 0 if its argument is known not to be even (because it is known to be odd or a non-integer). Calc's algebraic simplifications use this to simplify a test of the form ‘x % 2 = 0’. There is also an analogous dodd function.

The drange function returns a set (an interval or a vector of intervals and/or numbers; see Set Operations) that describes the set of possible values of its argument. If the argument is a variable or a function with a declaration, the range is copied from the declaration. Otherwise, the possible signs of the expression are determined using a method similar to dpos, etc., and a suitable set like ‘[0 .. inf]’ is returned. If the expression is not provably real, the drange function remains unevaluated.

The dscalar function returns 1 if its argument is provably scalar, or 0 if its argument is provably non-scalar. It is left unevaluated if this cannot be determined. (If Matrix mode or Scalar mode is in effect, this function returns 1 or 0, respectively, if it has no other information.) When Calc interprets a condition (say, in a rewrite rule) it considers an unevaluated formula to be “false.” Thus, ‘dscalar(a)’ is “true” only if a is provably scalar, and ‘!dscalar(a)’ is “true” only if a is provably non-scalar; both are “false” if there is insufficient information to tell.