Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Tuesday, 4 April 2017

Collatz Conjecture - a study.

God does not care about our mathematical difficulties. He integrates empirically. - Einstein.
In Mathematics, we often come across Conjectures. A conjecture is a conclusion or proposition based on incomplete information, for which no proof has been found[1].
Collatz Conjecture is named after Lothar Collatz [2]. The conjecture is also known as the conjecture.
Now, let us see how it works, it is, in fact very simple, no , no , no etc.
For any given positive integer ,

If we keep repeating this process,
We use the Half Or Triple Plus One acronymed to .
So, for any given positive number, say , we do the following steps:
15 is odd 15 3+1=4646 is even
23 is odd 233+1=703510653160
80402010516842
You get the idea now. In this blogpost, I will explore various aspects of the Collatz Conjecture. A loose structure of the blog is going to be,
  1. Collatz for Negative numbers.
  2. Collatz for Fractions.
  3. Collatz for Irrational Numbers.
  4. Collatz for Prime numbers and pattern recognition (if any) in the number of steps it needs for different scenarios to reach to one.
  5. What is the possible approach for the proof of collatz conjecture?
  6. Difficult and beautiful.


1.

I don’t understand why do they have the condition, in the conjecture. Let’s look at an example and see what happens when Collatz conjecture is applied to number, when .

For, , let us do the collatz conjecture,
-5 is odd -5 3+1 -14 is even -7 is odd -20 is even -10 is even -5 is odd -5 3+1 -14 is even -7 is odd -20 is even -10 is even -5

So, there it is.
If we use the Collatz conjecture to negative numbers, we get oscillating values and they even eventually set to the starting number itself.

Modified Collatz Conjecture:


These are called “Cycles” that exist for negative numbers and probably also for positive numbers. There is no form of proof, that says that there is absolutely no positive number that gives rise to a similar situation.

2.

For this section, we define something called Number-decimals.
Although, we already know that Even, Odd or Prime are only defined for whole numbers.
: Even-Number-decimal are defined as terminating fractions, say that are converted to decimals, say . Then we remove the decimal point from the number to get .
(If you find this definition to be strange, please comment below and mail me with a possible modification)

Now, if , then is an Even-Number-decimal and if , then then is an Odd-Number-decimal
So, it is pretty clear that the conjecture behaves the same in this scenario.

For example, take , which when converted to decimals comes as . Now, we run the algorithm on to get 1. Similarly for other fractions as well.


In the above approach, we considered fractions as decimal numbers, that is only how we can use the Collatz Conjecture on fractions; that too only if the decimal is terminating in nature. Fractions such as (), , etc. cannot be used in the Collatz conjecture for fractions.

3.

Collatz conjecture for irrational numbers is same as saying that we apply Collatz Conjecture to non-terminating decimals.
Just like , as mentioned above cannot work for Collatz; irrational numbers won’t work too.
For revision purpose, an irrational number is defined as a number that cannot be determined as the ratio of two integers. For illustration, , however, , no exact way of saying this.

4.

Collatz for primes will behave the same, that is my guess. Let’s have a look at it.


Let’s take all the primes from , namely, .

We are sure that all of them will reach eventually. I want to analyze that, in how many steps do they reach and if there is any correlation.

For this, I wrote a small script in R. The code is as follows:
collatz_numbers <- function(n, list_col=c()) {
  if(n==1) return(c(list_col, 1));
  collatz(ifelse(n%%2==0, n/2, 3*n +1), c(list_col, n))} 

We shall define the number of steps taken by the number to reach as .

So,
collatz_number(2)=1 ( = 1)
collatz_number(3)=3, 10, 5, 16, 8, 4, 2, 1 ( = 8)
collatz_number(5)=5, 16, 8, 4, 2, 1( = 6)
collatz_number(7)=7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1( = 16)
collatz_number(11) =11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 ( = 15)
collatz_number(13)=13, 40, 20, 10, 5, 16, 8, 4, 2, 1 ( = 10)
collatz_number(17)=17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1( = 13)
collatz_number(19)=19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1( = 21)
If we plot the numbers versus the number of steps it took to get to , we get the following plot,

Which is an increasing series, as per the Wikipedia article[1], have a look at this [3] to have a more clear idea.
The picture below also agrees to our finding,

One interesting observation is how HUGE numbers appear in the sequence. For that, I thoughtlessly try numbers out on my function,
collatz_number(20) = 20 10 5 16 8 4 2 1.
collatz_number(21)=21 64 32 16 8 4 2 1
collatz_number(22)=22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
collatz_number(23)=23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
collatz_number(24)=24 12 6 3 10 5 16 8 4 2 1
collatz_number(25)=25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
collatz_number(26)=26 13 40 20 10 5 16 8 4 2 1
collatz_number(27)=27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
collatz_number(28)=28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

