# Show that $s_n<2$

If $s_1=\sqrt{2}$ and $s_{n+1}=\sqrt {2+s_n}$ for $n\geq 1$ show that $s_n<2$ $\forall, n\geq1$ and $s_n$ is convergent.

The second part I supposed that is immediate from Cauchy sequence definition that $s_n$ is convergent.

The first part I think to use induction

For $n=1$ we have that $s_1=\sqrt{2}<2$

Suppose that is true for $n=k$ then we need to show for $n=k+1$. I wrote a few terms of this recursive sequence

$$s_1=\sqrt{2},\quad s_2=\sqrt{2+\sqrt{2}},\quad s_3=\sqrt{2+s_2}=\sqrt{2+\sqrt{2+\sqrt{2}}}$$ Then I think that $s_{n+1}$ can written as $$s_{n+1}=\sum_{i=1}^n2^{\frac{1}{2n}}$$

I do not know how to argue that the above term is less than $2$.

• $$s_{n+1} = \sqrt{2+s_n} < \sqrt{2 + 2} = 2$$ – peterwhy Jun 5 '17 at 23:25
• Your general form is wrong. – Simply Beautiful Art Jun 5 '17 at 23:25
• And it is not "obvious" that the sequence is Cauchy – Luiz Cordeiro Jun 5 '17 at 23:28

As peterwhy mentions,

$$s_n<2\implies s_{n+1}=\sqrt{2+s_n}<\sqrt{2+2}=2\tag{\color{green}\checkmark}$$

To show that it is convergent, you want to show that it is monotone increasing, which combined with the fact that it is bounded above will mean it converges:

$$s_n>s_{n-1}\implies s_{n+1}=\sqrt{2+s_n}>\sqrt{2+s_{n-1}}=s_n\tag{\color{green}\checkmark}$$

and thus you are done.

By the induction assumption, for some $k\in\mathbb N$, $$s_k < 2$$

Consider $n = k+1$,

$$s_{k+1} = \sqrt{2+s_k} < \sqrt{2 + 2} = 2$$

Together with the fact that $s_1 = \sqrt 2 < \sqrt 4 = 2$, by induction, all $s_n < 2$.

For $0<x<2$, $x^2 < 2x$ and $2x < 2 + x$.

To prove that the sequence $s_n$ is increasing,

\begin{align*}s_{n+1} &= \sqrt{2+s_n}\\ &> \sqrt{s_n + s_n}\\ &= \sqrt{2s_n}\\ &> \sqrt{s_n^2}\\ &= s_n \end{align*}

Since the sequence $s_n$ is increasing and bounded above, limit exists.

Hint: $\,s_{n+1}-2=(\sqrt {2+s_n}-2) \cdot \cfrac{\sqrt {2+s_n}+2}{\sqrt {2+s_n}+2} = \cfrac{2+s_n-4}{\sqrt {2+s_n}+2} = \cfrac{s_n-2}{\sqrt {2+s_n}+2}\,$, therefore $s_{n+1}-2$ has the same sign as $s_n -2\,$ and, by induction/telescoping, the same sign as $s_1-2\,$.

Using the fact that the square root function is increasing, and the induction hypothesis that $s_k<2$, we have: $$s_{k+1}=\sqrt{2+s_k}<\sqrt{2+2}=2$$

Assuming that the sequence does converge (you prove that later) write the limit as "S". Letting n go to infinity in $s_{n+1}= \sqrt{2+ s_n}$ we get $S= \sqrt{2+ S}$. Squaring both sides, $S^2= 2+ S$ so S must satisfy $S^2- S- 2= (S- 2)(S+ 1)= 0$ so S is either 2 or -1. Since all terms are positive, the limit (again, if there is a limit) must be 2.

Two show that this is convergent (so that the above is correct) you can show this sequence is increasing and bounded above. Since we got 2 as the limit above, it makes sense to show that $S_n< 2$ for all n. Yes, I would do that using induction on n. When n= 1, $S_n= \sqrt{2}< 2$. Assume that, for some k, $S_k< 2$. Then $S_{k+1}= \sqrt{2+ S_k}< \sqrt{2+ 2}= \sqrt{4}= 2$.


# Are positive definite matrices robust to “small changes”?

Let $A$ be a positive-definite matrix and let $B$ be some other symmetric matrix. Consider the matrix $$C=A+\varepsilon B.$$ for some $\varepsilon>0$. Is it true that for $\varepsilon$ small enough $C$ is also positive definite?

• Is $B$ required to be symmetric? – kimchi lover Jan 2 '18 at 1:02
• I guess, since "positive definite" usually refer to symmetric matrices. I have updated the question. – user_lambda Jan 2 '18 at 1:02
• Eigenvalues of $A+\epsilon B$ depends continuously on $\epsilon$. If they are all positive for $\epsilon=0$ then they are positive for all small $\epsilon$. – A.Γ. Jan 2 '18 at 1:37

Positive definite means that $\langle v, Av \rangle > 0$ for all nonzero vectors $v$; actually it suffices to check this condition for unit vectors. We have

$$\langle v, Cv \rangle = \langle v, Av \rangle + \epsilon \langle v, Bv \rangle.$$

Now, by the compactness of the unit sphere, $\langle v, Av \rangle$ takes on a minimum nonzero value $m$ on unit vectors (the smallest eigenvalue of $A$, although we don't need this), and $| \langle v, Bv \rangle |$ takes on a maximum nonzero value $M$ on unit vectors (the largest eigenvalue of $B$ in absolute value).

So we can take $\epsilon < \frac{m}{M}$, which gives

$$\langle v, Cv \rangle \ge m - \epsilon M > 0$$

for all unit vectors $v$. So $C$ is positive definite as desired.

• Why did you use the spectral radius of $B$ instead of $| \lambda_{\min} (B) |$? – Rodrigo de Azevedo Jan 2 '18 at 10:13
• That would also work. I just needed anything that would clearly give a lower bound. – Qiaochu Yuan Jan 2 '18 at 10:36

If by "positive definite" you mean "strictly positive definite", the answer is "yes". The set of strictly positive definite matrices is an open set in the space of symmetric matrices.

For the following reason. The positive definite matrices are the ones which satisfy a certain finite set of determinental inequalites (the principal minor determinants must all be strictly positive), each one of of which cuts out an open set in the space of matrices. Alternatively, from first principles, let $X$ be the closed unit sphere in vector space, let $Y$ be the symmetric matrices. The function $(v,A)\mapsto \|Av\|$ is continuous, so the set $S=\{(v,A): \|Av\|\le0\}\subset X\times Y$ is closed. Since $X$ is compact, the map $\pi:(v,A)\mapsto A$ is a closed map, so $\pi(S)$ is closed in $Y$, so the complement of $\pi(S)$ is open in $Y$. But that complement is the set of all $A$ for which $\|Av\|>0$ for all $v\in X$, that is to say, the set of all (strictly) positive definite matrices.

• And for positive semi-definite, it's strictly false: $\pmatrix{1 & 0 \\ 0 & 0 } + \epsilon \pmatrix{0 & 0 \\ 0 & -1}$ has eigenvalues $1, -\epsilon$ for every $\epsilon$. – John Hughes Jan 2 '18 at 1:09
• Is "strictly positive definite" different than "positive definite"? I'm unaware of that definition. – user_lambda Jan 2 '18 at 1:14
• @user_lambda: some authors use "positive definite" to mean "positive semidefinite," in the same way that some authors use "positive" to mean "nonnegative." – Qiaochu Yuan Jan 2 '18 at 1:17
• @QiaochuYuan: For what it's worth, I haven't heard of anyone using the word "positive" to mean "non-negative", but what I have seen is the notation of $\mathbb{R}_+$ to mean the non-negative reals. – Mehrdad Jan 2 '18 at 5:42
• @Mehrdad How about Bourbaki? – Daniel Fischer Jan 2 '18 at 15:16

To complement Qiaochu's answer, we have

$$\rm v^\top C \, v = v^\top A \, v + \varepsilon \, v^\top B \, v$$

where $\| \rm v \|_2 = 1$ and $\varepsilon > 0$. Note that

$$\{ \rm v^\top A \, v : \| v \|_2 = 1 \} = [ \lambda_{\min} (\mathrm A), \lambda_{\max} (\mathrm A) ]$$

$$\{ \rm v^\top B \, v : \| v \|_2 = 1 \} = [ \lambda_{\min} (\mathrm B), \lambda_{\max} (\mathrm B) ]$$

and, thus,

$$\{ \rm v^\top C \, v : \| v \|_2 = 1 \} = \left[ \color{blue}{\lambda_{\min} (\mathrm A) + \varepsilon \, \lambda_{\min} (\mathrm B)}, \lambda_{\max} (\mathrm A) + \varepsilon \, \lambda_{\max} (\mathrm B) \right]$$

We know that $\rm A \succ 0$, i.e., $\lambda_{\min} (\mathrm A) > 0$. If $\rm B$ is positive semidefinite, then $\lambda_{\min} (\mathrm A) + \varepsilon \, \lambda_{\min} (\mathrm B) > 0$, i.e., matrix $\rm C$ is positive definite for all values of $\varepsilon > 0$. After all, the conic combination of positive semidefinite matrices is also positive semidefinite. If $\rm B$ is not positive semidefinite, then

$$\lambda_{\min} (\mathrm A) + \varepsilon \, \lambda_{\min} (\mathrm B) = \lambda_{\min} (\mathrm A) - \varepsilon \, |\lambda_{\min} (\mathrm B)| > 0$$

yields the following upper bound on $\varepsilon$

$$\varepsilon < \color{blue}{\frac{\lambda_{\min} (\mathrm A)}{|\lambda_{\min} (\mathrm B)|}}$$


# Composition of functions

1. ### mnb96

638
This might be a silly question:

given a function $$g$$ is it possible to find a function $$f$$ such that $$f = f \circ g$$?

321
f=const.

3. ### csprof2000

287
That is the trivial solution. Perhaps there are others?

f(x) = f(g(x)) if...

f(x) = c, c constant.
g(x) = x, f any function.

If these two conditions don't hold, though...

Assume f has an inverse function. For instance, if f(x) = 2x, then (inv f)(x) = x/2. Then

f = f o g
<=>
f(x) = f(g(x))
<=>
x = g(x).

So if f has an inverse, g must equal x if f = f o g.

So you'd only be looking for functions which don't have an inverse.

But what we have is a little stronger than that, no? Since my argument makes no reference to intervals, it must be true on any interval. So g = x if you want a function f which is invertible over any interval.

The only function which is not invertible over any interval is - you guessed it - constant functions.

So, in summary:

if g(x) = x, then any function f(x) will do.

Otherwise, f(x) = c , c constant, is the only solution.

What about functions of more variables? Or generalized operations like differentiation? No idea.

4. ### John Creighto

811
If g(x)=x Then f(x) can be anything.

Let
g(n)=2n

Then f(n) can map even numbers to one constant and odd numbers to a different constant. (At least it works if n is discrete). Not sure if it works in the continuous case but I think it might.

Last edited: Feb 27, 2009
5. ### yyat

321
a) f(x)=abs(x), g(x)=-x
b) f(x)=cos(x), g(x)=x+2pi

6. ### csprof2000

287
Good point.

I guess then that my argument only works for functions which don't have an inverse where Domain(inv f) = Range(f). This, naturally, precludes functions such as abs(x) and cos(x)...

So I guess more though will have to be put into functions which are not bijections.

7. ### mnb96

638
Thanks a lot. You all made very good observations that helped me a lot.
BTW, it seems that if the function f admits an inverse there are not many choices, while if the function f is not invertible, many solutions exist but the problem is non-trivial, and it is difficult to say what kind of functions f and g would have to be, in order to satisfy f(x)=f(g(x)).

At the moment I am trying to solve the following (similar) problem:

$$f = (f \circ g) g'$$
where g is invertible, and g' denotes its derivative

If you find it interesting, suggestions are always welcome.
Thanks!

8. ### csprof2000

287
Well, similar suggestions - cases - are possible.

Assume f(x) = c, c constant. Then c = cg', g' = 1, and g = x + k, k constant.

Assume f(x) = x. Then x = gg', gdg = xdx, and (g^2)/2 = (x^2)/2 + k, k constant.

Assume f(x) = x^n. Then x^n = g^n g', g^n dg = x^n dx, [1/(n+1)]g^(n+1) = [1/(n+1)]x^(n+1) + k, k constant

Wow, that's an alright result. So for x to any power at all, it's possible. Is is true for any polynomial? Yes, it seems like it should be. So... for any polynomial, I believe you can use the above formula to reduce it.

