Jekyll2024-02-15T22:02:49-08:00http://localhost:4000/feed.xmlSudipta Kar/ সুদীপ্ত করSudipta Karsudipta.kar.8080@gmail.comGetting fibonacci sequence with immutability2016-12-16T00:00:00-08:002016-12-16T00:00:00-08:00http://localhost:4000/Getting%20Fibonacci%20Sequence%20with%20Immutability<p>Generating Fibonacci sequence is definitely not a complex problem to talk about. We all did it in our early stage of programming. So I am not going to explain what it is. Let’s talk about the ways we can generate it in our familiar languages.</p>
<p>When we were just a beginner if anyone asked us to generate this sequence up to nth values, we used to give a big smile and wrote the code using a for loop, right? We all know the way, so let’s skip it too. After a while, we learned about recursion and used recursion to generate the sequence like this,</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public static List<Integer> recursiveFibonacci(int position){
if(position == 1)
return Arrays.asList(1);
if(position == 2)
return Arrays.asList(1, 1);
List<Integer> previousSequence = recursiveFibonacci(position - 1);
List<Integer> newSequence = new ArrayList<Integer>(previousSequence);
newSequence.add(previousSequence.get(previousSequence.size() - 1) +
previousSequence.get(previousSequence.size() - 2));
return newSequence;
}
</code></pre></div></div>
<p>Here we are using lists and modifying it right? But what if we want to do the same task without modifying any object that means by maintaining immutability? In my previous post, I showed a demo of Stream in Java. Let's have a look at another demo of it.</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public static List<Integer> immutableFibonacci(int position){
return Stream.iterate(
new int[]{1, 1}, e -> new int[]{e[1], e[0] + e[1]})
.map(e -> e[0])
.limit(position)
.collect(Collectors.toList());
}
</code></pre></div></div>
<p>One liner right? ;) Honestly, I never even thought about doing this with one line of code.</p>
<p>Immutability is a mandatory part of functional programming. Is Java functional? Java 8 supports functional programming basically.<br />How would this code look like in a functional language like Scala?</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>def immutableFibonacci(position: Int) = {
(0 to position-1).foldLeft(List(1,1)) {
(list, i) => list:::List(list.last + list.init.last)
}.take(position)
}
</code></pre></div></div>
<p>But Scala also supports imperative programming, right? So what about a purely functional language like Erlang? Run this code ;)</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>tl(lists:reverse( lists:foldl(
fun(N, ListF) -> [hd(tl(ListF)) + hd(ListF)] ++ ListF end,
[1, 0],
lists:seq(0, position-2))
)).
</code></pre></div></div>
<p> </p>
<p>Thanks for reading this. Here is a bonus code of Python for the same purpose.</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>reduce(lambda sequence, _: sequence + [sequence[-1] + sequence[-2]],
range(position - 1), [1, 1])[:position]
</code></pre></div></div>Sudipta Karsudipta.kar.8080@gmail.comGenerating Fibonacci sequence is definitely not a complex problem to talk about. We all did it in our early stage of programming. So I am not going to explain what it is. Let’s talk about the ways we can generate it in our familiar languages.Learning from mistakes#1 – memory error in tfidfvectorizer2016-11-25T00:00:00-08:002016-11-25T00:00:00-08:00http://localhost:4000/Learning%20from%20Mistakes#1%20%E2%80%93%20Memory%20Error%20in%20TfidfVectorizer<p>I was trying to extract n-gram features from a large dataset and got a memory error. I was using TfIdfVectorizer of scikit-learn. And the error was from the large sparse matrix. I was not surprised as I was not sure if my 32GB ram is enough for around 1 million documents. However, I solved the error by the following steps.</p>
<ol>
<li>
<p>I changed the min_df parameter to 0.2. Here is the explanation of min_df parameter in scikit-learn.<br />
<em>min_df</em> : float in range [0.0, 1.0] or int, default=1.<br />
When building the vocabulary ignore terms that have a document frequency strictly lower than the given threshold. This value is also called cut-off in the literature. If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None.
So by changing this, I am limiting the size of the vocabulary. That helps to reduce the memory size.</p>
</li>
<li>
<p>I used Regexptokenizer and Porter stemmer to tokenize and stem the words. Stemming reduces the variation of words. So it greatly helps to reduce the vocabulary size.</p>
</li>
</ol>
<p>After doing these 2 little fixes, I got rid of the memory error 🙂 Basic problem was to reduce the vocabulary size. By the way, I was removing the stop words and lower casing from earlier.</p>
<p><em>Learning from Mistakes is a series where I will post the little challenges I face during my experiments and how I fix them. I hope someone in this world is facing the same problem and my goal is to help them.</em></p>Sudipta Karsudipta.kar.8080@gmail.comI was trying to extract n-gram features from a large dataset and got a memory error. I was using TfIdfVectorizer of scikit-learn. And the error was from the large sparse matrix. I was not surprised as I was not sure if my 32GB ram is enough for around 1 million documents. However, I solved the error by the following steps.Add opengl in code blocks2012-09-20T00:00:00-07:002012-09-20T00:00:00-07:00http://localhost:4000/Add%20OpenGL%20in%20Code%20Blocks<p>It is an easy procedure to add OpenGL in CodeBlocks and prepare your Windows PC for OpenGL Coding.
By the way, those who are thinking OpenGL is a kind of Programming Language, you are wrong. OpenGL is a Graphics Library. It will give you scope to design graphical interfaces with a bunch of code.
Lets see, How to add it in CodeBlocks.
I hope you have already installed CodeBlocks in your PC.</p>
<ol>
<li>Download GLUT (OpenGL Utility Tool) from here.</li>
<li>
<p>Unzip the .zip file. There you will find 5 files:
glut32.dll<br />
glut32.lib<br />
glut.h<br />
glut.def<br />
README-win32.txt<br />
Now you have to do some Copy-Paste works 🙂 .</p>
</li>
<li>Go to Windows > system in your System Drive. Usually its C:\Windows\system. Now copy the glut32.dll file there.</li>
<li>Go to C:\Program Files directory. Create a new folder and give it a suitable name. I named it “OpenGL Lib“.</li>
<li>Create 2 folders in C:\Program Files\OpenGL Lib called include & lib.
Copy glut32.lib in the lib folder.
Create a folder called GL in the include folder and copy glut.h in it.
You Are Almost Done 🙂
Now Open CODEBLOCKS. I am giving some Screenshots to help you.</li>
<li>Create a New Project in CodeBlocks. It should be a Glut Project. Then Click Next.
<img src="/assets/post_related/opengl/1.jpg" alt="New Glut project" />
<img src="/assets/post_related/opengl/2.jpg" alt="New Glut project" /></li>
<li>Give a Name to your Project. I named it Test Project.
<img src="/assets/post_related/opengl/3.jpg" alt="New Glut project" /></li>
<li>Select the Location of the Glut from Program Files Directory. Click Next.
<img src="/assets/post_related/opengl/4.jpg" alt="New Glut project" /></li>
<li>Then this screen will appear. Don’t change anything here. Click Finish.
<img src="/assets/post_related/opengl/5.jpg" alt="New Glut project" /></li>
<li>Open Management Bar (Shift + F2). Select Project tab. Open main.cpp. It is a sample code provided there.
<img src="/assets/post_related/opengl/6.jpg" alt="New Glut project" /></li>
<li>Write #include<windows.h> in Line 14. Go to Project> Build Options.
<img src="/assets/post_related/opengl/7.jpg" alt="New Glut project" /></li>
<li>
<p>Go to Linker Settings tab and Click Add.
A Little Window will Appear saying “Keep this as relative path?”. Select No and press OK.
<img src="/assets/post_related/opengl/8.jpg" alt="New Glut project" /></p>
</li>
<li>Build the project and Run.
<img src="/assets/post_related/opengl/9.jpg" alt="New Glut project" /></li>
</ol>
<p>If you have done everything correctly you will see this.</p>
<p>That’s all.</p>Sudipta Karsudipta.kar.8080@gmail.comIt is an easy procedure to add OpenGL in CodeBlocks and prepare your Windows PC for OpenGL Coding. By the way, those who are thinking OpenGL is a kind of Programming Language, you are wrong. OpenGL is a Graphics Library. It will give you scope to design graphical interfaces with a bunch of code. Lets see, How to add it in CodeBlocks. I hope you have already installed CodeBlocks in your PC.