Shortcuts

Vectors

A list of tensors can be created from k values or previously allocated tensors or a combination of both. Vectors are created, retrieved and modified using the same vector() interface function.

q)v:vector(1 2;3 4 5.0)

q)vector v
1 2
3 4 5f

q)free v

Creating a vector

vector(input1;input2;..) vector pointer
Given k arrays and/or tensor pointers, creates and returns a pointer to a newly created vector of tensors.

Note

When tensor pointers are included in the arguments to vector(), the tensor’s memory is managed by the vector and the previous handle to the tensor is no longer valid without a reference increment (see addref).

q)a:tensor 1 2 3
q)b:tensor 4 5
q)v:vector(a; addref b; 6 7 8.0)

q)vector v
1 2 3
4 5
6 7 8f

q)tensor a      /new vector manages the tensor memory
'stale pointer
  [0]  tensor a
       ^
q)tensor b      /both b & v have access to the same tensor
4 5

q)ref b   /the tensor is referenced by b & v
2

q)free v

q)ref b  /reference count decremented
1

Retrieving vector values

vector(ptr) val
Parameters:

ptr (vector) – an api-pointer to a previously created vector of tensors.

Returns:

arrays for each tensor in the given vector.

vector(ptr;ind) val
Parameters:
  • ptr (vector) – an api-pointer to a previously created vector of tensors.

  • ind (long) – long index or set of indices.

Returns:

array(s) for each index given.

q)v:vector(1 2;3 4 5i;6 7e)

q)dtype v
`long`int`float

q)vector(v;1)
3 4 5i

q)vector(v;1 0)
3 4 5i
1 2

Setting vector values

vector(ptr;ind;val) null
Parameters:
  • ptr (vector) – an api-pointer to a previously created vector of tensors.

  • ind (long) – a long index or set of indices into the vector of tensors.

  • val (ptr) – a corresponding value or set of values/tensors to assign to the vector replacing existing values.

  • val – an api-pointer to previously created tensor(s).

Returns:

null

q)v:vector(1 2;3 4 5i;6 7e)

q)vector v
1 2
3 4 5i
6 7e

q)vector(v; 1; "new tensor")
q)vector(v; 2 0; (011b; 1 2 3h))

q)vector v
1 2 3h
"new tensor"
011b

q)t:tensor 95 96 97.0
q)vector(v;1;t) /v[1] replaced with tensor t

q)vector(v;1)
95 96 97f

q)tensor t  /tensor memory managed by vector
'stale pointer
  [0]  tensor t
       ^

Retrieving tensor pointers

Use the tensor() function to extract pointers from a given vector and optional indices.

tensor(vec) tensors
tensor(vec;ind) tensors
Parameters:
  • vec (vector-pointer) – an api-pointer to a previously created vector of tensors.

  • ind (long) – an optional long index or list of indices into the vector

Returns:

return tensor pointer(s) for each tensor in the vector or corresponding to supplied index or list of indices

q)v:vector(1 2 3.0; 4 5i; 6 7 8e)
q)t:tensor(v;1)

q)tensor t
4 5i

q)r:tensor(v;1 2)

q)r       / list of pointers
56554656
56554448

q)tensor each r
4 5i
6 7 8e

q)dtype each r
`int`float

Docs

Access documentation for k api to PyTorch

View Docs

Examples

Examples using the k api to PyTorch

Examples

Github

C++ library source code and q/k examples

Github