Monday, December 22, 2014

Comparing Data Set With a Predefined Data Set Using Matrix Operations In Matlab

This post is related to Matlab. In Matlab it is always recommended to use matrix operations instead of using loops such as for or while.

The following algorithm will compare a given set of data from a predefined data set with only matrix operations without using loops

The algorithm will be explained using an example.

If the predefined dataset is given as $\mathcal{D}$ and the dataset given is $\mathcal{E}$,

$$\mathcal{D}=\begin{bmatrix} 0 &0& 0\\ 0& 0 &1\\ 0& 1 &0\\ 0& 1 &1\\ 1& 0& 0\\ 1& 0& 1\\ 1& 1& 0\\ 1& 1& 1 \\
\end{bmatrix} \qquad \mathcal{E}=\begin{bmatrix}
   1 &0 &1 \\ 1 &1 &1\\ 1 &1 &1\\ 0 &0 &1\\ 0 &1 &0
\end{bmatrix}$$

Define
 $$ \mathcal{S}=\mathcal{E}\cdot\mathcal{D}' $$

then

$$ \mathcal{S}=
\begin{bmatrix}
0& 1& 0& 1& 1& 2& 1& 2\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 0& 1& 0& 1& 0& 1\\
0& 0& 1& 1& 0& 0& 1& 1
 \end{bmatrix}$$

Get the sum of the each row of $\mathcal{D}$ and $\mathcal{E}$

$$\mathcal{D}_{r\_sum}= \begin{bmatrix}
 0\\
 1\\
 1\\
 2\\
 1\\
 2\\
 2\\
 3\\
\end{bmatrix}
\qquad
\mathcal{E}_{r\_sum}=\begin{bmatrix}
2\\
3\\
3\\
1\\
1\\
\end{bmatrix}$$

Now define a new matrix with the same dimension as of $\mathcal{S}$ for each $\mathcal{E}_{r\_sum}$ and $\mathcal{D}_{r\_sum}$ as $\mathcal{E}_{sum\_mat}$ and $\mathcal{D}_{sum\_mat}$

$$\mathcal{E}_{sum\_mat}=[\mathcal{E}_{r\_sum}  \mathcal{E}_{r\_sum}  \cdots  \mathcal{E}_{r\_sum}]$$
$$\mathcal{D}_{sum\_mat}=\begin{bmatrix}
\mathcal{D}_{r\_sum}' \\\mathcal{D}_{r\_sum}'\\ \vdots \\ \mathcal{D}_{r\_sum}'
\end{bmatrix}$$

For this example results will be,

$$\mathcal{E}_{sum\_mat}=\begin{bmatrix}
2& 2& 2& 2& 2& 2& 2& 2\\
3& 3& 3& 3& 3& 3& 3& 3\\
3& 3& 3& 3& 3& 3& 3& 3\\
1& 1& 1& 1& 1& 1& 1& 1\\
1& 1& 1& 1& 1& 1& 1& 1\\
\end{bmatrix}$$

$$\mathcal{D}_{sum\_mat}=\begin{bmatrix}
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
\end{bmatrix}$$

Now compare each $\mathcal{E}_{sum\_mat}$ and $\mathcal{D}_{sum\_mat}$ with $\mathcal{S}$ separately and if each of $\mathcal{S}$ is equal to the elements of the two matrices the symbol 1 is recorded. The result of the example is given below

$$\mathcal{E}_{com}=\begin{bmatrix}
  0&     0&     0&     0&     0&     1&     0&     1\\
    0 &    0 &    0 &    0 &    0 &    0 &    0 &    1\\
     0  &   0  &   0  &   0  &   0  &   0  &   0  &   1\\
     0   &  1   &  0   &  1   &  0   &  1   &  0   &  1\\
     0    & 0    & 1    & 1    & 0    & 0    & 1    & 1\\
 \end{bmatrix}$$

$$\mathcal{D}_{comp}=\begin{bmatrix}
     1&     1&     0    & 0&     1    & 1&     0    & 0\\
     1 &    1 &    1   &  1 &    1   &  1 &    1   &  1\\
     1  &   1  &   1  &   1  &   1  &   1  &   1  &   1\\
     1   &  1   &  0 &    0   &  0 &    0   &  0 &    0\\
     1    & 0    & 1&     0    & 0&     0    & 0&     0\\
