tag:blogger.com,1999:blog-22973654203098994002023-12-29T03:25:43.626-08:00Practical Code SolutionsSolutions and Ideas on Programming, Statistics and NetworkingUnknownnoreply@blogger.comBlogger161125tag:blogger.com,1999:blog-2297365420309899400.post-48453927996472221472016-11-18T05:39:00.002-08:002016-11-18T05:39:51.939-08:00Zubote Multiplayer Android Game is Available Now On Google Play Store!<div style="text-align: justify;">
<span style="background-color: white; color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 14px;">Zubote is a free turn-based strategy game that all of us have played with our friends on paper before.</span></div>
<div style="background-color: white; border: 0px; color: #333333; font-size: 14px; margin-bottom: 16px; padding: 0px; text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">Zubote, which you can play with Single and Multiplayer options, has a grid structure consisting of 24 squares in general. The dots draw lines and the lines form squares. At the end of the game, the person with the most number of squares becomes the winner of the game.</span></div>
<div style="background-color: white; border: 0px; color: #333333; font-size: 14px; margin-bottom: 16px; padding: 0px; text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">If you got the star in the game, you will have a great advantage in winning the game. In addition, surprising points in the question mark, you can give the game, can also cause a great frustration, if you got it. In other words, everyone in Zubote can win the game at any moment.</span></div>
<div style="background-color: white; border: 0px; color: #333333; font-size: 14px; margin-bottom: 16px; padding: 0px; text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">Zubote offers the following features to players:</span></div>
<div style="background-color: white; border: 0px; color: #333333; font-size: 14px; margin-bottom: 16px; padding: 0px;">
</div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">-Single player mode option gives you the possibility to practice against Android,</span></div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">-Multi player mode option allows you to play in real time with someone who is randomly online, whether you are with your own friends. Just play with your friends or other people from all over the world,</span></div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">-For your success, badges allow you to identify your rankings online,</span></div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">-Leaderboards and Achievements integration,</span></div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">-You can add people as friend during the game and can see your friend list,</span></div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">-Modern design,</span></div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">-Share your memories with friends on social platforms via Facebook, Twitter, Whatsapp, Email and SMS,</span></div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">-Free download and easy to play.</span></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
You can download this game from here: <a href="https://play.google.com/store/apps/details?id=gsatman.studios.zubote&hl=en" target="_blank">Zubote Multiplayer</a></div>
<div style="text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.phpservisi.com/wp-content/uploads/2016/11/ss1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.phpservisi.com/wp-content/uploads/2016/11/ss1.png" height="320" width="163" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.phpservisi.com/wp-content/uploads/2016/11/ss2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.phpservisi.com/wp-content/uploads/2016/11/ss2.png" height="320" width="163" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.phpservisi.com/wp-content/uploads/2016/11/ss3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.phpservisi.com/wp-content/uploads/2016/11/ss3.png" height="320" width="163" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.phpservisi.com/wp-content/uploads/2016/11/ss4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.phpservisi.com/wp-content/uploads/2016/11/ss4.png" height="320" width="163" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.phpservisi.com/wp-content/uploads/2016/11/ss5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.phpservisi.com/wp-content/uploads/2016/11/ss5.png" height="320" width="163" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.phpservisi.com/wp-content/uploads/2016/11/ss6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.phpservisi.com/wp-content/uploads/2016/11/ss6.png" height="320" width="163" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.phpservisi.com/wp-content/uploads/2016/11/ss7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.phpservisi.com/wp-content/uploads/2016/11/ss7.png" height="320" width="163" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<div style="background-color: white; border: 0px; color: #333333; font-family: Roboto, UILanguageFont, Arial, sans-serif; font-size: 14px; margin-bottom: 16px; padding: 0px; text-align: justify;">
If you find any problems with Zubote, do not hesitate to contact us for your requests and opinions or to share with us any other comments about Zubote.</div>
<div style="background-color: white; border: 0px; color: #333333; font-family: Roboto, UILanguageFont, Arial, sans-serif; font-size: 14px; margin-bottom: 16px; padding: 0px; text-align: justify;">
Enjoy the game!</div>
async:falsehttp://www.blogger.com/profile/03292613703398491039noreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-78746337375070960042016-05-24T17:45:00.001-07:002016-05-24T17:45:07.354-07:00RCaller 3.0 is released!<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-NmMxqs8pzYs/V0T1GbE8ZDI/AAAAAAAABKw/65sRjO7PF48ogGuW3k0xynDp8dcNkabQQCLcB/s1600/rcaller-3.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="200" src="https://1.bp.blogspot.com/-NmMxqs8pzYs/V0T1GbE8ZDI/AAAAAAAABKw/65sRjO7PF48ogGuW3k0xynDp8dcNkabQQCLcB/s200/rcaller-3.jpg" width="200" /></a></div>
RCaller 3.0 is released with new features.<br />
<br />
Please visit the page<br />
<br />
<b><a href="http://mhsatman.com/rcaller-3-0">http://mhsatman.com/rcaller-3-0</a></b><br />
<br />
for the source code, compiled binaries, other downloads and the blog post.<br />
<br />
Hope you enjoy the project!<br />
<br />
<br />Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-2297365420309899400.post-15001625109192581372015-09-06T13:01:00.000-07:002015-09-06T13:01:51.460-07:00Deleting a page of a pdf file in Ubuntu<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-dUM1TyalPZY/VeyadL7EabI/AAAAAAAABF8/_XB3bhUoH2A/s1600/pdf-icon-1.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="194" src="http://1.bp.blogspot.com/-dUM1TyalPZY/VeyadL7EabI/AAAAAAAABF8/_XB3bhUoH2A/s200/pdf-icon-1.png" width="200" /></a></div>
Suppose you have a pdf file with many pages and you want to delete a single page (or a list of pages) from this file in Ubuntu. You can first install the <b>pdftk</b> package using Ubuntu's package manager or by simply typing<br />
<br />
<b><span style="font-family: "Courier New",Courier,monospace;">sudo apt-get install pdftk</span></b><br />
<br />
in the command line.<br />
<br />
Now think that you want 4th page to be excluded from the pdf. You can type<br />
<br />
<b><span style="font-family: "Courier New",Courier,monospace;">pdftk old.pdf cat 1-3 5-end output new.pdf</span></b><br />
<br />
where the original pdf is old.pdf, new.pdf is the new one with 4th page is excluded.<br />
<br />
Hope this helps.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-8929920556515788412015-09-02T07:01:00.001-07:002015-09-02T07:01:25.511-07:00Parallel Numeric Integration using Python Multiprocessing Library<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-6ID38Z4W2o4/Veb4GsZ81JI/AAAAAAAABFk/hcbOJBd5iBQ/s1600/python_bigger.gif" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="200" src="http://2.bp.blogspot.com/-6ID38Z4W2o4/Veb4GsZ81JI/AAAAAAAABFk/hcbOJBd5iBQ/s200/python_bigger.gif" width="200" /></a></div>
Parallel algorithms can divide a problem on several cores. Some
algorithms can easly divide a problem into smaller ones such as matrix calculation and numerical
function integration because divided parts of these operations are
independent each other, that is, integrating a function for <b>a < x < b</b> and <b>b < x < c </b>domains equals to integrating the same function for <b>a < x < c</b>, when the divided numerical integrals are summed up.<br />
<br />
In Python, the class <b>Thread</b> can be extended by a user defined class for multi-threaded applications. However, because of the interpreter lock, a multi-threaded solution can take longer computation times even if the computer has more than one cores on the cpu. The solution is using the <b>multiprocessing</b> library.<br />
<br />
Suppose that we have the standard normal distribution function coded in Python:<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> def normal(x):
return ((1/math.sqrt(2*math.pi)) * math.exp(-0.5 * math.pow(x,2)))
</code></pre>
<br />
<br />
Now we define an integrator function that takes the function, the bounds of the domain and a Queue object for storing the result as parameters:<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> def Integrator(func, a, b, lstresults):
epsilon = 0.00001
mysum = 0.0
i = float(a)
while(i < b):
mysum += func(i) * epsilon
i += epsilon
lstresults.put(mysum)
</code></pre>
<br />
<br />
The function <b>Integrator</b> simply integrates the given function from <b>a</b> to <b>b</b>. This function uses a single core even the computer has more. However, this operation can be divided into several smaller parts. Now we define a <b>MultipleIntegrator</b> class to distribute these operation into <b>n</b> parts, where <b>n</b> is a user defined integer, optionally equals to number of threads.<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> class MultipleIntegrator:
def __init__(self):
"Init"
def do(self, func, a, b, cores):
self.cores = cores
integrationrange = (float(b) - float(a)) /float(cores)
self.integrators = [None]*cores
mya = float(a)
allresults = Queue()
result = 0.0
for i in range(0,cores):
self.integrators[i] = Process(target=Integrator, args=(func, mya, mya + integrationrange,allresults,))
self.integrators[i].start()
mya += integrationrange
for myIntegrator in self.integrators:
myIntegrator.join()
while(not allresults.empty()):
result += allresults.get()
return(result)
</code></pre>
<br />
<br />
When we instantiate this class and call the <b>do</b> function on a user defined function, the integration process of <b>func</b> will be shared on several cores.<br />
Lets integrate the <b>normal</b> function from -4 to 4 using 2 threads:<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> m = MultipleIntegrator()
print(m.do(normal, -10, 10,2))
</code></pre>
<br />
<br />
This result and the computation time is:<br />
<br />
<b><span style="font-family: "Courier New",Courier,monospace;">1.0000039893377686<br /><br />real 0m1.762s<br />user 0m3.384s<br />sys 0m0.024s</span></b><br />
<br />
<br />
If we increase the number of threads to 4:<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> m = MultipleIntegrator()
print(m.do(normal, -10, 10,4))
</code></pre>
<br />
<br />
The computation time is reported as:<br />
<br />
<br />
<br />
<b><span style="font-family: "Courier New",Courier,monospace;">1.0000039894435513<br /><br />real 0m1.364s<br />user 0m5.056s<br />sys 0m0.028s</span></b><br />
<br />
<br />
<br />
which is reduced by the multi-processing. The whole example is given below:<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> from multiprocessing import Process,Queue
import math
def normal(x):
return ((1/math.sqrt(2*math.pi)) * math.exp(-0.5 * math.pow(x,2)))
def Integrator(func, a, b, lstresults):
epsilon = 0.00001
mysum = 0.0
i = float(a)
while(i < b):
mysum += func(i) * epsilon
i += epsilon
lstresults.put(mysum)
class MultipleIntegrator:
def __init__(self):
"Init"
def do(self, func, a, b, cores):
self.cores = cores
integrationrange = (float(b) - float(a)) /float(cores)
self.integrators = [None]*cores
mya = float(a)
allresults = Queue()
result = 0.0
for i in range(0,cores):
self.integrators[i] = Process(target=Integrator, args=(func, mya, mya + integrationrange,allresults,))
self.integrators[i].start()
mya += integrationrange
for myIntegrator in self.integrators:
myIntegrator.join()
while(not allresults.empty()):
result += allresults.get()
return(result)
m = MultipleIntegrator()
print(m.do(normal, -10, 10,4))
</code></pre>
<br />
<br />
<br />
Note that, the standard normal function is a probability density function and can not be integrated to a number bigger than 1. All the results include many floating-point rounding errors. Many corrections can be added to Integrator for rounding issues. <br />
<br />
Hope you get fun!<br />
<br />
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-28172334189177210532015-08-30T14:30:00.001-07:002015-08-30T14:30:16.325-07:00Overloading Constructor in Python Classes<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-ZA67YzykZqA/VeNvgOmIldI/AAAAAAAABFU/TxN1_wGEmJ0/s1600/python_bigger.gif" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="200" src="http://3.bp.blogspot.com/-ZA67YzykZqA/VeNvgOmIldI/AAAAAAAABFU/TxN1_wGEmJ0/s200/python_bigger.gif" width="200" /></a></div>
In Python, the constructor __init__ can not be overloaded. If you are a Java or C++ programmer, you probably used this facility before because even the standard libraries of these languages use function overloading.<br />
<br />
Altough the language does not support constructor overloading, we can follow the factory design pattern for using multiple constructors in Python. In factory design pattern, we define static class members that create new instances with given parameters. In Python, if a function labeled with a <b>@classmethod</b>, then this function belongs to the class rather than an object. This is the same as the static methods in Java.<br />
<br />
Now think that we want to write a Python class with three constructors. In first constructor we want to set param1 to a specific value and others to zero. In second constructor we want to set param2 to a specific value and others to zero and goes on.<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> class Clazz:
def __init__(self):
"please use create1, create2 or create3"
@classmethod
def create1(cls, param1):
c = Clazz()
c.param1 = param1
c.param2 = 0
c.param3 = 0
return c
@classmethod
def create2(cls, param2):
c = Clazz()
c.param1 = 0
c.param2 = param2
c.param3 = 0
return c
@classmethod
def create3(cls,param3):
c = Clazz()
c.param1 = 0
c.param2 = 0
c.param3 = param3
return c
def dump(self):
print("Param1 = %d, Param2 = %d, Param3 = %d" %
(self.param1,self.param2,self.param3))
</code></pre>
<br />
<br />
<br />
Of course we don't need to set other parameters to zero in all static methods. Lets get the code more compact:<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> class Clazz:
param1 = 0
param2 = 0
param3 = 0
def __init__(self):
"please use create1, create2 or create3"
@classmethod
def create1(cls, param1):
c = Clazz()
c.param1 = param1
return c
@classmethod
def create2(cls, param2):
c = Clazz()
c.param2 = param2
return c
@classmethod
def create3(cls,param3):
c = Clazz()
c.param3 = param3
return c
def dump(self):
print("Param1 = %d, Param2 = %d, Param3 = %d" %
(self.param1,self.param2,self.param3))
</code></pre>
<br />
<br />
<br />
And now, we can instantiate some objects from this class using different factory methods:<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> myc1 = Clazz.create1(5)
myc1.dump()
myc2 = Clazz.create2(10)
myc2.dump()
myc3 = Clazz.create3(50)
myc3.dump()
myc4 = Clazz()
myc4.dump()
</code></pre>
<br />
<br />
myc1 calls the first static factory method. As it is expected, only the value of the first parameter is changed. Following methods sets the other parameters only. The output is<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> Param1 = 5, Param2 = 0, Param3 = 0
Param1 = 0, Param2 = 10, Param3 = 0
Param1 = 0, Param2 = 0, Param3 = 50
Param1 = 0, Param2 = 0, Param3 = 0
</code></pre>
<br />
<br />
As we seen at the last line of the output, the object myc4 is created using the __init__constructor and all the parameters have value of zero.<br />
<br />
Hope you get fun!<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-65118298877831491652015-08-25T12:58:00.003-07:002015-08-25T13:55:26.820-07:00Lisp for the C++ programmer: Numerical Integration<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFI/ZFbqcMjpnzM/s1600/LISP_logo_big.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="138" src="http://1.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFI/ZFbqcMjpnzM/s200/LISP_logo_big.png" width="200" /></a></div>
In the series of <b>Lisp for the C++ programmer</b>, we present some Common Lisp examples in a way similar to examples which are written in C++ or some other languages belong the same family tree of C++.<br />
<br />
Here is the example of Riemann sum in Common Lisp. We first define a function <b>f</b> which takes a single parameter <b>x</b> and the function <b><i>y = f(x) = x</i></b>, for simplicity. We will change this function later. It is known that integration of this function from <b>0</b> to <b>1</b> is <b>0.50</b>.<br />
<br />
Common Lisp code for numerical integration (approximatly result) is:<br />
<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> (defun f (x)
x
)
(defun integrate (f start stop)
(setq epsilon 0.0001)
(setq sum 0.0)
(loop for i from start to stop by epsilon do
(setq sum (+ sum (* (funcall f i) epsilon)))
)
sum
)
(print (integrate 'f 0 1))
</code></pre>
<br />
The result is <b>0.4999532</b>. Now we can use a more complex function, for example a normal distribution function with zero mean and unit variance. This function can be defined in Common Lisp as<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> (defun normal (x)
(setq a (* (/ 1 (sqrt (* 2 3.141592))) (exp (* -0.5 (* x x)))))
a
)
(defun integrate (f start stop)
(setq epsilon 0.0001)
(setq sum 0.0)
(loop for i from start to stop by epsilon do
(setq sum (+ sum (* (funcall f i) epsilon)))
)
sum
)
(print (integrate 'normal -1 1))
</code></pre>
<br />
<br />
<br />
In the code above, as it can clearly be seen, we integrate the standard normal distribution from -1 to 1 and the result is <b>0.6826645</b>.
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-50834986679232110372015-08-25T11:50:00.000-07:002015-08-25T12:08:08.793-07:00Lisp for the C++ programmer: Changing an element of a list<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFI/ZFbqcMjpnzM/s1600/LISP_logo_big.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="138" src="http://1.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFI/ZFbqcMjpnzM/s200/LISP_logo_big.png" width="200" /></a></div>
Here is the example of changing an element of a Common Lisp List and its equivalent code in C++ typed as List comment.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> ; #include <cstdlib>
; #include <cstdio>
;
; int main(){
; double *d = (double*) malloc (sizeof(double) * 3);
; d[0] = 1;
; d[1] = 2;
; d[2] = 4;
;
; puts("Current List:");
; printf("%f %f %f\n", d[0], d[1], d[2]);
;
; puts("New List:");
; d[0] = 100;
; printf("%f %f %f\n", d[0], d[1], d[2]);
; return(0);
;}
(setq aList (list 1 2 3))
(print "Current List:")
(print aList)
(setf (elt aList 0) 100)
(print "New List:")
(print aList)
</code></pre>
Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-2297365420309899400.post-36847694712439168022015-08-25T11:22:00.000-07:002015-08-25T11:50:35.332-07:00Lisp for the C++ programmer: for loop<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFI/ZFbqcMjpnzM/s1600/LISP_logo_big.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="138" src="http://1.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFI/ZFbqcMjpnzM/s200/LISP_logo_big.png" width="200" /></a></div>
Here is the example of <b>for</b> loop. Equivalent C++ code is commented on the top the Common Lisp code as comments.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> ; for (int i = 0; i <= 10; i++){
; printf("%f\n", i);
; printf("%f\n", i * 2);
; }
(loop for i from 0 to 10
do
(progn
(print i)
(print (* i 2))
)
)
</code></pre>
Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-2297365420309899400.post-11908402551846359062015-08-25T11:11:00.000-07:002015-08-25T11:59:23.080-07:00Lisp for the C++ programmer: Function definitions and function calls<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFI/ZFbqcMjpnzM/s1600/LISP_logo_big.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="138" src="http://1.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFI/ZFbqcMjpnzM/s200/LISP_logo_big.png" width="200" /></a></div>
Here is the simple Common Lisp example, in which a sum and an ArithmaticMean functions defined. Both C++ and Common Lisp code here calculate the arithmetic mean of 1,2,3,4,5 and 6 which is 3.5. C++ code is commented as in the Common Lisp file.<br />
<br />
<br />
<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> ; double sum (double *d, int len){
; double mysum = 0.0;
; for (int i = 0; i < len; i++){
; mysum += d[i];
; }
; return(mysum);
; }
;
; double ArithmeticMean (double *d, int len){
; return ( sum(d) / len );
; }
; int main(){
; double *mylist = (double*) malloc(sizeof(double) * 6);
; for (int i = 0; i < 6; i++){
; d[i] = (double)i;
; }
; printf("%f\n", ArithmeticMean(d, 6);
; return(0);
; }
(defun sum (aList)
(setq mysum 0.0)
(dotimes (i (length aList))
(setq mysum (+ mysum (nth i aList )))
)
mysum
)
(defun ArithmeticMean (aList)
(/ (sum aList) (length aList))
)
(print (ArithmeticMean '(1 2 3 4 5 6)))
</code></pre>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-27473006491065778642015-08-25T10:50:00.000-07:002015-08-25T11:51:32.167-07:00Lisp for the C++ programmer: cond expression<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFE/F9tNMVwOI5g/s1600/LISP_logo_big.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="138" src="http://4.bp.blogspot.com/-POzJxPHTXu4/Vdypb2yH3HI/AAAAAAAABFE/F9tNMVwOI5g/s200/LISP_logo_big.png" width="200" /></a></div>
Here is the example for the <b>cond</b> expression of Common Lisp and its C++ equivalent.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> ; int x = 10;
; if (x < 10) {
; puts("x is smaller than 10");
; } else if (x > 10){
; puts("x is bigger than 10");
; } else if (x == 10){
; puts("x equals to 10"));
; }
(setq x 10)
(cond
((< x 10) (print "x is smaller than 10"))
((> x 10) (print "x is bigger than 10"))
((= x 10) (print "x equals to 10"))
)
</code></pre>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-47304073542128840472015-03-25T05:26:00.001-07:002015-03-25T05:51:32.269-07:00Singleton Design Pattern in PHP Example<div style="text-align: justify;">
<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="http://4.bp.blogspot.com/-UVNed2wzu1E/VRKoZk7GatI/AAAAAAAAAhY/EZNYuxXDCXA/s1600/singleton.png" imageanchor="1" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" src="http://4.bp.blogspot.com/-UVNed2wzu1E/VRKoZk7GatI/AAAAAAAAAhY/EZNYuxXDCXA/s400/singleton.png" height="132" width="200" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Singleton Pattern</td></tr>
</tbody></table>
Hello! As we all know that, design patterns used to solve any issue in software engineering. All of design patterns have typical skills. For example, in the singleton pattern a class can distribute one instance of itself to other classes. In this article I'll show you how to design a PDO database class using singleton design pattern in PHP programming language.</div>
<br />
<br />
<br />
<br />
<br />
<pre class="brush: java">//singleton design pattern
class DB {
private static $status = NULL;
private static $info = 'mysql:host=localhost;dbname=dbname';
private static $username = 'username';
private static $password = 'password';
private function __construct() { }
public static function connect() {
if(!self::$status) {
try {
self::$status = new PDO(self::$info, self::$username, self::$password);
self::$status->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$status->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (PDOException $e) {
throw new Exception($e->GetMessage());
}
}
return self::$status;
}
public function __destruct() {
self::$status = NULL;
}
}
</pre>
<br />
<div style="text-align: justify;">
First, We define status as a static variable, because we want to control whether connection is exists via only one variable. Aftet that define database information as private static variables. After all we create our connection static function, that is connect().
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Here is the way is actually, if status variable is not null, code try to connect PDO database, otherwise connection is exists and will return true,NOT null.
<br />
Finally, in destruct function, connection has been closed by null value. When we want to connect database, we call the singleton like:<br />
<pre class="brush: java">DB::connect();
</pre>
If class has any other function, then like:<br />
<pre class="brush: java">DB::connect()->functionName($param);
</pre>
<br />
See you!
</div>
async:falsehttp://www.blogger.com/profile/03292613703398491039noreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-47602331667888904632015-03-22T02:33:00.001-07:002015-03-22T02:34:15.631-07:00Introduction to Fuzuli : JFuzuli REPL<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-wwlLZMWQ5Zs/VQ6HOpoNSZI/AAAAAAAABCA/hiEVUbUeG_A/s1600/fuzulilogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-wwlLZMWQ5Zs/VQ6HOpoNSZI/AAAAAAAABCA/hiEVUbUeG_A/s1600/fuzulilogo.png" height="142" width="200" /></a></div>
JFuzuli is the JVM implementation of our programming language Fuzuli which is based on LISP syntax and Algol family programming logic. Fuzuli is a modern collaboration of these two separate family of languages.<br />
<br />
Let's try JFuzuli:<br />
<br />
<br />
<br />
<br />
<span style="color: red; font-size: large;"><b>1. Download the Jar </b></span><br />
<br />
The current compiled jar of JFuzuli interpreter is release candidate 1.0. You can download it using the link <a href="https://github.com/jbytecode/fuzuli/releases/tag/v1.0_release_candidate" target="_blank">https://github.com/jbytecode/fuzuli/releases/tag/v1.0_release_candidate</a>. You can always find the newest releases in site <a href="https://github.com/jbytecode/fuzuli/releases/" target="_blank">JFuzuli Releases</a>.<br />
<br />
<span style="color: red; font-size: large;"><b>2. Open the Command Prompt</b></span><br />
<br />
After downloading the jar file, open your operation system's command prompt and locate the jar file by using <b>cd</b> (change directory) command.<br />
<br />
<b><span style="color: red; font-size: large;">3. Start trying it!</span></b><br />
<br />
In command prompt, type<br />
<b><i><span style="font-family: Courier New, Courier, monospace;"><br /></span></i></b>
<b><i><span style="font-family: Courier New, Courier, monospace;">java -jar JFuzuli.jar</span></i></b><br />
<br />
to start. You will see the options:<br />
<br />
Usage:<br />
java -jar JFuzuli.jar fzlfile<br />
java -jar JFuzuli.jar --repl<br />
java -jar JFuzuli.jar --editor<br />
<div>
<br /></div>
<div>
<br /></div>
<div>
You can specify a fuzuli source file to run. The option <b><i>--repl</i></b> opens a command shell. The last option <b><i>--editor</i></b> opens the GUI. Let's try the command shell. </div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace;"><b>java -jar JFuzuli.jar --repl</b></span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"><b>F: </b></span></div>
</div>
<div>
<br /></div>
The prompt F: waits for a convenient Fuzuli expression. Now we can try some basic commands:<br />
<br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">F: (+ 2 7)</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">9.0</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">F: (- 7 10)</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">-3.0</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">F: (require "lang.nfl")</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">0.0</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">F: (let mylist '(1 2 3))</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">[1.0, 2.0, 3.0]</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">F: (first mylist)</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">1.0</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">F: (last mylist)</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">3.0</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">F: (length mylist)</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">3</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">F: (nth mylist 0)</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">1.0</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">F: (nth mylist 1)</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace; font-size: x-small;">2.0</span><br />
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-q9lVOv0iwzg/VQ6MPkB45aI/AAAAAAAABCQ/M7qtOgB1aE4/s1600/JFuzuli_REPL.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-q9lVOv0iwzg/VQ6MPkB45aI/AAAAAAAABCQ/M7qtOgB1aE4/s1600/JFuzuli_REPL.png" height="406" width="640" /></a></div>
<div>
<br /></div>
<div>
Well, we introduce some basic operators, data types and commands here but not all of them. We always put an operator or command after an opening parenthesis, arguments follow this operator and a closing parenthesis takes place. This is the well-known syntax of LISP and Scheme. So what is the language properties, what are the commands, how to try more Fuzuli codes in JFuzuli??</div>
<div>
<br /></div>
<div>
Blog posts on JFuzuli : <a href="http://stdioe.blogspot.com.tr/search/label/jfuzuli">http://stdioe.blogspot.com.tr/search/label/jfuzuli</a></div>
<div>
Blog posts on Fuzuli: <a href="http://stdioe.blogspot.com.tr/search/label/fuzuli">http://stdioe.blogspot.com.tr/search/label/fuzuli</a> </div>
<div>
Our online interpreter is : <a href="http://fuzuliproject.org/index.php?node=tryonline">http://fuzuliproject.org/index.php?node=tryonline</a></div>
<div>
Fuzuli Language home page: <a href="http://fuzuliproject.org/" target="_blank">http://fuzuliproject.org/</a></div>
<div>
</div>
Have a nice read!<br />
<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-59088550084619402252015-03-19T13:38:00.000-07:002015-03-19T13:52:02.381-07:00Why is R awesome?<div class="separator" style="clear: both; text-align: center;">
<a href="http://dailydropcap.com/images/R-5.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://dailydropcap.com/images/R-5.jpg" height="200" width="191" /></a></div>
For someone it is a magic, somebody hates its notation (maybe you!), it has some weird rules and maybe it is just a programming language like others (That is also my opinion). As the other programming languages, R has its good and bad properties but I can say it is the best candidate as a <b>toolbox</b> of a statistician or researchers who work on data analysis.<br />
<br />
In this blog post, I collect <b>8</b> (from 0 to 7) nice properties of R. As a lecturer and researcher, I experienced that many students are more capable to understand some statistical concepts when I try to show and get them work using Monte Carlo simulations. In R, we are able to write compact codes to demonstrate these concepts which would be difficult to implement in an other programming language. R is not a simple toy, so we are always capable to enhance our knowledge, programming skills and get capabilities of writing better codes by introducing external codes that are written in real programming languages (an old joke of real man which uses C). <br />
<br />
<br />
So, if it is, why is R awesome ?<br />
<br />
<br />
<br />
<span style="color: red; font-size: large;">0. Syntax of Algol Family</span><br />
<br />
R has a weird assign operator but the remaining part is similar to Algol family languages such as C, C++, Java and C#. R has a similar facility of operator overloading (yes, it is not exactly the operator overloading), in other terms, single or compound character of symbols can be assigned to function names like this:<br />
<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: 'Ubuntu Mono'; font-size: 11.4pt !important; line-height: 1.2; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">'%_%' <- function(a,b){
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;"> return(exp(a+b))
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">}
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">5 %_% 2
</span>[1] 1096.633</pre>
<br />
<br />
<span style="color: red; font-size: large;">1. Vectors are primitive data types</span><br />
<br />
Yes, vectors are also primitives with an opening and a closing bracket in other members of Algol. In C/C++ they are arrays of primitives and objects in Java. Contrary this, binary operators are directly applicable on the vectors and matrices in R. For example estimation of least squares coefficients is a single line expression in R as:<br />
<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: 'Ubuntu Mono'; font-size: 11.4pt !important; line-height: 1.2; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="Apple-style-span" style="background-color: #e1e2e5; border-collapse: separate; border-spacing: 0px; font-size: 15px; line-height: 18px;"><pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; border: none; font-family: 'Ubuntu Mono'; font-size: 11.4pt !important; line-height: 1.2; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("x",cbind(1,1:30))
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("y",3+3*x[,2]+rnorm(30))
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">solve(t(x) %*% x) %*% t(x) %*% y
</span> [,1]
[1,] 2.858916
[2,] 3.003787</pre>
</span></pre>
<br />
This example shows the differences between a scaler and a vector:<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #333333;">></span> assign(<span style="background-color: #e0e0ff;">"x"</span>, c(<span style="color: #6600ee; font-weight: bold;">1</span>,<span style="color: #6600ee; font-weight: bold;">2</span>,<span style="color: #6600ee; font-weight: bold;">3</span>))
<span style="color: #333333;">></span> assign(<span style="background-color: #e0e0ff;">"a"</span>, <span style="color: #6600ee; font-weight: bold;">5</span>)
<span style="color: #333333;">></span> typeof(x)
[<span style="color: #6600ee; font-weight: bold;">1</span>] <span style="background-color: #e0e0ff;">"double"</span>
<span style="color: #333333;">></span> typeof(a)
[<span style="color: #6600ee; font-weight: bold;">1</span>] <span style="background-color: #e0e0ff;">"double"</span>
<span style="color: #333333;">></span> class(x)
[<span style="color: #6600ee; font-weight: bold;">1</span>] <span style="background-color: #e0e0ff;">"numeric"</span>
<span style="color: #333333;">></span> class(a)
[<span style="color: #6600ee; font-weight: bold;">1</span>] <span style="background-color: #e0e0ff;">"numeric"</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
No difference!
<br />
<br />
<br />
<span style="color: red; font-size: large;">2. Theorems get alive in minutes</span><br />
<br />
Suppose that X is a random variable that follows an <a href="http://en.wikipedia.org/wiki/Exponential_distribution" target="_blank">Exponential Distribution</a> with ratio = 5.<br />
Sum or mean of randomly selected samples with size of N follows a normal distribution. This is an explanation of the <a href="http://en.wikipedia.org/wiki/Central_limit_theorem" target="_blank">Central Limit Theorem</a> with an example. Theorems are theorems. But you may see a fast demonstration (and probably a proof for educational purposes only) and try to write a rapid application. A process of writing a code like this takes minutes if you use R.<br />
<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: 'Ubuntu Mono'; font-size: 11.4pt !important; line-height: 1.2; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("nsamp", 5000)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("n", 100)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("theta", 5.0)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("sums", rep(0,nsamp))
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">for (i in 1:nsamp){
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;"> sums[i] <- sum(rexp(n = n, rate = theta))
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">}
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">hist(sums)</span></pre>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-j21xdIpqse8/VQq4R3h1DII/AAAAAAAABA4/WlMB4lMzDRY/s1600/central_limit_theorem.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-j21xdIpqse8/VQq4R3h1DII/AAAAAAAABA4/WlMB4lMzDRY/s1600/central_limit_theorem.png" height="400" width="387" /></a></div>
<br />
<br />
<br />
<span style="color: red; font-size: large;">3. There is always a second plan for faster code</span><br />
<br />
Now suppose that we are drawing <i>50,000</i> samples randomly using the code above. What would be the computation time?<br />
<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: 'Ubuntu Mono'; font-size: 11.4pt !important; line-height: 1.2; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("nsamp", 50000)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("n", 100)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("theta", 5.0)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">assign("sums", rep(0,nsamp))
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">s <- system.time(
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;"> for (i in 1:nsamp){
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;"> sums[i] <- sum(rexp(n = n, rate = theta))
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;"> }
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">print(s)
</span> user system elapsed
0.582 0.000 0.572 </pre>
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: 'Ubuntu Mono'; font-size: 11.4pt !important; line-height: 1.2; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"></pre>
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: 'Ubuntu Mono'; font-size: 11.4pt !important; line-height: 1.2; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"></pre>
<br />
<br />
Drawing 50,000 samples with size 100 takes 0.582 seconds. Is it now fast enough? Lets try to write it in C++ !<br />
<br />
<br />
<pre><span class="pre" style="color: #000099;">#include <Rcpp.h>
</span><span class="keyword" style="color: #990000;">using namespace</span> Rcpp<span class="operator" style="color: #663300; font-weight: bold;">;</span><span class="comment" style="color: #999999; font-style: italic;">
// [[Rcpp::export]]
</span>NumericVector CalculateRandomSums<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">int</span> m<span class="operator" style="color: #663300; font-weight: bold;">,</span><span class="type" style="color: #ff6633;"> int</span> n<span class="operator" style="color: #663300; font-weight: bold;">) {</span>
NumericVector result<span class="operator" style="color: #663300; font-weight: bold;">(</span>m<span class="operator" style="color: #663300; font-weight: bold;">);</span><span class="type" style="color: #ff6633;">
int</span> i<span class="operator" style="color: #663300; font-weight: bold;">;</span><span class="flow" style="color: red;">
for</span><span class="operator" style="color: #663300; font-weight: bold;"> (</span>i<span class="operator" style="color: #663300; font-weight: bold;"> =</span><span class="int" style="color: #999900;"> 0</span><span class="operator" style="color: #663300; font-weight: bold;">;</span> i<span class="operator" style="color: #663300; font-weight: bold;"> <</span> m<span class="operator" style="color: #663300; font-weight: bold;">;</span> i<span class="operator" style="color: #663300; font-weight: bold;">++){</span>
result<span class="operator" style="color: #663300; font-weight: bold;">[</span>i<span class="operator" style="color: #663300; font-weight: bold;">] =</span> sum<span class="operator" style="color: #663300; font-weight: bold;">(</span>rexp<span class="operator" style="color: #663300; font-weight: bold;">(</span>n<span class="operator" style="color: #663300; font-weight: bold;">,</span><span class="float" style="color: #996600;"> 5.0</span><span class="operator" style="color: #663300; font-weight: bold;">));
}</span><span class="flow" style="color: red;">
return</span><span class="operator" style="color: #663300; font-weight: bold;">(</span>result<span class="operator" style="color: #663300; font-weight: bold;">);
}</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
After compiling the code within Rcpp, we can call the function <i>CalculateRandomSums() </i>from R.<br />
<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: 'Ubuntu Mono'; font-size: 11.4pt !important; line-height: 1.2; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">s <- system.time(
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">vect <- calculaterandomsums(50000,100<span class="GEWYW5YBMDB ace_keyword" style="color: blue;">)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: blue; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: blue;">print(s)
</span> user system elapsed
0.185 0.000 0.184 <!-----><!-----><!-----><!-----><!-----><!-----><!-----></span></pre>
<br />
Now our R code is <b>3.145946</b> times slower than the code written in C++. <br />
<br />
<br />
<span style="color: red; font-size: large;">4. Interaction with C/C++/Fortran is enjoyable</span><br />
<br />
Since a huge amount of R is written in C, migration of old C libraries is easy by writing wrapper methods using SEXP data types. <a href="http://www.rcpp.org/" target="_blank">Rcpp</a> masks these routines in a clever way. Fortran code is also<br />
linkable. Interaction with other languages makes use of old libraries in R and enables the possibility of writing faster new libraries. It is also possible to create instances of R in C and C++ applications.<br />
For an enjoyable example, have a look at the section 3. There is always a second plan for faster code.<br />
The R package <a href="http://cran.r-project.org/web/packages/eive/index.html" target="_blank">eive</a> includes a small portion of C++ code and it is a compact example of calling C++ functions from within R. Accessing C++ objects from R is also possible thank to Rcpp. <a href="http://stdioe.blogspot.com.tr/2015/03/accessing-c-objects-from-r-using-rcpp.html" target="_blank">Click here to see the explanation and an example</a>.<br />
<br />
<br />
<span style="color: red; font-size: large;">5. Interaction with Java</span><br />
<br />
Calling Java from R (<a href="https://www.rforge.net/rJava/" target="_blank">rJava</a>) and calling R from Java (<a href="https://www.rforge.net/rJava/" target="_blank">JRI</a>, <a href="http://stdioe.blogspot.com.tr/search/label/rcaller" target="_blank">RCaller</a>) are all possible. <a href="http://www.renjin.org/" target="_blank">Renjin</a> has a different concept as it is the R interpreter written in Java (Another possibility of calling R from Java , huh?). A detailed comparison of these method is given <a href="http://www.sciencedomain.org/abstract.php?iid=550&id=6&aid=4838" target="_blank">in this documentation</a> and <a href="http://stdioe.blogspot.com.tr/2011/07/rcaller-20-calling-r-from-java.html" target="_blank">this</a>.<br />
<br />
<br />
<span style="color: red; font-size: large;">6. Sophisticated variable scoping</span><br />
<br />
In R, functions have their own variable scopes and accessing variables at the top level is possible. Addition to this, variable scoping is handled by standard R lists (specially they are called environments) and in any side of code user based environments can be created. For detailed information visit <a href="http://stdioe.blogspot.com/2015/02/environments-in-r.html" target="_blank">Environment in R</a>.<br />
<br />
<br />
<span style="color: red; font-size: large;">7. Optional Object Oriented Programming (O-OOP) </span><br />
<br />
R functions take values of variables as parameters rather than their addresses. If a vector with size of 10,0000 is passed through a function, R first copies this vector then passes it to the function. After body of the function is performed, the copied parameter is then labeled as free for later garbage collecting. As C/C++ programmers know, passing objects with their addresses rather than their values is a good solution for using less memory and spending less computation time. Reference classes in R are passed to functions with their addresses in a way similar to passing C++ references and Java objects to functions and methods:<br />
<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f3f3; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32</pre>
</td><td><pre style="line-height: 125%; margin: 0;">Person <span style="color: #555555;"><-</span> setRefClass(
Class <span style="color: #555555;">=</span> <span style="color: #cc3300;">"Person"</span>,
fields <span style="color: #555555;">=</span> c(<span style="color: #cc3300;">"name"</span>,<span style="color: #cc3300;">"surname"</span>,<span style="color: #cc3300;">"email"</span>),
methods <span style="color: #555555;">=</span> list(
initialize <span style="color: #555555;">=</span> <span style="color: #006699; font-weight: bold;">function</span>(name, surname, email){
.self<span style="color: #555555;">$</span>name <span style="color: #555555;"><-</span> name
.self<span style="color: #555555;">$</span>surname <span style="color: #555555;"><-</span> surname
.self<span style="color: #555555;">$</span>email <span style="color: #555555;"><-</span> email
},
setName <span style="color: #555555;">=</span> <span style="color: #006699; font-weight: bold;">function</span>(name){
.self<span style="color: #555555;">$</span>name <span style="color: #555555;"><-</span> name
},
setSurname <span style="color: #555555;">=</span> <span style="color: #006699; font-weight: bold;">function</span>(surname){
.self<span style="color: #555555;">$</span>surname <span style="color: #555555;"><-</span> surname
},
setEMail <span style="color: #555555;">=</span> <span style="color: #006699; font-weight: bold;">function</span> (email){
.self<span style="color: #555555;">$</span>email <span style="color: #555555;"><-</span> email
},
toString <span style="color: #555555;">=</span> <span style="color: #006699; font-weight: bold;">function</span> (){
<span style="color: #006699; font-weight: bold;">return</span>(paste(name, <span style="color: #cc3300;">" "</span>, surname, <span style="color: #cc3300;">" "</span>, email))
}
) <span style="color: #0099ff; font-style: italic;"># End of methods</span>
) <span style="color: #0099ff; font-style: italic;"># End of class</span>
p <span style="color: #555555;"><-</span> Person<span style="color: #555555;">$</span>new(<span style="color: #cc3300;">"John"</span>,<span style="color: #cc3300;">"Brown"</span>,<span style="color: #cc3300;">"brown@server.org"</span>)
print(p<span style="color: #555555;">$</span>toString())
</pre>
</td></tr>
</tbody></table>
</div>
<br />
The output is<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0">[1] "John Brown brown@server.org"</pre>
<br />
Java and C++ programmers probably like this notation!<br />
<br />
<br />
Have a nice read!<br />
<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-40105772056622071182015-03-16T11:25:00.001-07:002015-03-20T06:00:06.360-07:00Compact Genetic Algorithms with R<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-2tEHikW5ynk/VQwZwqVDm1I/AAAAAAAABBQ/PS9vZNycDRg/s1600/ga.gif" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-2tEHikW5ynk/VQwZwqVDm1I/AAAAAAAABBQ/PS9vZNycDRg/s1600/ga.gif" /></a></div>
<br />
Compact Genetic Algorithm (CGA) is a member of Genetic Algorithms (GAs) and also Estimation of Distribution Algorithms (EDAs). Since it is based on a single chromosome rather than a population of chromosomes, it is compact.<br />
<br />
For detailed information, research papers [1] and [2] present a complete and a brief documentations, respectively.<br />
<br />
In this blog post, we give an example of use of compact genetic algorithms on ONEMAX function. ONEMAX function takes n-bits as parameters and returns the number of ones as integer. Since it is only one local optimum when all of the bits equal to 1, it is called ONEMAX.<br />
<br />
First of all, we load the R package <b>eive</b> which includes the wrapped C++ function <b>cga</b>.<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">require("eive")</span></pre>
<br />
The other step is to define the ONEMAX function.<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">ONEMAX <- function (x){
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;"> return(-sum(x))
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">}</span></pre>
<br />
Now we write the main part, optimization with cga:<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">result </span><span style="color: #c397d8; font-size: 15.1999998092651px; line-height: 18.2399997711182px;"><</span><span style="color: #c397d8; font-size: 15.1999998092651px; line-height: 18.2399997711182px;">- cga(chsize = 10 , popsize = 100 , evalFunc = ONEMAX)</span></pre>
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">result
</span> [1] 1 1 1 1 1 1 1 1 1 1</pre>
<br />
The result is a vector in which the bits are all equal to 1.<br />
<br />
The most important issue in this example is speed, because the algorithm is implemented in C++ and wrapped using Rcpp to be called within R.<br />
<br />
Here is the example of 1000 bits and the time consumed by the cga function call:<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">system.time(
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;"> result </span><span style="color: #c397d8; font-size: 15.1999998092651px; line-height: 18.2399997711182px;"><</span><span style="color: #c397d8; font-size: 15.1999998092651px; line-height: 18.2399997711182px;">- cga(chsize = 1000,popsize = 100,evalFunc = ONEMAX)</span></pre>
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">)
</span> user system elapsed
0.443 0.000 0.433 <span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">ONEMAX(result)
</span>[1] -994</pre>
<br />
This result seems to be considerably fast and 994 of 1000 bits are found as 1 by the function in 0.433 seconds. Lets increase the population size from 100 to 200:<br />
<br />
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">system.time(
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;"> result </span><span style="color: #c397d8; font-size: 15.1999998092651px; line-height: 18.2399997711182px;"><</span><span style="color: #c397d8; font-size: 15.1999998092651px; line-height: 18.2399997711182px;">- cga(chsize = 1000,popsize = 200,evalFunc = ONEMAX)</span></pre>
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">+ </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">)
</span> user system elapsed
0.891 0.000 0.866
<span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">print (ONEMAX(result))
</span>[1] -1000</pre>
<br />
Now, after setting the population size from 100 to 200, function doubles the time consumed to 0.866 seconds. But this time, 1000 of 1000 bits are 1, and the optimal solution is reached.<br />
<br />
Have a nice read !<br />
<br />
<br />
<br />
<span style="font-family: inherit;">[1] <span style="background-color: white; color: #222222; line-height: 16.1200008392334px;">Harik, Georges R., Fernando G. Lobo, and David E. Goldberg. "The compact genetic algorithm." </span><i style="background-color: white; color: #222222; line-height: 16.1200008392334px;">Evolutionary Computation, IEEE Transactions on</i><span style="background-color: white; color: #222222; line-height: 16.1200008392334px;"> 3.4 (1999): 287-297.</span></span><br />
<br />
<span style="font-family: inherit;">[2] <span style="background-color: white; color: #222222; line-height: 16.1200008392334px;">Satman, M. Hakan, and Erkin Diyarbakirlioglu. "Reducing errors-in-variables bias in linear regression using compact genetic algorithms." </span><i style="background-color: white; color: #222222; line-height: 16.1200008392334px;">Journal of Statistical Computation and Simulation</i><span style="background-color: white; color: #222222; line-height: 16.1200008392334px;"> ahead-of-print (2014): 1-20.</span></span><br />
<span style="font-family: inherit;"><span style="background-color: white; color: #222222; line-height: 16.1200008392334px;"><br /></span></span>
<span style="font-family: inherit;"><span style="background-color: white; color: #222222; line-height: 16.1200008392334px;"><br /></span></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-24790693783108923312015-03-16T10:39:00.000-07:002015-03-16T10:50:11.144-07:00Accessing C++ objects from R using Rcpp<div class="separator" style="clear: both; text-align: center;">
<a href="http://fc02.deviantart.net/fs71/f/2013/194/e/a/c___logo_by_aamir_4u-d6db74t.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://fc02.deviantart.net/fs71/f/2013/194/e/a/c___logo_by_aamir_4u-d6db74t.png" height="200" width="138" /></a></div>
<span style="font-family: inherit;">Rcpp (Seemless R and C++ integration) package for R provides an easy way of combining C++ and R code. Since R is an interpreter, a bulk of code would probably run at least 2 times slower than its counterpart written in C++. Speed is the most concerning issue many times, however, the main purpose of using C++ would be using an old native library with R.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">In this post blog, we give an example of accessing a C++ class from within R using Rcpp. This C++ class is defined with name MyClass and has two private double typed variables. This class also has getter and setter methods for its private fields.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">MyClass is defined as the code shown below:</span><br />
<br />
<pre><span class="pre" style="color: #000099;">
</span></pre>
<pre><span class="pre" style="color: #000099;">#include <Rcpp.h><rcpp .h="">
</rcpp></span><span class="keyword" style="color: #990000;">
using namespace</span> Rcpp<span class="operator" style="color: #663300; font-weight: bold;">;</span><span class="keyword" style="color: #990000;">
using namespace</span> std<span class="operator" style="color: #663300; font-weight: bold;">;</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
<pre></pre>
<pre><span class="keyword" style="color: #990000;">class</span> MyClass<span class="operator" style="color: #663300; font-weight: bold;"> {</span><span class="keyword" style="color: #990000;">
private</span><span class="operator" style="color: #663300; font-weight: bold;">:</span><span class="type" style="color: #ff6633;">
double</span> a<span class="operator" style="color: #663300; font-weight: bold;">,</span>b<span class="operator" style="color: #663300; font-weight: bold;">;</span><span class="keyword" style="color: #990000;">
public</span><span class="operator" style="color: #663300; font-weight: bold;">:</span>
MyClass<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">double</span> a<span class="operator" style="color: #663300; font-weight: bold;">,</span><span class="type" style="color: #ff6633;"> double</span> b<span class="operator" style="color: #663300; font-weight: bold;">);
~</span>MyClass<span class="operator" style="color: #663300; font-weight: bold;">();</span><span class="type" style="color: #ff6633;">
void</span> setA<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">double</span> a<span class="operator" style="color: #663300; font-weight: bold;">);</span><span class="type" style="color: #ff6633;">
void</span> setB<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">double</span> b<span class="operator" style="color: #663300; font-weight: bold;">);</span><span class="type" style="color: #ff6633;">
double</span> getA<span class="operator" style="color: #663300; font-weight: bold;">();</span><span class="type" style="color: #ff6633;">
double</span> getB<span class="operator" style="color: #663300; font-weight: bold;">();
};</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;"><span style="color: black; font-family: 'Times New Roman'; font-weight: normal; white-space: normal;">
</span></span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;"><span style="color: black; font-family: inherit; font-weight: normal; white-space: normal;">MyClass has its private double typed variables a and b, a constructor, a destructor, getter and setter methods for a and b, respectively. The implementation of MyClass is given below:</span></span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;"><span style="color: black; font-family: inherit; font-weight: normal; white-space: normal;">
</span></span></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre>MyClass<span class="operator" style="color: #663300; font-weight: bold;">::</span>MyClass<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">double</span> a<span class="operator" style="color: #663300; font-weight: bold;">,</span><span class="type" style="color: #ff6633;"> double</span> b<span class="operator" style="color: #663300; font-weight: bold;">){</span><span class="keyword" style="color: #990000;">
this</span><span class="operator" style="color: #663300; font-weight: bold;">-></span>a<span class="operator" style="color: #663300; font-weight: bold;"> =</span> a<span class="operator" style="color: #663300; font-weight: bold;">;</span><span class="keyword" style="color: #990000;">
this</span><span class="operator" style="color: #663300; font-weight: bold;">-></span>b<span class="operator" style="color: #663300; font-weight: bold;"> =</span> b<span class="operator" style="color: #663300; font-weight: bold;">;
}</span>
MyClass<span class="operator" style="color: #663300; font-weight: bold;">::~</span>MyClass<span class="operator" style="color: #663300; font-weight: bold;">(){</span>
cout<span class="operator" style="color: #663300; font-weight: bold;"> <<</span><span class="string" style="color: #009900;"> "Destructor called"</span><span class="operator" style="color: #663300; font-weight: bold;"> <<</span> std<span class="operator" style="color: #663300; font-weight: bold;">::</span>endl<span class="operator" style="color: #663300; font-weight: bold;">;
}</span><span class="type" style="color: #ff6633;">
void</span> MyClass<span class="operator" style="color: #663300; font-weight: bold;">::</span>setA<span class="operator" style="color: #663300; font-weight: bold;"> (</span><span class="type" style="color: #ff6633;">double</span> a<span class="operator" style="color: #663300; font-weight: bold;">){</span><span class="keyword" style="color: #990000;">
this</span><span class="operator" style="color: #663300; font-weight: bold;">-></span>a<span class="operator" style="color: #663300; font-weight: bold;"> =</span> a<span class="operator" style="color: #663300; font-weight: bold;">;
}</span><span class="type" style="color: #ff6633;">
void</span> MyClass<span class="operator" style="color: #663300; font-weight: bold;">::</span>setB<span class="operator" style="color: #663300; font-weight: bold;"> (</span><span class="type" style="color: #ff6633;">double</span> b<span class="operator" style="color: #663300; font-weight: bold;">){</span><span class="keyword" style="color: #990000;">
this</span><span class="operator" style="color: #663300; font-weight: bold;">-></span>b<span class="operator" style="color: #663300; font-weight: bold;"> =</span> b<span class="operator" style="color: #663300; font-weight: bold;">;
}</span><span class="type" style="color: #ff6633;">
double</span> MyClass<span class="operator" style="color: #663300; font-weight: bold;">::</span>getA<span class="operator" style="color: #663300; font-weight: bold;">(){</span><span class="flow" style="color: red;">
return</span><span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="keyword" style="color: #990000;">this</span><span class="operator" style="color: #663300; font-weight: bold;">-></span>a<span class="operator" style="color: #663300; font-weight: bold;">);
}</span><span class="type" style="color: #ff6633;">
double</span> MyClass<span class="operator" style="color: #663300; font-weight: bold;">::</span>getB<span class="operator" style="color: #663300; font-weight: bold;">(){</span><span class="flow" style="color: red;">
return</span><span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="keyword" style="color: #990000;">this</span><span class="operator" style="color: #663300; font-weight: bold;">-></span>b<span class="operator" style="color: #663300; font-weight: bold;">);
}</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
<pre></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold; white-space: normal;"><pre style="color: black; font-weight: normal;"><span style="font-family: inherit;">MyClass is defined nearly minimal. Since it is a C++ class it is not directly accessable from R. In this example, we write some wrapper methods to create instances of MyClass and return their addresses in memory to perform later function calls. In other terms, in R side, we register address of C++ objects to access them. </span></pre>
<pre style="color: black; font-weight: normal;"><span style="font-family: Times New Roman;">
</span></pre>
<pre style="color: black; font-weight: normal;"><span style="font-family: Times New Roman;">
</span></pre>
<pre style="color: black; font-weight: normal;"><pre><span class="comment" style="color: #999999; font-style: italic;">// [[Rcpp::export]]
</span><span class="type" style="color: #ff6633;">long</span> class_create<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">double</span> a<span class="operator" style="color: #663300; font-weight: bold;">,</span><span class="type" style="color: #ff6633;"> double</span> b<span class="operator" style="color: #663300; font-weight: bold;">){</span>
MyClass<span class="operator" style="color: #663300; font-weight: bold;"> *</span>m<span class="operator" style="color: #663300; font-weight: bold;"> =</span><span class="keyword" style="color: #990000;"> new</span> MyClass<span class="operator" style="color: #663300; font-weight: bold;">(</span>a<span class="operator" style="color: #663300; font-weight: bold;">,</span>b<span class="operator" style="color: #663300; font-weight: bold;">);</span>
class_print<span class="operator" style="color: #663300; font-weight: bold;">((</span><span class="type" style="color: #ff6633;">long</span><span class="operator" style="color: #663300; font-weight: bold;">)</span> m<span class="operator" style="color: #663300; font-weight: bold;">);</span><span class="flow" style="color: red;">
return</span><span class="operator" style="color: #663300; font-weight: bold;">((</span><span class="type" style="color: #ff6633;">long</span><span class="operator" style="color: #663300; font-weight: bold;">)</span>m<span class="operator" style="color: #663300; font-weight: bold;">);
}</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
<pre><span style="font-family: inherit; white-space: normal;">The method class_create is a C++ method and it has a special comment which will be used by Rcpp before compiling. After compiling process, class_create wrapper R function will be created to call its C++ counterpart. This function create an instance of class_create with given double typed values and returns the address of created object in type long integer. Here is the other wrapper functions:</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
<pre style="color: black; font-weight: normal;"></pre>
<pre style="color: black; font-weight: normal;"><pre><span class="comment" style="color: #999999; font-style: italic;">// [[Rcpp::export]]
</span><span class="type" style="color: #ff6633;">void</span> class_print<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">long</span> addr<span class="operator" style="color: #663300; font-weight: bold;">){</span>
MyClass<span class="operator" style="color: #663300; font-weight: bold;"> *</span>m<span class="operator" style="color: #663300; font-weight: bold;"> = (</span>MyClass<span class="operator" style="color: #663300; font-weight: bold;">*)</span>addr<span class="operator" style="color: #663300; font-weight: bold;">;</span>
cout<span class="operator" style="color: #663300; font-weight: bold;"> <<</span><span class="string" style="color: #009900;"> "a = "</span><span class="operator" style="color: #663300; font-weight: bold;"> <<</span> m<span class="operator" style="color: #663300; font-weight: bold;">-></span>getA<span class="operator" style="color: #663300; font-weight: bold;">() <<</span><span class="string" style="color: #009900;"> " b = "</span><span class="operator" style="color: #663300; font-weight: bold;"> <<</span> m<span class="operator" style="color: #663300; font-weight: bold;">-></span>getB<span class="operator" style="color: #663300; font-weight: bold;">() <<</span><span class="string" style="color: #009900;"> "\n"</span><span class="operator" style="color: #663300; font-weight: bold;">;
}</span><span class="comment" style="color: #999999; font-style: italic;">
// [[Rcpp::export]]
</span><span class="type" style="color: #ff6633;">void</span> class_destroy<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">long</span> addr<span class="operator" style="color: #663300; font-weight: bold;">){</span>
MyClass<span class="operator" style="color: #663300; font-weight: bold;"> *</span>m<span class="operator" style="color: #663300; font-weight: bold;"> = (</span>MyClass<span class="operator" style="color: #663300; font-weight: bold;">*)</span> addr<span class="operator" style="color: #663300; font-weight: bold;">;</span><span class="keyword" style="color: #990000;">
delete</span> m<span class="operator" style="color: #663300; font-weight: bold;">;
}</span><span class="comment" style="color: #999999; font-style: italic;">
// [[Rcpp::export]]
</span><span class="type" style="color: #ff6633;">void</span> class_set_a<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">long</span> addr<span class="operator" style="color: #663300; font-weight: bold;">,</span><span class="type" style="color: #ff6633;"> double</span> a<span class="operator" style="color: #663300; font-weight: bold;">){</span>
MyClass<span class="operator" style="color: #663300; font-weight: bold;"> *</span>m<span class="operator" style="color: #663300; font-weight: bold;"> = (</span>MyClass<span class="operator" style="color: #663300; font-weight: bold;">*)</span> addr<span class="operator" style="color: #663300; font-weight: bold;">;</span>
m<span class="operator" style="color: #663300; font-weight: bold;">-></span>setA<span class="operator" style="color: #663300; font-weight: bold;">(</span>a<span class="operator" style="color: #663300; font-weight: bold;">);
}</span><span class="comment" style="color: #999999; font-style: italic;">
// [[Rcpp::export]]
</span><span class="type" style="color: #ff6633;">void</span> class_set_b<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">long</span> addr<span class="operator" style="color: #663300; font-weight: bold;">,</span><span class="type" style="color: #ff6633;"> double</span> b<span class="operator" style="color: #663300; font-weight: bold;">){</span>
MyClass<span class="operator" style="color: #663300; font-weight: bold;"> *</span>m<span class="operator" style="color: #663300; font-weight: bold;"> = (</span>MyClass<span class="operator" style="color: #663300; font-weight: bold;">*)</span> addr<span class="operator" style="color: #663300; font-weight: bold;">;</span>
m<span class="operator" style="color: #663300; font-weight: bold;">-></span>setB<span class="operator" style="color: #663300; font-weight: bold;">(</span>b<span class="operator" style="color: #663300; font-weight: bold;">);
}</span><span class="comment" style="color: #999999; font-style: italic;">
// [[Rcpp::export]]
</span><span class="type" style="color: #ff6633;">double</span> class_get_a<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">long</span> addr<span class="operator" style="color: #663300; font-weight: bold;">){</span>
MyClass<span class="operator" style="color: #663300; font-weight: bold;"> *</span>m<span class="operator" style="color: #663300; font-weight: bold;"> = (</span>MyClass<span class="operator" style="color: #663300; font-weight: bold;">*)</span> addr<span class="operator" style="color: #663300; font-weight: bold;">;</span><span class="flow" style="color: red;">
return</span><span class="operator" style="color: #663300; font-weight: bold;">(</span>m<span class="operator" style="color: #663300; font-weight: bold;">-></span>getA<span class="operator" style="color: #663300; font-weight: bold;">());
}</span><span class="comment" style="color: #999999; font-style: italic;">
// [[Rcpp::export]]
</span><span class="type" style="color: #ff6633;">double</span> class_get_b<span class="operator" style="color: #663300; font-weight: bold;">(</span><span class="type" style="color: #ff6633;">long</span> addr<span class="operator" style="color: #663300; font-weight: bold;">){</span>
MyClass<span class="operator" style="color: #663300; font-weight: bold;"> *</span>m<span class="operator" style="color: #663300; font-weight: bold;"> = (</span>MyClass<span class="operator" style="color: #663300; font-weight: bold;">*)</span> addr<span class="operator" style="color: #663300; font-weight: bold;">;</span><span class="flow" style="color: red;">
return</span><span class="operator" style="color: #663300; font-weight: bold;">(</span>m<span class="operator" style="color: #663300; font-weight: bold;">-></span>getB<span class="operator" style="color: #663300; font-weight: bold;">());
}</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
<pre><span style="font-family: inherit;">Suppose the whole code is written in a file classcall.cpp. In R side, this code can be compiled and tested as shown below:</span></pre>
</pre>
</span></pre>
</pre>
</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;"><pre style="color: black; font-weight: normal;"><pre><span style="font-family: Times New Roman;">
</span></pre>
<pre><pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">require("Rcpp")
</span><span class="GEWYW5YBAEB ace_constant ace_language" style="color: #e78c45;">Loading required package: Rcpp</span><span class="GEWYW5YBAEB ace_constant ace_language" style="color: #e78c45;">
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">Rcpp::sourceCpp('rprojects/classcall.cpp')</span></pre>
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;"><table cellpadding="0" cellspacing="0" class="GEWYW5YBPDB ace_text-layer ace_line GEWYW5YBCT" style="border: none; color: #dedede; cursor: text; font-size: 11.4pt !important; font-stretch: inherit !important; outline: none; padding-bottom: 8px; padding-left: 6px; white-space: pre-wrap !important; width: 914px; word-wrap: break-word;"><tbody>
<tr><td align="left" style="font-size: 11.4pt !important; line-height: 1.2; vertical-align: top;"><pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; border: none; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 11.4pt !important; line-height: 1.2; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">myobj <- class_create(3.14, 7.8)
</span>a = 3.14 b = 7.8<span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">myobj
</span>[1] 104078752
</pre>
</td></tr>
<tr><td align="left" style="font-size: 11.4pt !important; line-height: 1.2; vertical-align: top;"></td></tr>
<tr><td align="left" style="font-size: 11.4pt !important; line-height: 1.2; vertical-align: top;"></td></tr>
</tbody></table>
</span></pre>
</pre>
</pre>
<pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; font-weight: normal; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">class_set_a(myobj,100)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">class_set_b(myobj,500)
</span><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">class_print(myobj)
</span>a = 100 b = 500</pre>
</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;"><pre class="GEWYW5YBFEB" id="rstudio_console_output" style="-webkit-user-select: text; background-color: black; border: none; color: #dedede; font-family: 'Ubuntu Mono', 'Droid Sans Mono', 'DejaVu Sans Mono', monospace; font-size: 15.1999998092651px; font-weight: normal; line-height: 18.2399997711182px; outline: none; white-space: pre-wrap !important; word-break: break-all;" tabindex="0"><span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">class_get_a(myobj)
</span>[1] 100
<span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">class_get_b(myobj)
</span>[1] 500
<span class="GEWYW5YBJEB ace_keyword" style="-webkit-user-select: text; color: #c397d8; white-space: pre;">> </span><span class="GEWYW5YBMDB ace_keyword" style="color: #c397d8;">class_destroy(myobj)
</span>Destructor called</pre>
</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;">
</span></pre>
<pre><span class="operator" style="color: #663300; font-weight: bold;"><pre style="color: black; font-weight: normal;">
</pre>
<pre style="color: black; font-weight: normal;">Have a nice read!</pre>
<pre style="color: black; font-weight: normal;">
</pre>
<pre style="color: black; font-weight: normal;">
</pre>
</span></pre>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-63953997914225754002015-03-14T15:29:00.000-07:002015-03-20T06:05:30.813-07:00SQLite with R - The sqldf package<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-46YeSWnnhs4/VQwbCIEjH_I/AAAAAAAABBk/RduSQ00IgEk/s1600/256px-Sqlite-square-icon.svg.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-46YeSWnnhs4/VQwbCIEjH_I/AAAAAAAABBk/RduSQ00IgEk/s1600/256px-Sqlite-square-icon.svg.png" height="200" width="200" /></a></div>
<br />
R 's data sorting functions <b>sort</b> and <b>order</b>, the data filtering function <b>which</b>, vector accessing operators <b>[]</b>, vector and matrix manipulation functions <b>cbind</b> and <b>rbind</b>, and other functions and keywords make data analysis easy in much situations. SQL (Structered Querying Language) is used for storing, adding, removing, sorting and filtering the data in which saved on a disk permenantly or memory.<br />
<br />
The R package <b>sqldf</b> builds a <b>SQLite</b> database using an R <b>data.frame</b> object. A <b>data.frame</b> is a matrix with richer properties in R. In this blog post, we present a basic introduction of <b>sqldf</b> package and its use in R.<br />
<br />
First of all, the package can be installed by typing:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">> install.packages("dftable")</span><br />
<br />
After installing the package, it can be got ready to use by typing:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">> require("dftable")</span><br />
Loading required package: sqldf<br />
Loading required package: gsubfn<br />
Loading required package: proto<br />
Loading required package: RSQLite<br />
Loading required package: DBI<br />
<div>
<br /></div>
<div>
<br /></div>
<div>
Now lets create two vectors with length of 100:</div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> assign("x", rnorm(100))</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> assign("y", rnorm(100))</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> assign("mydata", as.data.frame(cbind(x,y)))</span></div>
</div>
<div>
<br /></div>
<div>
We can see first 6 rows:</div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> head(mydata)</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> x y</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">1 -1.9357660 0.2784369</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">2 -0.6976428 1.4646022</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">3 0.1913628 0.1578977</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">4 0.3049607 0.6055087</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">5 2.3773249 1.1800434</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">6 0.4641791 1.7143130</span></div>
</div>
<div>
<br /></div>
<div>
Let's perform some <b>SQL</b> statements on this data frame using <b>sqldf</b>: </div>
<div>
<br /></div>
<div>
<span style="color: red; font-size: large;"><b>Averages of x and y</b></span></div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> sqldf("select avg(x), avg(y) from mydata")</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> avg(x) avg(y)</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">1 0.0790934 0.220756</span></div>
</div>
<div>
<br /></div>
<br />
<span style="color: red; font-size: large;"><b>Number of cases</b></span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">> sqldf("select count(x), count(y) from mydata")</span><br />
<span style="font-family: Courier New, Courier, monospace;"> count(x) count(y)</span><br />
<span style="font-family: Courier New, Courier, monospace;">1 100 100</span><br />
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="color: red; font-size: large;"><b>First Three Cases</b></span></div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> sqldf("select x,y from mydata limit 3")</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> x y</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">1 -1.9357660 0.2784369</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">2 -0.6976428 1.4646022</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">3 0.1913628 0.1578977</span></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="color: red; font-size: large;"><b>Minimum and Maximum Values</b></span></div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> sqldf("select min(x),max(x),min(y),max(y) from mydata")</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> min(x) max(x) min(y) max(y)</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">1 -2.155768 2.377325 -1.75477 2.531869</span></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="color: red; font-size: large;"><b>First 3 Cases of Ordered Data</b></span></div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> sqldf("select x,y from mydata order by x limit 3")</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> x y</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">1 -2.155768 0.6614813</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">2 -1.935766 0.2784369</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">3 -1.837502 0.1073177</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> sqldf("select x,y from mydata order by y limit 3")</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> x y</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">1 0.7665811 -1.754770</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">2 0.3373319 -1.736727</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">3 0.6199159 -1.335649</span></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="color: red; font-size: large;"><b>Insert into </b></span></div>
<div>
<br /></div>
<div>
dftable does not alter the data frame. After inserting a new case, a new data.frame is created and returned. In the example below, <b>sqldf</b> takes a vector of two <b>sql</b> statements as parameters and the result is in accessable with the name <b>main.mydata</b> rather than <b>mydata</b>. </div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace;">> tail (sqldf( </span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">+ c(</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">+ "insert into mydata values (6,7)"</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">+ ,</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">+ "select * from main.mydata"</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">+ )</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">+ )</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">+ )</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> x y</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">96 1.58024523 1.3937920</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">97 -1.79352203 0.2105787</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">98 0.02632872 -1.0567890</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">99 -0.60934162 -0.1359667</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">100 1.43393159 -0.9396326</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">101 6.00000000 7.0000000</span></div>
</div>
<div>
<br /></div>
<br />
<span style="color: red; font-size: large;"><b>Delete</b></span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">> sqldf( </span><br />
<span style="font-family: Courier New, Courier, monospace;">+ c(</span><br />
<span style="font-family: Courier New, Courier, monospace;">+ "delete from mydata where x < 0 or y < 0"</span><br />
<span style="font-family: Courier New, Courier, monospace;">+ ,</span><br />
<span style="font-family: Courier New, Courier, monospace;">+ "select * from main.mydata"</span><br />
<span style="font-family: Courier New, Courier, monospace;">+ )</span><br />
<span style="font-family: Courier New, Courier, monospace;">+ )</span><br />
<span style="font-family: Courier New, Courier, monospace;"> x y</span><br />
<span style="font-family: Courier New, Courier, monospace;">1 0.19136277 0.15789771</span><br />
<span style="font-family: Courier New, Courier, monospace;">2 0.30496074 0.60550873</span><br />
<span style="font-family: Courier New, Courier, monospace;">3 2.37732485 1.18004342</span><br />
<span style="font-family: Courier New, Courier, monospace;">4 0.46417906 1.71431305</span><br />
<span style="font-family: Courier New, Courier, monospace;">5 1.16290585 1.17154756</span><br />
<span style="font-family: Courier New, Courier, monospace;">6 0.49335335 0.19904607</span><br />
<span style="font-family: Courier New, Courier, monospace;">7 1.45769371 0.08291387</span><br />
<span style="font-family: Courier New, Courier, monospace;">8 0.78473338 1.07769098</span><br />
<span style="font-family: Courier New, Courier, monospace;">9 0.69043300 1.35040512</span><br />
<span style="font-family: Courier New, Courier, monospace;">10 1.47893118 1.01057351</span><br />
<span style="font-family: Courier New, Courier, monospace;">.....</span><br />
<br />
<br />
Have a nice read!<br />
<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-53158966636486904662015-03-14T13:24:00.002-07:002015-03-14T13:24:55.745-07:00Handling all variables in a workspace in R with RCaller<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.ncausa.org/images/NCA%20website%20icons/howToBrew.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://www.ncausa.org/images/NCA%20website%20icons/howToBrew.png" height="200" width="200" /></a></div>
It is known that the R assigns a value to a variable name by using the Assignment Symbol <b><- </b>which corresponds to assign function.<br />
<br />
RCaller handles results as list objects. Since R environments are list s, they can easily be converted to R lists (<a href="http://stdioe.blogspot.com.tr/2015/02/environments-in-r.html" target="_blank">Visit the previous blog post on R list here</a>).<br />
<br />
Here is an example of RCaller on getting all variables that are created in the run time in R side.<br />
<br />
<br />
<br />
<div style="background: #FFFFFF; color: black; font-family: Courier New, Courier; font-size: 10pt; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<br />
<div style="background: rgb(255, 255, 255); padding: 0px;">
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">package</span></strong> rcallerenvironments;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">import</span></strong> rcaller.RCaller;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">import</span></strong> rcaller.RCode;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">class</span></strong> RCallerEnvironments {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> main(String[] args) {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
RCaller rcaller = <strong><span style="color: #7f0055;">new</span></strong> RCaller();
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
RCode code = <strong><span style="color: #7f0055;">new</span></strong> RCode();
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
rcaller.setRscriptExecutable(<span style="color: #2a00ff;">"/usr/bin/Rscript"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addRCode(<span style="color: #2a00ff;">"a <- 3"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addRCode(<span style="color: #2a00ff;">"b <- 10.45"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addRCode(<span style="color: #2a00ff;">"d <- TRUE"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addRCode(<span style="color: #2a00ff;">"avector <- c(9,6,5,6)"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addRCode(<span style="color: #2a00ff;">"allvars <- as.list(globalenv())"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
rcaller.setRCode(code);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
rcaller.runAndReturnResult(<span style="color: #2a00ff;">"allvars"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
System.out.println(rcaller.getParser().getNames());
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">try</span></strong> {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
System.out.println(rcaller.getParser().getXMLFileAsString());
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
} <strong><span style="color: #7f0055;">catch</span></strong> (Exception e) {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
System.out.println(<span style="color: #2a00ff;">"Error in accessing XML"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
}
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
}
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
}
</div>
<div style="color: black;">
<div style="font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<span style="font-family: inherit;">The output is </span><br />
<div style="font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-J6-Cm6i83No/VQSY200CLDI/AAAAAAAAA_w/g7VZzR4l4TE/s1600/xml-rcaller.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-J6-Cm6i83No/VQSY200CLDI/AAAAAAAAA_w/g7VZzR4l4TE/s1600/xml-rcaller.png" height="124" width="640" /></a></div>
<div style="font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
</div>
</div>
<span style="font-family: inherit;">As it is seen in output, created variables avector, a, b and d are returned to Java side in a single call without any manual translations.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Have a nice read!</span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><br /></span></span>
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><br /></span></span>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-2297365420309899400.post-77788511282501769372015-03-13T17:22:00.000-07:002015-03-13T17:22:20.272-07:00RCaller 2.5 is available for downloading<div class="separator" style="clear: both; text-align: center;">
<a href="http://images.clipartpanda.com/coffee-clipart-yikMXrpeT.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://images.clipartpanda.com/coffee-clipart-yikMXrpeT.png" height="200" width="165" /></a></div>
We are happy to announce that our <b>'easy to use'</b> Java library for <b>calling R from Java</b> is available for downloading by now on. Developers access the compiled jar file in site<br />
<br />
<br />
<a href="https://github.com/jbytecode/rcaller/releases/tag/2.5" target="_blank">https://github.com/jbytecode/rcaller/releases/tag/2.5</a><br />
<br />
<br />
This release does not extend the main functionality of the library but now there are some handy functions for performing some calculations and later development of the library.<br />
<br />
<br />
<br />
<b>What is new</b>:<br />
<br />
* Official document bibtex added to cite RCaller in any projects or papers<br />
<br />
* RealMatrix class is implemented. Matrix operations are performed in more 'java-ish style'<br />
<br />
* RService is implemented for developing wrapper functions<br />
<br />
<br />
<b>Where to start?</b><br />
<br />
* Read the web page on RCaller <a href="http://mhsatman.com/tag/rcaller/">http://mhsatman.com/tag/rcaller/</a><br />
* Read blog entries in <a href="http://stdioe.blogspot.com.tr/search/label/rcaller">http://stdioe.blogspot.com.tr/search/label/rcaller</a><br />
* Have a look at the source tree in <a href="https://github.com/jbytecode/rcaller">https://github.com/jbytecode/rcaller</a><br />
* Download the library in <a href="https://github.com/jbytecode/rcaller/releases/tag/2.5" target="_blank">https://github.com/jbytecode/rcaller/releases/tag/2.5</a><br />
<br />
Have a nice try!<br />
<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-58985691017129003122015-03-13T15:40:00.000-07:002015-03-13T15:40:37.704-07:00Migration of RCaller and Fuzuli Projects to GitHub<div class="separator" style="clear: both; text-align: center;">
<a href="https://fleep.io/blog/wp-content/uploads/2014/07/github_icon.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="200" src="https://fleep.io/blog/wp-content/uploads/2014/07/github_icon.png" width="200" /></a></div>
Since Google announced that they are shutting down the code hosting service 'Google code' in which our two projects RCaller and Fuzuli Programming Language are hosted.<br />
<br />
We migrated our projects into the popular code hosting site GitHub.<br />
<br />
Source code of these projects will no longer be committed in Google code site. Please check the new repositories.<br />
<br />
GitHub pages are listed below:<br />
<br />
<br />
<br />
<br />
<br />
RCaller:<br />
<br />
<a href="https://github.com/jbytecode/rcaller" target="_blank">https://github.com/jbytecode/rcaller</a><br />
<br />
<br />
<br />
<br />
Fuzuli Project:<br />
<br />
<a href="https://github.com/jbytecode/fuzuli" target="_blank">https://github.com/jbytecode/fuzuli</a><br />
<br />
<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-76353288895835133942015-03-12T14:48:00.001-07:002015-03-12T14:48:15.083-07:00Android Device Information Tool ApplicationHello! I would like to share an Android application that can be used as a tool.<br />
<br />
The application provides information about software of your Android device. You can find out:<br />
<br />
-Device name<br />
-Device ID<br />
-Android version<br />
-Device product<br />
-Device manufacturer<br />
-Device brand<br />
-Device version<br />
-Device host<br />
-Device model<br />
-Device user<br />
-Device hardware<br />
-Device radio version<br />
-Device serial number<br />
-Device finger print<br />
<br />
If you need informations given above, you can download it from this <a href="https://play.google.com/store/apps/details?id=gsatman.studios.androiddeviceinfo" target="_blank">URL</a>.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="http://4.bp.blogspot.com/-jClebV1qHCg/VQHRUpIYBDI/AAAAAAAAAg8/fug_lizBk5I/s1600/Screenshot_2015-03-12-19-21-34.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://4.bp.blogspot.com/-jClebV1qHCg/VQHRUpIYBDI/AAAAAAAAAg8/fug_lizBk5I/s1600/Screenshot_2015-03-12-19-21-34.png" height="640" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Android Device Info App!</td></tr>
</tbody></table>
<br />async:falsehttp://www.blogger.com/profile/03292613703398491039noreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-79041229094457557652015-03-09T15:25:00.000-07:002015-03-09T15:25:11.129-07:00Nearest-Neighbor Clustering using RCaller - A library for Calling R from Java<div style="background: rgb(255, 255, 255); padding: 0px;">
<div class="separator" style="clear: both; color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: center;">
<a href="http://www.research-in-germany.de/mediaObject/en/Research-Performing-Organisations/Bild_go-cluster--S.-34----NICHT-IN-SOCIAL-MEDIA-VERWENDEN/original/Bild_go-cluster+(S.+34)+-+NICHT+IN+SOCIAL+MEDIA+VERWENDEN.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://www.research-in-germany.de/mediaObject/en/Research-Performing-Organisations/Bild_go-cluster--S.-34----NICHT-IN-SOCIAL-MEDIA-VERWENDEN/original/Bild_go-cluster+(S.+34)+-+NICHT+IN+SOCIAL+MEDIA+VERWENDEN.jpg" height="133" width="200" /></a></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
RCaller is a software for calling R from Java. A blog post includes the latest version of <a href="http://stdioe.blogspot.com.tr/2014/06/rcaller-24-has-just-been-released.html" target="_blank">downloadable jar and documentation here</a>. The latest news can always be traced using the RCaller label in <a href="http://stdioe.blogspot.com.tr/search/label/rcaller" target="_blank">Practical Code Solutions</a> blog.</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
A blog post on performing a k-means clustering analysis using RCaller is also available at <a href="http://stdioe.blogspot.com.tr/2015/03/k-means-clustering-with-rcaller-library.html" target="_blank">this link</a>.</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
In the code below, two double arrays, x and y, are created in Java side. These variables are then passed to R. In R side, distance matrix <b>d</b> is calculated. The R function <b>hclust</b> performs the main calculations. Finally, calculated heights of clustering tree and a dendrogram plot are returned to Java. The source code, output text and the returned plot are presented here:</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;"><br /></span></strong></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;"><br /></span></strong></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;"><br /></span></strong></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">package</span></strong> kmeansrcaller;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">import</span></strong> java.io.File;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">import</span></strong> rcaller.RCaller;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">import</span></strong> rcaller.RCode;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">class</span></strong> SingleLinkageClustering {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> main(String[] args) {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
RCaller caller = <strong><span style="color: #7f0055;">new</span></strong> RCaller();
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
RCode code = <strong><span style="color: #7f0055;">new</span></strong> RCode();
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
File dendrogram = <strong><span style="color: #7f0055;">null</span></strong>;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">double</span></strong>[] x = <strong><span style="color: #7f0055;">new</span></strong> <strong><span style="color: #7f0055;">double</span></strong>[]{1, 2, 3, 4, 5, 10, 20, 30, 40, 50};
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">double</span></strong>[] y = <strong><span style="color: #7f0055;">new</span></strong> <strong><span style="color: #7f0055;">double</span></strong>[]{2, 4, 6, 8, 10, 20, 40, 60, 80, 100};
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addDoubleArray(<span style="color: #2a00ff;">"x"</span>, x);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addDoubleArray(<span style="color: #2a00ff;">"y"</span>, y);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addRCode(<span style="color: #2a00ff;">"d <- dist(cbind(x,y))"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addRCode(<span style="color: #2a00ff;">"h&<- hclust(d, method=\"</span>single\<span style="color: #2a00ff;">")"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">try</span></strong> {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
dendrogram = code.startPlot();
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.addRCode(<span style="color: #2a00ff;">"plot(h)"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.endPlot();
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
} <strong><span style="color: #7f0055;">catch</span></strong> (Exception e) {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
System.out.println(<span style="color: #2a00ff;">"Plot Error: "</span> + e.toString());
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
}
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
caller.setRCode(code);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
caller.setRscriptExecutable(<span style="color: #2a00ff;">"/usr/bin/Rscript"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
caller.runAndReturnResult(<span style="color: #2a00ff;">"h"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
System.out.println(caller.getParser().getNames());
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">if</span></strong> (dendrogram != <strong><span style="color: #7f0055;">null</span></strong>) {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
code.showPlot(dendrogram);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
}
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">double</span></strong>[] heights = caller.getParser().getAsDoubleArray(<span style="color: #2a00ff;">"height"</span>);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">for</span></strong> (<strong><span style="color: #7f0055;">int</span></strong> i = 0; i < heights.length; i++) {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
System.out.println(<span style="color: #2a00ff;">"Height "</span> + i + <span style="color: #2a00ff;">" = "</span> + heights[i]);
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
}
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
}
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
}
</div>
<div style="border-top-color: rgb(204, 204, 204); border-top-style: dashed; border-top-width: 1px; text-align: right;">
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
The output is </div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>[merge, height, order, method, call, dist_method]</b></span></span></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>Height 0 = 2.23606797749979</b></span></span></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>Height 1 = 2.23606797749979</b></span></span></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>Height 2 = 2.23606797749979</b></span></span></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>Height 3 = 2.23606797749979</b></span></span></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>Height 4 = 11.1803398874989</b></span></span></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>Height 5 = 22.3606797749979</b></span></span></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>Height 6 = 22.3606797749979</b></span></span></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>Height 7 = 22.3606797749979</b></span></span></div>
<div style="text-align: left;">
<span style="font-size: 13.3333330154419px;"><span style="color: blue; font-family: Courier New, Courier, monospace;"><b>Height 8 = 22.3606797749979</b></span></span></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
The screen shot of the plotted graphics is here:</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: center;">
<a href="http://1.bp.blogspot.com/-_-AWTWZXRCo/VP4a2iBDi5I/AAAAAAAAA_U/VnazjQqmtac/s1600/RPlot5891975075680633236.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-_-AWTWZXRCo/VP4a2iBDi5I/AAAAAAAAA_U/VnazjQqmtac/s1600/RPlot5891975075680633236.png" height="640" width="640" /></a></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
Have a nice read!</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt; text-align: left;">
<br /></div>
</div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-60368380989025106282015-03-07T16:31:00.002-08:002015-03-07T16:46:30.295-08:00K-means clustering with RCaller - A library for calling R from Java<div class="separator" style="clear: both; text-align: center;">
<a href="http://admin.csrwire.com/system/press_release_images/15151/normal/1196188322_Java_Juice_How-to.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://admin.csrwire.com/system/press_release_images/15151/normal/1196188322_Java_Juice_How-to.jpg" height="167" width="200" /></a></div>
Here is an example of RCaller, a library for calling R from Java.<br />
<br />
In the code below, we create two variables x and y. K-means clustering function <b>kmeans</b> is applied on the data matrix that consists of x and y. The result is then reported in Java.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div style="background: #FFFFFF; color: black; font-family: Courier New, Courier; font-size: 10pt; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<strong><span style="color: #7f0055;">package</span></strong> kmeansrcaller;
<br />
<br />
<strong><span style="color: #7f0055;">import</span></strong> rcaller.RCaller;
<br />
<strong><span style="color: #7f0055;">import</span></strong> rcaller.RCode;
<br />
<br />
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">class</span></strong> KMeansRCaller {
<br />
<br />
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> main(String[] args) {
<br />
RCaller caller = <strong><span style="color: #7f0055;">new</span></strong> RCaller();
<br />
RCode code = <strong><span style="color: #7f0055;">new</span></strong> RCode();
<br />
<br />
<strong><span style="color: #7f0055;">double</span></strong>[] x = <strong><span style="color: #7f0055;">new</span></strong> <strong><span style="color: #7f0055;">double</span></strong>[]{1, 2, 3, 4, 5, 10, 20, 30, 40, 50};
<br />
<strong><span style="color: #7f0055;">double</span></strong>[] y = <strong><span style="color: #7f0055;">new</span></strong> <strong><span style="color: #7f0055;">double</span></strong>[]{2, 4, 6, 8, 10, 20, 40, 60, 80, 100};
<br />
<br />
code.addDoubleArray(<span style="color: #2a00ff;">"x"</span>, x);
<br />
code.addDoubleArray(<span style="color: #2a00ff;">"y"</span>, y);
<br />
<br />
code.addRCode(<span style="color: #2a00ff;">"result <- kmeans(cbind(x,y), 2)"</span>);
<br />
<br />
caller.setRCode(code);
<br />
<br />
caller.setRscriptExecutable(<span style="color: #2a00ff;">"/usr/bin/Rscript"</span>);
<br />
<br />
caller.runAndReturnResult(<span style="color: #2a00ff;">"result"</span>);
<br />
System.out.println(caller.getParser().getNames());
<br />
<br />
<strong><span style="color: #7f0055;">int</span></strong>[] clusters = caller.getParser().getAsIntArray(<span style="color: #2a00ff;">"cluster"</span>);
<br />
<strong><span style="color: #7f0055;">double</span></strong>[][] centers = caller.getParser().getAsDoubleMatrix(<span style="color: #2a00ff;">"centers"</span>);
<br />
<strong><span style="color: #7f0055;">double</span></strong>[] totalSumOfSquares = caller.getParser().getAsDoubleArray(<span style="color: #2a00ff;">"totss"</span>);
<br />
<span style="color: #3f7f5f;">// RCaller automatically replaces dots with underlines in variable names
</span><br />
<span style="color: #3f7f5f;">// So the parameter tot.withinss is accessible as tot_withinss
</span><br />
<strong><span style="color: #7f0055;">double</span></strong>[] totalWithinSumOfSquares = caller.getParser().getAsDoubleArray(<span style="color: #2a00ff;">"tot_withinss"</span>);
<br />
<strong><span style="color: #7f0055;">double</span></strong>[] totalBetweenSumOfSquares = caller.getParser().getAsDoubleArray(<span style="color: #2a00ff;">"betweenss"</span>);
<br />
<br />
<strong><span style="color: #7f0055;">for</span></strong> (<strong><span style="color: #7f0055;">int</span></strong> i = 0; i < clusters.length; i++) {
<br />
System.out.println(<span style="color: #2a00ff;">"Observation "</span> + i + <span style="color: #2a00ff;">" is in cluster "</span> + clusters[i]);
<br />
}
<br />
<br />
System.out.println(<span style="color: #2a00ff;">"Cluster Centers:"</span>);
<br />
<strong><span style="color: #7f0055;">for</span></strong> (<strong><span style="color: #7f0055;">int</span></strong> i = 0; i < centers.length; i++) {
<br />
<strong><span style="color: #7f0055;">for</span></strong> (<strong><span style="color: #7f0055;">int</span></strong> j = 0; j < centers[0].length; j++) {
<br />
System.out.print(centers[i][j] + <span style="color: #2a00ff;">" "</span>);
<br />
}
<br />
System.out.println();
<br />
}
<br />
<br />
System.out.println(<span style="color: #2a00ff;">"Total Within Sum of Squares: "</span> + totalWithinSumOfSquares[0]);
<br />
System.out.println(<span style="color: #2a00ff;">"Total Between Sum of Squares: "</span> + totalBetweenSumOfSquares[0]);
<br />
System.out.println(<span style="color: #2a00ff;">"Total Sum of Squares: "</span> + totalSumOfSquares[0]);
<br />
}
<br />
<br />
}
<br />
<div style="border-top: #ccc 1px dashed; text-align: right;">
<br /></div>
</div>
<br />
<br />
The output is
<br />
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>[cluster, centers, totss, withinss, tot_withinss, betweenss, size, iter, ifault]</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 0 is in cluster 2</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 1 is in cluster 2</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 2 is in cluster 2</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 3 is in cluster 2</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 4 is in cluster 2</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 5 is in cluster 2</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 6 is in cluster 2</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 7 is in cluster 1</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 8 is in cluster 1</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Observation 9 is in cluster 1</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Cluster Centers:</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>40.0 6.42857142857143 </b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>80.0 12.8571428571429 </b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Total Within Sum of Squares: 2328.57142857143</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Total Between Sum of Squares: 11833.9285714286</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><b>Total Sum of Squares: 14162.5</b></span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><br /></span></span>
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><br /></span></span>
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><br /></span></span>
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;">Have a nice read!</span></span><br />
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><br /></span></span>
<span style="font-family: Courier New, Courier;"><span style="font-size: 13.3333330154419px;"><br /></span></span>
<br />
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-76344129785004861152015-02-28T10:32:00.000-08:002015-02-28T10:32:18.756-08:00Levenshtein Distance in R<div class="separator" style="clear: both; text-align: center;">
<a href="http://k26.kn3.net/0792E4E24.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://k26.kn3.net/0792E4E24.jpg" height="200" width="150" /></a></div>
Edit distance and Levenshtein distance are nonparametric distance measures that not like well known metric distance measures such as Euclidean or Mahalanobis distances in some persfectives.<br />
<br />
Levenshtein distance is a measure of how many characters should be replaced or moved to get two strings same.<br />
<br />
In the example below, a string text is asked from the user in console mode. Then the input string is compared to colour names defined in R. Similar colour names are then reported:<br />
<br />
<br />
<br />
<span style="color: red;"><span style="font-family: "Courier New",Courier,monospace;"><b>user.string <- readline("Enter a word: ")<br />wordlist <- colours()<br />dists <- adist(user.string, wordlist)<br />mindist <- min(dists)<br />best.ones <- which(dists == mindist)<br /><br />for (index in best.ones){<br /> cat("Did you mean: ", wordlist[index],"\n")<br />}</b></span></span><br />
<br />
<br />
Here is the results:<br />
<br />
<b><span style="color: #3d85c6;"><span style="font-family: "Courier New",Courier,monospace;">Enter a word: turtoise<br />Did you mean: turquoise </span></span></b><br />
<b><span style="color: #3d85c6;"><span style="font-family: "Courier New",Courier,monospace;"><br /></span></span></b>
<b><span style="color: #3d85c6;"><span style="font-family: "Courier New",Courier,monospace;">Enter a word: turtle<br />Did you mean: purple </span></span></b><br />
<b><span style="color: #3d85c6;"><span style="font-family: "Courier New",Courier,monospace;"><br /></span></span></b>
<b><span style="color: #3d85c6;"><span style="font-family: "Courier New",Courier,monospace;">Enter a word: night blue<br />Did you mean: lightblue </span></span></b><br />
<br />
<b><span style="color: #3d85c6;"><span style="font-family: "Courier New",Courier,monospace;">Enter a word: parliament<br />Did you mean: darkmagenta </span></span></b><br />
<b><span style="color: #3d85c6;"><span style="font-family: "Courier New",Courier,monospace;"><br /></span></span></b>
<b><span style="color: #3d85c6;"><span style="font-family: "Courier New",Courier,monospace;">Enter a word: marooon<br />Did you mean: maroon</span></span></b><br />
<br />
<br />
Have a nice read<br />
<br />
<br />
<br />
<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-729711810109364462015-02-27T15:59:00.000-08:002015-02-27T15:59:15.257-08:00Frequency table of characters in a string in R<div class="separator tr_bq" style="clear: both; text-align: center;">
<a href="http://www.kelimecik.com/data/%C3%A7etele-ANd9GcRYDpobDyV96a4rkcVsA-xYC_I_B2jglVqUsxUqTeMOeNtX-TqQ8A.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://www.kelimecik.com/data/%C3%A7etele-ANd9GcRYDpobDyV96a4rkcVsA-xYC_I_B2jglVqUsxUqTeMOeNtX-TqQ8A.jpg" height="200" width="200" /></a></div>
<span style="font-size: x-large;">R</span>'s string manipulating functions includes splitting a string. One can think that parsing a string or extracting its characters into an array and generating the frequency information may be used in a language detection system. <br />
<br />
Here is a example on a text that is captured from the <a href="http://www.oracle.com/technetwork/java/javase/overview/javahistory-index-198355.html" target="_blank"><b>Oracle - History of Java</b></a> site. The code below defines a large string. The string is then parsed into its characters. After calculating the frequencies of each single character (including numbers, commas and dots) a histogram is saved in a file.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># Defining string </span><br />
<span style="color: red; font-family: 'Courier New', Courier, monospace;">s <- "Since 1995, Java has changed our world and our expectations. Today, with technology such a part of our daily lives, we take it for granted that we can be connected and access applications and content anywhere, anytime. Because of Java, we expect digital devices to be smarter, more functional, and way more entertaining. In the early 90s, extending the power of network computing to the activities of everyday life was a radical vision. In 1991, a small group of Sun engineers called the \"Green Team\" believed that the next wave in computing was the union of digital consumer devices and computers. Led by James Gosling, the team worked around the clock and created the programming language that would revolutionize our world – Java. The Green Team demonstrated their new language with an interactive, handheld home-entertainment controller that was originally targeted at the digital cable television industry. Unfortunately, the concept was much too advanced for the them at the time. But it was just right for the Internet, which was just starting to take off. In 1995, the team announced that the Netscape Navigator Internet browser would incorporate Java technology.Today, Java not only permeates the Internet, but also is the invisible force behind many of the applications and devices that power our day-to-day lives. From mobile phones to handheld devices, games and navigation systems to e-business solutions, Java is everywhere!"</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># First converting to lower case</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># then splitting by characters.</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># strsplit return a list, we are unlisting to a vector.</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">chars <- unlist(strsplit(tolower(s), ""))</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># Generating frequency table</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">freqs <- table(chars)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># Generating plot into a file</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">png("Graph.png")</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">hist(freqs,include.lowest=TRUE, breaks=46,freq=TRUE,labels=rownames(freqs))</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">dev.off()</span><br />
<br />
<br />
<br />
The generated output is<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/--VhSolE3ybQ/VPEBUN7nRHI/AAAAAAAAA-A/-9owx5ngDRc/s1600/Graph.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/--VhSolE3ybQ/VPEBUN7nRHI/AAAAAAAAA-A/-9owx5ngDRc/s1600/Graph.png" /></a></div>
<br />
We translate the text used in our example to Spanish using Google Translate site. The code is shown below:<br />
<br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># Defining string </span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">s <- "Desde 1995, Java ha cambiado nuestro mundo y nuestras expectativas. Hoy en día, con la tecnología de una parte de nuestra vida cotidia na tal, damos por sentado que se puede conectar y acceder a las aplicaciones y contenido en cualquier lugar ya cualquier hora. Debido a Java , esperamos que los dispositivos digitales para ser más inteligente, más funcional, y de manera más entretenida. A principios de los años 90 , que se extiende el poder de la computación en red para las actividades de la vida cotidiana era una visión radical. En 1991, un pequeño gr upo de ingenieros de Sun llamado \"Green Team\" cree que la próxima ola de la informática fue la unión de los dispositivos digitales de cons umo y ordenadores. Dirigido por James Gosling, el equipo trabajó durante todo el día y creó el lenguaje de programación que revolucionaría e l mundo - Java. El Equipo Verde demostró su nuevo idioma con una mano controlador interactivo, el entretenimiento en casa que fue dirigido o riginalmente a la industria de la televisión digital por cable. Por desgracia, el concepto fue demasiado avanzado para el ellos en el moment o. Pero fue justo para Internet, que estaba empezando a despegar. En 1995, el equipo anunció que el navegador de Internet Netscape Navigator incorporaría Java technology.Today, Java no sólo impregna el Internet, pero también es la fuerza invisible detrás de muchas de las aplicaci ones y dispositivos que alimentan nuestra vida del día a día. Desde teléfonos móviles para dispositivos de mano, juegos y sistemas de navega ción para e-business soluciones, Java está en todas partes!"</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># First converting to lower case</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># then splitting by characters.</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># strsplit return a list, we are unlisting to a vector.</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">chars <- unlist(strsplit(tolower(s), ""))</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># Generating frequency table</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">freqs <- table(chars)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"># Generating plot into a file</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">png("Graph.png")</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">hist(freqs,include.lowest=TRUE, breaks=46,freq=TRUE,labels=rownames(freqs))</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">dev.off()</span><br />
<div>
<br /></div>
<div>
<br /></div>
<div>
The generated plot for the Spanish translation is:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-_kItu9i5Htc/VPEDRHcou7I/AAAAAAAAA-M/bS_5M3ymvHQ/s1600/Graph.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-_kItu9i5Htc/VPEDRHcou7I/AAAAAAAAA-M/bS_5M3ymvHQ/s1600/Graph.png" /></a></div>
<div>
<br /></div>
<div>
Note that some characters such as space, dots and commas can be replaced from the string s using a code similar to this:</div>
<div>
<br /></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;"><b>news <- gsub(pattern=c(" "), "", x=s)</b></span></div>
<div>
<br /></div>
<div>
The code above removes all spaces from the string s and a new string variable news holds the modified string. Original variable remains same. </div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Have a nice read!</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2297365420309899400.post-90653103717466537762015-02-27T05:24:00.001-08:002015-02-27T05:27:03.356-08:00Environments in R<div class="separator" style="clear: both; text-align: center;">
<a href="http://etc.usf.edu/clipart/5000/5092/r_104_lg.gif" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://etc.usf.edu/clipart/5000/5092/r_104_lg.gif" height="200" width="163" /></a></div>
An environment is a special term in R, but its concept is used in many interpreters of programming languages. The term of <b>variable scope</b> is directly related with environments. An environment in R encapsulates a couple of variables or objects which itself is encapsulated by a special environment called <b>global environment</b>.<br />
<br />
After setting a variable to a value out of any function and class, the default holder of this variable is the <b>global environment</b>.<br />
<br />
Suppose we set t to 10 by<br />
<br />
<span style="color: red; font-family: Courier New, Courier, monospace;">t <- 10
</span><br />
<br />
and this is the same as writing<br />
<br />
<br />
<span style="color: red; font-family: Courier New, Courier, monospace;">assign(x="t", value=12, envir=.GlobalEnv)</span>
<br />
<br />
<br />
and the value of t is now 12:<br />
<br />
<br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> t <- 10</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> assign(x="t", value=12, envir=.GlobalEnv)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> t</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] 12</span><br />
<div>
<br /></div>
<br />
<br />
<br />
Instead of using the global environment, we can create new environments and attach them to parent environments. Suppose we create a new environment as below:<br />
<br />
<br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> my.env <- new.env()</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> assign(x="t", value="20", envir=my.env)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> t</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] 12</span><br />
<div>
<br /></div>
<div>
<br /></div>
Value of t is still 12 because we create an other t variable which is encapsulated by the environment my.env.<br />
<br />
<br />
Variables in environments are accessable using the <b>assign</b> and the <b>get</b> functions.<br />
<br />
<br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> get(x="t", envir=my.env)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] "20"</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> get(x="t", envir=.GlobalEnv)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] 12</span><br />
<div>
<br /></div>
<div>
<br /></div>
<div>
As we can see, values of the variables with same name are different, because they are encapsulated by separate environments.</div>
<div>
<br /></div>
<div>
exists() function returns TRUE if an object exists in an environment else returns FALSE. Examining existence of an object is crucial in some cases. </div>
<div>
<br /></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">> exists(x="t", envir=.GlobalEnv)</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] TRUE</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">> exists(x="t", envir=my.env)</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] TRUE</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">> exists(x="a", envir=my.env)</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] FALSE</span></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
is.environment() function returns TRUE if an object is an environment else returns FALSE. </div>
<div>
<br /></div>
<div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">> is.environment(.GlobalEnv)</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] TRUE</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">> is.environment(my.env)</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] TRUE</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">> is.environment(t)</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] FALSE</span></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Finally, environments are simply lists and a list can be converted to an environment easly. </div>
<div>
<br /></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">> my.list <- list (a=3, b=7)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> my.env </span><span style="color: red; font-family: 'Courier New', Courier, monospace;"><</span><span style="color: red; font-family: 'Courier New', Courier, monospace;">- as.environment(my.list)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> get("a", envir=my.env)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] 3</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">> get("b", envir=my.env)</span><br />
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] 7</span></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
The inverse process is converting an environment to a list: </div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">> as.list(.GlobalEnv)</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">$t</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] 12</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;"><br /></span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">$my.env</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;"><environment: 0x17804d8=""></environment:></span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;"><br /></span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">$my.list</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">$my.list$a</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] 3</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;"><br /></span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">$my.list$b</span></div>
<div>
<span style="color: red; font-family: Courier New, Courier, monospace;">[1] 7</span></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Happy R days :)</div>
<div>
<br /></div>
<div>
<br /></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />Unknownnoreply@blogger.com0