# How to repeat tensor elements to any size?

Is there a way to replicate/repeat/loop the values of a tensor to any size? I know PyTorch has repeat() and expand(), which allows for repetition of a tensor, but they only works for sizes that are multiples of the original size and it may be computationally wasteful to create tensor of bigger size and then remove the extra values. My question is, there a way to create an periodic sawtooth/triangle waveform/periodic pattern with the core PyTorch package that given a tensor defining a period it will repeat the pattern to any dimension/shape?

If we have a tensor like this:

``````>>> a = th.arange(1, 3, 0.5)
>>> a
tensor([1.0000, 1.5000, 2.0000, 2.5000])
``````

Can we get something like this:

``````>>> a.replicate(10)
tensor([1.0000, 1.5000, 2.0000, 2.5000, 1.0000, 1.5000, 2.0000, 2.5000, 1.0000, 1.5000])
``````

You could `repeat` the tensor and slice it:

``````a = torch.arange(1, 3, 0.5)

size = 10
a.repeat(size//a.size(0)+1)[:size]
``````

But, is there a way to do it with having to create additional value that will be sliced out? I am asking because if I need to do this in a loop, it will be computationally wasteful for periods that are long.

For my own curiosity, how does PyTorch implement repeat under the hood?