\end{bmatrix}$$

From these two results at each element mark as 1 if and only if the both elements of each matrix is 1. We will name it as $\mathcal{C}$

$$\mathcal{C}=\begin{bmatrix}
     0&     0    & 0&     0    & 0&     1    & 0&     0\\
     0 &    0   &  0 &    0   &  0 &    0   &  0 &    1\\
     0  &   0  &   0  &   0  &   0  &   0  &   0  &   1\\
     0   &  1 &    0   &  0 &    0   &  0 &    0   &  0\\
     0    & 0&     1    & 0&     0    & 0&     0    & 0\\
\end{bmatrix}$$

So the each column of $\mathcal{C}$ will denote the element of the predefined data set - $\mathcal{D}$ and the rows will represent each data point of $\mathcal{E}$. Element $c_{ij}$ of $\mathcal{C}$ will be 1 if $i^{th}$ data point of $\mathcal{E}$ is same as the $j^{th}$ data point of $\mathcal{D}$

The Matlab code for the above procedure is given below.






Friday, July 19, 2013

WONDER OF EYE VISION

When you walk on a railway if we can see a straight rail road, we see the two rails meet together at the horizon. But yet we know that the two rails are parallel to each other. This is due to the perception of the eye. The gist of the post is not to analyze the above matter but a similar perception that is bit advanced. This came to my mind when I was travelling on the train. Most of you might have experienced the same phenomena. That is, when you look outside of a travelling train, we can see that the objects near to the train move faster, while the objects that is distant move slower. We know that the concept of movement of the 'objects outside' is explained using the relative motion, but why do they move at different speeds? Let's analyze this scenario in this post.

In this post I've assumed that the eye lobe is a perfect sphere. Lets consider a point that is moving parallel at a $l$ distant from the eye.


Here,

$d$ = diameter of the eye lobe
$x$ = distant of a moving point from the reference
$s$ = arc length between the image of the point and the reference


$$x = l\sin \alpha$$

When differentiated with respect to time,

$$\frac{dx}{dt} = l\cos\alpha \frac{d\alpha}{dt}\qquad (1)$$
But,
$$ s = \frac{d}{2} (2\alpha)=d\alpha$$
When differentiated with respect to time,
$$\begin{align}
\frac{ds}{dt} &= \frac{d(d\alpha)}{dt}\qquad \text{($d$ = constant)}\\
\frac{d\alpha}{dt} &= \frac 1d \frac{ds}{dt}\qquad (2)\\
\end{align}$$

From equation 2 and 1,
$$\frac{dx}{dt} = l\cos \alpha \frac 1d \frac{ds}{dt}$$
Since,
$$\begin{align}
\frac{dx}{dt} &= v\qquad \text{($v$ = velocity)}\\
v &= l\cos \alpha \frac 1d \frac{ds}{dt}\\
\frac{ds}{dt} &= \frac{vd}{l\cos\alpha} \qquad (3)
\end{align}$$

Although the actual velocity of the point is $v$, the velocity observed by the eye is denoted by the expression $\frac{ds}{dt}$ (lets call it 'visible velocity').

Consider two instances where both points move at same velocity,

Since it is mentioned that the both points move at the same velocity ($v$) and the diameter of the eye lobe doesn't change at two scenarios

If the respective visible velocities are, $\frac{ds_1}{dt}$ and $\frac{ds_2}{dt}$, From equation 3,
$$\frac{ds_1}{dt} = \frac{vd}{l_1 \cos \alpha_1}, \quad \frac{ds_2}{dt} = \frac{vd}{l_2\cos \alpha_2}$$

Since,
$$\alpha_1 >\alpha_2\implies \cos \alpha_1 < \cos \alpha_2$$
and
$$l_1< l_2$$
Therefore,
$$\begin{align}
l_1\cos\alpha_1 &<l_2\cos\alpha_2 \\
\frac{1}{l_1\cos\alpha_1} & > \frac{1}{l_2\cos\alpha_2}\\
\frac{vd}{l_1\cos\alpha_1} & > \frac{vd}{l_2\cos\alpha_2}\\
\frac{ds_1}{dt} &> \frac{ds_2}{dt}
\end{align}$$

