Inspired by this question - What are the disadvantages of being able to define a matrix without typing any words?
There is a more general question of what are some syntaxes for initialising tensors and where are their strengths and weaknesses.
There are some obvious cases to cover. Illustrated by pseudo-code below:
An array:
array[int] = {1,2,3};
A matrix:
matrix[int][int] = {{1,2,3},
{4,5,6},
{7,8,9}};
A rank3 tensor:
tensor3d[int][int][int] = {
{{1,2,3},
{4,5,6},
{7,8,9}},
{{10,11,12},
{13,14,15},
{16,17,18}},
{{19,20,21},
{22,23,24},
{23,25,27}},
};
You need to be able to specify:
- The rank - number of dimnensions
- The shape - the number of elements in each dimension
- The values - values for each item
- The types of values contained
You might want to specify the shape in advance or it might be implied by your initialisation. You might have a syntax allowing the shape to be inferred or requiring it to agree with the initialisation.
Things get hairier still if we think about sparse matrices (assume all elements are zero except those specified), triangular matrices or matrices where there is some symmetry.
You might also want to support populating them alogrithmically by defining a function e.g.
tensor[1:int, 2:int, 3:int] = f(x,y,z) -> int { //calc value for pos x,yz... };
for a 1 by 2 by 3 tensor.
There is quite a lot of scope for variation and verbosity here.
What is the prior art here and where do existing approaches succeed or fail?