3
$\begingroup$

For a programming assignment, I need to map integers to integers based on a factor. By knowing what the input and output should be, I was able to come up with the following function:

$g\left(f,c\right)=\begin{cases} \left\lfloor \left( c-1\right) \div f\right\rfloor +1 & f<1\\ c & f=1\\ \dfrac{c+\mod\left(f-\mod\left(c,f\right),f\right)}{f} & f>1 \end{cases}$

where $f\in\left(0,100\right]$ is the factor and $c>0$, in this case, stands for coordinate, and is an integer.

This appears correct with my initial testing. One thing I noticed, however, is that the $f=1$ case is actually unnecessary. Both the $f<1$ and $f>1$ cases return $c$ when $f$ is 1, so I'm wondering if, somehow, there's only one case instead of two or three, but I'm unable to see it because I'm unused to simplifying equations with the floor or mod functions.

Note that while I came up with these as part of a programming assignment, from a coding point of view, the above is sufficient. Simplifying the above function isn't part of the assignment, just my own math geek interest.

In case it helps, given a factor and an $x$- or $y$-coordinate within a resized image, the above is supposed to return the $x$- or $y$-coordinate of the original pixel that should be used in the resized image.

  • 0
    If I'm not mistaken, I'm pretty sure the $f > 1$ case can be simplified to $\lceil \frac{f}{c} \rceil$2017-01-15
  • 1
    If we take $f=1$ in the $>1$ case, it looks like we get $\frac {1+ \mod (c-1,c)}c=\frac {c}c=1$. No?2017-01-15
  • 0
    I don't see how you get $c$ out of the $>1$ case. I typed the formula in to Excel and, using $f=1,c=7$ got $1$. So at least Excel parses the formula the same way I do.2017-01-15
  • 0
    I messed up the translation from Numbers to a math equation, transposing the $c$ and $f$ variables. That's been corrected.2017-01-15
  • 0
    Is `mod` the floating-point mod function here?2017-01-15
  • 0
    @DavidK I haven't written the above in code yet, and I haven't tested non-integer values of $f$ yet, so I'm not sure. My *guess* is that it is, if I understand what you mean by "floating-point mod function," yes. I'm about to double-check in my spreadsheet, but $g(1.5,2)$ and $g(1.5,3)$ should both return 2.2017-01-15
  • 0
    So, post edit, taking $f=c$ in the $>1$ case yields $\frac {c+0}c=1$. Taking $f=c$ in the $<1$ case yields $\frac {c-1}c+1=2-\frac 1c$. Yes? So the expressions are quite different.2017-01-15
  • 0
    @lulu Since $c\geq1$ (because $c$ is a positive integer), it is not possible to have $f=c$ in the $<1$ case.2017-01-15
  • 0
    @DavidK I understand that. I believe the OP to be asking "are the $<1$ and $>1$ cases formally equivalent?" That is to say, he is wondering if he drop the inequalities. My example was intended to show that he can not. I may have misinterpreted the question...but then what is the question?2017-01-15
  • 0
    @DavidK In truth, the question appears to be a bit confused. if $f\in (0,100]$ is an integer...how can $f<1$? The problem states that we are mapping "integers to integers" which I took to mean that the inputs were integers. But who knows?2017-01-15
  • 0
    @lulu You're correct. if the first and third cases can have different return values, then my question is answered, at least partially. The cases can't be equivalent. ty. It may be too complicated to consider, but now I'm wondering if there's some other set of functions that are equivalent to the above and don't have that restriction.2017-01-15
  • 0
    @lulu $f$ needn't be an integer. The integer I'm mapping is $c$ to the result.2017-01-15

1 Answers 1

1

When $a \geq 0$ and $b > 0$, the "modulo" function $\mathrm{mod}(a,b)$ can be implemented by $$ \mathrm{mod}(a,b) = a - b\left\lfloor\frac ab\right\rfloor. $$

Bearing in mind that $\lfloor n + x \rfloor = n + \lfloor x \rfloor$ when $n$ is an integer and that $\lfloor -x \rfloor = -\lceil x \rceil$, the $f>1$ formula works as follows:

\begin{align} f-\mathrm{mod}(c,f) &= f - c + f\left\lfloor\frac cf\right\rfloor, \\ \mathrm{mod}(f-\mathrm{mod}(c,f),f) &= \left(f - c + f\left\lfloor\frac cf\right\rfloor\right) - f \left\lfloor \frac{\left(f - c + f\left\lfloor\frac cf\right\rfloor\right)}{f} \right\rfloor \\ &= f - c + f\left\lfloor\frac cf\right\rfloor - f \left\lfloor 1 - \frac cf + \left\lfloor\frac cf\right\rfloor \right\rfloor \\ &= f - c + f\left\lfloor\frac cf\right\rfloor - f \left(1 + \left\lfloor\frac cf\right\rfloor\right) - f \left\lfloor -\frac cf \right\rfloor \\ &= - c - f \left\lfloor -\frac cf \right\rfloor \\ &= - c + f \left\lceil \frac cf \right\rceil, \\ \frac{c+\mathrm{mod}(f-\mathrm{mod}(c,f),f)}{f} &= \frac{c+ \left(- c + f \left\lceil \frac cf \right\rceil\right)}{f} \\ &= \left\lceil \frac cf \right\rceil. \end{align}

A simpler way to define $g$ is therefore $$ g(f,c) = \begin{cases} \left\lfloor \dfrac{c-1}{f}\right\rfloor + 1 & f<1, \\ c & f=1,\\ \left\lceil \dfrac cf \right\rceil & f>1. \end{cases}$$

When $f = 1$, $\left\lceil\frac cf\right\rceil = c = \left\lfloor\frac{c-1}f\right\rfloor+1.$ The $f < 1$ formula converges to this value as $f$ approaches $1$ from below, and the $f>1$ formula converges to this value as $f$ approaches $1$ from above, just as we should hope would occur.

But $\left\lceil\frac cf\right\rceil$ and $\left\lfloor\frac{c-1}f\right\rfloor+1$ are not equal everywhere. For $f > 1$, they are close: $\left\lceil\frac cf\right\rceil - \left\lfloor\frac{c-1}f\right\rfloor+1$ is either $0$ or $1.$ But for $f < 1$ the two formulas can give very different answers.

One could view the difference between the two functions this way: when there is a choice of two or more possible pixels from which to get the value of the pixel at coordinate $c$, $\left\lceil\frac cf\right\rceil$ tends to choose one at or near the "top" of the range (the largest possible coordinate) while $\left\lfloor\frac{c-1}f\right\rfloor+1$ tends to choose one at or near the "bottom" of the range.

We could imagine some tricks using the floor function and/or absolute value applied to $f$ in some way to signal when to take the "top" coordinate and when to take the "bottom" coordinate, but I imagine that would make the formula much more complicated than it already is, even with the three cases. It seems a much better idea to just combine the $f=1$ case with one of the others and have two cases in the definition of $g$.

The "rounded average" of the two formulas, $$ \bar g(f,c) = \left\lfloor\frac12\left(\left\lceil\frac cf\right\rceil + \left\lfloor\frac{c-1}f\right\rfloor\right)\right\rfloor+1, $$ might be a reasonable compromise, as it would tend to choose the "middle" of the range of coordinates that might be mapped to $c$.