@node Arithmetic, Aspect, Apropos, Commands @iftex @syntax Arithmetic. @end iftex @example Arithmetic @end example @pindex ?: @pindex Abs @pindex Acos @pindex Arithmetic @pindex Asin @pindex Atan @pindex Atan2 @pindex Concat @pindex Cos @pindex Dimen @pindex Exp @pindex Gamma @pindex Histogram @pindex Image @pindex Int @pindex Lg @pindex Ln @pindex Random @pindex Sin @pindex Sqrt @pindex String @pindex Sum @pindex Tan Arithmetic is allowed on vectors and scalars in SM, using the following operators, where expr is a expression, s_expr is a scalar expression (e.g. a number), and vector the name of a vector. @example Nonary (?): PI Pi @end example @findex arithmetic operators @findex vectors, arithmetic @example Unary: -expr Change sign ABS(expr) Absolute value ACOS(expr) Arccosine ASIN(expr) Arcsine ATAN(expr) Arctangent COS(expr) Cosine CTYPE() Return CTYPEs DIMEN(vector) Dimension of a vector EXP(expr) Exponential GAMMA(expr,expr) Incomplete gamma fn INT(expr) Integral part LG(expr) Log_10 LN(expr) Log_e RANDOM(s_expr) Random numbers SIN(expr) Sine SQRT(expr) Square root STRING(expr) Convert to a string SUM(expr) Sum_i expr_i TAN(expr) Tangent VECTOR[expr] Elements of an array ( expr ) Raise precedence @end example @example Binary: expr + expr Add expr - expr Subtract expr CONCAT expr Concatenate expr * expr Multiply expr / expr Divide expr ** expr Exponentiate expr & expr Bitwise AND expr | expr Bitwise OR expr % expr Modulus ATAN2(expr_y,expr_x) Atan2 @end example (note that a bitwise XOR may be achieved as @code{| - &}). There are also some special operators: @example HISTOGRAM(expr:expr) Construct histogram IMAGE(expr,expr) Extract cross section (x, y coordinates) IMAGE[expr,expr] Extract cross section (x, y indices) expr1 ? expr2 : expr3 expr2 if expr1 is true, else expr3 DO(s_expr, s_expr) Implicit DO loop DO(s_expr, s_expr, s_expr) Implicit DO loop with increment @end example @code{GAMMA} returns an incomplete Gamma function, so GAMMA(a,infty) = 1. @code{RANDOM} generates a vector of @code{s_expr} random numbers in the range [0,1]. If you don't specify a seed (using @code{SET RANDOM}) one will be chosen for you based on the current time. @code{ATAN2} is the same as C's function of the same name, and is equivalent to @code{ATAN(y/x)}. It gives a result in the range @code{-pi -- pi} dealing correctly with divisions by zero. @code{CTYPE} returns a vector of red + 256*green + 256^2*blue, giving the currently defined colours; CTYPE(STRING) returns their names (@pxref{Strings}). @code{DO} generates a range of values, e.g. @code{set x=do(1,5)} sets x to have the values @code{1 2 3 4 5}; this is the same as @code{set x=1,5}; but @code{set y=100 + sin(pi*do(0,10)/10)} is more interesting. You can specify an increment, so @code{do(5,1,-1)} is @code{5 4 3 2 1}. The expression @code{VECTOR[expr]} results in a vector of the same dimension as the @code{expr}, with elements taken from @code{VECTOR} (i.e. @code{VECTOR[INT(expr_i)]}). See, for example, the macro @code{interp}. @findex concatenate You can also use @code{WORD([ expr [ , @dots{} ]])} as part of an expression, where @code{WORD} is a macro taking zero or more arguments. @findex precedence of arithmetical operators The precedences are what you'd expect, with @code{**} being highest, then @code{*}, @code{/} and @code{%}, then @code{+} and @code{-}, then @code{CONCAT}, then the bitwise and logical operators, and finally @code{?:} has the lowest priority of all. You should not assume @emph{any} precedence between logical and bitwise operators, because if you do you'll be fooled (e.g. @code{1||0 | 0x6} is @code{7}, but @code{0x6 | 1||0} is 1); if you use parentheses all will be well (@code{1||(0 | 0x6)} is @code{1} and @code{0x6 | (1||0)} is 7). If you have defined an image file with the IMAGE command, @code{IMAGE(expr,expr)} is an expression to extract values from your image. The two expressions give the x and y values where the image is to be sampled. For example @code{SET x=0,1,.01 SET z=IMAGE(x,0.5)} will extract a horizontal cross section through an image. As an alternative, you can use (0-based) indices to extract the values with @code{IMAGE[expr,expr]}; there are examples under IMAGE (@pxref{Image}). @findex 2-dimensional graphics, cross section @code{HISTOGRAM(expr1:expr2)} constructs a histogram from a vector, where the data is in @code{expr1}, and @code{expr2} (which must be sorted) gives the location of the bins. If the values of @code{expr2} are equally spaced, the `location' of the bin means the centre; if they are not, a bin is defined by saying that points with value @code{(e2[i-1] + e2[i])/2 <= x < (e2[i] + e2[i+1])/2} go into the @code{i}th bin. Note that values on bin boundaries go into the higher bin. @findex binning, see histogram @findex histogram, binned from vector @findex vectors, extract histogram @code{a ? b : c} is very useful if you want to treat some value of an expression specially. You could do this with a loop but @code{?:} is much faster; for example @example set y = lg(x > 0 ? x : 1e-37) set y = sqrt(x >=0 ? x : 0) @end example @noindent Due to the way that SM evaluates these expressions you may see warnings from the parts of the expressions that are not used (i.e. where the logical expression is false). You can turn down the verbosity, of course, or you could try sending mail us mail to see if we can't fix it (but it isn't easy, or else we'd have done it already). See `Logical' for the logical operators, `Strings' for string operators, and `whatis' for finding out if strings are numbers, words, vectors, or whatever.