Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Saturday, 21 May 2016

Deriving Power Sums by Polynomial Interpolation

Most of us have seen these formulas: $$\sum_{i=k}^n k=1+2+\cdots+n=\dfrac{1}{2}n(n+1)\\ \sum_{i=k}^n k^2=1+4+\cdots+n^2=\dfrac{1}{6}n(n+1)(2n+1)\\ \sum_{i=k}^n k^3=1+8+\cdots+n^3=\dfrac{1}{4}n^2(n+1)^2\\ \sum_{i=k}^n k^4=1+16+\cdots+n^4=\dfrac{1}{30}n(n+1)(2n+1)(3n^2+3n-1).$$ These can be proved by mathematical induction, but we can actually derive them on our own!

We simply acknowledge the following fact: for any positive integer $p$, the power sum $\sum_{k=1}^n k^p$ can always be expressed as a polynomial $f(n)$ that has degree $p+1$ and has rational coefficients.

Let's try to derive the first and the last formula. For the first, let $f(n)=an^2+bn+c$. We have $f(1)=1,f(2)=3,f(3)=6$. We want to solve the linear system: $$a+b+c=1\\ 4a+2b+c=3\\ 9a+3b+c=6,$$ or equivalently in matrix form $$\begin{pmatrix}1&1&1\\ 4&2&1\\ 9&3&1\end{pmatrix}\begin{pmatrix}a\\b\\c\end{pmatrix}=\begin{pmatrix}1\\3\\6\end{pmatrix}.$$ Note that $$\begin{pmatrix}1&1&1\\ 4&2&1\\ 9&3&1\end{pmatrix}$$ is a Vandermonde matrix. Just type in vander([1 2 3])\[1;3;6] in Matlab and we have the solution: $a=1/2,b=1/2,c=0$.

For the last formula, let $f(n)=an^5+bn^4+cn^3+dn^2+en+f$. Then $f(1)=1,f(2)=17,f(3)=98,f(4)=354,f(5)=979,f(6)=2275$. Type vander([1 2 3 4 5 6]\[1;17;98;354;979;2275] in Matlab and we have $a=1/5, b=1/2, c=1/3, d=0, e=-1/30, f=0$. Finally, factor(1/5*x^5+1/2*x^4+1/3*x^3-1/30*x) gives the formula. The reader can also try to derive the formulas for $\sum_{i=k}^n k^5$ and $\sum_{i=k}^n k^6$.

Saturday, 23 April 2016

A problem on diagonalisation

Give examples of the following types of operators defined by a $2\times 2$ matrix or explain why such an operator can't exist:
i) diagonalisable, invertible,
ii) diagonalisable, not invertible,
iii) not diagonalisable, invertible,
iv) not diagonalisable, not invertible.



Let's just consider the case in $\Bbb R$.

i) $$\boxed{\begin{pmatrix}a&0\\0&b\end{pmatrix},\quad \text{a and b are not necessarily different},\quad a,b\neq 0}$$ Any diagonal matrix $D$ is diagonalisable: $$I_n DI_n=D.\\ \boxed{\begin{pmatrix}a&b\\b&a\end{pmatrix},\quad a\neq b,\quad a\neq -b,\quad\text{a can be zero.}}$$ Calculations: $(a-\lambda)^2-b^2=0\Rightarrow \lambda=a-b,a+b$. $$\ker\begin{pmatrix}a-(a+b)&b\\b&a-(a+b)\end{pmatrix}=\text{span}\bigg\{\begin{pmatrix}1\\1\end{pmatrix}\bigg\}\\ \ker\begin{pmatrix}a-(a-b)&b\\b&a-(a-b)\end{pmatrix}=\text{span}\bigg\{\begin{pmatrix}1\\-1\end{pmatrix}\bigg\}$$ The two eigenvectors form an eigenbasis. Alternatively, one can use the result: if $A\in M_{n\times n}(\Bbb F)$ has $n$ distinct eigenvalues in $\Bbb F$, then $A$ is diagonalisable over $\Bbb F$. $$\boxed{\begin{pmatrix}a&b\\b&c\end{pmatrix},\quad a\neq c,\quad b\neq 0,\quad\text{a can be zero.}}$$ The calculation is similar, but involves the use of quadratic formula. Since $a\neq c$ and $b\neq 0$, we have two distinct eigenvalues. Symmetric matrices are thus diagonalisable. $$\boxed{\begin{pmatrix}a&b\\0&c\end{pmatrix},\quad a\neq c,\quad b\neq 0}$$ Upper triangular matrices with distinct diagonal entries are diagonalisable because of the same reason: they have distinct eigenvalues. The above matrices are invertible since their determinants are non-zero.