f(x) = cos x, cos x = (cos g)g', (cos x)dx = (cos g)dg, sin(x) = sin(g) + k.

It seems like, unless I'm mistaken, this is the same thing every time:

f(x) = f(g(x))g'(x) is the same as solving the differential equation f(x)dx = f(g(x))dg.

So, as long as f is integrable, the problem is actually quite easy. Maybe I'm wrong. Thoughts?

9. ### mnb96

638
csproof2000: I think you just made it!
You gave the solution to the problem, and for some reason I didn't immediately spot that what I was trying to do is actually solving a differential equation.


# $-\iint_{A}(y+x)\,dA$ Evaluating

I am bit unsure about the following problem:

Evaluate the double integral:

$$-\iint_{A}(y+x)\,dA$$

over the triangle with vertices $(0,0), (1,1), (2,0)$

OK, so I figured here that I would do this by first evaluating the integral over the region bounded by the vertices $(0,0), (1,1), (1,0)$ and then evaluate the integral over the region bounded by the vertices $(1,0), (1,1), (2,0)$ before adding the two answers together, and then reversing the sign of this answer (since there is a minus sign in front of the original double integral). Thus, I begin by finding:

$$\int_{0}^{1}dx \int_{0}^{x}(y+x)\,dy$$

When solved this gives me the answer $\frac{1}{2}$.

Next I solve:

$$\int_{1}^{2}dx \int_{1}^{2-x}(y+x)\,dy$$

When solved this gives me the answer $-\frac{7}{6}$.

I have verified both the integrals in Wolframalpha, and they give me the same answer. I would therefore believe that the final answer should be:

$$-(\frac{1}{2} - \frac{7}{6}) = \frac{2}{3}$$

However, the final answer should, according to the book, be $-\frac{4}{3}$.

Thus, obviously I do something wrong here. If anyone can help me out, I would greatly appreciate it. Is it perhaps that it is not allowed to "split up" this into two separate integrals? I couldn't find a way to solve this without doing this.

-
I see one problem at least: The inner ($y$) integral in the second part should start at $0$. Anyway, you can avoid the splitting by doing the integrals with the $x$ integral on the inside. Try it! – Harald Hanche-Olsen Jul 15 '12 at 20:32
Thank you for your answer. But why should the second integral start at 0? The y-values being at 1 and end up at 0 (hence I chose y = 2-x for the upper value of the integral). – Kristian Jul 15 '12 at 20:39
Draw a picture. The whole triangle has its base at the $x$-axis, and so does each of the two pieces resulting from the split. – Harald Hanche-Olsen Jul 15 '12 at 20:41
You can also solve this with the Gauss integral theorem on $F(x,y) = xy$ and evaluate the integral on the boundary instead. Maybe this is easier, since the boundary is made of line segments. – Cocopuffs Jul 15 '12 at 20:42
It seems you may be confusing the two bounds. The bound that begins at $1$ and ends up at $0$ is the upper bound; the lower one is $0$ throughout. Also, you can tell from the sign that $-\frac76$ must be wrong, since the integrand is non-negative throughout the triangle. – joriki Jul 15 '12 at 20:45

Your second integral should be $$\int_{1}^{2}dx \int_{0}^{2-x}(y+x)dy.$$ Your lower $y$ limit was 1 instead of 0.


# Drawing the grid without lifting the pen

The below shape consist of $$24$$ segments with unit length. if we want to draw this shape without lifting the pen, what is the minimum length of the line we should draw? $$1)25\quad\quad\quad\quad\quad\quad2)26\quad\quad\quad\quad\quad\quad3)27\quad\quad\quad\quad\quad\quad4)28\quad\quad\quad\quad\quad\quad5)29\quad\quad\quad\quad\quad\quad$$

I think we can do it somehow by segments with the length $$28$$ (by passing twice the four middle segments on each side of outer big square).

I'm not sure how to solve this problem. is it possible to solve it mathematically or I should just try different ways to draw this?

Notice that if you draw it without lifting the pen, except for the starting and ending point you must draw the same number of lines going into each point as going out. This means that you must draw an even number of segments meeting every point except possibly two. How many extra segments does this imply you need at a minimum? Can you achieve this?

• Thank you for the answer. but unfortunately, I can't figure out how to continue from your hint. can you please elaborate more on that? Mar 11, 2021 at 17:04

You will be able to draw all the edges of the graph without lifting the pen and drawing every edge exactly once (starting and ending at the same node), iff there exists an Euler circuit in the graph.

We shall use the following theorem: a connected graph has an Euler circuit iff all the nodes of the graph has even degree.

The given grid graph does not have all nodes with even degree. Let's first find the nodes with odd degrees, as shown in the next figure. Notice that the nodes B,C, E,I, H,L and N,O have odd degrees (namely 3).

Let's add 4 additional (red) edges to the grid graph as shown in the next figure to make all the nodes have even degrees.

Now, in the augmented graph above, by the theorem, it must have an Euler circuit, so we shall be able to draw all the edges of the graph exactly once without lifting the pen.

We shall use Flury's algorithm to find an Euler circuit in the augmented graph first. The key idea is that when you have a choice between a bridge and a non-bridge, always choose the non-bridge (don't burn the bridges).

Since the augmented graph has all nodes with even degrees, we can start at any node, let's start at B, a node with degree 4 in the augmented graph.

Using the algorithm, the following animation shows how to construct an Euler circuit in the augmented graph.

Now, let's find where the additional edges (ones not present in the original graph) were used in the circuit found in the augmented graph.

Let's go back to our original graph by replacing those edges by the ones in the original graph, we had exactly 4 such additional edges needed to make all nodes in the original graph to have even degrees.

It implies that in order to have the Euler circuit in the original graph we need 4 additional edges (requiring 24+4=28 edges). It also means that in the original graph we need to traverse the corresponding 4 edges twice, in order to draw all the edges without lifting the pen, as shown below:

[EDIT]

If we don't need to start and end at the same node, it's sufficient to have an Euler trail instead of a circuit, needing one less edge: $$28-1=27$$ edges (only 3 edge segments, namely, EI, ON and HL are needed to be traversed twice, leading to minimum length of the line as $$24+3=27$$).

In this case the trail shown above starts at $$B$$ but ends at $$C$$ (instead of $$B$$ as before), since the edge $$BC$$ has already been visited once, we don't need to visit it again (as shown in the next figure).

• Wow! thank you very much for the answer and beautiful animation you used. I really appreciate that. Mar 11, 2021 at 19:15
• Great answer, except that you don't need an Euler circuit, but merely an Euler path. Which means we can save one line segment. Mar 12, 2021 at 1:04

The figure has 4 nodes with 2 lines connected to node. 8 nodes with 3 lines connected, and 4 nodes with 4 lines connected.

If you want to complete the circuit without lifting your pen, you can start at a node with an odd number of connections, and you can end at a node with an odd number of connections, for all the rest you must have an even number of connections. That is, if you get to the node on one path you leave the node on a different path.

This means that you must double up on some of the lines where it appears that there is a odd number of lines to make it an even number. What is the minimum number of lines you need to double up such that there are at most two nodes with an odd number of connections?

• Thank you very much. So for the start we connect two of the points with odd degrees and we left with three more segments passing through other odd degree points on other sides of outer big square. so we must pass them twice and the answer is $24+3=27$. am I right? Mar 11, 2021 at 17:42
• It must be at least 27. It is probably worth the effort to show that it can be done with 27 segments. Mar 11, 2021 at 17:46

Every node must have the same number of edges entering and leaving, except for the end points. There are 8 nodes on the graph with an odd degree, but we can make two of them the endpoints, so we have 6 nodes that need an "extra" edge. Finally, we can note that the odd-degree nodes come in adjacent pairs, so the "extra" edge leaving one node can be the "extra" edge entering another, so we only need half as many "extra" edges as we have non-endpoint, odd-degree nodes.


# Polynomial $p(x) = 0$ for all $x$ implies coefficients of polynomial are zero

I am curious why the following is true. The text I am reading is "An Introduction to Numerical Analysis" by Atkinson, 2nd edition, page 133, line 4.

$p(x)$ is a polynomial of the form:

$$p(x) = b_0 + b_1 x + \cdots + b_n x^n$$

If $p(x) = 0$ for all $x$, then $b_i = 0$ for $i=0,1,\ldots,n$.

Why is this true? For example, for $n=2$, I can first prove $b_0=0$, then set $x=2$ to get a linear system of two equations. Then I can prove $b_1=b_2 = 0$. Similarly, for $n=3$, I first prove $b_0=0$, then I calculate the rank of the resulting linear system of equations. That shows that $b_1=b_2=b_3=0$. But if $n$ is very large, I cannot keep manually solving systems of equations. Is there some other argument to show all the coefficients must be zero when the polynomial is always zero for all $x$?

Thanks.

-
Are you familiar with the fact that if $p(a) = 0$, then $p(x)$ is divisible by $x - a$? Use this $n+1$ times. –  Qiaochu Yuan Aug 28 '11 at 3:36
As explained in the answers, we can solve the problem by showing that a non-zero polynomial of degree $d$ has no more than $d$ roots. However, your approach can be made to work. To carry it out, one uses properties of the Vandermonde matrix. The details are somewhat more messy than the approach through counting roots, but accessible. –  André Nicolas Aug 28 '11 at 3:55
Hi Qiaochu Yuan, can you give me some more hints? I am aware of the Fundamental Theorem of Algebra, which can be found here: tutorial.math.lamar.edu/Classes/Alg/ZeroesOfPolynomials.aspx. If I divide a polynomial of degree $n$ by (x-a), and a is guaranteed to be a root (since the polynomial is a horizontal line at $y=0$), then I get as a result, another polynomial with degree $n-1$. After $n-1$ divisions, I get as a result, something like: $d + ex = 0$. $d$ and $e$ might not be the original polynomial coefficients. What would be the next step after this? –  jrand Aug 28 '11 at 4:35
For purposes of argumentation: false over finite fields... –  GEdgar Aug 28 '11 at 13:31
As you are interested in numerical methods the following is probably not of interest to you, but for the sake of completeness I feel compelled to point out that the conclusion is false, if your domain is finite. A non-zero polynomial can easily vanish at all points of, e.g. $\mathbf{Z}_m$ without being the zero polynomial. The easiest example is the polynomial $p(x)=x^2-x\in \mathbf{Z}_2[x]$ that vanishes at all the elements of $\mathbf{Z}_2$. If you don't study algebra, you can safely ignore this comment for now. –  Jyrki Lahtonen Aug 28 '11 at 13:37

HINT $\$ A nonzero polynomial over a field (or domain) has no more roots than its degree, as is easily proved by induction and the Factor Theorem. In fact if every natural was a root then the polynomial would be divisible by $\rm\:(x-1)\:(x-2)\:\cdots\:(x-n)\:$ for all $\rm\:n\in \mathbb N\:,\:$ which yields a contradiction for $\rm\:n\:$ greater than the degree of the polynomial.

Note that the proof of said statement depends crucially on the hypothesis that coefficient ring is an integral domain, i.e. a ring satisfying $\rm\:ab = 0\iff a=0\ \ or\ \ b=0\:.\:$ Over non-domains such as the integers modulo $\rm\:m\:$ not prime, polynomials can have more roots than their degree. In fact if this is true then one can use such roots to factor $\rm\:m\:,\:$ see here.

-
Following reading Bill Dubuque's, Shawn's, and Qiaochu Yuan's answer, I believe I must agree to the following fact. If a polynomial is equal to zero, then it must be of degree 0, and the coefficient must be set such that $b_0=0$. Since it is degree 0, the other coefficients must also be set to 0. –  jrand Aug 28 '11 at 12:42
@jrand: The degree of the polynomial zero is not zero. –  Did Aug 28 '11 at 13:02

Note that $p(x)=0$ implies derivatives of all orders are also $0$. Let $x=0$ into $p(x) = b_0 + b_1 x + \cdots + b_n x^n$ to obtain $b_0 = 0$.

Now differentiate both sides: $p'(x) = b_1 + 2b_2 x + 3b_3 x^2 + \cdots + nb_n x^{n-1}$. Let $x=0$ again, and we get that $b_1=0$.

If we continue to differentiate and substitution $x=0$ we will get that $b_k=0$ for $k=0,1,2,\cdots, n$. This idea can easily be made into a rigorous induction proof.

A nice corollary of this result is that two polynomials are equal if and only if they have the same degree and coefficients.

-