So this means that the visible velocity of the closer object is larger than the visible velocity of the object which is far.

This proves that when the eye is moving parallel to two images, it recognizes that the image situated nearer to the eye moving faster than the image situated behind






Monday, September 3, 2012

DIVISIBILITY OF A NUMBER BY 3,6 OR 9

In school at secondary level, we are used to follow a method to find whether a given number is divisible by 3,6, and 9.
  • Dividing by 3
    1. Add up all the digits in the number.
    2. Find out what the sum is. If the sum is divisible by 3, so is the number
    3. For example: 12123 (1+2+1+2+3=9) 9 is divisible by 3, therefore 12123 is too!
  • Dividing by 6
  1. Add up all the digits in the number.
  2. Find out what the sum is. If the sum is divisible by 3 and 2, the number is divisible by 6.
  3. For example: 12120 (1+2+1+2+0=6) 9 is divisible by 3, therefore 12123 is too!
  • Dividing by 9
  1. Almost the same rule and dividing by 3. Add up all the digits in the number.
  2. Find out what the sum is. If the sum is divisible by 9, so is the number.
  3. For example: 43785 (4+3+7+8+5=27) 27 is divisible by 9, therefore 43785 is too!
Now, knowing the method of finding the divisibility, let's try to prove how this work.

Take the Number that we consider as $P$ and it is represented in standard method as following,

$$ P=a_na_{n-1}a_{n-2}\dots a_{2}a_{1}$$

Where this can be represented accurately as following,

$$P=a_n\times 10^{n-1}+a_{n-1}\times 10^{n-2}+a_{n-2}\times 10^{n-3}+\dots +a_2\times 10^1+a_1\times 10^0$$
or
$$ P=\sum \limits_{r=1}^{n}a_r\times 10^{r-1} \qquad \qquad \dots \mathrm{(A)} $$

case 1: Divisibility of a number by 3

$$\begin{align}
\sum \limits_{r=1}^{n} a_r&=3N \qquad (N\in \mathbb{Z})\qquad \qquad \dots \mathrm{(i)}\\
\sum \limits_{r=2}^{n} a_r+a_1&=3N\qquad \qquad \qquad \dots \mathrm{(a)}\\
a_1&=3N-\sum \limits_{r=2}^{n}a_r\\
\end{align}$$

from (a) and (A)

$$\begin{align}
P&=\sum \limits_{r=2}^{n}a_r \times 10^{r-1} +a_1\\
&=\sum \limits_{r=2}^{n}a_r\times 10^{r-1}+3N -\sum \limits_{r=2}^{n} a_r\\
&=\sum \limits_{r=2}^{n}(a_r\times 10^{r-1}-a_r)+3N\\
&=3N+\sum \limits_{r=2}^{n} a_r(10^{r-1}-1)\\
&=3N+\sum \limits_{r=2}^{n}a_r(9\times \underbrace{111\dots 111}_{(r-1)\,ones})\\
&=3N+9\sum \limits_{r=2}^{n}a_r\left[ \sum \limits_{i=2}^{r}10^{r-i}\right]\\
P&=3\left[ N+3\sum \limits_{r=2}^{n} a_r\left( \sum \limits_{i=2}^{r} 10^{r-i} \right) \right] \\
\end{align}$$

So when the requirement stated in the equation (i) is fulfilled the number - P is devisible by 3.

ii.

$$\sum \limits_{r=1}^{n}a_r=3N$$ and if $$P=2N'\qquad (N,N' \in \mathbb{Z})$$

According to i., P is divisible by 3, but P is also divisible by 2.

From this we can deduct, when above requirements are fulfilled, P is divisible by 6.

iii.

$$\begin{align}
\sum \limits_{r=1}^{n}a_r&=9N \qquad \qquad (N\in \mathbb{Z})\qquad \dots \mathrm{(ii)} \\
\sum \limits_{r=2}^{n}a_r+a_1&=9N \\
a_1&=9N-\sum \limits_{r=2}^{n}a_r\qquad \qquad \dots \mathrm{(b)}\\
\end{align}$$