ii) $$\boxed{\begin{pmatrix}a&0\\0&0\end{pmatrix},\quad a\neq 0}$$ Again, any diagonal matrix is diagonalisable. $$\boxed{\begin{pmatrix}a&a\\a&a\end{pmatrix},\quad a\neq 0}$$ Calculation: $(a-\lambda)^2-a^2=0\Rightarrow \lambda=0,2a$. When $a=1$, the matrix is called matrix of ones. $$\boxed{\begin{pmatrix}a&a\\b&b\end{pmatrix}, \begin{pmatrix}a&b\\a&b\end{pmatrix},\quad a+b\neq 0,\;\;\text{a,b not both zero.}}$$Calculation: $(a-\lambda)(b-\lambda)-ab=0\Rightarrow \lambda=0,a+b$.

iii) $$\boxed{\begin{pmatrix}a&b\\0&a\end{pmatrix},\begin{pmatrix}a&0\\b&a\end{pmatrix},\quad \text{a and b are not necessarily different},\quad a,b\neq 0}$$ These are known as the shear transformations, which are not diagonalisable. Proof by contradiction: Suppose $\begin{pmatrix}a&b\\0&a\end{pmatrix}$ is diagonalisable. Then there exist a diagonal matrix $D$ and an invertible matrix $V$ s.t. $$\begin{pmatrix}a&b\\0&a\end{pmatrix}=V^{-1}DV.$$ We know just by looking at the matrix that $a$ is an eigenvalue with multiplicity $2$. So $D=\begin{pmatrix}a&0\\0&a\end{pmatrix}=aI_2$. We then have $\begin{pmatrix}a&b\\0&a\end{pmatrix}=aV^{-1}I_2V=aI_2=\begin{pmatrix}a&0\\0&a\end{pmatrix}$ which is a contradiction.

iv) $$\boxed{\begin{pmatrix}0&a\\0&0\end{pmatrix},\quad a\neq 0}$$ Its only eigenvalue is $0$ of multiplicity $2$.

From this problem, we see that invertibility does not necessarily nor sufficiently imply diagonalisability! Another important fact that one can observe from (ii) and (iv) is that if a square matrix $A$ is not invertible, then $\lambda=0$ is an eigenvalue of $A$. The converse also holds: if $\lambda=0$ is an eigenvalue of $A$, then $A$ is not invertible.

Claim: $A\in M_{n\times n}(\Bbb F)$ is singular iff $\lambda=0$ is an eigenvalue of $A$.
Proof: $\lambda=0$ is a solution of the characteristic equation $\lambda^n+c_1\lambda^{n-1}+\cdots+c_n=0$ iff $c_n=0$. By definition, $\det(\lambda I-A)=\lambda^n+c_1\lambda^{n-1}+\cdots+c_n$. When $\lambda=0$, we have $\det(\lambda I-A)=\det(-A)=(-1)^n\det(A)=c_n$. We thus have $\det(A)=0$ iff $c_n=0$ iff $\lambda=0.\;\Box$



Here's a summary:

i) diagonalisable, invertible: symmetric, upper triangular, diagonal
ii) diagonalisable, not invertible: nilpotent, scalar multiples of matrix of ones
iii) not diagonalisable, invertible: shear
iv) not diagonalisable, not invertible: nilpotent.