If you are willing to accept $\displaystyle \lim_{x \rightarrow \infty}\frac{1}{x^{m}}=0$ and $\displaystyle \lim_{x \rightarrow \infty}x^m=\infty$ for all $m>0$, then you can argue as follows. Suppose you have a polynomial $f(x)=b_nx^n+ \ldots b_0$ with $b_n \neq 0$ and $n\geq 1$, then $$\lim_{x\rightarrow \infty}f(x)=\lim_{x \rightarrow \infty} x^n(b_n+ \ldots \frac{a_0}{x^n})=\lim_{x \rightarrow \infty} x^nb_n=\infty.$$

Since the constant function $0$ does not have this property, it cannot be equal to a polynomial with degree greater or equal to $1$.

-
+1. Nice analytical solution with no derivatives. –  Did Aug 28 '11 at 22:22

A polynomial can be uniquely fitted by knowing its value at $d+1$ points, where $d$ is the degree of the polynomial. If it's 0 at all points, that's clearly more than $d+1$ points.

But we also know that a polynomial can be written as $(x-r_0)(x-r_1)...(x-r_k)$, where the $r_i$ are the roots of polynomial (possibly complex). Again, if there are an infinite number of roots...

-

Let $n\in \mathbb{N}$, and $$p(x)=b_0+b_1x+\ldots +b_nx^n$$ a polynomial of degree at most $n$. If $p(x)=0$ for every $x$, then $b_i=0$ for $i=0,\, 1,\ldots\, n$.

Proof:

By induction on $n$:

If $\deg(p)\leq 1$, then $p(x)=b_0+b_1x$. Since $p(0)=p(1)=0$ you get $b_0=b_1=0$.

Suppose that for any $$r(x)=c_0+c_1x+\ldots +c_nx^n$$ of degree at most $n$, if $r(x)=0$ for every $x$, then $c_i=0$ for $i=0,\, 1,\ldots\, n$. Let $$p(x)=b_0+b_1x+\ldots +b_{n+1}x^{n+1}.$$ Suppose that $p\equiv 0$. Since $p(0)=0$, you get that $b_0=0$. From here, I have two possible arguments. The first, that was my original one, is as follows:

We have $$p(x)=b_1x+b_2x^2+\ldots+b_{n+1}x^{n+1}=x(b_1+b_2x+\ldots+b_{n+1}x^{n}).$$ Then for all $x$, \begin{align*} 0&= p(x)\\ 0&= x(b_1+b_2x+\ldots+b_{n+1}x^{n}).\end{align*} Then for any $x\neq 0$, $$q(x):=b_1+b_2x+\ldots+b_{n+1}x^{n}=0$$ If $q\not\equiv 0$, then we have a polynomial of degree at most $n$ with infinitely many roots. This can't be, therefore $q\equiv 0$. Now, $\deg(q)\leq n$ and $q\equiv 0$, therefore by the induction hypothesis, $b_i=0$ for $i=1,\,\ldots ,\, n+1$.

The second argument was inspired by @Ragib Zaman's answer. Just differentiate both sides of $p(x)=0$, then $$b_1+2b_2x+\ldots+(n+1)b_{n+1}x^n=0$$ for all $x$. By the induction hypothesis, $kb_k=0$ for $k=1,\,\ldots,\,n+1$, and this implies that $b_k=0$ for $k=1,\,\ldots,\,n+1$

Since $b_0=0$, this proves that $b_i=0$ for $i=0,\, 1,\ldots,\, n+1$.

-
In general, by the fundamental theorem of algebra we can say that the number of different roots of a polynomial of degree $n$ is at most $n$. I the argument above $q$ must be identically $0$ because if not we have a polynomial with infinite different roots. –  leo Aug 28 '11 at 6:46
My comment is the hint of @Bill –  leo Aug 28 '11 at 7:12
This induction is flawed because during your nth inductive step you use as a fact a property (for every q of degree n, if q(x)=0 for every nonzero x, then q=0) that the hypothesis at the beginning of the step (for every polynomial p of degree n, if p(x)=0 for every x, then p=0) does not imply. –  Did Aug 28 '11 at 9:03
@Didier Piau: I'm trying to prove a statement about the coefficients of polynomials, not about the polynomials. If I understand correct your comment, you say me that the hypothesis of induction does not implies that the polynomial $q$ is identically $0$. Yes that is true, but i don't pretend it. The fact that $q\equiv 0$ is something that I want to apply my hypothesis of induction. –  leo Aug 28 '11 at 10:06
I think that the answer is now more clear. –  leo Aug 28 '11 at 10:14


# 2 different methods of calculating possibilities of people sitting at round table

I have 2 different problems that seem identical:

8 men and 8 women are going to sit at a round table where there are 16 seats. They take their seats randomly. How many ways can the 16 seats be taken so that no 2 women are sitting next to each other?

I solved this by doing $$2 * 8!8!$$

The method I used was that $8*8*7*7*6*6*5*5*4*4*3*3*2*2*1*1= 8!8!$ is all the ways you can sit these 8 women alternating them with the 8 men.

I then multiplied by two because you have 2 possibilities: the women can be sitting at the odd numbered seats, or at the even numbered ones.

Then I have this other problem:

In how many ways can a party of 4 men and 4 women be seated at a circular table so that no two women are adjacent?