From (b) and (A),

$$\begin{align}
P&=\sum \limits_{r=2}^{n}a_r\times 10^{r-1}+a_1\\
&=\sum \limits_{r=2}^{n}a_r\times 10^{r-1}+9N-\sum \limits_{r=2}^{n}a_r\\
&=\sum \limits_{r=2}^{n}(a_r\times 10^{r-1}-a_r)+9N\\
&=9N+\sum \limits_{r=2}^{n}a_r(10^{r-1}-1)\\
&=9N+\sum \limits_{r=2}^{n}a_r(9\times \underbrace{111\dots 111}_{(r-1)\,ones})\\
&=9N+9\sum \limits_{r=2}^{n}a_r\left[ \sum \limits_{i=2}^{r}10^{r-i} \right] \\
P&=9\left[ N+\sum \limits_{r=2}^{n}a_r\left( \sum \limits_{i=2}^{r}10^{r-i}\right) \right]\\
\end{align}$$

So when the requirement stated in the equation (ii) is fulfilled the number - P is divisible by 9.

Now, from the above i,ii and iii proofs we can say that when the sum of the digits of a number is equal to 3,9 or even, we can divide it by 3,6 and 9 appropriately. But there can be a situation where the sum of the digits of a large number is also another large number where we cannot directly say that it is divisible by 3 or 9. For example take the number - 11938197159817956982923. The sum of the digits of this number is equal to 123. The sum of the number is also a considerably larger number. In this situation what we do is take the second number and again take the sum of the digits of the number. So, if the sum of the digits of that number is divisible by 3,9 or divisible by 3 and even (in this case we get directly either 3,6 or 9), according to the above proof we can say that the second number is divisible by 3,9 or divisible by 6. If so, according to the above proof we can say that the first number that is given to us in the first place, is also divisible by 3,9 or 6 accordingly.








References

Sunday, August 19, 2012

GRADIENT EXPLAINED IN DETAILS

In vector calculus, the term Gradient is widely used. This mathematical derivation is used to achieve a vector that is perpendicular to a given surface. This geometrical representation is a very important and useful derivation that in use. In this post the idea is to point out a problem to that I've come across in this derivation, and how to clarify it, since the same problem might come across to other peers who study this subject.

The normal derivation goes as follows,

When $\phi(x,y,z)$ represents a scalar field and differentiable for all $x,y,z$

The Gradient is defined as,

$$\nabla \phi = \frac{\partial \phi}{\partial x}\mathbf{i}+\frac{\partial \phi}{\partial y}\mathbf{j}+\frac{\partial \phi}{\partial z}\mathbf{k}$$

Now, let $\vec{r}(t)$ be the position vector of a point on the surface $\phi=c$ (where $c$ is constant).Thus,

$$\vec{r}(t)=x(t)\mathbf{i}+y(t)\mathbf{j}+z(t)\mathbf{k}$$

The vector $\frac{d\vec{r}}{dt}$ is tangent to the surface $\phi =c$ and it can be further represented as,


$$\vec{r}(t)=\frac{dx}{dt}\mathbf{i}+\frac{dy}{dt}\mathbf{j}+\frac{dz}{dt}\mathbf{k}$$


Lets take the dot product of the tangent vector and the gradient.

$$\begin{align}
\nabla \phi \cdot  \left( \frac{d\, \vec{r}(t)}{dt} \right) &= \left(\frac{\partial \phi}{\partial x}\mathbf{i}+\frac{\partial \phi}{\partial y}\mathbf{j}+\frac{\partial \phi}{\partial z}\mathbf{k}\right) \cdot  \left( \frac{dx}{dt}\mathbf{i}+\frac{dy}{dt}\mathbf{j}+\frac{dz}{dt}\mathbf{k} \right)  \\
&=\frac{\partial \phi}{\partial x}\frac{dx}{dt}+\frac{\partial \phi}{\partial y}\frac{dy}{dt}+\frac{\partial \phi}{\partial z}\frac{dz}{dt}\\
\end{align}$$

