# How many seating arrangements of $m$ people in a row of $n$ seats are there if $k$ of the people must sit together?

Suppose there are five people $$A, B, C, D, E$$ to be seated in a row of eight seats $$S_1, \ldots, S_8$$.

(1) How many possibilities are there if $$A$$ and $$B$$ are to sit next to each other?

(2) How many possibilities are there if $$A$$ and $$B$$ are not to sit next to each other?

My Attempt:

First Approach:

There are $$8$$ options of a seat for $$A$$. However, if $$A$$ is to be seated in $$S_1$$ or $$S_8$$, then $$B$$ can only be seated in $$S_2$$ or $$S_7$$, respectively. On the other hand, if $$A$$ is to be seated in any one of $$S_2$$ through $$S_7$$, then $$B$$ has two options of seat in each case.

Once $$A$$ and $$B$$ have been seated, there are $$6$$ seats left for $$C$$, and then $$5$$ seats left for $$D$$, and finally $$4$$ seats left for $$E$$.

In this way, the total number of seating arrangements are $$2 \times 1 \times 6 \times 5 \times 4 + 6 \times 2 \times 6 \times 5 \times 4 = 240 + 1440 = 1680.$$

Is this solution correct?

Second Approach:

Now let us treat $$A, B$$ as one entity. Then we have four entities to be accommodated in seven spots, for which there are $$7 \times 6 \times 5 \times 4 = 840$$ ways. And, each one of these $$840$$ arrangements, the members $$A$$ and $$B$$ of the block $$A, B$$ can be arranged within the block in two distinct ways. Therefore there are $$840 \times 2 = 1680$$ possible seating arrangements.

Is my answer correct? If so, then are both of my approaches also correct? If not, then where are the problems.

More generally, I have the following question:

Let $$k, m, n$$ be any natural numbers such that $$k \leq m \leq n$$. Then how many ways are there in total of seating $$m$$ people $$P_1 \ldots, P_m$$ in $$n$$ seats $$S_1, \ldots, S_n$$ such that some $$k$$ of these people insist on sitting next to each other?

My Attempt Using the Second Approach:

Let us treat that $$k$$ people as one block. Then there are

\begin{align} & (n-k+1) \underbrace{(n-k) (n-k-1) \ldots }_{(m-k) \mbox{ factors}} \\ &= (n-k+1) \big[ \ (n-k) (n-k-1) \ldots \big( (n-k) - (m-k-1) \big) \ \big] \\ &= (n-k+1)(n-k) \ldots (n-m+1) \\ &= ^{n-k+1}P_{n-m} . \end{align}

Finally, corresponding to each of the above arrangements with the $$k$$ people considered as one block, there are $$k!$$ ways of arranging the $$k$$ people within the block amongst themselves.

Hence there are a total of $$k! \ ^{n-k+1}P_{n-m}$$ ways of seating $$m$$ people in $$n$$ seats with $$k$$ people seated next to each other.

• The problem is not clearly stated until it is said which seats are next to each other. Among many possibilties two seem plausible: seats in a single row, or in a circle (in which case $S_1$ would be next to $S_8$). Nov 30, 2019 at 17:23

Both of your solutions to the first problem are correct. However, your general formula is not. Notice that in the first problem, $$n = 8$$, $$m = 5$$, and $$k = 2$$, so your formula gives $$2!P(8 - 2 + 1, 8 - 5) = 2!P(7, 3) = 2! \cdot 7 \cdot 6 \cdot 5 = 2 \cdot 210 = 420$$ Let's see what went wrong.

We wish to seat $$m$$ people, $$k$$ of whom must sit consecutively, in $$n$$ seats. Since the block takes up $$k$$ of the $$n$$ places, it must begin in one of the first $$n - (k - 1) = n - k + 1$$ positions. Once the block has been placed, there are $$n - k$$ seats left for the remaining $$m - k$$ people. They can be arranged in those seats in $$P(n - k, m - k)$$ ways. The people within the block can be arranged in $$k!$$ ways, which gives us the formula $$(n - k + 1)P(n - k, m - k)k!$$ As a sanity check, let's try our formula when $$n = 8$$, $$k = 2$$, and $$m = 5$$. It gives $$(8 - 2 + 1)P(8 - 2, 5 - 2)2! = 7 \cdot P(6, 3) \cdot 2! = 7 \cdot 6 \cdot 5 \cdot 4 \cdot 2 = 1680$$ which agrees with the answer you obtained in your example.

Observe that \begin{align*} (n - k + 1)P(n - k, m - k) & = (n - k + 1) \cdot \frac{(n - k)!}{[(n - k) - (m - k)]!}\\ & = \frac{(n - k + 1)(n - k)!}{(n - m)!}\\ & = \frac{(n - k + 1)!}{(n - m)!}\\ & = \frac{(n - k + 1)!}{[(n - k + 1) - (m - k + 1)]!}\\ & = P(n - k + 1, m - k + 1) \end{align*} so we could write our formula in the form $$P(n - k + 1, m - k + 1)k!$$ As a sanity check, note that if $$n = 8$$, $$k = 2$$, and $$m = 5$$, then $$P(8 - 2 + 1, 5 - 2 + 1)2! = P(7, 4)2! = 7 \cdot 6 \cdot 5 \cdot 4 \cdot 2 = 1680$$

• thank you so much for such a beautiful answer! Nov 30, 2019 at 18:06

For part $$a.)$$, we can work on two cases: $$AB$$ ($$A$$ to the left of $$B$$) and $$BA$$. The possibilities for $$AB$$ are $$S_{1}S_{2}, S_{2}S_{3}, …, S_{7}S_{8}$$. The possibilities for $$BA$$ are also the same. So the answer is
$$7 \times \binom{6}{3} \times 3! + 7 \times \binom{6}{3} \times 3! = 1680$$

The $$\binom{6}{3}$$ is the number of ways we can choose 3 seats, and $$3!$$ is number of possible orders of $$C,D,E$$ in the 3 seats.

For part $$b.)$$ count all possible seating without any rule, and then subtract with $$1680$$.


# Math Help - Counting-Permutations

1. ## Counting-Permutations

I have a question asking me to find:
The sum of all 4 digit numbers containing the digits 2,4,6,8 without repetitions

I realise that the digits can be chosen in (4)_4=4!=24 ways since repetition is not permitted and order does matter

The final answer is 133320

Any help would be great

2. Let the 24 numbers be $a_1,\dots,a_{24}$. Further, let the $i$th number $a_i$ be $1000b_{i1}+100b_{i2}+10b_{i3}+b_{i4}$ where all $b_{ij}$ are in {2,4,6,8}. Let's find $S_1=\sum_{i=1}^{24}b_{i1}$. Each of the four numbers is encountered 6 times, so the $S_1 = 6(2 + 4 + 6 + 8) = 120$. Therefore, the first digits of all numbers contribute 120 * 1000 to the whole sum. Similarly, the second digits contribute 120 * 100 and so on.

3. Originally Posted by qwerty10
I have a question asking me to find:
The sum of all 4 digit numbers containing the digits 2,4,6,8 without repetitions
The final answer is 133320
Think about it, each of those four numbers will be in each decimal place six times. The ones column adds up to $6(2+4+6+8)=120.$
Check out $\displaystyle120\left( {\sum\limits_{k = 0}^3 {10^k } } \right) = ~?$.

4. Hello, qwerty10!

Find the sum of all 4 digit numbers containing the digits 2,4,6,8 without repetitions.

The answer is: 133,320.

List the $4! = 24$ permutations of the four digits
,. . and consider their sum.

. . $\begin{array}{cc}
&2468 \\ &2486 \\ &2648 \\ &2684 \\ &2846 \\ &2864 \\ &\vdots \\
&8246 \\ &8264 \\ &8426 \\ &8462 \\ &8624 \\ + & 8642 \\ \hline
\end{array}$

We find that each column has: six 2's, six 4's, six 6's, six 8's.

The total of each column is: . $6\!\cdot\!2 + 6\!\cdot\!4 + 6\!\cdot\!6 + 6\!\cdot\!8 \:=\:120$

Hence, the addition has the form:

. . $\begin{array}{cccccc}
&&& 1 & 2 & 0 \\ && 1 & 2 & 0 \\ & 1 & 2 & 0 \\ 1 & 2 & 0 \\ \hline 1 & 3 & 3 & 3 & 2 & 0 \end{array}$

Therefore, the sum is: . $133,\!320$

Edit: Plato beat me to it . . . *sigh*
.


The question is as follows:

I have the general solution of a differential equation as $P(t) = Ae^{kt}$ where $k < 0$ is a constant, and it describes the mass $P(t)$ of a radioactive isotope at future time $t$. I also have that the mass of the isotope is $10$ at time $0$ and it is $5$ at time $2$. I want to find the remainder of the isotope at time $3$.

So I can use that $P(0) = 10$ to obtain $A$ = $10$, and here now is where I get a bit confused. If I use $P(2) = 5$, I get $5 = 10e^{2k}$ and solving for $k$ I get $k = \frac{ln(0.5)}{2} = -0.34657$. And then substitute it all back in to get $P(3)$

However, when looking at other questions online, they talk a lot about half-life of the substance and we have that if we let $\tau$ denote the half-life of the substance, then $k = \frac{ln(2)}{\tau}$ (where they are using the general solution to be $Ae^{-kt}$ However, isn't $\tau$ in my case equal to $2$? So should I not be getting $k = \frac{ln(2)}{2}$ rather than $k = \frac{ln(0.5)}{2}$? Can someone please explain where I am going wrong?

Thank you

• You haven't don't anything wrong because $-\log{(0.5)}=\log{(2)}$. Which explains the negative sign, your solution is the same as theirs Mar 20 '18 at 12:51
• Ah ok I think I get where I am getting confused. So what I have written is fine, I just solved for $k$ where $k$ is negative whereas the one online solved for $k$ where $k$ is positive and when putting back into their formula they will take this into account and multiply it by negative $1$? Mar 20 '18 at 12:55

$Ae^{-kt}$ with $k=\frac{ln(2)}{\tau}$ is equivalent to $Ae^{kt}$ with $k=\frac{ln(0.5)}{\tau}$
• Thanks very much. One last thing, so using that then $P(3)$ is approx equal to $3.5$ yes? Mar 20 '18 at 12:58
• Yes, it makes sense as you can double check with half-life equal to two, which means that $P(4)=2.5$, and you know the exponential decay function is decreasing. Mar 20 '18 at 13:00


# 8.1.2 Insertion Sort

The list-sort-best-first procedure seems quite inefficient. For every output element, we are searching the whole remaining list to find the best element, but do nothing of value with all the comparisons that were done to find the best element.

An alternate approach is to build up a sorted list as we go through the elements. Insertion sort works by putting the first element in the list in the right place in the list that results from sorting the rest of the elements.

First, we define the list-insert-one procedure that takes three inputs: a comparison procedure, an element, and a List. The input List must be sorted according to the comparison function. As output, list-insert-one produces a List consisting of the elements of the input List, with the input element inserts in the right place according to the comparison function.

(define (list-insert-one cf el p) ; requires: p is sorted by cf
(if (null? p) (list el)
(if (cf el (car p)) (cons el p)
(cons (car p) (list-insert-one cf el (cdr p))))))

The running time for list-insert-one is in $\Theta(n)$ where $n$ is the number of elements in the input list. In the worst case, the input element belongs at the end of the list and it makes $n$ recursive applications of list-insert-one. Each application involves constant work so the overall running time of list-insert-one is in $\Theta(n)$.

To sort the whole list, we insert each element into the list that results from sorting the rest of the elements:

(define (list-sort-insert cf p)
(if (null? p) null
(list-insert-one cf (car p) (list-sort-insert cf (cdr p)))))

Evaluating an application of list-sort-insert on a list of length $n$ involves $n$ recursive applications. The lengths of the input lists in the recursive applications are $n-1$, $n-2$, $\ldots$, 0. Each application involves an application of list-insert-one which has linear running time. The average length of the input list over all the applications is approximately $\frac{n}{2}$, so the average running time of the list-insert-one applications is in $\Theta(n)$. There are $n$ applications of \scheme|list-insert-one|, so the total running time is in $\Theta(n^2)$.

Exercise 8.5. We analyzed the worst case running time of list-sort-insert above. Analyze the best case running time. Your analysis should identify the inputs for which list-sort-insert runs fastest, and describe the asymptotic running time for the best case input.

Exercise 8.6. Both the list-sort-best-first-sort and list-sort-insert procedures have asymptotic running times in $\Theta(n^2)$. This tells us how their worst case running times grow with the size of the input, but isn't enough to know which procedure is faster for a particular input. For the questions below, use both analytical and empirical analysis to provide a convincing answer.
a. How do the actual running times of list-sort-best-first-sort and list-sort-insert on typical inputs compare?
b. Are there any inputs for which list-sort-best-first is faster than list-sort-insert?
c. For sorting a long list of $n$ random elements, how long does each procedure take? (See Exercise 8.2 for how to create a list of random elements.)


# Some interesting observations on a sum of reciprocals

This recent question is the motivation for this post.

Consider the following equation $$\frac1{x-1}+\frac1{x-2}+\cdots+\frac1{x-k}=\frac1{x-k-1}$$ where $$k>1$$.

My claims:

1. There are $$k$$ solutions, all of which are real.

2. Let $$x_{\min}$$ be the minimum value of these $$k$$ solutions. Then as $$k\to\infty$$, $$x_{\min}$$ converges. (If it does, to what value does it converge?)

3. As $$k\to\infty$$, all of the solutions get closer and closer to an integer, which is bounded below. Furthermore, these integers will be $$1, 2, 3, \cdots, k-1, k+1$$.

To see these patterns, I provide the solutions of $$x$$ below. I used W|A for $$k\ge4$$. The values in $$\color{blue}{\text{blue}}$$ are those of $$x_{\min}$$.

$$\begin{array}{c|c}k&2&3&4&5&6\\\hline x&4.414&4.879&5.691&6.592&7.530\\&\color{blue}{1.585}&2.652&3.686&4.701&5.722\\&&\color{blue}{1.468}&2.545&3.588&4.615\\&&&\color{blue}{1.411}&2.487&3.531\\&&&&\color{blue}{1.376}&2.449\\&&&&&\color{blue}{1.352}\end{array}$$

Also, when $$k=2$$, the polynomial in question is $$x^2-6x+7$$, and when $$k=3$$, it is $$x^3-9x^2+24x-19$$.

The reason why I think $$x_{\min}$$ converges is because the difference between the current one and the previous gets smaller and smaller as $$k$$ increases.

Are my claims true?

• It is easy to prove that it has at least $k-1$ distinct real soutions Aug 7, 2018 at 14:22
• and also it is true that $1<x_{min}<2$ for every $k$ and it converges to 1 Aug 7, 2018 at 14:24
• @Exodd: if you do that you know it has $k$ distinct solutions because of the limits of each side as $x \to \pm \infty$ Aug 7, 2018 at 14:27
• The method I used (removing the asymptotes) has been the basis of significant improvements in chemical engineering calculations (in particular for the so-called Rachford-Rice and Underwood equations). I have published quite a lor of papers for these. I had a lot of fun with your (may I confess that I cannot resist an equation ?). Cheers. Aug 8, 2018 at 8:05
• I updated my answer for some improvements. Cheers and thanks for the problem. Aug 10, 2018 at 8:05

## 3 Answers

Both your claims are true.

if you call $$f(x) = \frac1{x-1}+\frac1{x-2}+\cdots+\frac1{x-k}-\frac1{x-k-1}$$ then $f(1^+) = +\infty$, $f(2^-) = -\infty$ and $f$ is continuous in $(1,2)$, so it has a root in $(1,2)$. The same you can say about $(2,3)$, $(3,4), \cdots, (k-1,k)$, so there are at least $k-1$ real distinct roots. $f$ is also equivalent to a $k$-degree polynomial with the same root, but a $k$-degree polynomial with $k-1$ real roots has in reality $k$ real roots.

The last root lies in $(k+1,+\infty)$, since $f(k+1^+) = -\infty$ and $f(+\infty) = +\infty$.

The least root $x_{\min}$ must lie in $(1,2)$, since $f(x)<0$ for every $x<1$. Moreover, $$f(x) = 0\implies x = 1 + \frac{1}{\frac1{x-k-1}-\frac1{x-2}-\cdots-\frac1{x-k}}$$ and knowing $1<x<2$, we infer $\frac1{x-k-1}>\frac1{x-2}$ and $$1<x = 1 + \frac{1}{\frac1{x-k-1}-\frac1{x-2}-\cdots-\frac1{x-k}} < 1 - \frac{1}{\frac1{x-3}+\cdots+\frac1{x-k}}\to 1$$ so $x_{\min}$ converges to $1$

About the third claim, notice that you may repeat the same argument for any root except the biggest. Let us say that $x_r$ is the $r-th$ root, with $r<k$, and we know that $r<x_r<r+1$. $$f(x_r) = 0\implies x_r = r + \frac{1}{\frac1{x_r-k-1}-\frac1{x_r-1}-\cdots-\frac1{x_r-k}}$$ but $\frac1{x_r-k-1}>\frac1{x_r-1}$ holds, so $$r<x_r = r + \frac{1}{\frac1{x_r-k-1}-\frac1{x_r-1}-\cdots-\frac1{x_r-k}} < r - \frac{1}{\frac1{x_r-2}+\cdots+\frac1{x_r-k}}\to r$$ so $x_r$ converges to $r$.

For the biggest root, we know $k+1<x_k$ and $$f(x_k) = 0\implies k+1 < x_k = k+1 + \frac{1}{\frac1{x_k-1}+\cdots+\frac1{x_k-k}} \to k+1$$

• I would've argued that the LHS is continuous, monotone, and ranges from $-\infty$ to $+\infty$ between natural numbers, while the RHS is continuous and monotone, and since the LHS is asymptotically larger than the RHS as $x\to\infty$, there must be a solution $x>k+1$, where the LHS is less than the RHS. In any case, sound argument. Aug 7, 2018 at 14:52
• I think they're quite equivalent. Now I'm wondering if $x_{max}$ converges to $k+1$ Aug 7, 2018 at 14:57
• @Exodd Nice proofs. The third claim of converging to integers extends the claim $x_{\min}$ to all solutions of $x$. I strongly believe that it's true after plotting in Desmos, but I can't construct a rigorous proof for it. Aug 7, 2018 at 14:59
• Hard to exactly say what that's supposed to mean, $k+1$ is a moving target :P Aug 7, 2018 at 15:00
• Let's say $x_{max}(k)-k-1 = o_k(1)$ Aug 7, 2018 at 15:00

For the base case, $$\tag1f_2(x)=\frac1{x-1}+\frac1{x-2}-\frac1{x-3},$$ one readily verifies that there is a root in $(1,2)$ and a root $x^*$ in $(3,+\infty)$.

If we multiply out the denominators of $$f_k(x)=\frac1{x-1}+\frac1{x-2}+\ldots+\frac1{x-k}-\frac1{x-k-1},$$ we obtain the equation $$\tag2(x-1)(x-2)\cdots(x-k-1)f_k(x)=0,$$ which is a polynomial of degree (at most) $k$, so we expect $k$ solutions, but some of these may be complex or repeated or happen to be among $\{1,2,\ldots, k+1\}$ and thus not allowed for the original equation. But $f_k(x)$ has simple poles with jumps from $-\infty$ to $+\infty$ at $1,2,3,\ldots, k$, and a simple pole with jump from $+\infty$ to $-\infty$ at $k+1$, and is continuous otherwise. It follows that there is (at least) one real root in $(1,2)$, at least one in in $(2,3)$, etc. up to $(k-1,k)$, so there are at least $k-1$ distinct real roots. Additionally, for $x>k+1$ and $k\ge2$, we have $$f_k(x)\ge f_2(x+k-2).$$ It follows that there is another real root between $k+1$ and $x^*+k-2$. So indeed, we have $k$ distinct real roots.

From the aboive, the smallest root is always in $(1,2)$. If follows from $f_{k+1}(x)>f_k(x)$ for $x\in(1,2)$ and the fact that all $f_k$ are strictly decreasing there, that $x_\min$ decreases with increasing $k$. As a decreasing bounded sequence, it does have a limit.

Considering that you look for the first zero of function $$f(x)=\sum_{i=1}^k \frac 1{x-i}-\frac1 {x-k-1}$$ which can write, using harmonic numbers, $$f(x)=H_{-x}-H_{k-x}-\frac{1}{x-k-1}$$ remove the asymptotes using $$g(x)=(x-1)(x-2)f(x)=2x-3+(x-1)(x-2)\left(H_{2-x}-H_{k-x}-\frac{1}{x-k-1} \right)$$ You can approximate the solution using a Taylor expansion around $x=1$ and get $$g(x)=-1+(x-1) \left(-\frac{1}{k}+\psi ^{(0)}(k)+\gamma +1\right)+O\left((x-1)^2\right)$$ Ignoring the higher order terms, this gives as an approximation $$x_{est}=1+\frac{k}{k\left(\gamma +1+ \psi ^{(0)}(k)\right)-1}$$ which seems to be "decent" (and, for sure, confirms your claims). $$\left( \begin{array}{ccc} k & x_{est} & x_{sol} \\ 2 & 1.66667 & 1.58579 \\ 3 & 1.46154 & 1.46791 \\ 4 & 1.38710 & 1.41082 \\ 5 & 1.34682 & 1.37605 \\ 6 & 1.32086 & 1.35209 \\ 7 & 1.30238 & 1.33430 \\ 8 & 1.28836 & 1.32040 \\ 9 & 1.27726 & 1.30914 \\ 10 & 1.26817 & 1.29976 \\ 11 & 1.26055 & 1.29179 \\ 12 & 1.25403 & 1.28489 \\ 13 & 1.24837 & 1.27884 \\ 14 & 1.24339 & 1.27347 \\ 15 & 1.23895 & 1.26867 \\ 16 & 1.23498 & 1.26433 \\ 17 & 1.23138 & 1.26039 \\ 18 & 1.22810 & 1.25678 \\ 19 & 1.22510 & 1.25346 \\ 20 & 1.22233 & 1.25039 \end{array} \right)$$ For infinitely large values of $k$, the asymptotics of the estimate would be $$x_{est}=1+\frac{1}{\log \left({k}\right)+\gamma +1}$$

For $k=1000$, the exact solution is $1.12955$ while the first approximation gives $1.11788$ and the second $1.11786$.

Using such estimates would make Newton method converging quite fast (shown below for $k=1000$).

$$\left( \begin{array}{cc} n & x_n \\ 0 & 1.117855442 \\ 1 & 1.129429575 \\ 2 & 1.129545489 \\ 3 & 1.129545500 \end{array} \right)$$

Edit

We can obtain much better approximations if, instead of using a Taylor expansion of $g(x)$ to $O\left((x-1)^2\right)$, we build the simplest $[1,1]$ Padé approximant (which is equivalent to an $O\left((x-1)^3\right)$ Taylor expansion). This would lead to $$x=1+ \frac{6 (k+k (\psi ^{(0)}(k)+\gamma )-1)}{\pi ^2 k+6 (k+\gamma (\gamma k+k-2)-1)-6 k \psi ^{(1)}(k)+6 \psi ^{(0)}(k) (2 \gamma k+k+k \psi ^{(0)}(k)-2)}$$ Repeating the same calculations as above, the results are $$\left( \begin{array}{ccc} k & x_{est} & x_{sol} \\ 2 & 1.60000 & 1.58579 \\ 3 & 1.46429 & 1.46791 \\ 4 & 1.40435 & 1.41082 \\ 5 & 1.36900 & 1.37605 \\ 6 & 1.34504 & 1.35209 \\ 7 & 1.32741 & 1.33430 \\ 8 & 1.31371 & 1.32040 \\ 9 & 1.30266 & 1.30914 \\ 10 & 1.29348 & 1.29976 \\ 11 & 1.28569 & 1.29179 \\ 12 & 1.27897 & 1.28489 \\ 13 & 1.27308 & 1.27884 \\ 14 & 1.26787 & 1.27347 \\ 15 & 1.26320 & 1.26867 \\ 16 & 1.25899 & 1.26433 \\ 17 & 1.25516 & 1.26039 \\ 18 & 1.25166 & 1.25678 \\ 19 & 1.24844 & 1.25346 \\ 20 & 1.24547 & 1.25039 \end{array} \right)$$

For $k=1000$, this would give as an estimate $1.12829$ for an exact value of $1.12955$.

For infinitely large values of $k$, the asymptotics of the estimate would be $$x_{est}=1+\frac{6 (\log (k)+\gamma +1)}{6 \log (k) (\log (k)+2 \gamma +1)+\pi ^2+6 \gamma (1+\gamma )+6}$$

• Interesting! What does that notation $\psi^{(0)}(k)$ mean by the way? Aug 8, 2018 at 8:10
• @TheSimpliFire. This is "just" the digamma function. Aug 8, 2018 at 8:18
• @TheSimpliFire. We use to write $\psi^{(n)}(k)$ for the polygamma function and, in usual notations,$\psi^{(0)}(k)=\psi(k)$ Aug 8, 2018 at 8:22


# Probability of selecting elements with replacement

If I'm selecting $N$ elements uniformly at random (with replacement) from $\{1, \dots, N\}$, what is the chance that a given value is selected at least once?

What is the name for the distribution for the more general case where I'm selecting $N$ elements from a domain with $M$ elements?

• by random do you mean equally-probable ? – user451844 Sep 6 '17 at 23:31
• @Roddy Uniformly at random means equally-probable. The key word is "uniformly". – Trevor Gunn Sep 6 '17 at 23:51
• @TrevorGunn yeah I'm stupid sorry. – user451844 Sep 6 '17 at 23:55

## 3 Answers

Your answer is a particular case of the binomial distribution. Let getting a $1$ be a Success. You have $N$ independent trials. In the general version, the success probability is $p = 1/M.$

If $X \sim \mathsf{Binom}(N, 1/M),$ then you seek $$P(X > 0) = 1 - (1-p)^N = 1 - (1 - 1/M)^N = 1 - \left(\frac{M-1}{M}\right)^N,$$ as in the Answer of @ConMan (+1), which I hope you will Accept. [On each trial, you might say that you are using a discrete uniform distribution on the integers $1$ through $M$.]

Example: What is the probability you get at least one $6$ in ten rolls of a fair die? Then $X = \mathsf{Binom}(10, 1/6)$ is the number of $6$'s and you seek $P(X > 0) = P(X \ge 1) = 0.8385,$ computed in R statistical software as:

1 - dbinom(0, 10, 1/6)
## 0.8384944

In the figure below, you want the total heights of the bars to the right of the dotted red line.

• All the answers here were excellent, but I accepted this one since I mentioned binomial distribution and included a graphic, and because you fixed up my notation in the question. – BeeOnRope Sep 7 '17 at 17:26

If you're drawing with equal probability and subsequent draws are independent of each other, then the probability that you do not draw, for example, "1" in a single draw from $M$ elements is $\frac{M-1}{M}$.

The probability that you do not draw any "1"s in $N$ draws is the product of the individual probabilities, i.e. $\frac{M-1}{M}\cdot\frac{M-1}{M}\cdot\ldots\cdot\frac{M-1}{M}=\left(\frac{M-1}{M}\right)^N = \left(1 - \frac{1}{M}\right)^N$.

The probability that you draw at least 1 "1" in the $N$ draws is the complement of the probability that you draw none, i.e. it is $1 - \left(1 - \frac{1}{M}\right)^N$.

The distribution for drawing $M$ elements with replacement from a set of $N$ elements is the uniform distribution on all $M$-tuples from $\{1,\dots,N\}$. For example, with $M = 3$ and $N = 2$ we get 8 ($=2^3$) $3$-tuples:

$$(1,1,1), (1,1,2), (1,2,1), (1,2,2), (2,1,1), (2,1,2), (2,2,1), (2,2,2)$$

and each is equally likely.

Viewed this way, the question of how likely it is to see a given value at least once comes down to counting those sequences that contain the value somewhere. We can count this by counting the tuples that do not contain that value. We know that there are $N^N$ total $N$-tuples and $(N - 1)^N$ tuples not containing a specified value. Thus there are

$$N^N - (N - 1)^N$$

tuples that contain a specified value. Since the distribution is uniform, the probability of selecting such a tuple is

$$\frac{N^N - (N - 1)^N}{N^N} = 1 - \left( 1 - \frac{1}{N} \right)^N.$$

Now let's say that we have the elements $\{1,\dots,N\}$, we draw $M$ of them and we want at least $P$ of them to equal $1$ (or any other fixed element). Then to count the number of $M$-tuples with $P$ $1$'s, first we select the $P$ slots to put the $1$'s in, then from the $M - P$ remaining slots, we have $N - 1$ options each. This gives

$$\binom{M}{P} (N - 1)^{M - P}.$$

Now we may divide by the total number of $M$-tuples ($N^M$) to get a probability of

$$\binom{M}{P} \frac{(N - 1)^{M - P}}{N^{M - P}} \cdot \frac{1}{N^P} = \binom{M}{P} \left( 1 - \frac{1}{N} \right)^{M - P} \left( \frac1N \right)^{P}.$$

This is the probability mass function of the binomial distribution. Namely $\mathsf{Bin}(M, \frac{1}{N})$.


# Cross product in higher dimensions

Suppose we have a vector $$(a,b)$$ in $$2$$-space. Then the vector $$(-b,a)$$ is orthogonal to the one we started with. Furthermore, the function $$(a,b) \mapsto (-b,a)$$ is linear.

Suppose instead we have two vectors $$x$$ and $$y$$ in $$3$$-space. Then the cross product gives us a new vector $$x \times y$$ that's orthogonal to the first two. Furthermore, cross products are bilinear.

Question. Can we do this in higher dimensions? For example, is there a way of turning three vectors in $$4$$-space into a fourth vector, orthogonal to the others, in a trilinear way?

• You might want to look at the Gram Schmitt method here :en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process – Furrane Jul 25 '17 at 10:06
• This construction has nothing to do with cross product, just happen to coincide in dimension 3. – Miguel Jul 25 '17 at 10:54
• A search for "generalized cross product" turns up a number of questions likely to be of interest, including Is the vector cross product only defined for 3D? and Generalized Cross Product. ;) (Not marking as a duplicate because you're better able to judge which question, if any, most nearly matches yours.) – Andrew D. Hwang Jul 25 '17 at 17:38
• You might be interested in the notion of the orthogonal complement. It can give you the vector orthogonal to a given set of $n-1$ independent vectors in $n$-space, like you're asking for $n=4$. But it can also give you $k$ independent vectors orthogonal to a given set of $n-k$ independent vectors in $n$-space. So you can take two vectors in 4-space and find two vectors perpendicular to them and to each other. – YawarRaza7349 Jul 25 '17 at 17:39
• – user57159 Jul 26 '17 at 3:18

Yes. It is just like in dimension $$3$$: if your vectors are $$(t_1,t_2,t_3,t_4)$$, $$(u_1,u_2,u_3,u_4)$$, and $$(v_1,v_2,v_3,v_4)$$, compute the formal determinant:$$\begin{vmatrix}t_1&t_2&t_3&t_4\\u_1&u_2&u_3&u_4\\v_1&v_2&v_3&v_4\\e_1&e_2&e_3&e_4\end{vmatrix}.$$ You then see $$(e_1,e_2,e_3,e_4)$$ as the canonical basis of $$\mathbb{R}^4$$. Then the previous determinant is $$(\alpha_1,\alpha_2,\alpha_3,\alpha_4)$$ with\begin{align*}\alpha_1&=t_4u_3v_2-t_3u_4v_2-t_4u_2v_3+t_2u_4v_3+t_3u_2v_4-t_2u_3v_4\\\alpha_2&=-t_4u_3v_1+t_3u_4v_1+t_4u_1v_3-t_1u_4v_3-t_3u_1v_4+t_1u_3v_4\\\alpha_3&=t_4u_2v_1-t_2u_4v_1-t_4u_1v_2+t_1u_4v_2+t_2u_1v_4-t_1u_2v_4\\\alpha_4&=-t_3u_2v_1+t_2u_3v_1+t_3u_1v_2-t_1u_3v_2-t_2u_1v_3+t_1u_2v_3\end{align*}It's a vector orthogonal to the other three.

I followed a suggestion taken from the comments on this answer: to put the entries $$e_1$$, $$e_2$$, $$e_3$$, and $$e_4$$ at the bottom. It makes no difference in odd dimension, but it produces the natural sign in even dimension.

Following another suggestion, I would like to add this remark:$$\alpha_1=-\begin{vmatrix}t_2&t_3&t_4\\u_2&u_3&u_4\\v_2&v_3&v_4\end{vmatrix}\text{, }\alpha_2=\begin{vmatrix}t_1&t_3&t_4\\u_1&u_3&u_4\\v_1&v_3&v_4\end{vmatrix}\text{, }\alpha_3=-\begin{vmatrix}t_1&t_2&t_4\\u_1&u_2&u_4\\v_1&v_2&v_4\end{vmatrix}\text{ and }\alpha_4=\begin{vmatrix}t_1&t_2&t_3\\u_1&u_2&u_3\\v_1&v_2&v_3\\\end{vmatrix}.$$

• Very lucid and appreciable answer.thanks – Arpit Yadav Jul 25 '17 at 10:18
• I'm pretty sure the row of basis vectors should be on the bottom to get the right-handedness correct; only in odd dimensions can this row be moved to the top without a change in sign whilst keeping the vectors in the same order. – user332714 Jul 25 '17 at 18:06
• (+1) This is the method I've used in a few answers. The formula is easy to remember! – robjohn Jul 25 '17 at 22:50
• @lastresort Nice remark. I shall edit my answer taking that into account. – José Carlos Santos Jul 25 '17 at 22:54
• Is $$\alpha_1 ={\rm det} \left| \matrix{ t_2 & t_3 & t_4 \\ u_2 & u_3 & u_4 \\ v_2 & v_3 & v_4} \right|$$ and so on? – John Alexiou Jul 26 '17 at 14:19

My answer is in addition to José's and Antinous's answers but maybe somewhat more abstract. In principle, their answers are using coordinates, whereas I'm trying to do it coordinate-free.

What you are looking for is the wedge or exterior product. The exterior power $$\bigwedge^k(V)$$ of some vector space $$V$$ is the quotient of the tensor product $$\bigotimes^k(V)$$ by the relation $$v\otimes v$$. To be somewhat more concrete and less abstract, this just means that for any vector $$v\in V$$ the wedge product $$v\wedge v=0\in\bigwedge^2(V)$$. Whenever you wedge vectors together, the result equals zero if at least two of the factors are linearly dependent. Think of what happens to the cross product in $$\mathbb{R}^3$$.

In fact, let $$e_1,e_2,\ldots,e_n$$ be a basis of an inner product space $$V$$. Then $$e_{i_1}\wedge e_{i_2}\wedge \ldots \wedge e_{i_k}$$ is a basis for $$\bigwedge^k(V)$$ where $$1\leq i_1 < i_2 < \ldots < i_k\leq n$$.

If $$V=\mathbb{R}^3$$ then $$v \wedge w$$ equals $$v \times w$$ up to signs of the entries. This seems a bit obscure because technically $$v\wedge w$$ should be an element of $$\bigwedge^2(\mathbb{R}^3)$$. However, the latter vector space is isomorphic to $$\mathbb{R}^3$$. In fact, this relation is true for all exterior powers given an orientation on the vector space. The isomorphism is called the Hodge star operator. It says that there is an isomorphism $$\star\colon\bigwedge^{n-k}(V)\to\bigwedge^{k}(V)$$. This map operates on a $$(n-k)$$-wedge $$\beta$$ via the relation $$\alpha \wedge \beta = \langle \alpha,\star\beta \rangle \,\omega$$ where $$\alpha\in\bigwedge^{k}(V)$$, $$\omega\in\bigwedge^n(V)$$ is an orientation form on $$V$$ and $$\langle \cdot,\cdot \rangle$$ is the induced inner product on $$\bigwedge^{k}(V)$$ (see wiki). Notice that the wiki-page defines the relation the other way around.

How does all this answer your question you ask? Well, let us take $$k=1$$ and $$V=\mathbb{R}^n$$. Then the Hodge star isomorphism identifies the spaces $$\bigwedge^{n-1}(\mathbb{R}^n)$$ and $$\bigwedge^{1}(\mathbb{R}^n)=\mathbb{R}^n$$. This is good because you originally wanted to say something about orthogonality between a set of $$n-1$$ linearly indepedent vectors $$v_1,v_2,\ldots,v_{n-1}$$ and their "cross product". Now let us exactly do that and set $$\beta :=v_1 \wedge v_2 \wedge \ldots \wedge v_{n-1}\in\bigwedge^{n-1}(\mathbb{R}^n)$$. Then the image $$\star\beta = \star(v_1 \wedge v_2 \wedge \ldots \wedge v_{n-1})$$ is a regular vector in $$\mathbb{R}^n$$ and the defining condition above implies for $$\alpha=v_i\in\mathbb{R}^n=\bigwedge^{1}(\mathbb{R}^n)$$ $$v_i \wedge (v_1 \wedge v_2 \wedge \ldots \wedge v_{n-1}) = \alpha \wedge \beta = \langle \alpha,\star\beta \rangle \,\omega = \langle v_i,\star\beta \rangle \,\omega.$$ However, the left hand side equals zero for $$i=1,2,\ldots,n-1$$, so that the vector $$\star\beta$$ is orthogonal to all vectors $$v_1,v_2,\ldots,v_{n-1}$$ which is what you asked for. So you might want to define the cross product of $$n-1$$ vectors as $$v_1 \times v_2 \times \ldots \times v_{n-1} := \star(v_1 \wedge v_2 \wedge \ldots \wedge v_{n-1})$$.

Maybe keep in mind that the other two answers implicitly use the Hodge star operation (and also a basis) to compute the "cross product in higher dimension" through the formal determinant which is encoded in the use of the wedge product here.

• So concretely, how do we actually know what the hodge star of a $k$-blade is? For example, work in $4$-space with the standard orientation. Suppose we want to know $\star(v_1 \wedge v_3).$ If I understand correctly, it's either $v_2 \wedge v_4$ or else $v_4 \wedge v_2$. How do we know which one? – goblin GONE Jul 25 '17 at 13:32
• It depends on the orientation that you choose for your vector space. Let's say $v_1,v_2,v_3,v_4$ form an oriented basis for $V$ (that is, $\omega = v_1\wedge v_2\wedge v_3\wedge v_4$) then $\star(v_1\wedge v_3)=v_4\wedge v_2$. This can be seen using the defining relation for $\alpha=v_i\wedge v_j$ cycling through all possible combinations $(i,j)$. This is what they say on the wiki-page linked above in the section "Computation of the Hodge star" albeit expressed a little bit complicated in my opinion. – Sven Pistre Jul 25 '17 at 14:25
• Of all combinations $(i,j)$ only $(2,4)$ and $(4,2)$ remain (because otherwise the left hand side equals zero). Then you assume $\star(v_1\wedge v_3)=v_k\wedge v_l$ and think about which combinations for $(k,l)$ remain on the right hand side of the def. relation. Then you will see that the only possible one is $(k,l)=(4,2)$. To see the last part, look at the definition of the induced scalar product on $\bigwedge^2(V)$. – Sven Pistre Jul 25 '17 at 14:28
• And also, as I forgot to mention this, $v_2\wedge v_4=-v_4\wedge v_2$. Changing the position of two vectors in a $k$-wedge just changes the sign. So really it only depends on the chosen orientation (or "right-handedness") of your vector space. – Sven Pistre Jul 25 '17 at 14:49
• @étale-cohomology The hodge star depends on a choice of inner product and orientation. So it is not canonical. I don't think that you can identify them canonically. – Sven Pistre Jul 25 '17 at 17:42

You can work out the cross product $p$ in $n$-dimensions using the following:

$$p=\det\left(\begin{array}{lllll}e_1&x_1&y_1&\cdots&z_1\\e_2&x_2&y_2&\cdots&z_2\\\vdots&\vdots&\vdots&\ddots&\vdots\\e_n&x_n&y_n&\cdots&z_n\end{array}\right),$$ where $\det$ is the formal determinant of the matrix, the $e_i$ are the base vectors (e.g. $\hat{i},\hat{j},\hat{k}$, etc), and $x,y,\ldots,z$ are the $n-1$ vectors you wish to "cross".

You will find that $x\cdot p=y\cdot p=\cdots=z\cdot p=0$.

It's wonderful the determinant produces a vector with this property.

• Are there any requirements on the basis vectors $e_1, ..., e_n$? Like, do they need to form an orthonormal basis, or something? – étale-cohomology Jul 25 '17 at 17:01
• Yeah but do they have to be? Can't we take any other basis? – étale-cohomology Jul 25 '17 at 20:35
• By changing the basis to $\widetilde e_i$ you will have to change the vector entries to the coefficients $\widetilde x_i$ in the basis expansion for the new basis. Remember that the above is only a formal determinant as this is not actually a matrix (since the first column consists of entries that are vectors themselves). So it does not matter if the basis is orthonormal or not but you will have to adjust your formal determinant formula. – Sven Pistre Jul 25 '17 at 21:48
• (+1) This is the logical extension of José Carlos Santos' answer to $\mathbb{R}^n$ (at first, this is what I thought he had given, but now I see his only covers $\mathbb{R}^4$). – robjohn Jul 25 '17 at 22:53
• @robjohn I actually posted my answer 2 minutes before he did :-) – Pixel Jul 25 '17 at 23:00

