### Compute

Category

Transformation

Function

Evaluates an expression on each data point in a specified field or value list.

Syntax

    output = Compute(expression, input, ...);

Inputs

Name | Type | Default | Description
--- | --- | --- | ---
`expression` | string | none | expression to be computed
`input` | field or value list | no default | input value
`...` | `...` | `...` | more input values

Outputs

Name | Type | Description
--- | --- | ---
`output` | field, value, or value list | output values

Functional Details

This module applies an expression to every data value in a field.

`expression` | is the mathematical expression to be applied to `input`. Table 1 lists the operators.
`input` | is the field or value list to which `expression` is to be applied. If there are more than one, the input fields must be isomorphic (i.e., their hierarchies must match exactly).

A single Compute module can operate on a maximum of 21 `input` values. In the user interface, the default number of enabled input tabs is two. The default number of enabled input tabs is two. (Tabs can be added to the module icon and removed with the appropriate `...Input Tab` options in the `Edit` pull-down menu of the VPE.)

In the user interface, variables have names and `expression` does not require quotation marks. If the vector variable is "sample," the vector elements are "sample.x," "sample.y," ... or "sample.0," "sample.1," ....

In the scripting language, the parameters of an expression are indicated by $n, where n is the index of the parameter, and counting begins at 0. The first three components of a vector can be expressed by ".x," ".y," and ".z," or by ".0," ".1," and ".2." The syntax for creating a vector from multiple inputs is [$0, $1, $2, ... ], where the commas separators are required.

Thus, in the user interface the expression used to add the value 3 to every data value of the input field would be "a + 3.," while in the scripting language it would be "$0 + 3."

Multiple expressions can be entered if they are separated by semicolons; the final expression is used as the output. For example, "temp=a.x; sin(temp)" is equivalent to "sin(a.x)".

Compute uses C-language-style order of precedence for mathematical operations.