I tried to enumerate all possibilities but failed. I can't imagine the amount of work it takes to characterise diagonalisation for $3\times 3$ matrices...

Questions:
$$\boxed{\begin{pmatrix}a&b\\c&d\end{pmatrix},\quad (a-d)^2=-4bc,\quad ad-bc\neq 0}$$ has repeated eigenvalues, so I consider it fitting (iii), but can the kernel corresponding to that one eigenvalue be spanned by two eigenvectors? Similar questions for $$\boxed{\begin{pmatrix}a&a\\a&b\end{pmatrix},\quad 5a^2+b^2=2ab,\quad a(b-a)\neq 0}\\
\boxed{\begin{pmatrix}a&a\\b&c\end{pmatrix},\quad a^2+c^2=2ac-4ab,\quad a(c-b)\neq 0}\\
\boxed{\begin{pmatrix}0&a\\b&c\end{pmatrix},\quad c^2=-4ab,\quad ab\neq 0}$$ I'm not even sure if there are other possibilities for (iii) and (iv).

Saturday, 2 April 2016

Primitive Roots, Discrete Logarithms

(a) Show that for each element $x\in \Bbb Z_{19}^\times$ there is a number $n\in \Bbb N$ s.t. $x=[2]^n.$
(b) Show that the function $$\log:\begin{align}\Bbb Z_{19}^\times&\to \Bbb Z_{18}\\ [2]^n&\mapsto n\end{align}$$ is well-defined and has the property that $\log(xy)=\log x+\log y,\log(x/y)=\log x-\log y$ and $\log x^r=r\log x$ for all $x,y\in \Bbb Z_{19}^\times$ and all $r\in \Bbb Z$.
(c) List the values of this function and use them to determine $30^{14}\;\text{mod}\;19$ and $25^{11}\;\text{mod}\;19$.



Food for thought:
Intuition for (a)? Is it related to period?
Why choose $2$ in particular? Will other numbers work? [Yes! Read till the end.]
Any applications in modular exponentiation? [Yes, discrete logarithms are used in cryptography!]

I only worked on (a) and (c).

(a) My failed attempts: I expressed $x$ as $x=2^n+19y\;\exists\; y\in \Bbb Z$ and tried to use the fact that $(2,19)=1$, which didn't work. The next day I thought of proving $$\log:\begin{align}\Bbb Z_{19}^\times&\to \Bbb Z_{18}\\ [2]^n&\mapsto n\end{align}$$ is surjective, but I can't find $n$ in terms of $x$ explicitly. A simple proof is to prove that $\Bbb Z_{19}$ is a cyclic group with generator $2$, which follows from the results in (c).

This problem is actually related to primitive roots. If $a$ is a primitive root modulo $n$, then every element of $\Bbb Z_n$ is a power of $a$. Or in the language of group theory: $a$ is a primitive root modulo $n$ if $a$ is a generator of the multiplicative group on non-zero objects modulo $n$. Here we state without proof a handy result for proving (a): $a$ is a primitive root modulo $p$ if it has order $p-1$. [To be proved later]

Recall Fermat's little theorem: if $p$ is a prime, $x\in \Bbb Z$ and $p\not\mid a$, then $a^{p-1}\equiv_p 1$. Recall also the definition of order of $a$ modulo $n$, $\text{ord}_n a$, for positive integer $n>1$ and integer $x$, if there exists a least positive integer $k$ s.t. $a^k\equiv_n 1$, then $\text{ord}_n a=k$ is the order. When we have $\text{ord}_p a=p-1$, $a$ is a primitive root modulo $p$.

Therefore, to prove (a), we need to prove that $2$ is a primitive root modulo $19$, which is true because $2^{18}\equiv_{19}1$.

(c) I used matlab to generate powers of two modulo 19.

