How do you do array calculations?

Hi @Max1, welcome to the forum! I have edited the title of your question to follow our guidelines.

Note that the “addition” and “multiplication” of several arrays in Typst are not mathematical operations: array1 + array1 will concatenate the arrays and array1 * 3 will concatenate 3 copies of array1.

For actual math operations, you have the functions array.sum and array.product that operate on the elements of an array:

#(1,2,3).product()  // returns 6

For operations with several arrays, you can use arrays functions like map, zip and fold to implement some of them easily. For example the sum of two vectors:

#let a = (1, 2, 3)
#let b = (0, 10, 100)

// Returns (1, 12 ,103)
#a.zip(b).map(array.sum)

I recently posted an example implementation of matrix multiplication along these lines: Minimal implementation of matrix multiplication

For heavy usage you can try to find a package that implements what you need. At some point there will probably be a comprehensive numerical package in the form of a WebAssembly plugin but I don’t think we’re there yet. But CeTZ itself has a lot of vector and matrix functions that you can access with cetz.util.vector and cetz.util.matrix. See for example here for the vector documentation. Note that these functions are documented as internals, so I guess they might change even in minor updates of the CeTZ package.

1 Like