Assignments
Assignments and Variables
Once variables or arrays have been declared, you can assign values, expressions, return values from functions, or other variables to them (unless they are declared as constants).
After these two assignments, var1 contains the value 24.
You can also perform multiple assignments in a single line using a comma-separated syntax, similar to declarations:
In this example, var1 receives the value 24, var2 is assigned 0, and the second element in the second row of the previously declared matrix is set to 5.
As with declarations, if there are more variables on the left-hand side, the compiler will attempt to assign the last value on the right-hand side to the remaining variables.
Assignments and Arrays
You can assign new values to individual indices of an already declared array:
Multiple values can also be assigned at once by passing a list of values. To reference the entire array, simply omit the brackets outside of a declaration.
Just like in declarations, you can pass a list of values to update multiple indices at once:
This will assign new values to indices 0-3 of the array. If the size of the array is larger than the list of values provided, the values in the remaining indices will remain unchanged. For example, if array is an array of size 5, the last index will not be affected by this assignment.
These assignments can also be combined with the one-line multiple assignment syntax.
Assignments and Multidimensional Arrays
When working with multidimensional arrays (NDArrays), CKSP offers an extended syntax for reinitializing, assigning, and manipulating data supporting both complete and partial array operations.
As with one-dimensional arrays, individual elements can be assigned new values. NDArray elements are referenced using a comma-separated syntax within the brackets.
Here, the third element in the second column is set to 12.
The wildcard operator * allows you to update entire dimensions or the whole array at once.
To reinitialize all elements of a 2D array with the same value, you can use the assignment syntax. Referencing the array can be done without brackets, as with one-dimensional arrays, or by using the wildcard operator as a placeholder for the dimensions.
To reinitialize just one dimension of an NDArray use the wildcard * to denote values in that dimension. In the following example, the third row of matrix is overwritten with the value 2.
Assignments between Multidimensional Arrays
You can copy data between NDArrays using the wildcard operator, either for the whole array or just a single dimension.
To copy all dimensions from one NDArray to another, use the following syntax.
A single dimension of an NDArray can also be copied to another dimension.
Here, the first row of identity is copied into the third row of matrix.
If the NDArrays have different dimensions, part of one array can be copied to another by specifying the relevant dimensions.
Here, all rows and columns are copied from matrix into the second slice of matrix_d3.
Limitations of Wildcard Notation in Assignments
When using the wildcard operator, dimensions must match in size. Otherwise, unexpected results or errors may occur, such as the compiler attempting to access indices beyond the array boundaries.
If multiple dimensions are to be copied, they must be adjacent. Otherwise, the assignment must be performed for each dimension individually. Hence, the use of matrix_nd3[*, 1, *] is not allowed, as the wilcards in dimensions 1 and 3 are not adjacent.
Assigning Arrays to Multidimensional Arrays and Vice Versa
You can assign a 1D array directly to a dimension of an NDArray, or copy a dimension of an NDArray into a 1D array.
A one-dimensional array can be directly assigned to a specific dimension of an NDArray.
Compound Assignments
In addition to regular assignments, CKSP supports compound assignments. These operators combine an arithmetic or logical operation with an assignment in a single step.
Compound assignments are available for:
- Addition (
+=) - Subtraction (
-=) - Multiplication (
*=) - Division (
/=) - String Concatenation (
&=)
They work on all available variable types.
Implementation Detail
The compiler tries to optimize compound assignments into efficient KSP code.
For example, += 1 will be lowered to an inc(1) instruction,
and -= 1 to dec(1), avoiding a separate arithmetic step.