(I took the problem from here: http://www.imsc.res.in/~kamalakshya/cupboard/comb_mag.pdf)

And the solution, according to the website, is:

Answer: The 4 men can be seated at the circular table such that there is a vacant seat between every pair of men in (4-1)! =3! Ways. Now 4 vacant seats can be occupied by 4 women in 4! Ways. Hence the required number of seating arrangements = 3!4! = 144

However, I can't seem to use the method from the 1st problem on this one. All the possible combinations of men and women, sitting alternated with men, would be $4!4!$ Then, multiplying by 2 the solution would be $2 * 4!4! = 1152$

If I use the method from the answer of the 2nd problem with the 1st one, I get $7!8!$, which is different from $2 * 8!8!$

My question is, how can this be? Is there any difference between these 2 problems that I am not seeing?

EDIT: I believe the 1st solution is correct. Check here Probability of men and women sitting at a table alternately

• You are assuming that the seats are labeled. The answer to the second problem assumes the seats are not labeled and takes rotational invariance into account. – N. F. Taussig Oct 16 '16 at 1:29
• Take into account circular permutations. The 1st solution is wring 2nd is correct. – Navin Oct 16 '16 at 1:30
• @navinstudent math.stackexchange.com/questions/1969572/… – SilenceOnTheWire Oct 16 '16 at 1:32
• @N.F.Taussig That makes sense, but how do you infer which method to use from reading the problems, without looking at their solutions? Neither problem mentions anything about the seats being labeled. – SilenceOnTheWire Oct 16 '16 at 1:52
• If the seats are not described as labeled, I would assume that they are unlabeled and that rotational invariance applies, as in the second problem. – N. F. Taussig Oct 16 '16 at 2:57


# Conditional Probability with balls in an urn

Two balls, each equally likely to be colored either red or blue, are put in an urn. At each stage one of the balls is randomly chosen, its color is noted, and it is then returned to the urn. If the first two balls chosen are colored red, what is the probability that (a) both balls in the urn are colored red; (b) the next ball chosen will be red?

I'm wondering if my method for part (a) is correct:

Let $P(R)$ be the probability of picking a red ball: $P(R)=\frac{1}{2}$

Let $P(B)$ be the probability of picking a blue ball: $P(B)=\frac{1}{2}$

Let $P(C)$ be the probability of the condition, i.e., picking two red balls consecutively: $P(C)=P(C|H_1)P(H_1)+P(C|H_2)P(H_2)+P(C|H_3)P(H_3)$

where $P(H_1)$ is the probability both balls in the urn are red: $P(H_1)=(P(R))^2=(\frac{1}{2})^2=\frac{1}4$

$P(H_2)$ is the probability that both balls in the urn are blue: $P(H_2)=(P(B))^2=(\frac{1}{2})^2=\frac{1}4$

$P(H_3)$ is the probability that one ball is red and one is blue inside the urn: $P(H_3)=1-(P(H_1)+P(H_2))=1-\frac{1}2=\frac{1}2$ since the sum of the mutually exclusive hypotheses or events must sum to 1.

Thus $P(C)=1\times\frac{1}4+\frac{1}4\times0+\frac{1}2\times\frac{1}4=\frac{3}8$

and $P(H_1|C)= \frac{P(C \bigcap H_1)}{P(C)}=\frac{P(C|H_1)P(H_1)}{P(C)}=\frac{1\times\frac{1}4}{\frac{3}8}=\frac{2}3$

For part (b), I know that $P(R|C)$, the probability of picking a red ball given that the first two balls picked were red is to be found

$P(R|C)= \frac{P(C \bigcap R)}{P(C)}=\frac{P(C|R)P(R)}{P(C)}$

How can I find $P(C|R)$?

-
There are three possible urns that you might have: both balls red (RR), both balls blue (BB), one of each color (RB) which occur with probabilities $1/4, 1/4, 1/2$ respectively. Now calculate $P(\text{red,red}\mid \text{RR})$, $P(\text{red,red}\mid \text{BB})$, $P(\text{red,red}\mid \text{RB})$, and combine them via the law of total probability to get $P(\text{red,red})$. Then, use Bayes' formula to obtain $P(\text{BB}\mid \text{red,red})$ – Dilip Sarwate Mar 2 '13 at 3:04

The probability that the contents of the urn are two red is indeed $\frac{1}{4}$, as is the probability of two blue, and the probability of mixed is therefore $\frac{1}{2}$. The derivation could have been done more quickly.

Question (a) asks for the probability both are red given that the two drawn balls are red. Let $R$ be the event both are red, and $D$ be the event both drawn balls are red. We want $\Pr(R|D)$. By the usual formula this is $\frac{\Pr(R\cap D)}{\Pr(D)}$.

To find $\Pr(D)$, note that if both balls are red (probability $\frac{1}{2}$), then the probability of $D$ is $1$, while if one ball is red and the other is not (probability $\frac{1}{2}$) then the probability of $D$ is $\frac{1}{4}$. Thus the probability of $D$ is $\left(\frac{1}{4}\right)(1)+\left(\frac{1}{2}\right)\left(\frac{1}{4}\right)$. This is $\frac{3}{8}$.

The probability of $R\cap D$ is $\frac{1}{4}$. So the ratio is indeed $\frac{2}{3}$.

For (b), you can use the calculation of (a). With probability $\frac{2}{3}$ we are drawing from a double red, and we will get red with probability $1$. With probability $\frac{1}{3}$ the urn is a mixed one, and the probability of drawing a red is $\frac{1}{2}$, for a total of $\frac{2}{3}\cdot 1+\frac{1}{3}\cdot\frac{1}{2}$.

One can also solve (b) without using the result of (a). With I hope self-explanatoru notation, we want $\Pr(RRR|RR)$. The probabilities needed in the conditional probability formula are easily computed. We have $\Pr(RRR\cap RR)=\Pr(RRR)=\frac{1}{4}\cdot 1+\frac{1}{2}\cdot\frac{1}{8}=\frac{5}{16}$. Divide by $\frac{3}{8}$.

-


# Finding a curve that intersects any line on the plane

Question Is there a curve on plane such that any line on the plane meets it (a non zero ) finite times ?

What are the bounds on the number of such intersections.

My question was itself inspired by this "Can you draw circles on the plane so that every line intersects at least one of them but no more than 100 of them?"

• Clearly if your curve is the graph of an odd polynomial, the number of intersections is at most the degree of the polynomial. – Cameron Williams Sep 18 '13 at 15:08
• One possibility is a pair of parabolas, say one concave up and one concave down, situated so that they touch tangentially (or intersect) and therefore cannot be separated by a hyperplane. – hardmath Sep 18 '13 at 15:08
• Please try to make titles of questions more informative. E.g., Why does $a\le b$ imply $a+c\le b+c$? is much more useful for other users than A question about inequality. From How can I ask a good question?: Make your title as descriptive as possible. In many cases one can actually phrase the title as the question, at least in such a way so as to be comprehensible to an expert reader. – Lord_Farin Sep 18 '13 at 15:08
• @CameronWilliams Correct, but just being an odd degree polynomial; does not guarantee that every line in the plane will intersect it – ARi Sep 18 '13 at 15:10
• @ARi: Actually it does imply that, except degree 1. – hardmath Sep 18 '13 at 15:13

Cubic parabola $$y=x^3$$ has this property. The max number of such intersections is given by the Fundamental theorem of algebra:

$$x^3=ax+b$$ can have at most 3 solutions.

• Yes but will all of the solutions be real for all real (a,b) pairs – ARi Sep 18 '13 at 15:14
• For any $a$, there will be only one real solution for all sufficiently large $|b|$. – hardmath Sep 18 '13 at 15:17
• I think the important fact here (i.e., relevant to the question), is that $\,x^3-ax-b=0\;$ has always at least one real solution and, of course, at most three different ones. Nice answer. +1 – DonAntonio Sep 18 '13 at 15:22
• So the upper bound for intersections will be three and the lower one 0. – ARi Sep 18 '13 at 15:29
• Well, a tighter and always attainable lower bound is one, in fact...and this fulfills the question's conditions. – DonAntonio Sep 18 '13 at 15:31

As already shown, any cubic polynomial (and indeed, any odd-degree polynomial) has the requisite property by the fundamental theorem of algebra.

What's more, a simple perturbation argument should be enough to show that any (sufficiently) smooth curve that meets every line in at least one point will meet some lines in at least three points. Consider a point tangent to the curve where the second derivative 'with respect to the tangent line' is non-zero; that is, a non-reflex tangent point, or locally extremal point. (Such points must exist if the curve is non-trivial). Now, consider pencils of lines 'near' this intersection point; displaced infinitesimally one way from the tangent, they must have another point of intersection with the curve, and this point can be made 'generic' so that it doesn't vanish under small perturbations. Then displace infinitesimally the other direction; the 'generic' point of intersection is still a point of intersection, but the tangent turns into two points of intersection.


1. ## Arithmetic Series Help

First of all I'm not sure if this is the right section, so I apologise if it's not.

I was hoping that someone could help me out with the following question:

From a piece of wire 5000mm long, n pieces are cut, each piece being 10mm longer than the previous piece. Ui is the length of the ith piece and Si is the cumulative length of i pieces.

Express Un and Sn in terms of U1 and n.

2. $U_n = u_1 + 10(n-1)$

$S_n = \sum_{i=1}^n U_i = \sum_{i=1}^n [u_1 + 10(i-1)]$ $= u_1 + 10\sum_{i=1}^n i -\sum_{i=1}^n 1 = u_1 + 10\frac{n(n+1)}{2} - n = u_1 +n(5n + 4)$

Also, $S_n = 5000 \Rightarrow u_1 = 5000 - 5n^2 - 4n.$

3. Your answer for Un looks right so thank you for that.

However the others don't seem to add up, I had to find U1 as the second part of the question and I've calculated it as being 80 which I've checked and it is right. If you put 80 into your formulae it doesn't equal 5000.

Thank you anyway though bud.

4. Hello, JP103!

Anonymous1 made some small errors. . .

$S_n \;=\; \sum_{i=1}^n U_i$

. . $=\; \sum_{i=1}^n \bigg[U_1 + 10(i-1)\bigg]$

. . $=\; \sum_{i=n}^n\bigg[U_1 + 10i - {\color{red}10}\bigg]$

. . $= \;\sum_{n=1}^nU_1 + 10\sum_{n=1}^ni - 10\sum_{n-1}^n1$

. . $=\;{\color{red}n}U_1 + 10\frac{n(n+1)}{2} - 10n$

$\boxed{S_n \;=\;nU_1 + 5n(n-1)}$

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Using the sum-formula: . $S_n \:=\:\frac{n}{2}\bigg[2a + (n-1)d\bigg]$

. . we have: . $S_n \;=\;\frac{n}{2}\bigg[U_1 + (n-1)10\bigg] \quad\Rightarrow\quad S_n \;=\;nU_1 + 5n(n-1)$


How to prove if a function is bijective?

I am having problems being able to formally demonstrate when a function is bijective (and therefore, surjective and injective). Here's an example:

How do I prove that $g(x)$ is bijective?

\begin{align} f &: \mathbb R \to\mathbb R \\ g &: \mathbb R \to\mathbb R \\ g(x) &= 2f(x) + 3 \end{align}

However, I fear I don't really know how to do such. I realize that the above example implies a composition (which makes things slighty harder?). In any case, I don't understand how to prove such (be it a composition or not).

For injective, I believe I need to prove that different elements of the codomain have different preimages in the domain. Alright, but, well, how?

As for surjective, I think I have to prove that all the elements of the codomain have one, and only one preimage in the domain, right? I don't know how to prove that either!

EDIT

f is a bijection. Sorry I forgot to say that.

-
Normally one distinguishes between the two different arrows $\mapsto$ and $\to$. One writes $f:\mathbb{R}\to\mathbb{R}$ to mean $f$ is a function from $\mathbb{R}$ into $\mathbb{R}$. The notation $x\mapsto x^3$ means the function that maps every input value to its cube. –  Michael Hardy Jul 1 '12 at 20:39
Wouldn't you have to know something about $f$? Is $f$ a bijection? –  Dylan Moreland Jul 1 '12 at 20:40
My apologies! Yes, f is a bijection. –  Omega Jul 1 '12 at 20:40
Both of your deinitions are wrong. Maybe all you need in order to finish the problem is to straighten those out and go from there. I've posted the definitions as an answer below. –  Michael Hardy Jul 1 '12 at 20:45

The way to verify something like that is to check the definitions one by one and see if $g(x)$ satisfies the needed properties.

Recall that $F\colon A\to B$ is a bijection if and only if $F$ is:

1. injective: $F(x)=F(y)\implies x=y$, and
2. surjective: for all $b\in B$ there is some $a\in A$ such that $F(a)=b$.

Assuming that $R$ stands for the real numbers, we check.

Is $g$ injective?

Take $x,y\in R$ and assume that $g(x)=g(y)$. Therefore $2f(x)+3=2f(y)+3$. We can cancel out the $3$ and divide by $2$, then we get $f(x)=f(y)$. Since $f$ is a bijection, then it is injective, and we have that $x=y$.

Is $g$ surjective?

Take some $y\in R$, we want to show that $y=g(x)$ that is, $y=2f(x)+3$. Subtract $3$ and divide by $2$, again we have $\frac{y-3}2=f(x)$. As before, if $f$ was surjective then we are about done, simply denote $w=\frac{y-3}2$, since $f$ is surjective there is some $x$ such that $f(x)=w$. Show now that $g(x)=y$ as wanted.

Alternatively, you can use theorems. What sort of theorems? The composition of bijections is a bijection. If $f$ is a bijection, show that $h_1(x)=2x$ is a bijection, and show that $h_2(x)=x+2$ is also a bijection. Now we have that $g=h_2\circ h_1\circ f$ and is therefore a bijection.

Of course this is again under the assumption that $f$ is a bijection.

-
"Subtract 3 and divide by 2, again we have (y−3)/2=f(x). As before, if f was surjective then we are about done" I'm not sure, but why would we be done there? –  Omega Jul 1 '12 at 21:18
@Omega: If $f$ was surjective, then there is some $x$ such that $f(x)=\frac{y-3}2$, show now that $g(x)=y$. –  Asaf Karagila Jul 1 '12 at 21:20
You know, it had me thinking: according to your method to find out if it is injective, no matter what function I test it with, I always manage to get the final equality (x = y). How would a function ever be not-injective? –  Omega Jul 1 '12 at 22:34
@Omega: No, assume that $f(x)=0$ for all $x$, suppose that $x,y$ are any two real numbers (perhaps different and perhaps not), does $f(x)=f(y)$ tell you something about $x=y$ or $x\neq y$? No, because taking $x=1$ and $y=2$ gives $f(1)=0=f(2)$, but $1\neq 2$. Note that my answer relies critically on the fact that $f$ itself is injective. –  Asaf Karagila Jul 1 '12 at 22:37

To prove a function is bijective, you need to prove that it is injective and also surjective.

"Injective" means no two elements in the domain of the function gets mapped to the same image.

"Surjective" means that any element in the range of the function is hit by the function.

Let us first prove that $g(x)$ is injective. If $g(x_1) = g(x_2)$, then we get that $2f(x_1) + 3 = 2f(x_2) +3 \implies f(x_1) = f(x_2)$. Since $f(x)$ is bijective, it is also injective and hence we get that $x_1 = x_2$.

Now let us prove that $g(x)$ is surjective. Consider $y \in \mathbb{R}$ and look at the number $\dfrac{y-3}2$. Since $f(x)$ is surjective, there exists $\hat{x}$ such that $f(\hat{x}) = \dfrac{y-3}2$. This means that $g(\hat{x}) = 2f(\hat{x}) +3 = y$. Hence, given any $y \in \mathbb{R}$, there exists $\hat{x} \in \mathbb{R}$ such that $g(\hat{x}) = y$. Hence, $g$ is also surjective.

Hence, $g(x)$ is bijective.

In general, if $g(x) = h(f(x))$ and if $f(x) : A \to B$ and $h(x): B \to C$ are both bijective then $g(x): A \to C$ is also bijective.

In your case, $f(x)$ was bijective from $\mathbb{R} \to \mathbb{R}$ and $h(x) = 2x+3$ is also bijective from $\mathbb{R} \to \mathbb{R}$.

-

You haven't said enough about the function $f$ to say whether $g$ is bijective.

"Injective" means different elements of the domain always map to different elements of the codomain.

"Surjective" means every element of the codomain has at least one preimage in the domain.

Later edit: What you've now added---that $f$ is a bijection---bring us to the point where we can answer the question. However, maybe you should look at what I wrote above. Since both definitions that I gave contradict what you wrote, that might be enough to get you there.

-
Sorry, yes, f is bijective. Thank you for clarifying that :) –  Omega Jul 1 '12 at 20:43

First show that $g$ is injective ($1$-$1$) by showing that if $g(x)=g(y)$, then $x=y$. This isn’t hard: if $g(x)=g(y)$, then $2f(x)+3=2f(y)+3$, so by elementary algebra $f(x)=f(y)$. By hypothesis $f$ is a bijection and therefore injective, so $x=y$.

Now show that $g$ is surjective. To do this, you must show that for each $y\in\Bbb R$ there is some $x\in\Bbb R$ such that $g(x)=y$. That requires finding an $x\in\Bbb R$ such that $2f(x)+3=y$ or, equivalently, such that $f(x)=\frac{y-3}2$. But $f$ is known to be a bijection and hence a surjection, so you know that there is such an $x\in\Bbb R$.

In general this is one of the two natural ways to show that a function is bijective: show directly that it’s both injective and surjective. The other is to construct its inverse explicitly, thereby showing that it has an inverse and hence that it must be a bijection. You could take that approach to this problem as well:

$$g^{-1}(y)=f^{-1}\left(\frac{y-3}2\right)\;,$$

since

\begin{align*} g\left(f^{-1}\left(\frac{y-3}2\right)\right)&=2f\left(f^{-1}\left(\frac{y-3}2\right)\right)+3\\ &=2\left(\frac{y-3}2\right)+3\\ &=y\;, \end{align*}

and since $f$ is a bijection, $f^{-1}\left(\frac{y-3}2\right)$ exists for every $y\in\Bbb R$.

Added: As Marc reminds me, this is only half the job: if you take this approach, you must either show directly that $g$ is injective, as I did above, or verify that the function that I called $g^{-1}$ above is a two-sided inverse, i.e., that $g^{-1}\big(g(x)\big)=x$ for $x\in\Bbb R$. This is not particularly difficult in this case:

\begin{align*} g^{-1}\big(g(x)\big)&=g^{-1}\big(2f(x)+3\big)\\ &=f^{-1}\left(\frac{\big(2f(x)+3\big)-3}2\right)\\ &=f^{-1}\big(f(x)\big)\\ &=x\;, \end{align*}

since $f$ is a bijection.

-
When using the "inverse" criterion, you should be careful in really checking that a purported inverse is an inverse, both ways. With $g^{-1}$ denoting your purported inverse, your final argument checked that $g(g^{-1}(y))=y$ for all $y\in\mathbb R$; this only shows that $g$ is surjective (it has a right inverse, also called a section). To show that $g$ is also injective you need to separately check that $g^{-1}(g(x))=x$ for all $x\in\mathbb R$. –  Marc van Leeuwen Jul 1 '12 at 21:22
@Marc: Yes, I should probably say as much; I hadn’t originally intended to mention this approach at all, and did so only as an afterthought. I was implicitly assuming that the obvious injectivity had already been checked, but that’s not clear from what I wrote. –  Brian M. Scott Jul 1 '12 at 21:26


Angle of a triangle inscribed in a square

Say we have a square $ABCD$. Put points $E$ and $F$ on sides $AB$ and $BC$ respectively, so that $BE = BF$. Let $BN$ be the altitude in triangle $BCE$. What is $\angle DNF$?