All the largest numbers in the sequence are made bold. We can clearly see their chaotic nature, but there is never really any chaos, we just don’t look from a broad enough perspective.


5.

I have failed pathetically, at finding a proper proof for the the conjecture. However, I have some points that could be the lines on which someone could work for proving the conjecture. I will mention them in bullet points,
  • The first task is to show that for all positive numbers, there is not a single number that gives rise to a cycle (to know what is a “cycle”, read section 1).
  • Understanding the behavior of steps with increasing or decreasing numbers.
  • Understanding that is usually the universal way of converting any number into an Even number, so I guess, he played the CLEVER trick there.
  • Looking at even and odd as and and finding if they follow a particular pattern. Further, we can convert these Binary strings to Hexadecimal strings and see if there is a possibility of a Collatz conjecture in Alphabetical domain. [Original idea by Pragyaditya Das].

6.

Collatz conjecture is very nice, if you haven’t seen it already, let me point it out to you.

No matter how many time we do and to a system; if we semi-periodically keep pulling from the system; it will eventually lead to .

Collatz conjecture is the simplest mathematical open-problem available; you can explain all your non-math, non-science or non-engineering friends about it; hell! they might even give a noob try to prove it even.


Cheers! with a sad end…

Wednesday, 29 March 2017

A Byronic Hero, Harry Potter and Bernoulli Numbers.

George Gordon Byron (22-01-1788 to 19-4-1824), is commonly known as Lord Byron, was a British Poet, a Politician and one of the brightest figures of the Romantic Movement. He is very easily regarded as one of the greatest British Poets of all times.
Considered to be the first modern-style celebrity. His image and thought of the Byronic Hero fascinated the public, his peers and his followers from the Literature community and from other communities as well. [3]
The first Byronic Hero, was probably Bryon himself. His wife Annabella coined the common, uncommon term “Byromania” to closely explain and depict the clamor around him. He was the classic depiction of the modern day Rockstar. He was fond of self-promotion and was supremely self-aware. All his portraits were drawn with a mindset of advertising him as a “Man of Action” and not as a poet or a romanticist. [2]
Lord Byron was a bisexual; however, the claims are yet to confirmed. Of course, the confirmation is not possible due to the heavy wave of suppression it has faced for decades. [1]
The figure, Byronic Hero, infuses much of Lord Byron’s work; in fact, many consider Byron, himself to be a perfect example of the characteristics of the Byronic Hero.
Popular study points that Lord Byron’s Byronic Hero was influenced by John Milton’s famous piece Satan from Paradise Lost. [4]
An excerpt is as follows:
In shape and gesture proudly eminent,
Stood like a tower. His form had yet not lost
All her original brightness, nor appeared
Less then Archangel ruined, and the excess
Of glory obscured: as when the sun new-risen
Looks through the horizontal misty air
Shorn of his beams, or from behind the moon,
In dim eclipse, disastrous twilight sheds
On half the nations, and with fear of change
Perplexes monarchs. Darkened so, yet shone
Above them all the archangel; but his face
Deep scars of thunder had intrenched, and care
Sat on his faded cheek, but under brows
Of dauntless courage, and considerate pride
Waiting revenge. Cruel his eye, but cast
Signs of remorse and passion, to behold
The fellows of his crime, the followers rather
(Far other once beheld in bliss), condemned
Forever now to have their lot in pain.
The Byronic Hero is a variant of the Romantic Hero [5]. Although there are traits and characteristics that exemplify the type, both Byron’s own persona as well as characters from his writings are considered to provide defining features.
The Byronic Hero first made it’s public appearance with Lord Byron’s semi-autobiographical epic narrative poem Childe Harold’s Pilgrimage, can be read for free on Project Gutenberg here Childe Harold’s Pilgrimage.
Thomas Babington Macaulay, popularly known Lord Macaulay, a British Historian and critic described the character as “a man proud, moody, cynical, with defiance on his brow, and misery in his heart, a scorner of his kind, implacable in revenge, yet capable of deep and strong affection“.
Byronic Hero was the instrument to many Romantic Classics and Gothic Love pieces of the 19th Century. One of the noteworthy one is Emily Brontë’s Wuthering Heights. The notorious, yet loved, Heathcliff is a classic depiction of a Byronic Hero.
A picture depicting the salient features of Heathcliff’s characteristics is as follows:
http://www.shmoop.com/wuthering-heights/heathcliff.html
In American Renaissance, writers like Poe and Hawthrone took active reference in their work from the Byronic Hero.
In fact, Professor Severus Snape, of the Harry Potter Series, played by Late Alan Rickman is a Byronic Hero. He is dark, he is arrogant; yet demonstrated supreme love and sacrifice.
enter image description here
In this blog, as we are talking of Lord Byron; I don’t want to miss the chance to show my admiration for Ada Lovelace, Lord Byron’s daughter. Who is widely regarded as the first computer programmer. She was the first person to actually recognize the capabilities of Charles Babbage’s Analytical Engine, beyond pure mathematical tasks.
Charles Babbage’s seminar at University of Turin in 1840 on his Analytical Engine was recorded by Luigi Menabrea, a young Italian Engineer and future Prime Minister of the country. The language of recording was French. That is when Charles Wheatstone, the founder of the Wheatstone Bridge, asked Ada to translate that. She not only translated the paper, but also added detailed notes to it. She broke-down the lecture into parts from to . In section , she explained the method of Bernoulli’s number using the Analytical Engine.
The following picture shows her algorithm:
Ada's Algo
For people wondering what are Bernoulli’s numbers, let me provide a very simple explanation,
Benoulli’s numbers are defined from the power series expansion of ; for integers we write so that,

