Tuesday, August 25, 2015

Lisp for the C++ programmer: Function definitions and function calls

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.





 ; 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)))  
   

Lisp for the C++ programmer: cond expression

Here is the example for the cond expression of Common Lisp and its C++ equivalent.







 ; 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"))  
 )  
   

Wednesday, March 25, 2015

Singleton Design Pattern in PHP Example

Singleton Pattern
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.





//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; 
 }
}

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().

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.
Finally, in destruct function, connection has been closed by null value. When we want to connect database, we call the singleton like:
DB::connect();
If class has any other function, then like:
DB::connect()->functionName($param);

See you!

Sunday, March 22, 2015

Introduction to Fuzuli : JFuzuli REPL


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.

Let's try JFuzuli:




1. Download the Jar 

The current compiled jar of JFuzuli interpreter is release candidate 1.0. You can download it using the link https://github.com/jbytecode/fuzuli/releases/tag/v1.0_release_candidate. You can always find the newest releases in site JFuzuli Releases.

2. Open the Command Prompt

After downloading the jar file, open your operation system's command prompt and locate the jar file by using cd (change directory) command.

3. Start trying it!

In command prompt, type

java -jar JFuzuli.jar

to start. You will see the options:

Usage:
java -jar JFuzuli.jar fzlfile
java -jar JFuzuli.jar --repl
java -jar JFuzuli.jar --editor


You can specify a fuzuli source file to run. The option --repl opens a command shell.  The last option --editor opens the GUI.  Let's try the command shell. 

java -jar JFuzuli.jar --repl
F: 

The prompt F: waits for a convenient Fuzuli expression. Now we can try some basic commands:

F: (+ 2 7)
9.0
F: (- 7 10)
-3.0
F: (require "lang.nfl")
0.0
F: (let mylist '(1 2 3))
[1.0, 2.0, 3.0]
F: (first mylist)
1.0
F: (last mylist)
3.0
F: (length mylist)
3
F: (nth mylist 0)
1.0
F: (nth mylist 1)
2.0


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??

Fuzuli Language home page: http://fuzuliproject.org/
Have a nice read!


Thursday, March 19, 2015

Why is R awesome?

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 toolbox of a statistician or researchers who work on data analysis.

In this blog post, I collect 8 (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).


So, if it is, why is R awesome ?



0. Syntax of Algol Family

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:


> '%_%' <- function(a,b){
+    return(exp(a+b))
+ }
> 5 %_% 2
[1] 1096.633


1. Vectors are primitive data types

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:


> assign("x",cbind(1,1:30))
> assign("y",3+3*x[,2]+rnorm(30))
> solve(t(x) %*% x) %*% t(x) %*% y
         [,1]
[1,] 2.858916
[2,] 3.003787

This example shows the differences between a scaler and a vector:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
> assign("x", c(1,2,3))
> assign("a", 5)
> typeof(x)
[1] "double"
> typeof(a)
[1] "double"
> class(x)
[1] "numeric"
> class(a)
[1] "numeric"

No difference!


2. Theorems get alive in minutes

Suppose that X is a random variable that follows an Exponential Distribution with ratio = 5.
Sum or mean of randomly selected samples with size of N follows a normal distribution.  This is an explanation of the Central Limit Theorem 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.


> assign("nsamp", 5000)
> assign("n", 100)
> assign("theta", 5.0)
> assign("sums", rep(0,nsamp))
> 
> for (i in 1:nsamp){
+     sums[i] <- sum(rexp(n = n, rate = theta)) 
+ }
> hist(sums)




3. There is always a second plan for faster code

Now suppose that we are drawing 50,000 samples randomly using the code above. What would be the computation time?


> assign("nsamp", 50000)
> assign("n", 100)
> assign("theta", 5.0)
> assign("sums", rep(0,nsamp))
> 
> s <- system.time(
+     for (i in 1:nsamp){
+         sums[i] <- sum(rexp(n = n, rate = theta)) 
+     }
+ )
> 
> print(s)
   user  system elapsed 
  0.582   0.000   0.572 




Drawing 50,000 samples with size 100 takes 0.582 seconds. Is it now fast enough? Lets try to write it in C++ !


#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]
NumericVector CalculateRandomSums(int m, int n) {
   NumericVector result(m);
   int i;
   for (i = 0; i < m; i++){
     result[i] = sum(rexp(n, 5.0));
   }
   return(result);
}


After compiling the code within Rcpp, we can call the function CalculateRandomSums() from R.


> s <- system.time(
+ vect <- calculaterandomsums(50000,100)
> print(s)
   user  system elapsed 
  0.185   0.000   0.184 

Now our R code is 3.145946 times slower than the code written in C++.


4. Interaction with C/C++/Fortran is enjoyable

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. Rcpp masks these routines in a clever way. Fortran code is also
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.
For an enjoyable example, have a look at the section 3. There is always a second plan for faster code.
The R package eive 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. Click here to see the explanation and an example.


5. Interaction with Java

Calling Java from R (rJava) and calling R from Java (JRI, RCaller) are all possible. Renjin 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 in this documentation and this.


6. Sophisticated variable scoping

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 Environment in R.


7. Optional Object Oriented Programming (O-OOP) 

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:



 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
Person <- setRefClass(
    Class = "Person",
    fields = c("name","surname","email"),
    methods = list(
        initialize = function(name, surname, email){
            .self$name <- name
            .self$surname <- surname
            .self$email <- email
        },
        
        setName = function(name){
            .self$name <- name
        },
        
        setSurname = function(surname){
            .self$surname <- surname
        },
        
        setEMail = function (email){
            .self$email <- email
        },
        
        toString = function (){
            return(paste(name, " ", surname, " ", email))
        }   
    ) # End of methods
) # End of class



p <- Person$new("John","Brown","brown@server.org")
print(p$toString())

The output is

[1] "John   Brown   brown@server.org"

Java and C++ programmers probably like this notation!


Have a nice read!