I'm inclined to say that it's a right angle because that's what it looks like from what I've drawn, but I have no idea how to proceed.

-
I have given an argument why your conjecture is true. I am more interested in why you looked at this problem and how you came up with this neat little conjecture. –  user17762 Jun 5 '12 at 5:29
If you want a purely Euclidean-geometry proof, you might start by noting the similarity of triangles $EBC$, $ENB$, and $BNC$, and then trying to prove that triangles $BNF$ and $CND$ are similar. I'm not sure exactly how the proof will go, but it feels doable. –  Rahul Jun 5 '12 at 5:32
@Marvis I'm in a 2nd year geometry course, and this is part of my homework due on Friday. I'm being a keener and getting it done early :) I just guessed on the right angle part. –  Charlie Jun 5 '12 at 5:46

$\angle DNF = 90^\circ \Longleftrightarrow \angle BNF = \angle CND$. It suffics to prove that $\triangle BNF \sim \triangle CND$. Well, it's trivial: $\angle NBF = \angle BEC = \angle NCD$ and $\displaystyle \frac{NB}{BF} = \frac{NB}{BE} = \sin\angle NEB = \cos\angle NCB = \frac{NC}{CB} = \frac{NC}{CD}$. Q.E.D

-
That $NB/BE = NC/CB$ follows from the similarity of right triangles $ENB$ and $BNC$ without need for trigonometry. Nice proof! –  Rahul Jun 5 '12 at 7:16
@RahulNarain Trigonometry is an abbreviation of similiar triangles here because we don't use angle transformation formulas such as $\sin(\alpha+\beta)=\cdots$. –  Frank Science Jun 5 '12 at 7:20
Indeed; what I meant to say was that the use of trigonometric notation is not needed. It suggests that more powerful machinery is being employed than actually is. –  Rahul Jun 5 '12 at 9:12

Firstly We can write
(1) Oklid relation from $\triangle CNB$

$h^2=m(k+x)$

(2) Pisagor relation from $\triangle DPN$

$a^2=(x+k)^2+(x+k+m-h)^2$

(3) Pisagor relation from $\triangle FRN$

$b^2=h^2+k^2$

(4) Pisagor relation from $\triangle DCF$

$c^2=x^2+(x+k+m)^2$

if DNF triange is a right triangle,It must satisfy $a^2+b^2=c^2$.

$(x+k)^2+(x+k+m-h)^2+h^2+k^2=x^2+(x+k+m)^2$

$(x+k)^2+(x+k+m)^2-2h(x+k+m)+h^2+h^2+k^2=x^2+(x+k+m)^2$

$(x+k)^2-2h(x+k+m)+2h^2+k^2=x^2$

$xk+k^2-h(x+k+m)+h^2=0$

$xk+k^2-h(x+k+m)+m(k+x)=0$

$-h(x+k+m)+(k+m)(k+x)=0$

$\frac{x+k}{x+k+m}=\frac{h}{k+m}$

This result is equal to the rates of thales formula for similar triangles $\triangle CRN \sim \triangle CBE$

Thus $a^2+b^2=c^2$ is correct for $\triangle DNF$ .

-

Let us do it through coordinate geometry. Let $B$ be the origin. Lets fix the coordinates first. $$A = (0,a)\\ B = (0,0) \\ C = (a,0)\\ D = (a,a)$$ Since $E$ and $F$ are equidistant from $B$, lets say $$F = (b,0)\\ E = (0,b)$$ where $0 \leq b \leq a$. The equation of the line $CE$ is $\dfrac{x}{a} + \dfrac{y}{b} = 1$. The equation of $BN$ is $y = \dfrac{a}{b}x$. This gives us the coordinate of $N$ as $\left( \dfrac{ab^2}{a^2 + b^2},\dfrac{a^2b}{a^2 + b^2} \right)$.

The slope of $FN$ is $m_1 = \dfrac{a^2b/(a^2+b^2) - 0}{ab^2/(a^2+b^2)-b} = \dfrac{a^2}{ab - a^2 - b^2}$.

The slope of $DN$ is $m_2 = \dfrac{a^2b/(a^2+b^2) - a}{ab^2/(a^2+b^2)-a} = \dfrac{ab - a^2 - b^2}{b^2 - a^2 - b^2} = - \left(\dfrac{ab-a^2-b^2}{a^2} \right)$.

Hence, the product of the slopes is $m_1m_2 = -1$. Hence your conjecture is indeed correct. I am waiting for someone to post a nicer geometric argument.

-

HINT: Try to resort to Homothetic transformation and you're done immediately. Another way is to connect $N$ to the middle of the $DF$ (let's call that point $M$) and prove that you have there $NM$=$DM$=$MF$ (here you may resort to Apollonius' theorem). You also could resort to the cyclic quadrilaterals as another approaching way.

-


# Density of rational and irrational numbers

Since $\mathbb{Q}$ is countable and $\mathbb{R} \backslash \mathbb {Q}$ is not, what does this tell us about the density of rational and irrational numbers along the real number line? Saying that there exists more irrational numbers than rational numbers seems rather vague becuase we're comparing infinites. How do we even define density here?

• There is a mathematical notion of dense, and the rationals and irrationals are each dense in the real numbers. However, it seems you are thinking of a different notion of denseness, perhaps more related to cardinality... – angryavian Nov 7 '16 at 0:31
• "what does this tell us about the density of rational and irrational numbers along the real number line?" Nothing: there are countable non-dense sets, countable dense sets, uncountable dense sets and uncountable non-dense sets. Countability and density are totally unrelated notions. – Crostul Nov 7 '16 at 0:50
• @Alephnull The Cantor set is such an example. – Crostul Nov 7 '16 at 8:09
• @Crostul But there are not co-countable nowhere dense sets (sets which are nowhere dense by have countable complement). So because $\mathbb{Q}$ is countable, we can at least conclude that $\mathbb{R}\setminus\mathbb{Q}$ is somewhere dense. – Reese Nov 7 '16 at 9:13
• @Alephnull It's pretty straightforward - a nowhere dense set by definition must exclude an entire interval. That interval cannot be countable. – Reese Nov 7 '16 at 11:03

One can have any combination of (countably infinite, uncountable) and (dense in the line, not dense in the line). (Unless, as was suggested in the comments, you have some notion of density other than the one defined by Captain Falcon.)

Countably infinite and dense in the reals: Rationals

Countably infinite and not dense in the reals: Integers

Uncountable and dense in the reals: Irrationals

Uncountable and not dense in the reals: Unit interval

• Ah ah, I appreciate the reference to Captain Falcon. However, "C." stands for "Cyril" ; Falcon being my name. :) – C. Falcon Nov 7 '16 at 0:58
• Another example, a bit worse, is the Cantor set. It is uncountable and not dense, in fact it equals its closure which has (Lebesgue) length zero. – Jeppe Stig Nielsen Nov 7 '16 at 10:11

A subset $A$ of $\mathbb{R}$ is said dense if and only if any elements of $\mathbb{R}$ is a limit of a sequence of elements of $A$.

Indeed, both $\mathbb{Q}$ and $\mathbb{R}\setminus\mathbb{Q}$ are dense in $\mathbb{R}$ in that sense.

As you pointed out, $\mathbb{Q}$ is countable, whereas $\mathbb{R}\setminus\mathbb{Q}$ is not, but the notion of density as nothing to do with countability.

Remark. To see that $\mathbb{Q}$ is dense in $\mathbb{R}$, let us pick $x\in\mathbb{R}$ and for all $n\in\mathbb{N}$ let us define: $$x_n:=\frac{\lfloor 10^nx\rfloor}{10^n}.$$ It is a sequence of $\mathbb{Q}$ which converges towards $x$ using squeeze theorem. For the density of $\mathbb{R}\setminus\mathbb{Q}$ consider: $$y_n:=x_n+\frac{\sqrt{2}}{n+1}.$$

• Nice answer, Captain! (salute) – Todd Wilcox Nov 7 '16 at 5:35

As clarified in other answers, cardinality by itself does not answer density questions.

Regarding the definition of density, there are two definitions. One is topological, saying that a set $A$ is dense if it intersects every non-empty open set. For the real line this is equivalent to saying that $A$ is dense (in the real line $\Bbb R$) if it intersects every open interval $(x,y)$ with $x<y$. In other words, for all choices of $x$ and $y$ with $x<y$ there is $a\in A$ with $x<a<y$. The latter condition usually is termed "order-dense" (in $\Bbb R$) and could be defined without reference to topology, for any linear order in place of $\Bbb R$. That is, $A$ is order-dense in a linear order $(L,<)$ is for all $x,y\in L$ with $x<y$ there is $a$ in $(x,y)$, that is, $x<a<y$ with $a\in A$. Finally a set $A$ is order-dense in itself if $A$ is order-dense in the linear order $(A,<)$. The set $\Bbb Q$ of all rational numbers is order-dense in the set $\Bbb R.$ The set $\Bbb Q$ is also order-dense in itself. The set $\Bbb Z$ of all integers is not order-dense in itself, and it is not order-dense in $\Bbb R$. The middle-third Cantor set $C$ is not order-dense in itself as it has "gaps", e.g. the interval $(\frac13,\frac23)$ contains no elements of $C$. (But, $C$ is (topologically) dense in itself, as it has no isolated points.) $C$ is no-where dense (in $\Bbb R$), meaning that the complement of its (topological) closure in $\Bbb R$ is (topologically) dense (and, as it happens, order-dense).

If $X$ is a topological space and $A$ is a subset then we say that $A$ is dense in $X$ provided that the closure of $A$ contains $X$. When $X=\Bbb R$ this means either of the following two conditions: (1) the set $A$ intersects every non-empty open interval, or (2) for every real number $x$ there is a sequence of points of $A$ converging to $x$. On the other hand, if $A=\Bbb Z$ then $A$ is dense in itself (topologically), but not order-dense in itself. Indeed, for every $m\in\Bbb Z$ the constant sequence $\langle m,m,...\rangle$ converges to $m$. On the other hand, the interval $(m,m+1)$ contains no integers.

In case you interpreted "dense" not in the topological definition, but as saying "many more irrationals than rationals", there's another field of math that provides an alternative way of seeing this.

In Measure Theory there's the concept of the measure of a set, which is an alternative characterization of its size. We want a way to define a function on sets, so let $X\subseteq 2^\mathbb{R}$ be a collection of sets (I have a specific one in mind here, namely the Lebesgue $\sigma$-algebra, but don't worry about that). Now, define the function: $$\mu:X\to\mathbb{R}_{\geq 0}\cup\lbrace\infty\rbrace$$ that must fulfill three axioms:

1. Non-negativity. For any subset $E\in X$, we have that $\mu(E)\geq 0$. Intuitively, things can't have negative size.

2. $\mu(\emptyset) = 0$. This should also seem intuitive, "the lack of a set" has no measure (there will be other sets with no measure though!).