Multiplying both sides by ; will give,
,
.
Very confusing, right? To me too. Some rigorous math has to go in to understand this; and Lovelace wrote an algorithm for this when women weren’t even allowed to attend school.
Cheers!

Monday, 7 November 2016

How I made my first "serious" R package?

Hello there,

It was when I was working on a Human Activity Recognition system using R when a very good and challenging idea occurred to me.

The idea was not something that could be done with minimal effort, in fact, it was one of the toughest projects that I have ever done.

The idea was, to CREATE AN R PACKAGE OF MY OWN.

So, I read up some blogs on the internet and go to know how to create a basic R package. 

The associated packages needed to proceed with the work are :

  1. Devtools.
  2. ROxygen2
The above-mentioned packages can be easily installed by the following code:



After you are done with the above step. You need to load both the packages. Simple enough, I will still add the code here,

Now, we are all set-up.

One thing I would like to point out is that packages are nothing but functions.

So, let's write a function for our package.

I would like to write a function for getting the Benford Score of a given digit.

I don't know if Benford score is a legit term, but what I mean by it is, given a digit ( say $i$ ),
what is the probability that the first digit of any random number will be $i$ .

Wikipedia Article on Benford Law,

Benford's law, also called the first-digit law, is an observation about the frequency distribution of leading digits in many real-life sets of numerical data. The law states that in many naturally occurring collections of numbers, the leading significant digit is likely to be small.For example, in sets which obey the law, the number 1 appears as the most significant digit about 30% of the time, while 9 appears as the most significant digit less than 5% of the time. By contrast, if the digits were distributed uniformly, they would each occur about 11.1% of the time.Benford's law also makes (different) predictions about the distribution of second digits, third digits, digit combinations, and so on.
Mathematically, the probability of $i$ being the first digit of any number is given by,

$\hspace{2.0cm}$  $\large {P(i)}$ = $\large {log_{10} (1 + \frac{1}{i})}$

Writing it into a function won't be a big deal.

Let's start then, there isn't much to explain. Therefore, the code is as follows,


Now, when you run,


You must get something like this : [1] 30.103 

This is the probability of $1$ occurring in the first place.

Go ahead and try for other digits as well, and you will see the probabilities decreasing as you go from $1$ to $9$.

Yeah, this is the Zipf's Law for numbers.

Okay, enough with the small talk.

Let's make some babies now,

Oops! Sorry. Packages.

Follow the following steps,
  1. Save the function file as main.R in a folder.
    No specific reason, I just like the name "main".
  2. Press Ctrl+Shift+H and travel to the folder in which main.R is saved and select that folder as the working directory. 
  3. Now, type create("Myfirstpkg"),
    You should see something like this,

    Creating package 'Myfirstpkg' in 'C:/Users/user/Desktop/pkgFolder'
    No DESCRIPTION found. Creating with values:

    Package: package

    Title: What the Package Does (one line, title case)

    Version: 0.0.0.9000
    Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre")) Description: What the package does (one paragraph). Depends: R (>= 3.3.2) License: What license is it under? Encoding: UTF-8 LazyData: true * Creating `package.Rproj` from template. * Adding `.Rproj.user`, `.Rhistory`, `.RData` to ./.gitignore
  4. Now, inside the folder, you will see another folder named after your package name, here it is Myfirstpkg

  5. Go inside Myfirstpkg folder, there will be another folder named "R", put your main.R in that folder.
  6. Now, do some documentation,
  7. After this, go inside the Myfirstpkg through RStudio console (Refer Step 1) and then, type, document(),

    You should see something like this, 


After all these, you need to edit the DESCRIPTION file as follows,


The above image is taken from Writing R Extensions.

After you are done with it, do the following,


Yaaaaay!!

You made your first R package. :)

For more serious development, upload your file to Win Builder R.

If you pass all the cases without any error, submit it to CRAN. :)

Please have a look at my package : csvFileDescriptor-on Github. Install it and please star it if you like the functionality.

Cheers.