Native functions standard library

Math

math.copy(A,B)

Copy the array B in the array A, element by element: A_{i} = B_{i}.

math.fill(A,c)

Fill each element of the array A with the constant c: A_{i} = c.

math.addscalar(A, B, c)

Compute A_{i} = B_{i} + c where c is a scalar.

math.add(A, B, C)

Compute A_{i} = B_{i} + C_{i} where A, B and C are three arrays of the same size.

math.sub(A, B, C)

Compute A_{i} = B_{i} - C_{i} where A, B and C are three arrays of the same size.

math.mul(A, B, C)

Compute A_{i} = B_{i} \cdot C_{i} where A, B and C are three arrays of the same size.

Note that this is not a dot product.

math.div(A, B, C)

Compute A_{i} = B_{i} / C_{i} where A, B and C are three arrays of the same size.

An exception will be triggered if a division by zero occurs.

math.min(A, B, C)

Write the minimum of each element of B and C in A where A, B and C are three arrays of the same size: A_{i} = \mathrm{min}(B_{i}, C_{i}).

math.max(A, B, C)

Write the maximum of each element of B and C in A where A, B and C are three arrays of the same size: A_{i} = \mathrm{max}(B_{i}, C_{i}).

math.clamp(A,B,C,D)

Clamp each element B_{i} and store it in A_{i} so that C_{i} < A_{i} < D_{i}.

math.dot(r, A, B, n)

Compute the dot product between two arrays of the same size A and B: r = \frac{\sum_{i}(A_{i}\cdot B_{i})}{2^{n}}

math.stat(V, min, max, mean)

Compute the minimum, the maximum and the mean values of array V.

math.argbounds(A, argmin, argmax)

Get the indices argmin and argmax corresponding to the minimum respectively maximum values of array A.

math.sort(A)

Sort the array A in place.

math.muldiv(A, B, C, D)

Compute multiplication-division using internal 32-bit precision: A_{i} = \frac{B_{i}\cdot C_{i}}{D_{i}}.

An exception will be triggered if a division by zero occurs.

math.atan2(A, Y, X) [1]

Compute A_{i}=\arctan\left(\frac{Y_{i}}{X_{i}}\right) using the signs of Y_{i} and X_{i} to determine the quadrant of the output, where A, Y and X are three arrays of the same size.

Note that X_{i} = 0 and Y_{i} = 0 will produce A_{i} = 0.

math.sin(A, B) [1]

Compute A_{i} = \sin(B_{i}) where A and B are two arrays of the same size.

math.cos(A, B) [1]

Compute A_{i} = \cos(B_{i}) where A and B are two arrays of the same size.

math.rot2(A, B, angle) [1]

Rotate the array B by angle, write the result to A.

Note that A and B must both be arrays of size 2.

math.sqrt(A, B)

Compute A_{i} = \sqrt{B_{i}} where A and B are two arrays of the same size.

math.rand(v)

Return a random value v in the range -32768:32767.

Since a scalar is considered to be an array of size one, you can use these functions on scalars:

var theta = 16384
var cos_theta
call math.cos(cos_theta, theta)

Double-ended queues

The Deque native library provides functions for double-ended queue operations in an object-oriented style on specially-formatted arrays. The array for a deque object must be of size $2 + m \cdot k\,\,$ where $k$ is the size of the tuples [2] in the deque, and $m$ is the maximum number of tuples to be stored.

An index $i$ into a deque is between two elements: the integer $i$ counts the number of elements to the left of the index.

deque.size(Q,n)

Set $n$ to the number of elements in deque $Q$. If $n=0$ then $Q$ is empty. Note that $n$ must be divided by the tuple size to obtain the number of tuples in $Q$.

deque.push_front(Q,A)

Insert tuple $A$ before the first tuple of deque $Q$.

deque.push_back(Q,A)

Insert tuple $A$ after the last tuple in deque $Q$.

deque.pop_front(Q,A)

Remove the first length($A$) elements of deque $Q$ and place them in tuple $A$. <<<<<<< HEAD

deque.pop_back(Q,A)

Remove the last length($A$) elements of deque $Q$ and place them in tuple $A$.


deque.pop_back(Q,A)

Remove the last length($A$) elements of deque $Q$ and place them in tuple $A$.

>>>>>>> nsis deque.get(Q,A,i) ^^^^^^^^^^^^^^^^^^^^^^^^ Copy into tuple $A$ , length($A$) elements from deque $Q$ starting from index $i$.

deque.set(Q,A,i)

Copy into deque $Q$ starting at index $i$, length($A$) elements from tuple $A$.

deque.insert(Q,A,i)

Shift right the suffix of deque $Q$ starting at index $i$ by length($A$) elements, then copy tuple $A$ into the deque $Q$ at that index.

deque.erase(Q,i,k)

Erase $k$ elements from deque $Q$ at index $i$ by shifting the suffix starting from $i+k$ left. Length $k$ should be the tuple size or a multiple.

Example

Here is a simple motion queue, that accepts operations defined by a time and motor speeds, and executes them first-in, first-out.

var operation[3] # Tuple of size 3
var Queue[2 + (3*40)] # Store up to 40 operation tuples
var n

sub motion_add
    call deque.push_back(Queue, event.args[0:2])

onevent timer0
    call deque.size(Queue, n)
    if n > 0 then
        call deque.pop_front(Queue, operation)
        timer.period[0] = operation[0]
        motor.left.target = operation[1]
        motor.right.target = operation[2]
    end
[1](1, 2, 3, 4) The trigonometric functions map the angles [-pi,pi[ radians to -32768,32767. The resultant sin and cos values are similarly mapped, namely [-1.0,1.0[ to -32768,32767.
[2]A tuple is simply a small array of values that are inserted in the deque together