2.3.1.3 Operators

Introduction

LabTalk supports assignment, arithmetic, logical, relational, and conditional operators:

Arithmetic Operators   +     -     *     /     ^     &     |
String Concatenation   +
Assignment Operators   =     +=     -=     *=     /=     ^=
Logical and Relational Operators   >     >=     <     <=     ==     !=     &&     ||
Conditional Operator   ? :

These operations can be performed on scalars and in many cases they can also be performed on vectors (datasets). Origin also provides a variety of built-in numeric, trigonometric, and statistical functions which can act on datasets.

When evaluating an expression, Origin observes the following precedence rules:

  1. Exposed assignment operators (not within brackets) are evaluated.
  2. Operations within brackets are evaluated before those outside brackets.
  3. Multiplication and division are performed before addition and subtraction.
  4. The (>, >=, <, <=) relational operators are evaluated, then the (== and !=) operators.
  5. The logical operators || is prior to &&.
  6. Conditional expressions (?:) are evaluated.

Arithmetic Operators

Origin recognizes the following arithmetic operators:

Operator Use
+
Addition
-
Subtraction
*
Multiplication
/
Division
^
Exponentiate (X^Y raises X to the Yth power) (see note below)
&
Bitwise And operator. Acts on the binary bits of a number.
|
Bitwise Or operator. Acts on the binary bits of a number.

Note: For 0 raised to the power n (0^n), if n > 0, 0 is returned. If n < 0, a missing value is returned. If n = 0, then 1 is returned (if @ZZ = 1) or a missing value is returned (if @ZZ = 0).

These operations can be performed on scalars and on vectors (datasets). For more information on scalar and vector calculations, see Performing Calculations below.

The following example illustrates the use of the exponentiate operator: Enter the following script in the Command window:

1.3 ^ 4.7 =

After pressing ENTER, 3.43189 is printed in the Command window. The next example illustrates the use of the bitwise and operator. Enter the following script in the Command window:

if (27&41 == 9)
{type "Yes!"}

After pressing ENTER, Yes! is displayed in the Command window.

Note: 27&41 == 9 because

27 = 0000000000011011
41 = 0000000000101001

with bitwise & yields:

0000000000001001 (which is equal to 9)

Note: Multiplication must be explicitly included in an expression. For example, 2*X must be used instead of 2X to indicate the multiplication of the variable X by the constant 2.

Define a constant

We can also define global constants in the CONST.CNF file under User File Folder:

//Euler's number
const e = 2.718281828459045

A Note about Logarithmic Conversion

  • To convert a dataset to a logarithmic scale, use the following syntax:
col(c) = log(col(c));
  • To convert a dataset back to a linear scale, use the following syntax:
col(c) = 10^(col(c));

String Concatenation

Very often you need to concatenate two or more strings of either the string variable or string register type. All of the code segments in this section return the string "Hello World."

The string concatenation operator is the plus-sign (+), and can be used to concatenate two strings:

aa$ ="Hello";
bb$="World";
cc$=aa$+" "+bb$;
cc$=;

To concatenate two string registers, you can simply place them together:

%J="Hello";
%k="World";
%L=%J %k;
%L=;

If you need to work with both a string variable and a string register, follow these examples utilizing %( ) substitution:

aa$ ="Hello";
%K="World";
dd$=%(aa$) %K;
dd$=;
dd$=%K;
dd$=aa$+" "+dd$;
dd$=;
%M=%(aa$) %K;
%M=;

Assignment Operators

Origin recognizes the following assignment operators:

Operator Use
=
Simple assignment.
+=
Addition assignment.
-=
Subtraction assignment.
*=
Multiplication assignment.
/=
Division assignment.
^=
Exponential assignment.

These operations can be performed on scalars and on vectors (datasets). For more information on scalar and vector calculations, see Performing Calculations in this topic.

The following example illustrates the use of the -= operator.

In this example, 5 is subtracted from the value of A and the result is assigned to A:

A -= 5;

In the next example, each value in Book1_B is divided by the corresponding value in Book1_A, and the resulting values are assigned to Book1_B.

Book1_B /= Book1_A;

In addition to these assignment operators, LabTalk also supports the increment and decrement operators for scalar calculations (not vector).

Operator Use
++
Add 1 to the variable contents and assign to the variable.
--
Subtract 1 from the variable contents and assign to the variable.

The following for loop expression illustrates a common use of the increment operator ++. The script prints the data stored in the second column of the current worksheet to the Command window:

for (ii = 1; ii <= wks.maxrows; ii++)
   {type ($(col(2)[ii])); }

Logical and Relational Operators

Origin recognizes the following logical and relational operators:

Operator Use
>
Greater than
>=
Greater than or equal to
<
Less than
<=
Less than or equal to
==
Equal to
!=
Not equal to
&&
And
||
Or

An expression involving logical or relational operators evaluates to either true (non-zero) or false (zero). Logical operators are almost always found in the context of Conditional and Loop Structures.

Numeric Comparison

The most common comparison is between two numeric values. Generally, at least one is a variable. For instance:

if aa<3 type "aa<3";

Or, both items being compared can be variables:

if aa<=bb type "aa<=bb";

It is also possible, using parentheses, to make multiple comparisons in the same logical statement:

if (aa<3 && aa<bb) type "aa is lower";

String Comparison

You can use the == and != operators to compare two strings. String comparison (rather than numeric comparison) is indicated by open and close double quotations (" ") either before, or after, the operator. The following script determines if the %A string is empty:

if (%A == ""){type "empty"};

The following examples illustrates the use of the == operator:

x = 1;     // variable x is set to 1 
%a = x;    // string a is set to "x" 
if (%a == 1);
      type "yes";
else
      type "no";

The result will be yes, because Origin looks for the value of %a (the value of x), which is 1. In the following script:

x = 1;     // variable x is set to 1 
%a = x;    // string a is set to "x" 
if ("%a" == 1)
      type "yes";
else
      type "no";

The result will be no, because Origin finds the quotation marks around %a, and therefore treats it as a string, which has a character x, rather than the value 1.

Conditional Operator (?:)

The ternary operator or conditional operator (?:) can be used in the form:

Expression1 ? Expression2 : Expression3

This expression first evaluates Expression1. If Expression1 is true (non-zero), Expression2 is evaluated. The value of Expression2 becomes the value for the conditional expression. If Expression1 is false (zero), then Expression3 is evaluated and Expression3 becomes the value for the entire conditional expression. Note that Expressions1 and Expressions2 can themselves be conditional operators. The following example assigns the value which is greater (m or n), to variable:

m = 2;
n = 3;
variable = (m>n?m:n);
variable =

LabTalk returns: variable = 3

In this example, the script replaces all column A values between 5.5 and 5.9 with 5.6:

col(A) = col(A)>5.5&&col(A)<5.9?5.6:col(A);

Note: A Threshold Replace function tReplace(dataset, value1, value2 [, condition]) is also available for reviewing values in a dataset and replacing them with other values based on a condition. In the tReplace(dataset, value1, value2 [, condition]) function, each value in the dataset is compared to value1 according to the condition. When the comparison is true, the value may be replaced with Value2 or -Value2 depending on the value of condition. When the comparison is false, the value is retained or replaced with a missing value depending on the value of condition. The treplace() function is much faster than the ternary operator. See tReplace().

Performing Calculations

You can use LabTalk to perform both

  • scalar calculations (mathematical operations on a single variable), and
  • vector calculations (mathematical operations on entire datasets).

Scalar Calculations

You can use LabTalk to express a calculation and store the result in a numeric variable. For example, consider the following script:

inputVal = 21;
myResult = 4 * 32 * inputVal;

The second line of this example performs a calculation and creates the variable, myResult. The value of the calculation is stored in myResult.

When a variable is used as an operand, and will store a result, shorthand notation can be used. For example, the following script:

B = B * 3;

could also be written:

B *= 3;

In this example, multiplication is performed with the result assigned to the variable B. Similarly, you can use +=, -=, /=, and ^=. Using shorthand notation produces script that executes faster.

Vector Calculations

In addition to performing calculations and storing the result in a variable (scalar calculation), you can use LabTalk to perform calculations on entire datasets as well.

Vector calculations can be performed in one of two ways: (1) strictly row-by-row, or (2) using linear interpolation.

Row-by-Row Calculations

Vector calculations are always performed row-by-row when you use the two general notations:

datasetB = scalarOrConstant <operator> datasetA;

datasetC = datasetA <operator> datasetB;

This is the case even if the datasets have a different numbers of elements. Suppose there are three empty columns in your worksheet: A, B, and C. Run the following script:

col(a) = {1, 2, 3};
col(b) = {4, 5};
col(c) = col(a) + col(b);

The result in column C will be {5, 7, --}. That is, Origin outputs a missing value for rows in which one or both datasets do not contain a value.

Vector calculations can also involve a scalar. In the above example, type:

col(c) = 2 * col(a);

Column A is multiplied by 2 and the results are put into the corresponding rows of column C.

Instead, execute the following script (assuming newData does not previously exist):

newData = 3 * Book1_A;

A temporary dataset called newData is created and assigned the result of the vector operation.

Calculations Using Interpolation

Origin supports interpolation through range notation and X-Functions such as interp1 and interp1xy. Please refer to Interpolation for more details.