From multivariable chain rule,

$$\frac{\partial \phi}{\partial x}\frac{dx}{dt}+\frac{\partial \phi}{\partial y}\frac{dy}{dt}+\frac{\partial \phi}{\partial z}\frac{dz}{dt}=\frac{d\phi}{dt}$$

$$\therefore \quad \nabla \phi \cdot \left [\frac{d\, \vec{r}(t)}{dt} \right ] = \frac{d\phi}{dt}$$

since it is stated that $\phi =c$ therefore the derivative of a constant is equal to zero irrespective of the variable.

$$\begin{align}\therefore \quad \nabla \phi \cdot  \left[ \frac{d\, \vec{r}(t)}{dt} \right] &= \frac{d\phi}{dt}\\ &=0 \\ \end{align}$$

Having the dot product of two vector equal to zero we can conclude that these vectors are perpendicular to each other. We know that vector - $\vec{r}(t)$ is tangent to the surface $\phi =c$. Some might say that with this we can conclude that the gradient is perpendicular to the surface. But does this fact is enough to say that the gradient  is perpendicular to the surface? The image says else,

Gradients perpendicular to the tangent vector

This is where one might get stuck. But keep it for a while and look into the lines and surfaces in three dimensional space and their position vector representation in parametric method.

We know that the position vector of a line can be represented with one parameter.

Let's this parameter as $t$ and represent it's position vector by $\vec{r}(t)$.

Now let's look into its derivative and find out its geometrical representation. (This representation is already used in above proof)

Let $AB$ be a chord on the 3-D plane and $\vec{r}(t)$ be the position vector of a point on that chord. $t$ is the parameter which defines the position of the point on the chord.

$$\vec{r}(t)=x(t)\mathbf{i}+y(t)\mathbf{j}+z(t)\mathbf{k}$$

$$\begin{align} \overrightarrow{PQ}&=\overrightarrow{PO}+\overrightarrow{OQ}\\ &=-\vec{r}(t)+[\vec{r}(t)+\Delta \vec{r}(t)]\\ &=\Delta \vec{r}(t)\; [or\; \vec{r}(t+\Delta t)-\vec{r}(t)]\\ \end{align}$$

When $Q \rightarrow P$, $\Delta t \rightarrow 0$ and the line $PQ$ becomes tangent to the curve.Then let's consider $\frac{\Delta \vec{r}(t)}{\Delta t}$

$$\lim_{\Delta t \rightarrow 0} \frac{\Delta \vec{r}(t)}{\Delta t}=\frac{d\, \vec{r}(t)}{dt}$$

$\therefore \frac{d\, \vec{r}(t)}{dt}$ is tangent to the curve.

$$ \frac{d\, \vec{r}(t)}{dt}=\frac{dx}{dt}\mathbf{i}+\frac{dy}{dt}\mathbf{j}+\frac{dz}{dt}\mathbf{k}$$

Let $t=f(u)$ then,
$$ \frac{d\, \vec{r}(t)}{du}= \frac{d\, \vec{r}(t)}{dt}.\frac{dt}{du}$$

Since this is just a multiplication of a scalar, only the modulus value will be changed in the vector. Because of this we can conclude that there exists only one tangent vector to a curve.

Now lets go to a surface.

Please note that $P$,$Q$,$R$ are on the surface.

As mentioned in the one parametric curve following things can be shown,


  • when $Q$ approaches $P$ the $PQ$ line becomes tangent to the surface (same for the $PR$ line).
  • $\overrightarrow{PQ}=\vec{r}(\Delta u,v)-\vec{r}(u,v)$ and  $\overrightarrow{PR}=\vec{r}(u, \Delta v)-\vec{r}(u,v)$
and since,
$$\frac{\partial \vec{r}}{\partial u}=\lim_{\Delta u \rightarrow 0} \frac{\vec{r}(\Delta u,v)-\vec{r}(u,v)}{\Delta u}$$
  • $ \frac{\partial \vec{r}}{\partial u}$ and $ \frac{\partial \vec{r}}{\partial v}$ is tangent to the surface.
Unlike in curves, now we already have two tangents to the surface at a point.