2^0 = 1 (mod 19)
2^1 = 2 (mod 19)
2^2 = 4 (mod 19)
2^3 = 8 (mod 19)
2^4 = 16 (mod 19)
2^5 = 13 (mod 19)
2^6 = 7 (mod 19)
2^7 = 14 (mod 19)
2^8 = 9 (mod 19)
2^9 = 18 (mod 19)
2^10 = 17 (mod 19)
2^11 = 15 (mod 19)
2^12 = 11 (mod 19)
2^13 = 3 (mod 19)
2^14 = 6 (mod 19)
2^15 = 12 (mod 19)
2^16 = 5 (mod 19)
2^17 = 10 (mod 19)
2^18 = 1 (mod 19)
2^19 = 2 (mod 19)
2^20 = 4 (mod 19)

'Logarithm table' form:
n: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
log(2^n): 1 2 4 8 16 13 7 14 9 18 17 15 11 3 6 12 5 10

We now find $30^{14}\;\text{mod}\;19$ and $25^{11}\;\text{mod}\;19$: $$\begin{align}30^{14}&\equiv_{19}2^{14}\;15^{14}\\
&\equiv_{19}2^{14}\;34^{14}\\
&\equiv_{19}2^{14}\;2^{14}\;17^{14}\\
&\equiv_{19}2^{14}\;2^{14}\;(-2)^{14}\\
&\equiv_{19}2^{6}\quad \qquad [2^{18}=1]\\
&\equiv_{19}7\;\;\Box
\end{align}$$
$$\begin{align}25^{11}&\equiv_{19}6^{11}\\
&\equiv_{19}2^{11}\;3^{11}\\
&\equiv_{19}2^{11}\;22^{11}\\
&\equiv_{19}2^{11}\;2^{11}\;(-8)^{11}\\
&\equiv_{19}2^4\;(-2^{33})\\
&\equiv_{19}-2\\
&\equiv_{19}17\;\;\Box
\end{align}$$
Lessons learnt:
At first I thought $[2]^n\mapsto n$ means $n$ is one value, namely, $[2]^1\mapsto 1$, but later I realised it doesn't make sense! The values of the range are just the values of $2^n\;\text{mod}\;19$, which are $1,2,3,\cdots,18$. This makes sense because there are $\phi(19)=19-1=18$ elements in $\Bbb Z_{19}^\times$, which explains why the range is $\Bbb Z_{18}$.
Also, I should rely on my brain rather than matlab: $\text{mod}(30^{14},19)$ in matlab gives $0$ but the answer is really $7$. ($\text{mod}(11^{14},19)$ in matlab gives $7$ though...)

Extensions:

3^0 = 1 (mod 19)
3^1 = 3 (mod 19)
3^2 = 9 (mod 19)
3^3 = 8 (mod 19)
3^4 = 5 (mod 19)
3^5 = 15 (mod 19)
3^6 = 7 (mod 19)
3^7 = 2 (mod 19)
3^8 = 6 (mod 19)
3^9 = 18 (mod 19)
3^10 = 16 (mod 19)
3^11 = 10 (mod 19)
3^12 = 11 (mod 19)
3^13 = 14 (mod 19)
3^14 = 4 (mod 19)
3^15 = 12 (mod 19)
3^16 = 17 (mod 19)
3^17 = 13 (mod 19)
3^18 = 1 (mod 19)
3^19 = 3 (mod 19)
3^20 = 9 (mod 19)

Replacing $2$ by $3$ works.
$$\log:\begin{align}\Bbb Z_{19}^\times&\to \Bbb Z_{18}\\ [3]^n&\mapsto n\end{align}\\ 30^{14}\equiv_{19}3^{14}\;(-3)^{28}\equiv_{19}3^6\equiv_{19}7\\ 25^{11}\equiv_{19}6^{11}\equiv_{19}2^{11}3^{11}\equiv_{19}15\cdot 10\equiv_{19}17$$ In fact, $10,13,14,15$ also work because they are also primitive roots. (Question: How to find all primitive roots modulo a number?)

More to learn:
cryptography
an advanced proof that $\Bbb Z_p^\times \cong \Bbb Z_{p-1}$
cyclicity of $\Bbb Z_p^\times$
using primitive roots to solve problems