2
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ Could you provide some test cases? Thanks \$\endgroup\$
    – Aiden Chow
    Feb 26 at 20:09
  • 3
    \$\begingroup\$ Also a worked example. I'm not really too sure what the question is asking. \$\endgroup\$
    – Aiden Chow
    Feb 26 at 20:16

2 Answers 2

1
\$\begingroup\$

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]])
        ¤      -   nilad followed by links as a nilad:
   ⁴           -     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
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 28 bytes

FE²N≔EιEζ∕ΣEμ§μ÷⁺×κLμπιLμζIζ

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

FE²N

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

≔EεEζ∕ΣEμ§μ÷⁺×κLμπεLμζ

Rescale and transpose the array.

»Iζ

Output the final array.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.