3. Countable additivity on disjoint subsets. If $E_1,E_2$ are disjoint subsets of $\mathbb R$ (so $E_1\cap E_2 = \emptyset$), then: $$\mu(E_1\cup E_2) = \sum_{i = 1}^2 \mu(E_i)$$ This will actually be true for countably many disjoint subsets, so if $\lbrace E_i\rbrace$ are all pairwise disjoint, then: $$\mu(\cup_{i= 1}^\infty E_i)= \sum_{i = 1}^\infty \mu(E_i)$$ For finite sums, the intuition for this should be clear (think about each set as a circle, like how you make a Venn diagram. If there's no overlap between circles, the total "area" should be just all the individual areas added together).

Unfortunately, the above identity working for countable sums is less clearly true to our intuition. If we accept it though, we get some interesting results. The most applicable one to this is that we can calculate: $$\mu(\mathbb R\setminus\mathbb Q) = \infty,\quad\mu(\mathbb Q) = 0$$

So, in terms of measure theory, there are essentially no rational numbers, but infinitely many irrationals.


What is the probability that $X<Y$?

If two dice are rolled repeatedly, and $X$ is the number of tosses until $3$ is rolled, and $Y$ is the number of tosses until a $5$ is rolled, what is the probability$(X < Y)$? Also, if $Z$ is the number of rolls until a $10$ is rolled, what the probability$(X < Y < Z)$?

Is $P(X < Y) = \sum_{n=2}^{\infty}\sum_{k=1}^{\infty}((32/36)^{(n-1)}(4/36)-(34/36)^{(k-1)}(2/36)]$?

-
Is it assumed that $X$, $Y$ and $Z$ are independent ? –  Sasha Sep 23 '11 at 16:50
Yes X Y and Z are independent as the dice are balanced –  lord12 Sep 23 '11 at 16:50
No, $X$, $Y$ and $Z$ are not independent. For example, if $X = 1$ then $Y \ne 1$ and $Z \ne 1$. –  Robert Israel Sep 23 '11 at 18:40
It's not clear whether "a 3 is rolled" is referring to a 3 coming up on at least one of the two dice, or the sum of the two being 3. But since "a 10 is rolled" can only refer to the sum, I guess we can assume the 3 and 5 are also referring to the sum. –  Robert Israel Sep 23 '11 at 18:43
@Robert: Those were my thoughts too. But also notice that the first line says "until 3 is rolled" and not "until a 3 is rolled". So I suppose this indeed refers to the sum being 3, rather than one of them being 3. –  TMM Sep 23 '11 at 18:47

(a) $P(X<Y)$

Suppose $q = P(3 \text{ before } 5)$. When will we see a $3$ before we see a $5$? That is if we first see a $3$ on the first roll (we are done), or if we don't see a $3$ or a $5$ on the first roll and then see a $3$ before a $5$ after that. In other words:

$$q = P(3) \cdot 1 + P(5) \cdot 0 + P(\text{neither } 3 \text{ nor } 5) \cdot q$$

Since we know that $P(3) = \frac{2}{36}$, $P(5) = \frac{4}{36}$ we get $P(\text{neither } 3 \text{ nor } 5) = 1 - \frac{2}{36} - \frac{4}{36} = \frac{30}{36}$, so

$$q = \frac{2}{36} + \frac{30}{36} q \quad \Longleftrightarrow \quad q = \frac{1}{3}$$

Note that we can also rewrite the first equation as

$$q = P(3) \cdot 1 + P(5) \cdot 0 + P(\text{neither } 3 \text{ nor } 5) \cdot q \quad \Longleftrightarrow \quad q = \frac{P(3)}{P(3) + P(5)} = \frac{P(3)}{P(3 \text{ or } 5)} = P(3\ |\ 3 \text{ or } 5)$$

This is in accordance with a comment made by Dilip: We can simply ignore all throws which are neither a $3$ or a $5$, as they are irrelevant. Then the probability of seeing a $3$ first is simply the conditional probability of throwing a $3$, given that it's either a $3$ or a $5$.

(b) $P(X<Y<Z)$

We can use the same approach as above. First, let us ignore all throws which are not $3$, $5$ or $10$. We now need that the first event is a $3$, which happens with probability

$$P(X<Y,X<Z) = \frac{P(X)}{P(X \text{ or } Y \text{ or } Z)} = \frac{P(X)}{P(X) + P(Y) + P(Z)} = \frac{2}{2+4+3} = \frac{2}{9}$$

Now if this happens, we only need that in all successive events of $3,5,10$ we first see some number of $3$s, and then see a $5$ (and not a $10$ yet). But that means that after this first event, we can again ignore all events $X$, and look at the first throw resulting in either a $5$ or a $10$. Then

$$P(Y<Z | X<Y,X<Z) = \frac{P(Y)}{P(Y \text{ or } Z)} = \frac{P(Y)}{P(Y)+P(Z)} = \frac{4}{4+3} = \frac{4}{7}$$

So the combined probability is then

$$P(X<Y<Z) = P(X<Y,X<Z) \cdot P(Y<Z | X<Y, X<Z) = \frac{2}{9} \cdot \frac{4}{7} = \frac{8}{63}$$

-

Following on from @Thijs analysis, which is actually a tool called "first step analysis", we can get the probability that $P(X<Y<Z)$. So we have to wait until one of these events happens. $P(X)=\frac{2}{36}$ $P(Y)=\frac{4}{36}$ $P(Z)=\frac{3}{36}$. So we have probability of $\frac{27}{36}$ of achieving "nothing" each trial, and effectively going back to the first step.

So at effective trial one, if $X$ occurs we have success, if $Y$ or $Z$ occurs, we have failure.

$$P(X<Y<Z)=\frac{2}{36}P(X<Y<Z|X<Y,Z)+\frac{3}{36}P(X<Y<Z|Z<X,Y)$$ $$+\frac{4}{36}P(X<Y<Z|Y<X,Z)+\frac{27}{36}P(X<Y<Z)$$

The middle two probabilities are zero, and the first is simply $P(Y<Z)$, and applying first step analysis again we have

$$P(Y<Z)=\frac{4}{7}$$

Plugging back into the previous equation and solving we get

$$P(X<Y<Z)=\frac{8}{36(7)}+\frac{27}{36}P(X<Y<Z)=\frac{8}{63}$$

-
Nice, your follow-up is more in line with my previous analysis than my own follow-up! :) –  TMM Sep 23 '11 at 18:37

Here's a simpler way than one involving infinite series (such as that posted by Sasha). Roll a die until you get either a 3 or a 5. What's the probability that it's a 3? It's just the conditional probability of getting a 3, given that you've got either a 3 or a 5: $$\Pr(W=3\mid W=3\text{ or }W=5) = \frac{\Pr(W=3)}{\Pr(W=3\text{ or }W=5)} = \frac{2/36}{6/36}= \frac13.$$

-


11/9/19

# Evaluating Arbitrary Precision Integer Expressions in Julia using Metaprogramming

While watching the Mathologer masterclass on power sums

I came across a challenge to evaluate the following sum
$$1^{10}+2^{10}+\cdots+1000^{10}$$

This can be easily evaluated via brute force in Julia to yield

function power_cal(n)
a=big(0)
for i=1:n
a+=big(i)^10
end
a
end

julia> power_cal(1000)
91409924241424243424241924242500

Note the I had to use big to makes sure we are using BigInt in the computation. Without that, we would be quickly running in into an overflow issue and we will get a very wrong number.

In the comment section of the video I found a very elegant solution to the above sum, expressed as

(1/11) * 1000^11 + (1/2) * 1000^10 + (5/6) * 1000^9 – 1000^7 + 1000^5-(1/2) * 1000^3 + (5/66) * 1000 = 91409924241424243424241924242500

If I try to plug this into the Julia, I get

julia> (1/11) * 1000^11 + (1/2) * 1000^10 + (5/6) * 1000^9 - 1000^7 + 1000^5-(1/2) * 1000^3 + (5/66) * 1000
-6.740310541071357e18

This negative answer is not surprising at all, because we obviously ran into an overflow. We can, of course, go through that expression and modify all instances of Int64 with BigInt by wrapping it in the big function. But that would be cumbersome to do by hand.

## The power of Metaprogramming

In Julia, metaprogramming allows you to write code that creates code, the idea here to manipulate the abstract syntax tree (AST) of a Julia expression. We start to by “quoting” our original mathematical expressing into a Julia expression. In the at form it is not evaluated yet, however we can always evaluate it via eval.

julia> ex1=:((1/11) * 1000^11 + (1/2) * 1000^10 + (5/6) * 1000^9 - 1000^7 + 1000^5-(1/2) * 1000^3 + (5/66) * 1000)

:((((((1 / 11) * 1000 ^ 11 + (1 / 2) * 1000 ^ 10 + (5 / 6) * 1000 ^ 9) - 1000 ^ 7) + 1000 ^ 5) - (1 / 2) * 1000 ^ 3) + (5 / 66) * 1000)

julia> dump(ex1)
Expr
args: Array{Any}((3,))
1: Symbol +
2: Expr
args: Array{Any}((3,))
1: Symbol -
2: Expr
args: Array{Any}((3,))
1: Symbol +
2: Expr
args: Array{Any}((3,))
1: Symbol -
2: Expr
3: Expr
3: Expr
args: Array{Any}((3,))
1: Symbol ^
2: Int64 1000
3: Int64 5

julia> eval(ex1)
-6.740310541071357e18

The output of dump show the full AST in all its glory (…well almost the depth is a bit truncated). Notice that here all our numbers are interpreted as Int64.
Now we walk through the AST and change all occurrences of Int64 with BigInt by using the big function.

function makeIntBig!(ex::Expr)
args=ex.args
for i in eachindex(args)
if args[i] isa Int64
args[i]=big(args[i])
end
if args[i] isa Expr
makeIntBig!(args[i])
end
end
end

julia> ex2=copy(ex1)
:((((((1 / 11) * 1000 ^ 11 + (1 / 2) * 1000 ^ 10 + (5 / 6) * 1000 ^ 9) - 1000 ^ 7) + 1000 ^ 5) - (1 / 2) * 1000 ^ 3) + (5 / 66) * 1000)

julia> makeIntBig!(ex2)

julia> eval(ex2)
9.14099242414242434242419242425000000000000000000000000000000000000000000000014e+31

We see an improvement here, but the results are not very satisfactory. The divisions yield BigFloat results, which had a tiny bit of floating point errors. Can we do better?
Julia has support for Rational expressions baked in. We can use that improve the results. We just need to search for call expressions the / symbol and replace it by the // symbol. For safety we just have to makes sure the operands are as subtype of Integer.

function makeIntBig!(ex::Expr)
args=ex.args
if ex.head == :call && args[1]==:/ &&
length(args)==3 &&
all(x->typeof(x) <: Integer,args[2:end])
args[1]=://
args[2]=big(args[2])
args[3]=big(args[3])
else
for i in eachindex(args)
if args[i] isa Int64
args[i]=big(args[i])
end
if args[i] isa Expr
makeIntBig!(args[i])
end
end
end
end

julia> ex2=copy(ex1);

julia> makeIntBig!(ex2)

julia> eval(ex2)
91409924241424243424241924242500//1

Now that is much better! We have not lost any precision and we ended us with a Rational expression.

Finally, we can build a macro so the if we run into such expressions in the future and we want to evaluate them, we could just conveniently call it.

