On this page:
scalar?
tensor?
tensor
shape
tlen
tref
list->tensor
build-tensor
rank
reshape
trefs
8.12

4 Tensor functions🔗ℹ

procedure

(scalar? s)  boolean?

  s : any
Returns #t if its argument is a scalar.

procedure

(tensor? t)  boolean?

  t : any
Returns #t if its argument is a tensor. A scalar? is also a tensor?.

procedure

(tensor element ...)  tensor?

  element : tensor?
Returns a new tensor formed from the provided element ... All provided elements must have the same shape.

procedure

(shape t)  shape?

  t : tensor?
If t is a scalar?, its shape is the empty list, (list). Otherwise, its shape is a list starting with the number of elements in t followed by the shape of the elements of t.

For example, the shape of
2.348
is (list).

The shape of
(tensor 1.0 3.1 2.9)
is (list 3).

The shape of
(tensor (tensor 1.0 3.1 2.9)
        (tensor 2.8 -6.34 0.98))
is (list 2 3).

procedure

(tlen t)  natural?

  t : tensor?
The number of elements in t.

For example,
(tlen (tensor 1.0 3.1 2.9))
is 3 and

(tlen
  (tensor (tensor 1.0 3.1 2.9)
          (tensor 2.8 -6.34 0.98)))
is 2.

procedure

(tref t i)  tensor?

  t : tensor?
  i : (and/c natural? (</c (tlen t)))
Returns the ith element of t.

For example,
(tref (tensor 1.0 3.1 2.9) 0)
is 1.0 and

(tref (tensor 1.0 3.1 2.9) 1)
is 3.1 and

procedure

(list->tensor lst)  tensor?

  lst : (listof tensor?)
Constructs a tensor using the members of lst as elements. All members of lst must have the same shape.

procedure

(build-tensor s filler)  tensor?

  s : shape?
  filler : (-> (listof natural?) scalar?)
Constructs a new tensor with shape s where each scalar is determined by invoking the function filler with a list representing the position of the scalar. The list is a sequence of indices of the recursively nested tensor elements.

(build-tensor (list 2 3) (λ (idx) (+ (ref idx 0) (ref idx 1))))
produces
(tensor (tensor 0 1 2)
        (tensor 1 2 3))

procedure

(rank t)  natural?

  t : tensor?
Returns the length of the shape of t.

procedure

(reshape s t)  tensor?

  s : shape?
  t : tensor?
Attempts to rearrange the nesting of scalars in t so that the resulting shape of the rearranged tensor is s. Fails if the rearrangement is not possible, i.e., when the product of the numbers in s does not equal the total number of scalars in t.

procedure

(trefs t b)  tensor?

  t : tensor?
  b : (list (and/c natural? (</c (tlen t))))
Here b is a list of indices into t. Returns a tensor consisting of the elements (tref t i) for each i in b in the order of appearance in b.