import torch

gainW = torch.tensor(1.1)
gainH = torch.tensor(2.5)
res = 5
a = torch.zeros(res,res)
a[0,0] = 0.1
for i in range(1, res):
    a[i, 0] = torch.cos(a[i-1, 0] * gainH)

for j in range(1, res):
    a[:, j] = torch.sin(a[:, j-1] + gainW)

This might be faster.
As your case requires the previous computation result to get the next, it’s difficult to avoid for loops entirely.