## Thursday, June 12, 2014

### mathjax test & Demo

MathJax setup in Blogger:
http://mytechmemo.blogspot.com.au/2012/02/how-to-write-math-formulas-in-blogger.html

MathJax Examples

Note:
1. I had to hunt for the "HTML/Javascript" gadget, down the list aways.
2. I ended up putting the gadget in as a footer.
3. You'll have to add that gadget to all blogs you want it to work for.
4. Preview and Edit mode don't compute the TeX. You need to save the doc, then view the post.
5. In compose "Options", "Line Breaks", I'm using 'Press "Enter" for line breaks.
6. The "MyTechMemo" author doesn't use the exact code he suggests, though it works for me. His actual gadget is:
 Powered by <a href="http://www.mathjax.org/docs/1.1/start.html">MathJax</a> <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script> 
Alternate Hub Config in gadget, replace just first line.
MathJax.Hub.Config({
TeX: { equationNumbers: { autoNumber: "AMS" } },
tex2jax: {
inlineMath: [ ['$','$'], ["\$","\$"] ],
displayMath: [ ['$$','$$'], ["\$","\$"] ],
processEscapes: true }
});


Using "all", numbers all equations.
"AMS" numbers only specified equations.
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "all"} } }); </script> 

Equation Examples:
http://cdn.mathjax.org/mathjax/latest/test/sample-eqnum.html

Numbered Equn:

E = mc^2

Align:
\begin{align}
a_1& =b_1+c_1\\
a_2& =b_2+c_2-d_2+e_2
\end{align}

Split:

\begin{split}
a& =b+c-d\\
& =g+h\\
& =i
\end{split}

http://cdn.mathjax.org/mathjax/latest/test/sample.html
The probability of getting $k$ heads when flipping $n$ coins is:

$P(E) = {n \choose k} p^k (1-p)^{ n-k}$

Raw Code:

$$E = mc^2$$

\begin{align}
a_1& =b_1+c_1\\
a_2& =b_2+c_2-d_2+e_2
\end{align}

$$\begin{split} a& =b+c-d\\ & \quad +e-f\\ & =g+h\\ & =i \end{split}$$

$P(E) = {n \choose k} p^k (1-p)^{ n-k}$ 

Using AsciiMath (with TeX and MathML), http://www1.chapman.edu/~jipsen/mathml/asciimathsyntax.html
 <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML"> </script> 

Or, without Tex:
 <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_HTMLorMML"> </script> 

x = (-b +- sqrt(b^2-4ac))/(2a) .

Raw Code (back ticks as start/end tokens):
x = (-b +- sqrt(b^2-4ac))/(2a) .


Alternate start/end tokens, part of main package, doesn't work in Mathjax:

amath x^2 or a_(m n) or a_{m n} or (x+1)/y or sqrtx endamath
x^2 or a_(m n) or a_{m n} or (x+1)/y or sqrtx

Raw Code:
amath x^2 or a_(m n) or a_{m n} or (x+1)/y or sqrtx endamath


TeX graph example from AsciiMath, doesn't work in MathJax.
\begin{graph} width=300; height=200; xmin=-6.3; xmax=6.3; xscl=1; plot(cos(log(x))); plot(2.2sin(x)) \end{graph}

\begin{graph} width=300; height=200; xmin=-6.3; xmax=6.3; xscl=1; plot(cos(log(x))); plot(2.2sin(x)) \end{graph}


Example 1:
http://mytechmemo.blogspot.com.au/2012/12/determine-whether-ellipse-intersect.html

Assume an ellipse of width $\sigma$ and length $\kappa \sigma$ is centered at $(x_0, y_0)$, and has angle $\theta_0$ with the $x$-axis. How do we determine whether it intersects a horizontal line or a vertical line?

It turns out the criteria is very simple:
• The ellipse intersects a horizontal line $y = y_1$ if and only if the following equation holds: $\triangle_1 = \sigma^2 \left(\cos^2(\theta_0) + \kappa^2 \sin^2(\theta_0)\right) - (y_1-y_0)^2 \geq 0$
• The ellipse intersects a vertical line $x = x_1$ if and only if the following equation holds:
$\triangle_2 = \sigma^2 \left(\sin^2(\theta_0) + \kappa^2 \cos^2(\theta_0)\right) - (x_1-x_0)^2 \geq 0$

Raw code:

Assume an ellipse of width $\sigma$ and length $\kappa \sigma$ is centered at $(x_0, y_0)$, and has angle $\theta_0$ with the $x$-axis. How do we determine whether it intersects a horizontal line or a vertical line?

It turns out the criteria is very simple:

The ellipse intersects a horizontal line $y = y_1$ if and only if the following equation holds: $\triangle_1 = \sigma^2 \left(\cos^2(\theta_0) + \kappa^2 \sin^2(\theta_0)\right) - (y_1-y_0)^2 \geq 0$
The ellipse intersects a vertical line $x = x_1$ if and only if the following equation holds:  $\triangle_2 = \sigma^2 \left(\sin^2(\theta_0) + \kappa^2 \cos^2(\theta_0)\right) - (x_1-x_0)^2 \geq 0$



Example 2:
http://mytechmemo.blogspot.com.au/2012/04/color-balls.html

Thus if we let $\mu_i$ be the expected number of steps getting to one color when the bag has i distinct colors, then we have
$\mu_i = 1 + \frac{i(i-1)}{n(n-1)} \mu_{i-1}+\left(1-\frac{i(i-1)}{n(n-1)}\right) \mu_i.$
From this we can use induction to prove that
$\mu_i = \frac{i-1}{i} n(n-1).$
Therefore we have
$\mu_n = (n-1)^2.$

Raw code:

Thus if we let $\mu_i$ be the expected number of steps getting to one color when the bag has i distinct colors, then we have
$\mu_i = 1 + \frac{i(i-1)}{n(n-1)} \mu_{i-1}+\left(1-\frac{i(i-1)}{n(n-1)}\right) \mu_i.$
From this we can use induction to prove that
$\mu_i = \frac{i-1}{i} n(n-1).$
Therefore we have
$\mu_n = (n-1)^2.$


Example 3:
http://mytechmemo.blogspot.com.au/2011/02/nodes-of-height-h-in-n-element-heap.html

This is exercise 6.3-3 of CLRS version 2.

Question:Show that there are at most $\lceil n/2^{h+1} \rceil$ nodes of height $h$ in any $n$-element heap.

Solution: First some facts
1. According to exercise 6.1-7, an $n$-element heap has exactly $\lceil n/2 \rceil$ leaves.
2. Notice that the nodes with height $i$ become leaves after deleting all the nodes with height $0,\cdots, i-1$.

Let $y_i$ be the total number of elements of the new tree after deleting all the nodes with height $0,\cdots, i-1$, and let $x_i$ be the number of leaves of the new tree. Then we have $x_i = \lceil y_i/2 \rceil$ by fact #1 and $y_{i+1}=y_i - x_i$ by fact #2. Thus we have $y_{i+1} \leq y_i/2$, and this leads to

$$y_i \leq n/2^i, \quad \forall n=0,1,\cdots.$$

The final conclusion follows from the relation $x_i = \lceil y_i/2 \rceil$.

Raw code:

This is exercise 6.3-3 of CLRS version 2.

Question:Show that there are at most $\lceil n/2^{h+1} \rceil$ nodes of height $h$ in any $n$-element heap.

Solution: First some facts

According to exercise 6.1-7, an $n$-element heap has exactly $\lceil n/2 \rceil$ leaves.
Notice that the nodes with height $i$ become leaves after deleting all the nodes with height $0,\cdots, i-1$.

Let $y_i$ be the total number of elements of the new tree after deleting all the nodes with height $0,\cdots, i-1$, and let $x_i$ be the number of leaves of the new tree. Then we have $x_i = \lceil y_i/2 \rceil$ by fact #1 and $y_{i+1}=y_i - x_i$ by fact #2. Thus we have $y_{i+1} \leq y_i/2$, and this leads to

$$y_i \leq n/2^i, \quad \forall n=0,1,\cdots.$$

The final conclusion follows from the relation $x_i = \lceil y_i/2 \rceil$.


Example 4:

There are 20 person in a Christmas party. What is the probability that at least one of them get his own gift?

Solution:
Let $A_i$=person i get his own gift
Then we want $P(A_1 \cup A_2 \cup \cdots \cup A_n)$,
which is equal to
$$\sum P(A_i)-\sum P(A_i \cap A_j) + \sum P(A_i \cap A_j \cap A_k) - \cdots + (-1)^n \sum P(A_1 \cap A_2 \cap \cdots \cap A_n)$$

The general term is
$$C(n, k)\cdot \frac{(n-k)!}{n!} = \frac{1}{k!}$$

$$1-\frac{1}{2!}+\frac{1}{3!}-\cdots+\frac{1}{20!}$$

Raw code:

There are 20 person in a Christmas party. What is the probability that at least one of them get his own gift?

Solution:
Let $A_i$=person i get his own gift
Then we want $P(A_1 \cup A_2 \cup \cdots \cup A_n)$,
which is equal to
$$\sum P(A_i)-\sum P(A_i \cap A_j) + \sum P(A_i \cap A_j \cap A_k) - \cdots + (-1)^n \sum P(A_1 \cap A_2 \cap \cdots \cap A_n)$$

The general term is
$$C(n, k)\cdot \frac{(n-k)!}{n!} = \frac{1}{k!}$$

$$1-\frac{1}{2!}+\frac{1}{3!}-\cdots+\frac{1}{20!}$$