macro eval_bigInt(ex)
makeIntBig!(ex)
quote # Removing the denominator if it is redundant
local x=$ex (x isa Rational && x.den==1) ? x.num : x end end and we can now simply evaluate our original expression as julia> @eval_bigInt (1/11) * 1000^11 + (1/2) * 1000^10 + (5/6) * 1000^9 - 1000^7 + 1000^5-(1/2) * 1000^3 + (5/66) * 1000 91409924241424243424241924242500 07/28/18 # Exploring left truncatable primes Recently I came across a fascinating Numberphile video on truncatable primes I immediately thought it would be cool to whip a quick Julia code to get the full enumeration of all left truncatable primes, count the number of branches and also get the largest left truncatable prime. using Primes function get_left_primes(s::String) p_arr=Array{String,1}() for i=1:9 number_s="$i$s" if isprime(parse(BigInt, number_s)) push!(p_arr,number_s) end end p_arr end function get_all_left_primes(l) r_l= Array{String,1}() n_end_points=0 for i in l new_l=get_left_primes(i) isempty(new_l) && (n_end_points+=1) append!(r_l,new_l) next_new_l,new_n=get_all_left_primes(new_l) n_end_points+=new_n # counting the chains append!(r_l,next_new_l) end r_l, n end The first function just prepends a number (expressed in String for convenience) and checks for it possible primes that can emerge from a single digit prepending. For example: julia> get_left_primes("17") 2-element Array{String,1}: "317" "617" The second function, just makes extensive use of the first to get all left truncatable primes and also count the number of branches. julia> all_left_primes, n_branches=get_all_left_primes([""]) (String["2", "3", "5", "7", "13", "23", "43", "53", "73", "83" … "6435616333396997", "6633396997", "76633396997", "963396997", "16396997", "96396997", "616396997", "916396997", "396396997", "4396396997"], 1442) julia> n_branches 1442 julia> all_left_primes 4260-element Array{String,1}: "2" "3" "5" "7" "13" "23" ⋮ "96396997" "616396997" "916396997" "396396997" "4396396997" So we the full list of possible left truncatable primes with a length 4260. Also the total number of branches came to 1442. We now get the largest left truncatable primes with the following one liner: julia> largest_left_prime=length.(all_left_primes)|>indmax|> x->all_left_primes[x] "357686312646216567629137" After this fun exploration, I found an implementation in Julia for just getting the largest left truncatable prime for any base in Rosseta Code. 04/1/18 # Iterating with Dates and Time in Julia Julia has good documentation on dealing with Dates and Time, however that is often in the context constructing and Date and Time objects. In this post, I am focus on the ability to iterate over Dates and Times. This is very useful in countless application. We start of by capturing this moment and moving ahead into the future julia> this_moment=now() 2018-04-01T23:13:33.437 In one hour that will be julia> this_moment+Dates.Hour(1) 2018-04-02T00:13:33.437 Notice that Julia was clever enough properly interpret that we will be on the in another day after exactly one hour. Thanks to it multiple dispatch of the DateTime type to be able to do TimeType period arithmatic. You can then write a nice for loop that does something every four hours for the next two days. julia> for t=this_moment:Dates.Hour(4):this_moment+Dates.Day(2) println(t) #or somethings special with that time end 2018-04-01T23:13:33.437 2018-04-02T03:13:33.437 2018-04-02T07:13:33.437 2018-04-02T11:13:33.437 2018-04-02T15:13:33.437 2018-04-02T19:13:33.437 2018-04-02T23:13:33.437 2018-04-03T03:13:33.437 2018-04-03T07:13:33.437 2018-04-03T11:13:33.437 2018-04-03T15:13:33.437 2018-04-03T19:13:33.437 2018-04-03T23:13:33.437 Often we are not so interested in the full dates. For example if we are reading a video file and we want to get a frame every 5 seconds while using VideoIO.jl. We can deal here with the simpler Time type. julia> video_start=Dates.Time(0,5,20) 00:05:20 Here we are interested in starting 5 minutes and 20 seconds into the video. Now we can make a nice loop from the start to finish for t=video_start:Dates.Second(5):video_start+Dates.Hour(2) h=Dates.Hour(t).value m=Dates.Minute(t).value s=Dates.Second(t).value ms=Dates.Millisecond(t).value # Do something interesting with ffmpeg seek on the video end 02/9/18 # When Julia is faster than C, digging deeper In my earlier post I showed an example where Julia is significantly faster than c. I got this insightful response So I decided to dig deeper. Basically the standard c rand() is not that good. So instead I searched for the fastest Mersenne Twister there is. I downloaded the latest code and compiled it in the fastest way for my architecture. /* eurler2.c */ #include <stdio.h> /* printf, NULL */ #include <stdlib.h> /* srand, rand */ #include "SFMT.h" /* fast Mersenne Twister */ sfmt_t sfmt; double r2() { return sfmt_genrand_res53(&sfmt); } double euler(long int n) { long int m=0; long int i; for(i=0; i<n; i++){ double the_sum=0; while(1) { m++; the_sum+=r2(); if(the_sum>1.0) break; } } return (double)m/(double)n; } int main () { sfmt_init_gen_rand(&sfmt,123456); printf ("Euler : %2.5f\n", euler(1000000000)); return 0; } I had to compile with a whole bunch of flags which I induced from SFMT‘s Makefile to get faster performance. gcc -O3 -finline-functions -fomit-frame-pointer -DNDEBUG -fno-strict-aliasing --param max-inline-insns-single=1800 -Wall -std=c99 -msse2 -DHAVE_SSE2 -DSFMT_MEXP=1279 -ISFMT-src-1.5.1 -o eulerfast SFMT.c euler2.c And after all that trouble we got the performance down to 18 seconds. Still slower that Julia‘s 16 seconds. $ time ./eulerfast
Euler : 2.71824

real    0m18.075s
user    0m18.085s
sys 0m0.001s

Probably, we could do a bit better with more tweaks, and probably exceed Julia‘s performance with some effort. But at that point, I got tired of pushing this further. The thing I love about Julia is how well it is engineered and hassle free. It is quite phenomenal the performance you get out of it, with so little effort. And for basic technical computing things, like random number generation, you don’t have to dig hard for a better library. The “batteries included” choices in the Julia‘s standard library are pretty good.

02/8/18

# When Julia is faster than C

On e-day, I came across this cool tweet from Fermat’s library

So I spend a few minutes coding this into Julia

function euler(n)
m=0
for i=1:n
the_sum=0.0
while true
m+=1
the_sum+=rand()
(the_sum>1.0) && break;
end
end
m/n
end

Timing this on my machine, I got

julia> @time euler(1000000000)
15.959913 seconds (5 allocations: 176 bytes)
2.718219862

Gave a little under 16 seconds.

Tried a c implementation

#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

double r2()
{
return (double)rand() / (double)((unsigned)RAND_MAX + 1);
}

double euler(long int n)
{
long int m=0;
long int i;
for(i=0; i<n; i++){
double the_sum=0;
while(1) {
m++;
the_sum+=r2();
if(the_sum>1.0) break;
}
}
return (double)m/(double)n;
}

int main ()
{
printf ("Euler : %2.5f\n", euler(1000000000));

return 0;
}

and compiling with either gcc

gcc  -Ofast euler.c

or clang

clang  -Ofast euler.c

gave a timing twice as long

$time ./a.out Euler : 2.71829 real 0m36.213s user 0m36.238s sys 0m0.004s For the curios, I am using this version of Julia julia> versioninfo() Julia Version 0.6.3-pre.0 Commit 93168a6 (2017-12-18 07:11 UTC) Platform Info: OS: Linux (x86_64-linux-gnu) CPU: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz WORD_SIZE: 64 BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell) LAPACK: libopenblas64_ LIBM: libopenlibm LLVM: libLLVM-3.9.1 (ORCJIT, haswell) Now one should not put too much emphasis on such micro benchmarks. However, I found this a very curious examples when a high level language like Julia could be twice as fast a c. The Julia language authors must be doing some amazing mojo. 01/2/18 # Visualizing the Inscribed Circle and Square Puzzle Recently, I watched a cool mind your decsions video on an inscribed circle and rectangle puzzle. In the video they showed a diagram that was not scale. I wanted to get a sense of how these differently shaped areas will match. There was a cool ratio between the outer and inner circle radii that is expressed as $$\frac{R}{r}=\sqrt{\frac{\pi-2}{4-\pi}}$$. I used Compose.jl to rapidly do that. using Compose set_default_graphic_size(20cm, 20cm) ϕ=sqrt((pi -2)/(4-pi)) R=10 r=R/ϕ ctx=context(units=UnitBox(-10, -10, 20, 20)) composition = compose(ctx, (ctx, rectangle(-r/√2,-r/√2,r*√2,r*√2),fill("white")), (ctx,circle(0,0,r),fill("blue")), (ctx,circle(0,0,R),fill("white")), (ctx,rectangle(-10,-10,20,20),fill("red"))) composition |> SVG("inscribed.svg") 08/6/17 # Solving the code lock riddle with Julia I came across a neat math puzzle involving counting the number of unique combinations in a hypothetical lock where digit order does not count. Before you continue, please watch at least the first minute of following video: The rest of the video describes two related approaches for carrying out the counting. Often when I run into complex counting problems, I like to do a sanity check using brute force computation to make sure I have not missed anything. Julia is fantastic choice for doing such computation. It has C like speed, and with an expressiveness that rivals many other high level languages. Without further ado, here is the Julia code I used to verify my solution the problem. 1. function unique_combs(n=4) 2.  pat_lookup=Dict{String,Bool}() 3.  for i=0:10^n-1 4.  d=digits(i,10,n) # The digits on an integer in an array with padding 5.  ds=d |> sort |> join # putting the digits in a string after sorting 6.  get(pat_lookup,ds,false) || (pat_lookup[ds]=true) 7.  end 8.  println("The number of unique digits is$(length(pat_lookup))")
9. end

In line 2 we create a dictionary that we will be using to check if the number fits a previously seen pattern. The loop starting in line 3, examines all possible ordered combinations. The digits function in line 4 takes any integer and generate an array of its constituent digits. We generate the unique digit string in line 5 using pipes, by first sorting the integer array of digits and then combining them in a string. In line 6 we check if the pattern of digits was seen before and make use of quick short short-circuit evaluation to avoid an if-then statement.

07/14/17

# Julia calling C: A more minimal example

Earlier I presented a minimal example of Julia calling C. It mimics how one would go about writing C code, wrapping it a library and then calling it from Julia. Today I came across and even more minimal way of doing that while reading an excellent blog on Julia’s syntactic loop fusion. Associated with the blog was notebook that explores the matter further.

Basically, you an write you C in a string and pass it directly to the compiler. It goes something like

using Libdl
C_code= raw"""
double mean(double a, double b) {
return (a+b) / 2;
}
"""
const Clib=tempname()
open(gcc -fPIC -O3 -xc -shared -o \$(Clib * "." * Libdl.dlext) -, "w") do f
print(f, C_code)
end

The tempname function generate a unique temporary file path. On my Linux system Clib will be string like "/tmp/juliaivzRkT". That path is used to generate a library name "/tmp/juliaivzRkT.so" which will then used in the ccall:

meanc(a,b)=ccall((:mean,Clib),Float64,(Float64,Float64),a,b)
julia> meanc(3,4)
3.5

This approach would not be recommended if you are writing anything sophisticated in C. However, it is fun to experiment with for short bits of C code that you might like to call from Julia. Saves you the hassle of creating a Makefile, compiling, etc…

06/29/17

# Solving the Fish Riddle with JuMP

Recently I came across a nice Ted-Ed video presenting a Fish Riddle.

I thought it would be fun to try solving it using Julia’s award winning JuMP package. Before we get started, please watch the above video-you might want to pause at 2:24 if you want to solve it yourself.

To attempt this problem in Julia, you will have to install the JuMP package.

julia> Pkg.add("JuMP")

JuMP provides an algebraic modeling language for dealing with mathematical optimization problems. Basically, that allows you to focus on describing your problem in a simple syntax and it would then take care of transforming that description in a form that can be handled by any number of solvers. Those solvers can deal with several types of optimization problems, and some solvers are more generic than others. It is important to pick the right solver for the problem that you are attempting.

The problem premises are:
1. There are 50 creatures in total. That includes sharks outside the tanks and fish
2. Each SECTOR has anywhere from 1 to 7 sharks, with no two sectors having the same number of sharks.
3. Each tank has an equal number of fish
4. In total, there are 13 or fewer tanks
5. SECTOR ALPHA has 2 sharks and 4 tanks
6. SECTOR BETA has 4 sharsk and 2 tanks
We want to find the number of tanks in sector GAMMA!

Here we identify the problem as mixed integer non-linear program (MINLP). We know that because the problem involves an integer number of fish tanks, sharks, and number of fish inside each tank. It also non-linear (quadratic to be exact) because it involves multiplying two two of the problem variables to get the total number or creatures. Looking at the table of solvers in the JuMP manual. pick the Bonmin solver from AmplNLWriter package. This is an open source solver, so installation should be hassle free.

julia> Pkg.add("AmplNLWriter")

We are now ready to write some code.

using JuMP, AmplNLWriter

# Solve model
m = Model(solver=BonminNLSolver())

# Number of fish in each tank
@variable(m, n>=1, Int)

# Number of sharks in each sector
@variable(m, s[i=1:3], Int)

# Number of tanks in each sector
@variable(m, nt[i=1:3]>=0, Int)

@constraints m begin
# Constraint 2
sharks[i=1:3], 1 <= s[i] <= 7
numfish[i=1:3], 1 <= nt[i]
# Missing uniqueness in restriction
# Constraint 4
sum(nt) <= 13
# Constraint 5
s[1] == 2
nt[1] == 4
# Constraint 6
s[2] == 4
nt[2] == 2
end

# Constraints 1 & 3
@NLconstraint(m, s[1]+s[2]+s[3]+n*(nt[1]+nt[2]+nt[3]) == 50)

# Solve it
status = solve(m)

sharks_in_each_sector=getvalue(s)
fish_in_each_tank=getvalue(n)
tanks_in_each_sector=getvalue(nt)

@printf("We have %d fishes in each tank.\n", fish_in_each_tank)
@printf("We have %d tanks in sector Gamma.\n",tanks_in_each_sector[3])
@printf("We have %d sharks in sector Gamma.\n",sharks_in_each_sector[3])

In that representation we could not capture the restriction that “no two sectors having the same number of sharks”. We end up with the following output:

We have 4 fishes in each tank.
We have 4 tanks in sector Gamma.
We have 4 sharks in sector Gamma.

Since the problem domain is limited, we can possible fix that by adding a constrain that force the number of sharks in sector Gamma to be greater than 4.

@constraint(m,s[3]>=5)

This will result in an answer that that does not violate any of the stated constraints.

We have 3 fishes in each tank.
We have 7 tanks in sector Gamma.
We have 5 sharks in sector Gamma.

However, this seems like a bit of kludge. The proper way go about it is represent the number of sharks in the each sector as binary array, with only one value set to 1.

# Number of sharks in each sector
@variable(m, s[i=1:3,j=1:7], Bin)

We will have to modify our constraint block accordingly