Let $u=f(t,n)$ and $v=g(t,n)$

$$\frac{\partial \vec{r}}{\partial u}\frac{\partial u}{\partial t}+\frac{\partial \vec{r}}{\partial v}\frac{\partial v}{\partial t}=\frac{\partial \vec{r}}{\partial t}\qquad \qquad (1)$$


Consider,

$$\overrightarrow{PQ}=\frac{\partial \vec{r}}{\partial u} \qquad \qquad \overrightarrow{PR}=\frac{\partial \vec{r}}{\partial v}$$

since $\frac{\partial u}{\partial t}$ and $\frac{\partial v}{\partial t}$ are scalars we can take,

$$\overrightarrow{PQ'}=\frac{\partial \vec{r}}{\partial u}\frac{\partial u}{\partial t} \qquad \qquad \overrightarrow{PR'}=-\frac{\partial \vec{r}}{\partial v}\frac{\partial v}{\partial t}$$

then,

$$\begin{align}
\overrightarrow{R'Q'}&=\overrightarrow{R'Q}+\overrightarrow{QP'}\\
&=-\left[ -\frac{\partial \vec{r}}{\partial v}\frac{\partial v}{\partial t}\right] + \frac{\partial \vec{r}}{\partial u}\frac{\partial u}{\partial t} \\ &= \frac{\partial \vec{r}}{\partial u}\frac{\partial u}{\partial t}+\frac{\partial \vec{r}}{\partial v}\frac{\partial v}{\partial t}\\ &=\frac{\partial \vec{r}}{\partial t} \qquad [from (1)]\\ \overrightarrow{R'Q'}&=\frac{\partial \vec{r}}{\partial t}\\ \end{align}$$

We know that $\overrightarrow{PR}$ and $\overrightarrow{PQ}$ vectors are tangent to the surface. So we can conclude that the vector $\overrightarrow{R'Q'}$ also is tangent to the surface. This means the vector $\frac{\partial \vec{r}}{\partial t}$ is tangent to the surface. Similarly, the vector $\frac{\partial \vec{r}}{\partial n}$ is also tangent to the surface.

Since there exists infinitely many $(t,n)$ couples where $u=f(t,n)$ and $v=g(t,n)$. Eventually we can conclude that there are infinitely many tangents to a surface at a point.

Also before going to the original problem let's remind another property in partial differentiation.
i.e.,
$$\frac{du}{dt}=\frac{\partial u}{\partial x}\frac{dx}{dt}+\frac{\partial u}{\partial y}\frac{dy}{dt}$$

where $u=f(x,y)$ and $x=g(t)$ and $y=l(t)$

this can be applied to vectors as,
$$\frac{d\vec{r}}{dt}=\frac{\partial \vec{r}}{\partial x}\frac{dx}{dt}+\frac{\partial \vec{r}}{\partial y}\frac{dy}{dt}$$

we can show that $\frac{d\vec{r}}{dt}$ is a vector which is on the plane where the vectors $\frac{\partial \vec{r}}{\partial t}$ and $\frac{\partial \vec{r}}{\partial n}$ are on.

So now we can clearly see that there are infinitely many $\frac{d\vec{r}}{dt}$ also.

Now lets go back to our original problem.

The answer to the problem that we had here can be easily solved (or rather clarified) with the word which is occasionally used above - "infinitely many".

Let's take the equation,

$$\nabla \phi \cdot  \left( \frac{d\, \vec{r}(t)}{dt} \right) =0$$

Here, $\frac{d\vec{r}}{dt}$ represents infinitely many vectors which is tangent to the surface.

Since these vectors are on the plane which is tangent to the surface, so the gradient is tangent to that surface. Which eventually proves that the gradient is perpendicular to the surface.

So this is my explanation for the gradient. And it is more of a clarification since there is nothing wrong on the proof, but most of us get a bit confused in this.



















Tuesday, August 7, 2012

BINARY NUMBER SYSTEM EXPLAINED

With this post I am publishing my one of the old works that's been done in the year of 2010. This paper is about the binary number system. Mainly it focuses on the 1s compliment and the 2s compliment binary number system. 

This the first post that I am publishing.