1
$\begingroup$

I have a formula to be calculated such as; $|n_1 - n_2| + |n_3 - n_4|$. I want to calculate it with two tuples of values such as;

$(n_1,n_2)$ and $(n_3,n_4)$

When I try this method, it is equivalent;

$$|310-111| + |440-342| = (310 + 440) - (111 + 342)$$

When I try this method, it is also equivalent but this method won't work with above equation;

$$|111 - 309| + |342 - 207| = (342 - 111) + (309 - 207)$$

What is the problem here, where am I thinking wrong? Thanks in advance...

$\endgroup$
  • $\begingroup$ Please elaborate what you mean by "this method won't work with the above equation." $\endgroup$ – Cameron Buie Mar 23 at 14:23
  • $\begingroup$ In the second method, I first substract the elements of each tuple and then add them up. It worked for the second equation. But when I tried same way with first equation, it didn't work. $\endgroup$ – Ozan Yurtsever Mar 23 at 14:24
  • 1
    $\begingroup$ Your "first method" appears to simply be assuming that $|n_1-n_2|+|n_3-n_4| \color{red}{=}^\dagger n_1-n_2+n_3-n_4 = (n_1+n_3)-(n_2+n_4)$. This should be obviously incorrect. You in effect just removed the absolute value signs entirely which changes the meaning of the expression in any situation where what is enclosed by the absolute value signs is negative. $\endgroup$ – JMoravitz Mar 23 at 14:27
1
$\begingroup$

The issue, here, is that you're ordering your tuple, and using the given order as if it is important in some way. We can instead see that $$|310-111|+|440-342|=(440-111)+(310-342).$$

As for why this is the case, it is because $|x|=-x$ when $x$ is negative, and $|x|=x$ otherwise. So, we have: $$|310-111|=310-111\\|440-342|=440-342\\|111-309|=-(111-309)=-111+309=309-111\\|342-207|=342-207.$$ From the above, we can regroup (by commutativity, distributivity, and associativity), once we've removed the absolute value bars.

Since you seem to be looking for a general formula, here's one way we can go.

Observe that, regardless of $x,y$ we will necessarily have $|x-y|=\max(x,y)-\min(x,y).$ In the case that $x=y,$ this is easy to prove. In the case that $x>y,$ we know that $x-y>0,$ so that it's again easy to prove. In the case that $x<y,$ the third example above illustrates how we can prove it.

Given that, we have for any $n_1,n_2,n_3,n_4$ that $$|n_1-n_2|+|n_3-n_4|=\bigl(\max(n_1,n_2)-\min(n_1,n_2)\bigr)+\bigl(\max(n_3,n_4)-\min(n_3,n_4)\bigr),$$ or equivalently, $$|n_1-n_2|+|n_3-n_4|=\bigl(\max(n_1,n_2)+-\min(n_1,n_2)\bigr)+\bigl(\max(n_3,n_4)+-\min(n_3,n_4)\bigr).$$

At this point, we could use associativity to drop the parentheses, then use commutativity to obtain $$|n_1-n_2|+|n_3-n_4|=\max(n_1,n_2)+\max(n_3,n_4)+-\min(n_1,n_2)+-\min(n_3,n_4).$$ Now we can use distributivity (together with the identity $-x=-1\cdot x$ to see that

\begin{eqnarray}|n_1-n_2|+|n_3-n_4| &=& \max(n_1,n_2)+\max(n_3,n_4)+-1\cdot\min(n_1,n_2)+-1\cdot\min(n_3,n_4)\\ &=& \max(n_1,n_2)+\max(n_3,n_4)+-1\cdot\bigl(\min(n_1,n_2)+\min(n_3,n_4)\bigr)\\ &=& \max(n_1,n_2)+\max(n_3,n_4)+-\bigl(\min(n_1,n_2)+\min(n_3,n_4)\bigr)\\ &=& \bigl(\max(n_1,n_2)+\max(n_3,n_4)\bigr)-\bigl(\min(n_1,n_2)+\min(n_3,n_4)\bigr)\end{eqnarray}

This certainly isn't the only way we could formulate it, but it gets the job done.

$\endgroup$
  • $\begingroup$ So, can't I generalize a formula for |n1−n2|+|n3−n4| to use it with any given differently ordered tuples ? $\endgroup$ – Ozan Yurtsever Mar 23 at 14:37
  • $\begingroup$ Not simply. It will explicitly depend on which of $n_1,n_2$ is greater, and which of $n_3,n_4$ is greater. I am in the process of adjusting my answer to address generalizing. $\endgroup$ – Cameron Buie Mar 23 at 14:40
3
$\begingroup$

$$|x| = \begin{cases} x & x\geq0\\ -x & x< 0 \end{cases}$$

So if $x\geq 0$ you can think of $|x|$ as $x$ as you did with the first equation.

However you can't do that with the second equation because $111-309$ is negative.

$\endgroup$
3
$\begingroup$

$|n_1-n_2|+|n_3-n_4|=(n_1+n_3)-(n_2+n_4)$ is true if $n_1\geq n_2$ and $n_3\geq n_4$. Only then, you can get rid of the absolute value signs (because $|x|=x\iff x\geq 0$).

$\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.