Yes, and apart from other answers an interesting approach to think about it is using Clifford's algebra.

This can introduce you the basic concept in a nonrigorous but approachable manner.

• Thank you for your answer, however that article is extremely long and it's difficult to find a Clifford-style answer to my question by reading through it. Can I ask you to write up some details on how to compute an actual cross product using Clifford approach's? – goblin GONE Jul 26 '17 at 8:36


# Finding the interval of values of an unknown element such that a square matrix is positive semidefinite

I'm looking for a way to determine which values of $$c \in \mathbb{R}$$ make the matrix $$A$$ positive semidefinite:

$$A = \begin{bmatrix} 1 & c \\ c & 1 \end{bmatrix}$$

I'm looking for a way to determine which values of c∈R make the matrix A positive semidefinite:

By definition, A matrix is called positive semidefinite if it is symmetric and all its eigenvalues are non-negative. Hence

Clear["Global*"]
mat = {{1, c}, {c, 1}}
If[SymmetricMatrixQ[mat],
eigv = Eigenvalues[mat];
Reduce[eigv[[1]] >=  0 && eigv[[2]] >= 0, c]
]

So any value between -1 and 1 will do. The above code ofcourse assumes the matrix is 2 by 2, but it can easily be made more general.

• Congrats on 100K. Dec 6 '21 at 15:53

