# Resize the image

Given an $$\n\times m\$$ matrix $$\A\$$ and two integers $$\w,h\$$, output a matrix of $$\w\times h\$$ called $$\B\$$, such that $$B_{i,j} = \int_{i-1}^i\mathbb dx\int_{j-1}^j A_{\left\lceil \frac xw\cdot n\right\rceil,\left\lceil \frac yh\cdot m\right\rceil}\mathbb dy\text{ (1-index),}$$ $$B_{i,j} = \int_i^{i+1}\mathbb dx\int_j^{j+1} A_{\left\lfloor \frac xw\cdot n\right\rfloor,\left\lfloor \frac yh\cdot m\right\rfloor}\mathbb dy\text{ (0-index),}$$ or "split a square into $$\n\times m\$$ smaller rectangles, fill each with the value given in $$\A\$$, then resplit into $$\w\times h\$$ one and get average of each small rectangle" (which is a simple image rescaling algorithm and that's why this title is used)

Shortest code in each language wins. You can assume reasonable input range, which may give good to few languages though.

Test cases:

$$\begin{matrix}1&1&1\\ 1&0&1\\ 1&1&1\end{matrix}, (2,2) \rightarrow \begin{matrix}\frac 89&\frac 89\\ \frac 89&\frac 89\end{matrix}$$ $$\begin{matrix}1&1&1\\ 1&0&1\\ 1&1&0\end{matrix}, (2,2) \rightarrow \begin{matrix}\frac 89&\frac 89\\ \frac 89&\frac 49\end{matrix}$$ $$\begin{matrix}1&0\\0&1\end{matrix}, (3,3) \rightarrow \begin{matrix}1&\frac 12&0\\ \frac 12&\frac 12&\frac 12\\ 0&\frac 12&1\end{matrix}$$ $$\begin{matrix}1&0\\0&1\end{matrix}, (3,2) \rightarrow \begin{matrix}1&\frac 12&0\\ 0&\frac 12&1\end{matrix}$$

Sample solution just by definition

• Could you provide some test cases? Thanks Feb 26 at 20:09
• Also a worked example. I'm not really too sure what the question is asking. Feb 26 at 20:16

# Jelly,  20  14 bytes

I can't help but think that more of the manipulation could be put inside the reduction ƒ - done

xs€⁴Z¹ƭL¤ZÆmðƒ


A full-program accepting [w, h] and A that prints a representation of B.

Try it online!
Or see the test-suite (this uses the register ® in place of the 2nd program argument, ⁴ to make a reusable Link, and formats each result as a grid).

### How?

We repeat row elements w times, split these into chunks of length n transpose the result, and take the means, then we do the same to the result, but with h and m instead of w and n.

xs€⁴Z¹ƭL¤ZÆmðƒ - Main Link: [w,h]; A
ðƒ - reduce [A,w,h] by:
x              -   repeat (row elements) ([[1,2]]x3->[[1,1,1,2,2,2]])
⁴           -     2nd program argument, A
ƭ        -     call in turn:
Z          -     ...1st time: transpose
¹         -     ...2nd time: do nothing
L       -     length -> 1st=n; 2nd=m
s€            -   split each (row) into chunks of length (n or m)
Z     -   transpose
Æm   -   arithmetic mean (vectorises)
- implicit print


# Charcoal, 28 bytes

ＦＥ²Ｎ≔ＥιＥζ∕ΣＥμ§μ÷⁺×κＬμπιＬμζＩζ


Try it online! Link is to verbose version of code. Takes input in the order w, h, A. Explanation:

ＦＥ²Ｎ


Repeat twice, once for the width, once for the height.

≔ＥεＥζ∕ΣＥμ§μ÷⁺×κＬμπεＬμζ


Rescale and transpose the array.

»Ｉζ


Output the final array.