Stagger, stack, sum
R, 88 81 bytes
function(A,N,P,o=0*diag(P*(N-1)+1)){for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o}
Try it online!
Takes a list
of matrices, A
, N
, and P
.
Builds the requisite matrix of zeros o
and adds elementwise the contents of A
to the appropriate submatrices in o
.
JavaScript (ES6), 102 bytes
Takes input as (n,p,a)
.
(n,p,a)=>[...Array(--n*p+1)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))
Try it online!
How?
Redimensioning matrices filled with a default constant (\$0\$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width \$w\$ right away:
$$w=(n-1)\times p+1$$
For each cell at \$(x,y)\$, we compute:
$$s_{x,y}=\sum_{i=0}^{p-1}{a_i(x-i\times (n-1),y-i\times (n-1))}$$
where undefined cells are replaced with zeros.
Jelly, 15 12 bytes
⁹ṖŻ€ƒZƲ⁺+µ@/
Try it online!
How it works
⁹ṖŻ€ƒZƲ⁺+µ@/ Main link. Argument: A (array of matrices)
µ Begin a monadic chain.
@/ Reduce A by the previous chain, with swapped arguments.
Dyadic chain. Arguments: M, N (matrices)
Ʋ Combine the links to the left into a monadic chain with arg. M.
⁹ Set the return value to N.
Ṗ Pop; remove its last row.
Z Zip; yield M transposed.
ƒ Fold popped N, using the link to the left as folding function and
transposed M as initial value.
Ż€ Prepend a zero to each row of the left argument.
The right argument is ignored.
⁺ Duplicate the chain created by Ʋ.
+ Add M to the result.