Operations applied to invalid elements of fields typically result in invalid elements. (For more information, see "Invalid Positions and Invalid Connections Components" in the IBM Visualization Data Explorer User's Guide.) For example, if at a particular element field "a" and "c" are valid, but field "b" is not, "a+b" is invalid, and "a? b: c" is valid only if "a" is 0; otherwise, it is invalid. An exception is the "invalid" function, which returns the integer 1 if the entry is invalid and 0 otherwise.

Operations such as multiplying a vector by a scalar have the expected effect: each element of the vector is multiplied by the scalar. Likewise, the addition of two equal-length vectors or equal-size matrices results in an element-by-element sum. The Compute module defines the multiplication (`*`) of two equal-length vectors as an element-by-element multiplication (as opposed to the dot product). Other operations, such as the sine of a vector, are defined similarly.

Constants may be specified as double precision by using scientific notation with "d" indicating the exponent (e.g., `1d0` is double-precision `1`).

Note that you can convert a "ref" type invalid component (see "Invalid Positions and Invalid Connections Components" in IBM Visualization Data Explorer User's Guide) using the expression "byte (invalid (a))". Then use Replace to substitute this new array into the original field as the "invalid positions" or "invalid connections" component.

Table 1. Operators for the Compute Module

Functions | Types of Operands
--- | ---
Trigonometric Functions (argument in radians) |
| sin(a), cos(a), tan(a), asin(a), acos(a), atan(a), atan2(a, b) | float, double
Hyperbolic Functions |
| sinh(a), cosh(a), tanh(a) | float, double
Logarithmic Functions |
| log(a), In(a) (natural logarithm), log10(a) |
| (log base 10--see Note 1), exp(a) | float, double
IEEE inquiry functions |
| isnan(a), finite(a) | float, double
Unary Functions |
| +a, -a (negation) | any type
Binary Functions |
| a+b, a-b, a*b, a/b, a%b (modulus--see Note 1), |
| a^b or a**b (exponentiation--see Note 4) | any type
Vector Functions (see Note 1) |
| a dot b or dot(a, b) | float vector
| a cross b or cross(a,b) | float 3-vector
| mag(a) | double, float vector
| norm(a) | float vector
Miscellaneous Functions |
| sqrt(a) | float, double, complex
| pow(a, b) (see Note 4) | float, double, complex
| abs(a) (see Note 2) | double, float, integer, complex
| arg(a) | complex only
| sign(a) | all real types
| min(a, b, ...), max(a, b, ...) | scalar
| invalid(a) (see Note 5) | any type
| random(a,seed) (see note 8) | produces random values in the range 0<=r<1 for each item in a.
Type Manipulation Functions |
| int(a), float(a), byte(a), char(a), double(a), short(a), | float, integer, byte,
| sbyte, ubyte, ushort, uint (see Note 6) | short, double
| trunc(a), floor(a), ceil(a), rint(a) | float, double
| complex(a,b) or complex(a), | float, integer, byte, short, double
| real(a) | complex only
| imag(a) | complex only
Vector Construction |
| [a, b, ...] | any type
Vector Selection Functions |
| a.x or a.0, a.y or a.1, and so on | vector
| select(a,b) (selects bth element of a, |
| where element is of rank r-1) | a is a vector, b is an integer
Conditional Functions |
| a?b:c if a != 0, then b, else c. (b and c must be of the same type.) Expressions b and c are always evaluated, and the output value depends on the value of a. | a is an integer
Logical Operations |
| binary: <, >, <=, >= (true = 1; false = 0) | any scalar type
| binary: ==, != | any type
| Unary: ! (not), binary: && (and), || (or) | integer
Bitwise Operations |
| and(a,b), xor(a,b), or(a,b), not(a) (one's complement) | byte, int
String functions (see Note 7) |
| strcmp(a,b) compares strings a and b and returns 0 if a==b, a negative integer if a<b, and a positive integer if a>b stricmp is identical to strcmp except that it ignores case | strings
| strlen(a) returns the length of string a | string
| strstr(a,b) finds substring b in string a and returns 1-based offset of b in a. Thus strstr('artist','art') = 1 strstr('artist','picasso') = 0 strstr('monet','one') = 2 stristr(a,b) is identical to strstr except that it ignores case | strings

Notes:

1. The vector functions, modulus function, and log10() function do not accept complex numbers as arguments.

2. Given an integer or floating-point value, the abs() function yields the absolute value. Given a complex number, the abs() function yields a real absolute value.

3. If a string is passed as a parameter to Compute, it is treated as an array of bytes, with its values being the ASCII values of the string elements.

4. This function returns a floating-point value if inputs are floating point or integer. It returns a complex if the first input is complex.

5. Returns integer 1 if entity is marked invalid, and 0 otherwise.

6. Byte and char are same as ubyte.

7. Strings can be passed into Compute as the data component or directly from String and StringList interactors. Strings can also be specified in the Compute expression directly by enclosing them in single quotes. A single quote character is represented by a pair of single quotes. Thus if a is the string "can't", then strcmp(a,'can''t') = 0.

8. The random number generator will be seeded by the integer `seed`. If `a` corresponds to a group then each member of `a` will be seeded by `seed`+n, where n corresponds to the member's enumerated location in `a`. This results in repeatable behavior even when a composite field is being processed in parallel on an SMP machine. To generate different random results, use a different `seed`.

To operate on a component other than "data," use the Mark and Unmark modules together with Compute.

A single Compute module can operate on a maximum of 21 `...Input` values. The default number of enabled input tabs is two. (Tabs can be added to the module icon and removed with the appropriate `...Input Tab` options in the `Edit` pull-down menu of the VPE.)

Note: | Other than a divide by zero, Compute operations are unchecked.

Components

Modifies the "data" and "invalid positions" or "invalid connections" components. All other components are propagated to the output.

Script Language Examples

1. The Compute module converts all the temperature data to Fahrenheit.

       tempf = Compute("$0*(9.0/5.0) + 32", tempc);
       ·

2. The input field is a vector field of dimension 9. The output field is scalar, consisting of the sixth component of the input field.

       new_field = Compute("$0.5",field);

3. The input field is a vector field, and the output field is also a vector field, with the x component multiplied by 2.

       new_field = Compute("[2*$0.x, $0.y, $0.z]", field);

   or

       new_field = Compute("[2, 1, 1]*$0", field);

4. Here `field1` is a vector field and `field2` and `field3` are scalar. Therefore, the output field will have a data component equal (on a point-by-point basis) to the magnitude of `field1` added to the quantity `field2` divided by 4.5 times `field3`.

       new_field = Compute("mag($0) + $1/($2*4.5)", field1, field2, field3);

5. The Mark module to place the "positions" component of the 2-dimensional object `slice` in the "data" component, allowing Compute to operate on the positions. The formula string is assigned to `function` to simplify the call to Compute and is equivalent to the following formula for converting `slice` positions to 2-dimensional vectors: [ x, y, z ] = [ sin ( 2 π × ((x + 1) / 3.9) ) , y , - cos ( 2 π × ((x + 1) / 3.9) )]. When the computation is done, the Unmark module replaces the original "data" component for use in `warped`. The resulting positions are warped onto the shape of a cylinder.

       ·
       slice = Slice(electrondensity, "z", 5);
           // Mark the positions so that they can be computed on
           // The original x positions go from -1 to 2.9
           // The original y positions go from -3 to 2.9
       markedslice = Mark(slice,"positions");
           // Warp the positions onto the shape of a cylinder
       pi = 3.14159;
       exp = "[sin(2*$1*($0.x+1)/3.9), $0.y, -cos(2*$1*($0.x+1)/3.9)]";
       warped = Compute(exp, markedslice, pi);
           // Unmark the warped positions, returning them to the positions
           // component
       warped = Unmark(warped, "positions");
       ·

6. This example differs from the previous one in that the `exp`) function warps the positions onto a double cone shape, by implementing the following formula: [ x, y, z ] = [ y × sin( 2 π × ((x + 1) / 3.9) ) , y , -y × cos ( 2 π × ((x + 1) / 3.9) ) ]

       ·
           // Now warp the positions onto the shape of a doubled cone
           // by multiplying the x and z positions by the original
           // y value, which goes from -3 to 2.9
       exp = "[$0.y*sin(2*$1*($0.x+1)/3.9),$0.y, -$0.y*cos(2*$1*($0.x+1)/3.9)]";
       warped = Compute(exp, markedslice, pi);
           // Unmark the warped positions, returning them to the positions
           // component
       warped = Unmark(warped, "positions");
       ·

Example Visual Programs

Many of the example visual programs use Compute, including:

    ComputeOnData.net
    DataDrivenInteractors.net
    PlotTwoLines.net
    WarpingPositions.net

See Also

Mark, Unmark

[ OpenDX Home at IBM | OpenDX.org ]