Program: CALC Purpose: Calculate expressions (using variables) and exercise GIPSY input syntax Category: CALCULATION, UTILITY File: calc.c Author: M.G.R. Vogelaar Keywords: ** VARNAME= Give variable name(s) to use in expression: [x] Specify the variable names you want to use in the expression in EXPRESSION= The maximum number of variables is 32. A variable name cannot exceed 16 characters. Default is one variable called 'x'. ** FILENAME= Give name of ASCII file: [write data to screen only] It is possible to write results to an ASCII file also. FILENAME= expects the name of your file. The default will skip writing to file. If a file is opened then it will be closed after the loop over EXPRESSION= is aborted. OVERWRITE= File exists, ok to overwrite? [Y]/N Only prompted if FILENAME= is an existing file. EXPRESSION= Enter Hermes expr. or math funct. with var....: [abort] The expression is an expression that can be evaluated by Hermes (using Hermes variables) or it is an mathematical expression which can contain variables that were entered in VARNAME= (default: 'x'). If variables are part of the expression, then their values must be entered with VALUE= before the expression can be evaluated. examples: EXPRESSION=sin(pi/2) EXPRESSION=sin(rad(x)) VALUE=0:30:5 EXPRESSION=descr(AURORA,CRVAL1) EXPRESSION=image(AURORA,-2 -2 2 2) EXPRESSION=table(AURORA,xytable,x,1:) EXPRESSION=file(xvalues.txt,1,1:10) VALUE= Values x(1..n), ... : [abort] Enter the value(s) for the variable(s). The maximum number of variables is limited only by available memory. Alternative input is possible with 1) recall file 2) file and table functions Description: CALC can be used as a calculator in GIPSY and you can also test the GIPSY input syntax for numbers. An expression can contain variables or it follows the Hermes syntax for number input. The calculated values are written to screen and to the GIPSY log file (GIPSY.LOG) and to a file (FILENAME=). If your expression is rejected at first (because it is not a valid 'Hermes' expression), it will be treated as a mathematical expression which can contain variables. The expression is given in a loop in EXPRESSION= and the variables that can be used are in VARNAME= (the default variable name is x). If the expression contains one or more variables then you are prompted to enter values for the variable(s) with VALUE= This keyword follows the standard GIPSY input syntax for numbers. After each evaluation the output is printed on screen (and written to file if FILENAME= is used) and the program returns to the EXPRESSION= prompt. If an ASCII file on disk was opened, it will be closed after the EXPRESSION= loop is aborted. The input string in EXPRESSION= may contain: A) Hermes compatible input: 1 2 3/3 sin(pi) evaluates to 1.0 2.0 1.0 0.0 log(10)::4 evaluates to 1.0 1.0 1.0 1.0 log(10):log(100):2/4 evaluates to 1.0 1.5 2.0 10**[0 1 5] evaluates to 1 10 100000 [1:3]+[90:70:-10] evaluates to 91 82 73 [20:30]?[3 4 5] evaluates to 22 23 24 B) Mathematical expressions (with variables), containing: 1) functions; syntax ff(..); where ff is one of the following available functions: abs(x) absolute value of x sqrt(x) square root of x sin(x) sine of x asin(x) inverse sine of x cos(x) cosine of x acos(x) inverse cosine of x tan(x) tangent of x atan(x) inverse tan of x atan2(x,y) inverse tan (mod 2pi) x = sin, y = cos exp(x) exponential of x ln(x) natural log of x log(x) log (base 10) of x sinh(x) hyperbolic sine of x cosh(x) hyperbolic cosine of x tanh(x) hyperbolic tangent of x rad(x) convert x to radians deg(x) convert x to degrees erf(x) error function of x erfc(x) 1-error function max(x,y) maximum of x and y min(x,y) minimum of x and y sinc(x) sin(x)/x (sinc function) sign(x) sign of x (-1,0,1) mod(x,y) gives remainder of x/y int(x) truncates to integer nint(x) nearest integer ranu(x,y) generates uniform noise between x and y rang(x,y) generates gaussian noise with mean x and dispersion y ranp(x) generates poisson noise with mean x ifeq(x,y,a,b) returns a if x == y, else b ifne(x,y,a,b) returns a if x != y, else b ifgt(x,y,a,b) returns a if x > y, else b ifge(x,y,a,b) returns a if x >= y, else b iflt(x,y,a,b) returns a if x < y, else b ifle(x,y,a,b) returns a if x <= y, else b ifblank(x,a,b) returns a if x == BLANK, else b 2) constants; syntax cc; where cc is one of the following available constants: PI 3.14159.... C speed of light (SI) H Planck (SI) K Boltzmann (SI) G gravitation (SI) S Stefan-Boltzman (SI) M mass of sun (SI) P parsec (SI) BLANK Universal undefined value Note: the Hubble constant is not included. 3) operators; syntax op; where op is one of the following available operators: + addition - subtraction * multiplication / division ** power 4) parameters; up to 32 variable names (less than 16 characters) can be specified. Notes: Example: example 1: Calculate sin(x) for x=0, 5, ..., 30 calc CALC EXPRESSION=sin(rad(x)) CALC VALUE=0:30:5 !x sin(rad(x)) !==================== 0 +0 5 +0.0871557 10 +0.173648 15 +0.258819 20 +0.34202 25 +0.422618 30 +0.5 example 2: Calculate 2/6 3/6 4/6 and 5/6 using the expression x/y Note the order of the input in VALUE= calc varname=x y CALC EXPRESSION=x/y CALC VALUE=2 3 4 5 6::4 !x y x/y !============================== 2 6 +0.333333 3 6 +0.5 4 6 +0.666667 5 6 +0.833333 Note: First the four x values are given and second the four y values. example 3: Calculate 2/6 3/6 4/6 and 5/6 using the expression x/y and a recall file! calc varname=x y CALC EXPRESSION=x/y CALC VALUE=