@constraints m begin
# Constraint 2
sharks[i=1:3], sum(s[i,:]) == 1
u_sharks[j=1:7], sum(s[:,j]) <=1 # uniquness
# Constraint 4
sum(nt) <= 13
# Constraint 5
s[1,2] == 1
nt[1] == 4
# Constraint 6
s[2,4] == 1
nt[2] == 2
end

We invent a new variable array st to capture the number of sharks in each sector. This simply obtained by multiplying the binary array by the vector $$[1,2,\ldots,7]^\top$$

@variable(m,st[i=1:3],Int)
@constraint(m, st.==s*collect(1:7))

We rewrite our last constraint as

# Constraints 1 & 3
@NLconstraint(m, st[1]+st[2]+st[3]+n*(nt[1]+nt[2]+nt[3]) == 50)

After the model has been solved, we extract our output for the number of sharks.

sharks_in_each_sector=getvalue(st)

…and we get the correct output.

This problem might have been an overkill for using a full blown mixed integer non-linear optimizer. It can be solved by a simple table as shown in the video. However, we might not alway find ourselves in such a fortunate position. We could have also use mixed integer quadratic programming solver such as Gurobi which would be more efficient for that sort of problem. Given the small problem size, efficiency hardly matters here.

06/12/17

# Reading DataFrames with non-UTF8 encoding in Julia

Recently I ran into problem where I was trying to read a CSV files from a Scandinavian friend into a DataFrame. I was getting errors it could not properly parse the latin1 encoded names.

I tried running

using DataFrames
dataT=readtable("example.csv", encoding=:latin1)

but the got this error

ArgumentError: Argument 'encoding' only supports ':utf8' currently.

The solution make use of (StringEncodings.jl)[https://github.com/nalimilan/StringEncodings.jl] to wrap the file data stream before presenting it to the readtable function.

f=open("example.csv","r")
s=StringDecoder(f,"LATIN1", "UTF-8")
close(f)
The StringDecoder generates an IO stream that appears to be utf8 for the readtable function.


Can a sequence be neither decreasing nor increasing?

Given the definition of a increasing sequence: "A sequence $$(a_n)_{n\in\mathbb{N}}$$ is increasing if for all $$n\in \mathbb{N}$$, $$a_n\le a_{n+1}$$."

My question is: by this definition isn't the sequence $$(1,1,1,1,1,\dotsc)$$ an increasing sequence then?

• Yes, of course. Some people will say the sequence is strictly increasing if $a_n<a_{n+1}$ for all $n$. ... The answer to your title question is of course — just take a sequence like $1,2,1,2,1,2,\dots$. – Ted Shifrin Mar 12 at 22:21
• If you are upset by the language that "increasing" should in your opinion be reserved only for strict inequality between terms, then you might prefer to instead use the term "weakly increasing" instead. Also, before you ask, yes, the definitions work out then that a constant sequence like $1,1,1,\dots$ is simultaneously an increasing sequence and a decreasing sequence and it is easy to prove that any sequence which is simultaneously both must be a constant sequence. – JMoravitz Mar 12 at 22:27

Definition: A sequence $$(a_n)$$ is increasing if $$a_n \le a_{n+1}$$ for all $$n$$,

(or something similar), then the sequence $$(1,1,1,\dotsc)$$ is an increasing sequence. It is increasing precisely because it satisfies the property which defines an increasing sequence (per the definition given).

This may not feel right, as our intuition from natural language is that this sequence is constant, and therefore not increasing. There are two bits of advice that I would give (the first when you have to deal with other people's exposition, the second when you are doing your own work):

1. Just get used to it. There are many terms in mathematics which come from natural language, but which don't mean the same thing in mathematics as they do in vernacular English. The terms "open" and "closed" are a big bugaboo for intro to topology students, for example. As a mathematician, you should learn to get used to words that are given technical definitions which contradict plain English interpretation (and, indeed, may contradict each other, as different authors may define the same term differently.

2. Find another term. In his series on analysis, Simon comments early on that weak vs strict inequalities are possibly ambiguous. He therefore declares that, in his text, "increasing" always means "nondecreasing" (and, if I recall correctly, he also declares that "positive" means "nonnegative"; the point is that he doesn't want to have to say things like "nonnegative nondecreasing"). A similar strategy may help you: if you don't want to call a constant sequence "increasing", use the words "nondecreasing" (for a sequence where $$a_n \le a_{n+1}$$), and use "increasing" (or even "strictly increasing") for sequences which satisfy $$a_n < a_{n+1}$$.

Simon, Barry, Real analysis. A comprehensive course in analysis, part 1, Providence, RI: American Mathematical Society (AMS) (ISBN 978-1-4704-1099-5/hbk). xx, 789 p. (2015). ZBL1332.00003.


# Unitary time condition

I have a confusion with regards to the principle of QM that states that time evolution must be unitary. In particular, given that states transform through time as $|\Psi(t)\rangle = U(t)|\Psi(0)\rangle$; does the condition: $$\langle\Phi(0)|\Psi(0)\rangle=0 \ \implies \ \langle\Phi(t)|\Psi(t)\rangle=0$$

imply that $U$ must be unitary, or is it imposed on $U$?

I realize that with the before mentioned condition it is posible to prove that for two orthonormal memebers of the basis, $i,j$: $\langle i| U^{\dagger}U | j \rangle = 0$. Is it posible to prove that for the same member the product is 1?

• If $U$ is unitary then $U^{\dagger}$ is the inverse of $U$ by definition. Sep 27 '17 at 14:10
• Yes, but the question is about how to show that, that is the case Sep 27 '17 at 14:18
• Since $\vert \Psi(t)\rangle = U\vert\Psi(0)\rangle$ then clearly $$\langle \Phi(t)\vert \Psi(t)\rangle = \langle \Phi(0)\vert U^\dagger\,U\vert \Psi(0)\rangle = \langle\Phi(0)\vert\Psi(0)\rangle$$ valid $\forall t$ implies $U^\dagger U=\hat 1\, \forall t$ as well, provided your kets are arbitrary. (Hopefully this should be enough.) Sep 27 '17 at 14:32
• The condition of orthogonality preservation you stated alone would also allow for operators that satisfy $U^\dagger U = c \cdot 1$ with any constant $c$. Usually one says that unitarity comes from norm preservation, i.e. $< \psi(t) ,\psi(t)> = <\psi(0),\psi(0)>$.
– Luke
Sep 27 '17 at 15:23

It is an interesting elementary problem. After I while I proved the following proposition where I use $A^*$ for the adjoint of $A$.

Proposition. Let $U: H \to H$ be a bounded operator over a Hilbert space $H$. The following conditions are equivalent.

(a) For $x,y \in H$,$\quad$ $\langle x|y\rangle =0$ implies $\langle Ux|Uy\rangle =0$

(b) $U^*U = cI$ for some real $c\geq 0$.

Before proving the statement I observe that even if $c=1$, $U$ is not necessarily unitary, because unitarity is $U^*U=UU^*=I$. And here $UU^*=I$ generally fails when $H$ is infinite dimensional (otherwise it is trivially true as a consequence of $U^*U=I$). For $c=1$, $U$ is an isometry not necessarily surjective.

Proof. It is obvious that (b) implies (a), so we prove that (a) implies (b). Condition (a) can be rephrased as $y \perp x$ implies $y \perp U^*Ux$. As a consequence $U^*Ux \in \{\{x\}^\perp\}^\perp$ which is the linear span of $x$. In other words $U^*U x = \lambda_x x$ for some $\lambda_x\in \mathbb C$. My goal is now proving that $\lambda_x$ does not depend on $x$.

To this end, consider a couple of vectors $x \perp y$ with $x, y \neq 0$. Using the argument above we have $$U^*U x = \lambda_x x\:,\quad U^*U y = \lambda_y y\:, \quad U^*U (x+y) = \lambda_{x+y} (x+y)\:.\tag{1}$$ Linearity of $U^*U$ applied to the last identity leads to $$U^*Ux + U^*Uy = \lambda_{x+y}x + \lambda_{x+y}y\:,$$ namely $$U^*Ux- \lambda_{x+y}x = -(U^*Uy- \lambda_{x+y}y)\:\:.$$ Exploiting the first two identities in (1) we get $$(\lambda_x- \lambda_{x+y})x = -(\lambda_y- \lambda_{x+y})y\:.$$ Since $x \perp y$ and $x,y \neq 0$, the only possibility is that $$\lambda_x = \lambda_{x+y} = \lambda_y\:.$$ So a couple of orthogonal non-vanishing vectors has the same $\lambda_x$.

To conclude consider a Hilbert basis $\{x_n\}$ of $H$ so that, if $z\in H$, $$z = \sum_n c_n x_n \tag{2}$$ for complex numbers $c_n$. Since $U^*U$ is continuous ($U$ is bounded), $$U^*Uz = \sum_n c_n U^*Ux_n = \sum_n c_n \lambda_{x_n}x_n\tag{3}$$ But we know from the previous argument that $\lambda_{x_n} = \lambda_{x_m}$ so that, indicating with $c$ the common value of the $\lambda_{x_n}$, (3) can be rewritten as $$U^*Uz = \sum_n c_n cx_n = c\sum_n c_n x_n = cz\:.$$ Since $z\in H$ was arbitrary, we have found that $$U^*U=c I\:.$$ Taking the adjoint of both sides we obtain $c=\overline{c}$ so that $c$ is real. Finally, $$0 \leq \langle Ux | Ux\rangle = \langle x| U^*U x\rangle = c \langle x| x \rangle$$ so that $c\geq 0$. QED

• This is nice but does the part "Is it posible to prove that for the same member the product is 1?" not imply $c=\lambda_x=1$? Sep 27 '17 at 19:32
• I do not understand, we already know that $U$ such thst $U^*U= cI$ satisfies the initial hypotheses also for $c\neq 1$. If it were possible to prove that $c=1$ we would exclude this case that we know exists. Sep 27 '17 at 19:40
• Something's clearly throwing me off here. I'll come bac k to in a couple of days. Sep 27 '17 at 19:57


# Combinations question - why is my approach wrong?

I'm learning Permutations and Combinations and while trying to solve this simple question, I stuck on the solution:-

From a group of 7 men and 6 women, five persons are to be selected to form a committee so that at least 3 men are there on the committee. In how many ways can it be done?

The solution was to consider all three possibilities one by one- 3 men and 2 women, 4 men and 1 women, all 5 men. Calculate the combinations for each case and add them.

But I was trying it from a different approach- Firstly select 3 men from 7 (gives 35 combinations) and multiply it with the combinations of selecting 2 more committee members from 10 remaining members. So my answer came out to be 35 x 45 = 1575. But the answer is 756 which is not even a multiple of 35. So is my approach wrong? Why so?

-

If you first select 3 men and then fill up with two arbitrary people, then you count each combination with exactly three men once, but others are counted repeatedly. For example, you count all-male committees 10 times, once for each way to choose the three first men in retrospect from the final five men.

Smaller numbers example: Given John, Jack and Jill, form a committee of two people with thge constraint that at least one is a man. Actually, there are three such ocmmittees possible (becaus ethe constraint is no constraint at all). But your method would give four: First select John as male, then select either Jack or Jill as second. Or first select Jack as male, then select John or Jill as second. The committee "Jack and John" is counted twice by you.

-
I don't understand your first paragraph, but your second paragraph, with the simpler example, was very helpful. Thanks. –  a.real.human.being Oct 5 '14 at 1:00

You’re counting every committee with $4$ men $\binom43=4$ times: if the men are A, B, C, and D, and the woman is F, you’re counting it once with A, B, and C as the first $3$ and D and F as the extra $2$, once with A, B, and D as the first $3$ and C and F as the extra $2$, once with A, C, and D as the first $3$ and B and F as the extra $2$, and once with B, C, and D as the first $3$ and A and F as the extra $2$.

Similarly, you’re counting every committee with $5$ men $\binom53=10$ times: if the men are A, B, C, D, and E, you’re counting the committee once with A, B, and C as the first $3$ and D and E as the extra $2$, and so one for every possible $3$-$2$ split of the five men.

Only the committees of $3$ men and $2$ women are counted correctly, exactly once each.

Since some committees are counted once, some four times, and some ten times, you don’t even get an answer that’s related to the correct one in some very simple way, as you would if, for example, you counted every committee twice. From the correct answer you know that there are $525$ committees with $3$ men, $210$ with $4$ men, and $21$ with $5$ men, and sure enough,

$$525+4\cdot210+10\cdot21=1575\;,$$

$$\binom 73\cdot\binom 62+\binom 74\cdot\binom 61+\binom 75\cdot\binom 60=35\cdot15+35\cdot6+21\cdot1=756$$