cp = CharacteristicPolynomial[{{1, c}, {c, 1}}, t]

(* Out[142]= 1 - c^2 - 2 t + t^2 *)

There are two important things to check. (i) Where do roots cross the y axis? (ii) Where might roots be complex-valued?

For (i) we just solve for c under the assumption thatt==0

Solve[cp == 0 && t == 0, {c, t}]

(* Out[143]= {{c -> -1, t -> 0}, {c -> 1, t -> 0}} *)

This splits the real line for c into three regions abd simply plugging in a value in each region will show that both solutions are nonnegative in the finite part.

For (ii) We check where the discriminant with respect to t vanishes. This will give conditions on c.

Discriminant[cp, t]

(* Out[144]= 4 c^2 *)

So it only vanishes at the origin. We already know that on neither side do we have complex valued roots in t. So there are no further restrictions.

An advantage to this approach is that it extends to higher dimensions.

If you compute the Cholesky decomposition of the matrix, and simplify it under the assumptions you gave:

Simplify[CholeskyDecomposition[{{1, c}, {c, 1}}], c ∈ Reals]
{{1, c}, {0, Sqrt[1 - c^2]}}

you can immediately read off the condition that Sqrt[1 - c^2] >= 0 for positive-semidefiniteness. Thus,

Reduce[%[[-1, -1]] >= 0, c, Reals]
-1 <= c <= 1
`


# Floor function algebra question

I don't know how to put floor functions in but...

Solve $$\dfrac{19x + 16}{10} = \left \lfloor \dfrac{4x+7}{3}\right \rfloor$$

I have so far worked out that the RHS can either be $$(4x+7)/3 - 0.33$$, $$(4x+7)/3 - 0.67$$ or itself. When I solve for each of these three equations, I get $$x=12/17, 22/17, 2/17$$. From there, I subbed $$x$$ back into the equation to try and see which one works but none did. Can I have some help?

• You can get the floor functions with \lfloor and \rfloor, e.g., \lfloor \pi \rfloor = 3. If you need bigger ones, as around a fraction, for instance, use \left\lfloor and \right\rfloor, e.g., \left\lfloor\frac{4x+7}{3}\right\rfloor. May 21 '20 at 3:24
• You seem to be assuming that $4x+7$ is an integer. There's no reason to think it is. May 21 '20 at 3:37
• X must be of the form $$10{\alpha}+6;\alpha \inf \mathbb{Z}$$ May 21 '20 at 4:53
• @saulspatz so would it be safe to say that the RHS is in the range of $(4x+7)/3$ to $(4x+7)/3-0.99$ ?
– user377742
May 21 '20 at 8:52
• Look at my answer. May 21 '20 at 16:09

I'll get you started.

We know that $$x-1<\lfloor x\rfloor<=x$$, so we have $$\frac{4x+4}3<\frac{19x+16}{10}\leq\frac{4x+7}3\\ 40x+40<57x+48\leq40x+70\\ \frac{-8}{17} so that $$x=n+\varepsilon$$ where $$n\in\{-1,0,1\}$$ and $$0\leq\varepsilon<1$$.

Now we can test each of the three possibilities for $$n$$ separately. Suppose $$x=1+\varepsilon$$. Then $$\frac{19x+16}{10}=\frac{35+19\varepsilon}{10}$$ is an integer between $$3.5$$ and $$5.4$$ so there are only two possibilities for $$\varepsilon$$. Check these to see if $$x=1+\varepsilon$$ satisfies the equation. Repeat the process for $$n=0$$ and $$n=-1$$.

Since algebra with floor functions is rarely nice, I like to graph it if possible to visualize the solutions. In your case, this is the graph of $$\frac{19x+16}{10}-\Big\lfloor \frac{4x+7}{3}\Big\rfloor$$

(This is using Desmos by the way) Notice how any solutions would pass through the $$y=0$$, and there seem to be $$4$$. One seems to occur at $$x \approx -.3158$$ and by plugging this into the floor function we see it approaches $$1.912$$, the floor of which is $$1$$. So we are looking for $$x$$ such that $$\frac{19x+16}{10} = 1$$. The solution is $$x = \frac{-6}{19}$$ which, by substituting it back in, works. Similarly, we can show that we need $$x \approx .2105$$ such that $$\frac{19x+16}{10} = 2$$, giving the solution $$x = \frac{4}{19}$$. Then we need $$x \approx .7368$$ such that $$\frac{19x+16}{10} = 3$$, giving the solution $$x = \frac{14}{19}$$. Finally, we need $$x \approx 1.2632$$ such that $$\frac{19x+16}{10} = 4$$, giving the solution $$x = \frac{24}{19}$$. From here, you can use strict inequalities to prove that there are no more solutions.

$$\dfrac{19x + 16}{10} = \left \lfloor \dfrac{4x+7}{3}\right \rfloor$$

Let $$\dfrac{19x + 16}{10} = n \in \mathbb Z$$.

Then $$x = \dfrac{10n-16}{19}$$

and $$\dfrac{4x+7}{3} = \dfrac{40n+69}{57}$$.

So

$$n \le \dfrac{40n+69}{57} < n + 1$$

$$57n \le 40n+69 < 57n + 57$$

$$0 \le -17n+69 < 57$$

$$-69 \le -17n < -12$$

$$\dfrac{12}{17} < n \le 4\dfrac{1}{17}$$

So now you can find the values of $$n$$ and then the values of $$x$$.

First deal with the fact that $$\dfrac{19x + 16}{10}$$ is an integer $$k$$.

So $$19x = 10k -16$$ is and integer $$m$$ and $$x = \frac m{19}$$ for some integer $$m$$.

Now deal with $$\dfrac{19x + 16}{10}=\lfloor \dfrac{4x+7}{3} \rfloor$$ so

$$\dfrac{19x + 16}{10} \le \dfrac{4x+7}{3}< \dfrac{19x + 16}{10}+1=\dfrac{19x +26}{10}$$

Replace $$x$$ with $$\frac m{19}$$ and

$$\dfrac {m + 16}{10} \le \dfrac {\frac 4{19}m + 7}3 < \frac {m+26}{10}$$

$$3m + 48 \le \frac 40{19}m +70 < 3m + 78$$

$$-22 \le \frac {40}{19}m - 3m < 8$$

$$-22*19 \le 40m - 57m = -17*m < 8*19$$

$$\frac {-8*19}{17} < m \le \frac {22*19}{17}$$

$$-8 \frac {16}{17} < m \le 24 \frac {10}{17}$$

So $$-8 < m \le 24$$

But $$m = 10k-16$$ so $$m\equiv -16\equiv -6 \equiv 4 \pmod{10}$$ so so

$$m =-6,4,14,24$$ are acceptable values.

And $$x =-\frac 6{19},\frac 4{19}, \frac {14}{19}$$ and $$\frac {24}{29}$$ are acceptable answers.


# How does ListInterpolation work?

ListInterpolation takes an array of data and interpolates between the entries. Let us interpolate between the elements of the identity matrix. For reference, this is what the identity matrix looks like:

MatrixPlot[IdentityMatrix[5]]

Now let's interpolate, and request first order interpolation:

if = ListInterpolation[IdentityMatrix[5], {{0, 1}, {0, 1}}, InterpolationOrder -> 1];
Plot[if[x, x], {x, 0, 1}]

The result is a piecewise function as expected, but not a piecewise linear function! Why? What does first order interpolation mean here?

The kind of linear interpolation I am familiar with is what ListDensityPlot does:

ListDensityPlot[IdentityMatrix[5], DataRange -> {{0, 1}, {0, 1}}, Mesh -> All]

Construct a Delaunay triangulation of the data and interpolate on each triangle.

This is not what ListInterpolation does (or even Interpolation when the data is strictly on a square grid). What does ListInterpolation do, then?

Inspired by this question:

• Well, it is doing bilinear interpolation in your first example, I would think. You definitely will get something with quadratic pieces if you sample across diagonals. – J. M. is in limbo Jun 15 '16 at 14:00
• @J.M. Well, we all have to learn at some point :-) Yes, you are right, and this is due to my ignorance of math. But I won't delete it. If you convert that to a one-line answer, I'll accept it. – Szabolcs Jun 15 '16 at 14:01
• Docs say the only methods available to ListInterpolation are Spline and Hermite – N.J.Evans Jun 15 '16 at 14:02
• Currently my comment is not that pedagogically useful; I'll try to come up with a sufficiently illuminating example first, unless somebody beats me to it. :) – J. M. is in limbo Jun 15 '16 at 14:04
• Szabolcs, please consider leaving the question up for a little longer. I was not aware of that behavior either, and I would find it instructive to see an interesting example, if @J.M. or others have the time / inclination. – MarcoB Jun 15 '16 at 14:43

I'll just treat the simplest case of interpolating across four noncoplanar points. The extension to general grids is straightforward (one just joins multiple pieces appropriately).

Consider the following bivariate interpolating polynomial:

ip[x_, y_] = InterpolatingPolynomial[{{{0, 0}, 1}, {{0, 1}, -1/2},
{{2, 0}, 1/2}, {{2, 1}, 0}}, {x, y}];

and the following InterpolatingFunction[]:

if = ListInterpolation[{{1, -1/2}, {1/2, 0}}, {{0, 2}, {0, 1}}, InterpolationOrder -> 1];

They are the same:

Plot3D[ip[x, y] - if[x, y] // Chop, {x, 0, 2}, {y, 0, 1}]

Let's have a look at what ip[] looks like:

Expand[ip[x, y]]
1 - x/4 - (3 y)/2 + (x y)/2

Huh. That last term certainly isn't linear. What gives?

In a bilinear interpolation such as this one, although the procedure is something along the lines of "linearly interpolate for each grid line in one direction, and then linearly interpolate those results", the result is inevitably quadratic. This should come as no surprise to people familiar with the hyperbolic paraboloid, which is one of the simplest examples of a ruled surface, or a surface formed by sweeping out a line in space. In fact, z == 1 - x/4 - (3 y)/2 + (x y)/2 is indeed a hyperbolic paraboloid.

{Plot3D[ip[x, y], {x, 0, 2}, {y, 0, 1}, MeshFunctions -> {#1 - #2 &}],
Plot[ip[x, x], {x, 0, 1}]} // GraphicsRow


# How to find out the global minimum of the following expression

What is the global minimum of the expression \begin{align} |x-1| &+ |x-2|+|x-5|+|x-6|+|x-8|+|x-9|+|x- 10| \\&+ |x-11|+|x-12|+|x-17|+|x-24|+|x-31|+ |x-32|? \end{align}

I've solved questions of this sort before but there were only 3 terms. I solved those by expanding all the terms in the modulus and drawing a graph. This question came in a paper which requires the student to solve it within 5 minutes. What's a better method?

Unfortunately I needed some minutes to think about the problem before finding a solution that can be calculated very quickly:

Imagine the graph of the function $$f_a(x)=|x-a|$$. Having the graph in mind you see that the derivation $$f'(x)=-1$$ for $$x and $$f'(x)=1$$ for $$x>a$$.

For the intervals: $$(-\infty,1)$$, $$(1,2)$$, $$(2,5)$$, ..., $$(32,\infty)$$ we can now easily calculate the derivative $$f'(x)=f'_1(x)+f'_2(x)+f'_5(x)+...-f'_{32}(x)$$:

In the range $$(-\infty,1)$$ it is $$f'(x)=-1-1-1-...-1+1=-11$$.
In the range $$(1,2)$$ it is $$f'(x)=+1-1-1-...-1+1=-9$$.
In the range $$(2,5)$$ it is $$f'(x)=+1+1+1-...-1+1=-7$$.
...

In every step we simply have to invert one sign so "-1" becomes "+1". This means the derivative is changing by 2 at the points x=1,2,5,...

We start by calculating the derivative for $$x<1$$; it is -11.

Now we simply go through the ranges:

<1: -11
1..2: -9
2..5: -7
5..6: -5
6..8: -3
8..9: -1
9..10: +1
10..11: +3
11..12: +5
12..17: +7
17..24: +9
24..31: +11
31..32: +13
>32: +11

At $$x=32$$ the derivation decreases by 2 because of the minus sign before $$|x-32|$$; you could of course adapt this method for sums of elements of the form $$b|x-a|$$.

We see that for $$x<9$$ the derivation is negative and for $$x>9$$ the derivation is positive. We also know that the function is continuous. (This is important because the derivation is not defined at x=1,2,5,...) This means that the function is strictly decreasing respectively increasing for $$x<9$$ and for $$x>9$$.

So we know that the global minimum must be at $$x=9$$.

• The right answer. Congratulations! Although I don't know if your method works when the number of terms is even, say $f(x) = |x-1|+ |x-2|$ or $f(x) = |x-11|+|x-12|+|x-17|+|x-24|$ (the minimum or maximum is an interval). Nov 25 '18 at 22:20
• @David Why should it be a problem if a whole interval represents the minimum or maximum? In the example of $|x-1|+|x-2|$ we would have $f'(x)=0$ in the whole interval 1...2. Nov 26 '18 at 6:28
• This answers the previous version of my typo question but the method is absolutely wonderful. Thanks!
– user619072
Nov 27 '18 at 17:36

You can in principle write out the function in a lot of intervals. But it would probably take too long. However, I will use this fact, without doing it explicitly. We know that if we do write this function, it is going to be linear on each interval (sum of linear functions is a linear function), and it's going to be continuous (sum of continuous functions is a continuous function). We also know that on a line you get minimum at one end, the other, or both (constant line). So all you need to do is to calculate your function at $$1,2,5,6,...$$ and find the minimum.

• I like how this answer asks us to imagine the graph of the function. I find this "imagine" technique useful, while still being intuitive. Similar examples includes "imagine the multiplication table of this finite group" (the group could be huge) or "imagine the truth table for this proposition" (the formula could be long). Nov 25 '18 at 22:02

The answer (minimizer) in this case is $$10$$, the median of the sequence $$(1,2,5,6,8,9,10,11,12,17,24,31,32).$$

You can plug in $$x=10$$ in the function and you would find that the minimum value is $$96$$. In general, the solution to the following minimization problem

$$\min\{|x-a_1| + |x-a_2| + \cdots + |x-a_n|\}$$ is the median of $$(a_1,\ldots,a_n)$$. To see why, consider first when $$n=2$$, and without loss of generality assume $$a_1. Then $$|x-a_1|+|x-a_2|$$ is the distance between $$x$$ and $$a_1$$ plus the distance between $$x$$ and $$a_2$$. It is easy to see that only when $$x$$ is in the middle of $$a_1$$ and $$a_2$$ should the sum of distances be minimal, which equals $$|a_2-a_1|$$ in this case. In this case the minimizer is not unique. Any points in $$[a_1,a_2]$$ is a minimizer.

When $$n=3$$, the function is $$|x-a_1|+|x-a_2|+|x-a_3|$$, and we order the parameters again so that $$a_1. When $$x$$ coincides with $$a_2$$, i.e. $$x=a_2$$, the value becomes $$|a_2-a_1|+|a_2-a_3|=|a_3-a_1|$$, the distance between $$a_3$$ and $$a_1$$. But when $$x\in[a_1,a_3], x\neq a_2$$, the value of the function is $$|x-a_2|+|x-a_1|+|x-a_3| = |a_3-a_1| + |x-a_2|,$$ which is largar than $$|a_3-a_1|$$, the distance between $$a_3$$ and $$a_1$$. Similarly the value would become larger when $$x$$ is outside $$[a_1,a_3]$$. So in this case, the minimizer is unique and is equal to $$a_2$$, the median of $$(a_1,a_2,a_3)$$.

In general, when $$n$$ is odd, there exists a unique minimizer, which is equal to the (unique) median of the parameters $$(a_1,\ldots,a_n)$$. When $$n$$ is even, the function is minimal and constant over the range $$[a_i,a_j]$$, where $$a_i$$ and $$a_j$$ are the two middle values.

• That doesn't seem to be correct. Wolfram says the minimum is 51 at x=9. See here Nov 25 '18 at 20:50
• I see now that the confusion comes from the $−|x−32|$ in the problem statement @MartinRosenau. Note the minus sign. Fei Li solved it for a + sign. Nov 25 '18 at 21:18
• I plot the graph using wxmaxima and get the same result as @IlikeSerena. Note the minus sign at the end. Nov 25 '18 at 21:48
• Maybe the OP @CaptainQuestion made a typo. We need his or her classification, but I think the purpose of this question is exactly what I have demonstrated. Nov 25 '18 at 22:00
• @AlexVong So I hoped, but the next sentence "which equals $|a_2-a_1|$ in this case." excludes this interpretation. Nov 25 '18 at 22:39

TL;DR: Put the absolute values in ascending order, and look at the sum of the leading coefficients. One by one, change the signs in the sum from right to left. When the sum changes signs, you have passed a local extremum. When the sum equals zero, there is an extremum on an entire interval.

As an alternative to Andrei's excellent answer, or perhaps an extension, you could also look at the derivative. Clearly the function is continuous everywhere, and it is differentiable in all but finitely many points, call them $$a_1,\ldots,a_n$$ in ascending order. Then we want to minimize $$f(x)=\sum_{k=1}^nc_k|x-a_k|=\sum_{k=1}^n(-1)^{\delta_{x\leq a_k}}c_k(x-a_k),$$ where $$\delta$$ denotes the Kronecker delta, in this case defined as $$\delta_{x\leq a_k}:=\left\{\begin{array}{ll}1&\text{ if } x\leq a_k\\0&\text{ otherwise}\end{array}\right..$$ This has derivative (for all $$x$$ apart from the $$a_k$$) $$f'(x)=\sum_{k=1}^n(-1)^{\delta_{x\leq a_k}}c_k.$$ The expression has a local minimum at $$x$$ if either $$f'(x)=0$$, or if $$x=a_k$$ for some $$k$$ and $$f'(y)<0$$ for $$a_{k-1} and $$f'(y)>0$$ for $$a_k.

This is all rather formal; in practice this means you put the $$c_k$$ in ascending order, so here $$n=13$$ and $$c_1=\cdots=c_{12}=1$$ and $$c_{13}=-1$$, and find all $$m$$ such that flipping the last $$m$$ signs in the sum $$c_1+c_2+c_3+c_4+c_5+c_6+c_7+c_8+c_9+c_{10}+c_{11}+c_{12}+c_{13}$$ makes the sum change signs compared to changing the last $$m-1$$ signs. Here a quick look gives $$c_1+c_2+c_3+c_4+c_5+c_6-c_7-c_8-c_9-c_{10}-c_{11}-c_{12}-c_{13}=1,$$ $$c_1+c_2+c_3+c_4+c_5-c_6-c_7-c_8-c_9-c_{10}-c_{11}-c_{12}-c_{13}=-1,$$ so $$f$$ has a local minimum at $$a_6=9$$, and it is not difficult to see that there is no other minimum.


# A mistake on computing $\int \frac{dx}{\sqrt{x+1}+1}$

I have to find $$\int \frac{dx}{\sqrt{x+1}+1}$$. This was my attempt, which is wrong and I cannot find where exactly is the mistake.

First I write $$\frac{1}{\sqrt{x+1}+1}=\frac{\sqrt{x+1}-1}{x}=\frac{\sqrt{x+1}}{x}-\frac{1}{x}$$, therefore $$\int \frac{dx}{\sqrt{x+1}+1}=\int \frac{\sqrt{x+1}}{x}dx-\log\left (x\right )$$, so I only have to deal with $$\int \frac{\sqrt{x+1}}{x}dx$$.

Setting $$u=\sqrt{x+1}$$, we obtain $$du=\frac{1}{2\sqrt{x+1}}dx$$, therefore $$dx=2udu$$ and $$x=u^2-1$$, so

$$\int\frac{\sqrt{x+1}}{x}dx=\int\frac{2u^2}{u^2-1}du=\int \left(2+\frac{2}{u^2-1}\right)du=2\sqrt{x+1}+\int \left(\frac{1}{u-1}-\frac{1}{u+1}\right)du=$$

$$=2\sqrt{x+1}+\log \left (\sqrt{x+1}-1\right )-\log \left (\sqrt{x+1}+1\right )=2\sqrt{x+1}+\log\left ( \frac{\sqrt{x+1}-1}{\sqrt{x+1}+1} \right )$$

But then $$\int \frac{dx}{\sqrt{x+1}+1}=2\sqrt{x+1}+\log\left ( \frac{\sqrt{x+1}-1}{\sqrt{x+1}+1} \right )-\log\left (x\right )=2\sqrt{x+1}+\log\left ( \frac{x+2-2\sqrt{x+1}}{x^2} \right )$$, which is not what I am supposed to obtain. The actual answer is $$2\sqrt{x+1}-2\log\left ( 1+\sqrt{x+1} \right )$$.

Where is my mistake?

(I already know a correct way to solve it, I just want to know where I am committing a mistake).

• I believe that you went wrong in your first step. That “simplification” made everything a lot harder. Hint: Start with a u-substitution if u=x+1 . – Sina Babaei Zadeh Mar 2 '19 at 22:36
• The standard method suggestst to set $u=\sqrt{x+1}$, i.e. $x=u^2-1,\; u\ge 0$, from the very beginning. – Bernard Mar 2 '19 at 22:44

But you have been right all along!

Notice that $$\log\frac{\sqrt{x+1}-1}{\sqrt{x+1}+1} - \log x = \log\frac{\sqrt{x+1}-1}{x(\sqrt{x+1}+1)} = \log \frac{1}{(\sqrt{x+1}+1)^2} = -2\log(\sqrt{x+1}+1)$$ by your very first step.

Also, I would suggest that you use $$\int\frac{dx}{x} = \log|x|$$ (with the absolute value sign, to extend the domain).

What I would also suggest is to use an online function grapher if you are not sure that your solution is correct. If two functions have identical graphs or if they differ by a constant value, then both are correct solutions of the integral.

• Oh, I see... My answer was correct, but it was not clear why. I wrote $\log\left ( \frac{\sqrt{x+1}-1}{\sqrt{x+1}+1} \right )=\log\left ( \frac{x+2-2\sqrt{x+1}}{x} \right )$ (i.e., i was rationalizing the denominator), but I should have rationalized the numerator, leading to $\log\left ( \frac{x}{\left ( \sqrt{x+1}+1 \right )^2} \right )$, which gives the solution. Thank you! – solomeo paredes Mar 2 '19 at 23:04

Note that $$\ln\frac{x+2-2\sqrt{x+1}}{x^2}=\ln\frac{(\sqrt{x+1}-1)^2}{x^2}=2\ln\frac{\sqrt{x+1}-1}{x}$$ if we further simplify, $$2\ln(\frac{\sqrt{x+1}-1}{x}\cdot\frac{\sqrt{x+1}+1}{\sqrt{x+1}+1}) =2\ln\frac{1}{\sqrt{x+1}+1}=-2\ln(\sqrt{x+1}+1)$$ You did nothing wrong.

Note that $$\frac{x+2-2\sqrt{x+1}}{x^2} =\frac {(x+2)^2-4(x+1)} {x+2+2\sqrt{x+1}} \cdot\frac 1{x^2}\ ,$$ and this has nothing to do with $$\frac{\sqrt{x+1}-1}{\sqrt{x+1}+1} \cdot \frac 1x = \frac{(\sqrt{x+1}-1)(\sqrt{x+1}+1)}{(\sqrt{x+1}+1)(\sqrt{x+1}+1)} \cdot \frac 1x = \frac 1{(\sqrt{x+1}+1)^2}\ ,$$ which leads to the solution. So the last step is the bad one.

You're fine.

$$\log\left(\frac{\sqrt{x+1}-1}{\sqrt{x+1}+1}\right) -\log x$$

$$= \log\left(\frac{\sqrt{x+1}-1}{\sqrt{x+1}+1}\frac{\sqrt{x+1}+1}{\sqrt{x+1}+1}\right) -\log x$$

$$=\log\left( \frac{x}{ (\sqrt{x+1}+1)^2}\right) -\log x$$

$$=\log x -2\log(\sqrt{x+1}+1)- \log x$$

$$=-2\log(\sqrt{x+1}+1).$$

No mistake! $$log((\frac{\sqrt{x+1}-1}{x})^2)=-2log(\frac{x}{\sqrt{x+1}-1})=-2log(\frac{x(\sqrt{x+1}+1)}{(\sqrt{x+1}-1)(\sqrt{x+1}+1)})=-2log\frac{x(\sqrt{x+1}+1)}{x}$$