Skip to content

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).

var1 := 4 * 5
var1 := var1 + 4

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:

var1, var2, matrix[1,1] := 4 * 5 + 4, 0, 5

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.

var1, var2, var3 := 0

Assignments and Arrays

0.0.6

You can assign new values to individual indices of an already declared array:

declare array[5]: int[]
array[0] := 3

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:

array := [6,5,4,3]

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.

To reinitialize an already declared array, i.e., assign a value to every element of the array, a single enclosed value can be used, as already shown in declarations. In the following example, all values of the array will be set to 0.

array := [0]

These assignments can also be combined with the one-line multiple assignment syntax.


Assignments and Multidimensional Arrays

0.0.6

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.

matrix[2,3] := 12

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.

1
2
3
declare matrix[3, 3]: int[][] := [[1,2,3],[4,5,6],[7,8,9]]
matrix[*, *] := [-1]   // Reinitializes all elements to -1
matrix := [-1]         // Also reinitializes everything to -1

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.

declare matrix[3, 3]: int[][] := [[1,2,3],[4,5,6],[7,8,9]]
matrix[2, *] := [2]  // matrix := [[1,2,3],[4,5,6],[2,2,2]]

An NDArray can also be initialized by assigning a list of values to a specific dimension.

declare matrix[3, 3]: int[][] := [[1,2,3],[4,5,6],[7,8,9]]
matrix[2, *] := [2, 3, 4]  // matrix := [[1,2,3],[4,5,6],[2,3,4]]

Here, the third row of matrix is overwritten with the tuple [2,3,4].


Assignments between Multidimensional Arrays

0.0.7

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.

1
2
3
declare matrix[3, 3]: int[][] := [[1,2,3],[4,5,6],[7,8,9]]
declare identity[3, 3] := [[1,0,0], [0,1,0], [0,0,1]]
matrix[*, *] := identity[*, *]  // matrix := [[1,0,0], [0,1,0], [0,0,1]]

A single dimension of an NDArray can also be copied to another dimension.

1
2
3
declare matrix[3, 3]: int[][] := [[1,2,3],[4,5,6],[7,8,9]]
declare identity[3, 3] := [[1,0,0], [0,1,0], [0,0,1]]
matrix[2, *] := identity[0, *]  // matrix := [[1,0,0], [0,1,0], [1,0,0]]

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.

1
2
3
declare matrix[3, 3]: int[][] := [[1,2,3],[4,5,6],[7,8,9]]
declare matrix_d3[3, 3, 3]: int[][][]
matrix_d3[1, *, *] := matrix[*, *] 

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

0.0.7

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.

1
2
3
declare arr[3]: int[]
arr := [1, 2, 3]
ndarray[0, *] := arr  // Assigns the values of arr to the first row of the NDArray

You can copy the values from a dimension of an NDArray into a one-dimensional array.

1
2
3
declare arr[3]: int[]
arr := [1, 2, 3]
arr := ndarray[0, *]  // Copies the first row of the NDArray into arr

Compound Assignments

0.0.7

In addition to regular assignments, CKSP supports compound assignments. These operators combine an arithmetic or logical operation with an assignment in a single step.

1
2
3
4
var := 5
var += 3    // Equivalent to: var := var + 3
var *= 2    // Equivalent to: var := var * 2
var -= 1    // Equivalent to: var := var - 1

Compound assignments are available for:

  • Addition (+=)
  • Subtraction (-=)
  • Multiplication (*=)
  • Division (/=)
  • String Concatenation (&=)

They work on all available variable types.

declare array[3]: int[] := [1, 2, 3]
array[0] += 10    // array := [11, 2, 3]

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.