Tuesday, November 12, 2013

Feature subset selection Using Genetic Algorithm in MATLAB


function callGeneticAlgo

global mat
global trainInd
global testInd
[trainInd,~,testInd] = dividerand(1420,0.7,0,0.3);
global counter
global errList
counter = 1;
errList = [];
fileName=  '../features/alltopPNPDMF.feature' ;
mat = load(fileName);

[x,fval,exitflag,output,population,score] = gaFeaSelection(1588,100,10800); % param1 = #feature excludig label
% param2 =  population size
% param3 = sec to test (3 hour = 10800 sec)

dlmwrite('selected.GA',x,'delimiter','\n');

display('Done');

end


function [x,fval,exitflag,output,population,score] = gaFeaSelection(nvars,PopulationSize_Data,TimeLimit_Data)
% This is an auto generated MATLAB file from Optimization Tool.

% Start with the default options
options = gaoptimset;
% Modify options setting
options = gaoptimset(options,'PopulationType', 'bitString');
options = gaoptimset(options,'PopulationSize', PopulationSize_Data);
options = gaoptimset(options,'TimeLimit', TimeLimit_Data);
options = gaoptimset(options,'MutationFcn', {  @mutationuniform [] });
options = gaoptimset(options,'Display', 'iter');
options = gaoptimset(options,'PlotFcns', { @gaplotbestf });
[x,fval,exitflag,output,population,score] = ...
ga(@feaSelobjFun,nvars,[],[],[],[],[],[],[],options);

end



function [ evalValue ] = feaSelobjFun( x )
%FEASELOBJFUN Summary of this function goes here
%   Detailed explanation goes here
global mat
global trainInd
global testInd
global counter
global errList

Data = mat(:,1:end-1);
Label = mat(: , end);
selectedFeature = Data(:,(x~=0))*diag(x(x~=0));

svmStruct = svmtrain(selectedFeature(trainInd,:),Label(trainInd),...
            'kernel_function','linear'  ,...
            'method' , 'SMO' , 'kktviolationlevel',.55);
predictedOut = svmclassify(svmStruct,selectedFeature(testInd,:));

[X,Y,Thr,AUC] = perfcurve(Label(testInd),predictedOut,1);
 evalValue = 1-AUC;
errList(counter) = fval;
counter = counter + 1;

end



Friday, November 1, 2013

Feature subset selection toolbox collection



0. DEAP:


DEAP: Evolutionary Algorithms Made Easy
Genetic algorithm based multi-objective feature selection techniques.


http://jmlr.org/papers/volume13/fortin12a/fortin12a.pdf

1. Weka
Filter, Wrapper

2. Java-ML: A Machine Learning Library 

http://jmlr.org/papers/volume10/abeel09a/abeel09a.pdf

Entropy based methods (4)
Stepwise addition/removal (2)
SVMRFE
Random forests
Ensemble feature selection


3. MATLAB:

Sequential feature selection:

http://www.mathworks.com/help/stats/feature-selection.html

Genetic Algorithm based:

http://www.mathworks.com/matlabcentral/fileexchange/29553-feature-selector-based-on-genetic-algorithms-and-information-theory/content/GA_feature_selector.m

4. KELL

http://sci2s.ugr.es/keel/algorithms.php#featureselection

Imbalanced set problems: Tools review to solve


1. Weka (Java Based)


  • You can subsample the majority class (try the filter SpreadSubsample ,
    GSVM-RU ).  
  • You can oversample the minority class, creating synthetic examples (try SMOTE).  
  • You can make your classifier cost sensitive (try the metaclassifier CostSensitiveClassifier).  
http://weka.wikispaces.com/space/content?tag=cost-sensitive

Each of the methods has it's own strengths and weaknesses, refer to the papers referenced in the documentation of each one.  If you use any of these and you need accurate probability estimates, you can use an isotonic regression to calibrate the output.

2. MATLAB


 3. KELL tool (Java Based)

http://sci2s.ugr.es/keel/software/prototypes/openVersion/Algorithms_20130703.pdf

4. LASVM

http://leon.bottou.org/projects/lasvm

publication: Fast Kernel Classifiers with Online and Active Learning



MATLAB optimization toolbox usage with genetic algorithm


Useful tutorial


http://www.mathworks.com/products/global-optimization/description3.html

Best example of implementatoin with Constraint, objective function

http://www.mathworks.com/help/gads/examples/constrained-minimization-using-the-genetic-algorithm.html

More about how to use multi-objective

http://www.mathworks.com/discovery/multiobjective-optimization.html


http://www.mathworks.com/help/gads/examples/performing-a-multiobjective-optimization-using-the-genetic-algorithm.html

http://www.mathworks.com/help/gads/examples/multiobjective-genetic-algorithm-options.html

Example GAMULTOBJ (can handle Multiple Objective)  GA(can handle 1 objective)

Constrained Minimization Problem
We want to minimize a simple fitness function of two variables x1 and x2
   min f(x) = 100 * (x1^2 - x2) ^2 + (1 - x1)^2;
    x
  min f(x) = 100 * (x1^2 + x2) ^2 + (1 + x1)^2;
    x 
such that the following two nonlinear constraints and bounds are satisfied
   x1*x2 + x1 - x2 + 1.5 <=0, (nonlinear constraint)
   10 - x1*x2 <=0,            (nonlinear constraint)
   0 <= x1 <= 1, and          (bound)
   0 <= x2 <= 13              (bound)

Implementation of  Singleobjective Function
   function y = simple_objective(x)
    y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
 
Implementation of  Multiobjective Function

   function y = simple_multiobjective(x)
    y(1) = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
    y(2) = 100 * (x(1)^2 + x(2)) ^2 + (1 + x(1))^2; 

Implementation of the Constraint Function
   function [c, ceq] = simple_constraint(x)
   c = [1.5 + x(1)*x(2) + x(1) - x(2);
   -x(1)*x(2) + 10];
   ceq = [];
 
Implementation of the GAMULTOBJ/ GA
 
ObjectiveFunctionOne = @simple_objective;
ObjectiveFunctionMult = @simple_multiobjective;
nvars = 2;
A = []; b = [];
Aeq = []; beq = [];
LB = [0 0];   % Lower bound , you can also set []
UB = [1 13];  % Upper bound , you can also set []
options = gaoptimset('PlotFcns',{@gaplotpareto,@gaplotscorediversity});

[x,fval,extflag, output,pop, score] = gamultiobj( ObjectiveFunctionMult, nvars, A, b, Aeq, beq, LB, UB, options )

[x,fval,extflag, output,pop, score] = gamultiobj( ObjectiveFunctionOne, nvars, A, b, Aeq, beq, LB, UB, options )


But in GA you can add non-linear constraint

http://www.mathworks.com/help/gads/examples/constrained-minimization-using-the-genetic-algorithm.html

function [c, ceq] = simple_constraint(x)
   c = [1.5 + x(1)*x(2) + x(1) - x(2);
   -x(1)*x(2) + 10];
   ceq = [];

[x,fval,extflag, output,pop, score] = ga(ObjectiveFunctionOne,nvars,[],[],[],[],LB,UB , ConstraintFunction);

Example GAMULTOBJ (can handle Multiple Objective)  GA(can handle 1 objective) for BITSTRING

%% using gamultobj
ObjectiveFunction = @myFitnessFnc_bitString;
nvars = 2;    % Number of variables
LB = [0 0];   % Lower bound
UB = [1 13];  % Upper bound
options = gaoptimset('PopulationSize',60,...
          'ParetoFraction',0.7,'PlotFcns',@gaplotpareto, 'PopulationType','bitstring');
[x fval flag output population score] = gamultiobj(ObjectiveFunction,nvars, [],[],[],[],[] ,[] ,options )

%% using GA

%% usig ga
ObjectiveFunction = @myFitnessFnc_bitString;
nvars = 2;    % Number of variables
options = gaoptimset('PopulationSize',60,...
          'ParetoFraction',0.7,'PlotFcns',@gaplotpareto, 'PopulationType','bitstring');
[x fval flag output population score] = ga(ObjectiveFunction,nvars, options)

Tuesday, October 29, 2013

video lecture on different topics



1. Linear Programming / Linear Optimization

fundamental of operation research
Lec-3 Linear Programming Solutions IIT madras

http://www.youtube.com/watch?v=XEA1pOtyrfo



http://nptel.iitm.ac.in



2. 

Wednesday, October 23, 2013

Call matlab from C/C++ java or Call C/C++ java from Matlab matlab binary calling



Call Matlab from c/c++ , java etc



http://www.mathworks.com/help/matlab/matlab_external/calling-matlab-software-from-a-c-application.html

http://www.mathworks.com/help/matlab/matlab_external/compiling-engine-applications-with-the-mex-command.html#bsq78dr-9


Set env var First


export LD_LIBRARY_PATH=/mnt/kaustapps/MATLAB-faculty/R2011b.app/bin/glnxa64/:/mnt/kaustapps/MATLAB-faculty/R2011b.app/sys/os/glnxa64/:$LD_LIBRARY_PATH

UNIX Engine Example engdemo

To verify the build process on your computer, use the C example engdemo.c or the C++ example engdemo.cpp.
  1. Copy one of the programs, for example, engdemo.c, to your current working folder:
    copyfile(fullfile(matlabroot,...
      'extern','examples','eng_mat','engdemo.c'),...
      '.', 'f');
  2. Build the executable file:
    mex('-v', '-f', fullfile(matlabroot,...
      'bin','engopts.sh'),...
      'engdemo.c');
  3. Verify that the build worked by looking in your current working folder for the engdemo application:
    dir engdemo
  4. Run the example in MATLAB:
    !engdemo
For more information about the engdemo applications, see Call MATLAB Functions from C Applications.
 
 


Call C/C++ , java from Matlab




http://www.mathworks.com/help/matlab/ref/mex.html

http://www.mathworks.com/help/matlab/create-mex-files.html


Sunday, October 20, 2013

libsvm usage


FAQ
=====
http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#/Q4:_Training_and_prediction


DONWLOAD
========

Just need to download 1 zip file from main page. That's all.

http://www.csie.ntu.edu.tw/~cjlin/libsvm/

INSTALL
==========
make

If you want to use parameter estimation, you need to change the code a bit and do following

make clean;
make install;


DATAFORMAT
==============
label  1:feat#1  2:feat#2  3:feat#3      N:feat#N


Some available data
===============
http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/

Use heart_scale data. It works perfectly for all plot, cv and parameter estimation.

2-class CLASSIFICATION with RBF kernel with 5 fold CV
=================================

train (support vectors are generated):
./svm-train -s 0 -t 2   -g  0.03125 -c 0.25   train.dat  train.model

train with CV(No support  vectors are shown, just show your score: AUC,F-score)

./svm-train -s 0 -t 2 -v 5  train.dat  > train.cv

testing:
./svm-predict test.dat train.model test.output

Now parse output file (containing predicted label) to calculate sen, spe and accuracy.

Regression
================

train:
./svm-train -s 3  engine.train  engine.train.model
 testing:
./svm-predict  engine.train.model engine.output

Now parse output file (containing predicted values) to calculate RMS etc.

Parameter Searching for RBF kernel (only supported kernel)
====================================

http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/eval/index.html

a. Change in source file as mentioned in above link
b. use grid.py in folder tools
c. Read  the README file inside tools to select range of parameters.
d. use following command:

 python grid.py -log2c -5,5,1 -log2g -4,0,1 -v 10  ../../data/heart_scale

- it searches log2c of "c" parameter in range [-5,,5] with increment 1. And searches log2g of "g" parameter in range [-4,0] with increment 1.  with 10 fold CV using data heart_scale

e. Select the maximum score(i.e. AUC, F-score ) from the output file and it contains the log2(param) of kernel parameter. So, take inverseLog for final parameter.

f. If you wanna use other criteria besides AUC (default ) then change the

 double (*validation_function)(const dvec_t&, const ivec_t&) = auc;
in eval.cpp to the evaluation function you preferred. You can also assign "precision", "recall", "fscore", or "bac" here.

FEATURE SELECTION
===================



Windows
=============

svm-train -s 3  D:\matlabWorkspace\fuelPerfHeavyNapthaSVM\engine.train.svm D:\matlabWorkspace\fuelPerfHeavyNapthaSVM\engine.train.svm.model
svm-predict    D:\matlabWorkspace\fuelPerfHeavyNapthaSVM\engine.test.svm  D:\matlabWorkspace\fuelPerfHeavyNapthaSVM\engine.train.svm.model D:\matlabWorkspace\fuelPerfHeavyNapthaSVM\predicted

Wednesday, October 2, 2013

matlab cross validation with svm [draft not final]



function test

clc;
matPos = csvread('pos.dat');
noPos= size(matPos,1);
noFeature = size(matPos,2);
labelPos= ones(noPos,1);
% matPos= [matPos labelPos ];

matNeg = csvread('neg.dat');
noNeg= size(matNeg,1);
labelNeg= -1*ones(noNeg,1);
% matNeg= [matNeg labelNeg ];

% svmStruct = svmtrain(featureInTrain,featureOutTrain,'kernel_function','linear'  , 'options' ,smo_opts); %,'rbf_sigma',100,'boxconstraint',25

noFold=5;
c = cvpartition([labelPos ; labelNeg],'kfold', noFold);
strArray = java_array('java.lang.String', 2);
strArray(1) = java.lang.String('1');
strArray(2) = java.lang.String('-1');

myorder = cell(strArray)

f = @(xtr,ytr,xte,yte) confusionmat(yte,@(xtr,ytr,xte)crossfun(xtr,ytr,xte, exp(z(1)),exp(z(2))),'order', [1 -1] );
cfMat = crossval(f,[matPos; matNeg], [ labelPos ; labelNeg],'partition',c);
cfMat = reshape(sum(cfMat),3,3)


minfn = @(z)crossval('mcr',[matPos; matNeg], [ labelPos ; labelNeg],...
    'Predfun', @(xtrain,ytrain,xtest)crossfun(xtrain,ytrain,xtest, exp(z(1)),exp(z(2))), ...
    'partition',c   );
 

opts = optimset('TolX',5e-4,'TolFun',5e-4);
[searchmin fval] = fminsearch(minfn,randn(2,1),opts)

load('fisheriris');
y = species;
X = meas;
order = unique(y); % Order of the group labels
cp = cvpartition(y,'k',10); % Stratified cross-validation

f = @(xtr,ytr,xte,yte)confusionmat(yte,classify(xte,xtr,ytr),'order',order);

cfMat = crossval(f,X,y,'partition',cp);
cfMat = reshape(sum(cfMat),3,3)



display('done')
end





function yfit = crossfun(xtrain,ytrain,xtest, rbf_sigma,boxconstraint)

% Train the model on xtrain, ytrain,
% and get predictions of class of xtest
svmStruct = svmtrain(xtrain,ytrain, 'boxconstraint',boxconstraint, ...
    'Kernel_Function','rbf','rbf_sigma',rbf_sigma );
    %'kernel_function','linear'  , 'options' ,smo_opts );
  %  'Kernel_Function','rbf','rbf_sigma',rbf_sigma, );
  
 
yfit = svmclassify(svmStruct,xtest);

yfit

end







Wednesday, September 18, 2013

Java Template code

package com.cbrc.pipeline2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class Test {


    String foldIn;
    String fnmFasta;

    String foldOut;
    String fnmOut;
   
    void init(String rootIn, String rootOut, String fnmInFasta, String fOut) {
       
        this.foldIn = rootIn;
        this.fnmFasta = this.foldIn + fnmInFasta;
   
        this.foldOut = rootOut;
        this.foldOut = this.foldOut + fOut;

    }


    void loadFasta(){

        try {
            FileInputStream fstream = new FileInputStream(fnmFasta);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String befTrim , strLine;
            while ((befTrim = br.readLine()) != null) {
                strLine = befTrim.trim();


            }

            br.close();
            in.close();
            fstream.close();


        } catch (Exception e) {

            e.printStackTrace();
        }


    }



    void writeSelectedRegion(){


        try {

            BufferedWriter bwr = new BufferedWriter(new FileWriter(this.fnmOut));
            StringBuffer buf = new StringBuffer();




            bwr.write(buf+"");

            bwr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    void doProcessing() {
       
        loadFasta();
        writeSelectedRegion();
    }

    public static void main(String[] args) {

        Test obj = new Test();

        obj.init(args[0], args[1] , args[2] , args[3]);
        //        obj.init("./", "test.fa", "20", "72" , "seq.prf");

        obj.doProcessing();


    }

}

Thursday, August 1, 2013

cluster using grid qsub



source /home/ge2011.11/cbrc/common/settings.sh

qsub -cwd  -e error.log -b y /usr/bin/blastpgp -i N_C_ternimus_Paxillin.fa  -d /home/data/GenomeAA/blastdb/nr -e 1e-10 -m 7 -o psiblastout.xml -j 3

qstat


Tuesday, July 23, 2013

JAVA SORT COLLECTION List or Vector or arraylist or array




http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html


Sort vector or List

import java.util.*; 
 


public class Test

{
 

class BedFormat implements Comparable 

 
  {
    String chrom;
    int end;
    public int compareTo(BedFormat obj) {
       
        return (this.end > obj.end ) ? 1  :  -1 ;
    } 
 
 
 

public static void main()

{ 
     List vp = new ArrayList();

     vp.add(new BedFormat());
     vp.add(new BedFormat());
      .... 
     vp.add(new BedFormat()); 
     Collections.sort( vp  );

     // print 
     // it will print according to end variable of object

}
 
}
 
 

Example : Finding Median from a list

 
class ExpReplica implements Comparable{

     
     double valReplica;
     
     
     public ExpReplica(double valReplica) {
   super();
   this.valReplica = valReplica;
  }


  public int compareTo(ExpReplica obj) {
        
         return (this.valReplica > obj.valReplica ) ? 1  :  -1 ;
     }


  public double getValReplica() {
   return valReplica;
  }


  public void setValReplica(double valReplica) {
   this.valReplica = valReplica;
  } 
  
 }  


 void FindMedianFromArray()
 {
  
     Double tmp[] = { 6.7 , 2.3 , 8.9, 11.1 , 91.5 };
   
   double finalValue=0.0;
   int listSize;
     
   List myArrayList = new ArrayList();
   for(int c=0; c   {
    myArrayList.add(new ExpReplica( tmp[c] ) );
   }

   Collections.sort( myArrayList  );
    
   listSize=myArrayList.size();
    
   if(listSize==1)
   {
    finalValue = ( (ExpReplica)myArrayList.get(0) ).getValReplica() ;
    
   }else if(listSize%2 ==0) // even - select the big one
   {
    finalValue =  ( (ExpReplica)myArrayList.get(listSize/2) ).getValReplica() ;
   }else // odd - take the middle one
   {
    finalValue =  ( (ExpReplica)myArrayList.get(listSize/2) ).getValReplica() ;
   }
    
   System.out.println( "Median:" + "\t"+ finalValue); 
  
 }
 

Friday, July 12, 2013

Java Thread tutorial



// Wait for the child threads to finish job before main thread ends

public Class TestJoin{
     public static void main(String args[]) {

        Vector vecThread = new Vector();
        for(int curLen=1; curLen<=5;curLen= curLen+1) //
        {
            vecThread.add( new Thread(new Inner(curLen)) );
        }
        for(int i=0;i        {
            vecThread.get(i).start();
        }
        try {
            for(int i=0;i            {
                vecThread.get(i).join();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


}

Class Inner implements Runnable{

      int startLen;
      public Inner(int start) {
            super();
            this.startLen = start;
      }
      private void doJob(){
      } 
   
     public void run(){
            doJob();
     }
}


}

Doing the same thread using ThreadPools (It is the standard way)


http://www.journaldev.com/1069/java-thread-pool-example-using-executors-and-threadpoolexecutor

Monday, June 24, 2013

Gaussian Process GP



https://www.youtube.com/watch?v=16oPvgOd3UI

function GP_1d

kernel=5;
switch kernel
    case 1; k = @(x,y) 1*x'*y; % linear
    case 2; k = @(x,y) 1*min(x,y); % brownian motion
    case 3; k = @(x,y) exp(-100*(x-y)'*(x-y)); % squared
    case 4; k = @(x,y) exp(-1*sqrt(x-y)'*(x-y)); % Ornistin   
    case 5; k = @(x,y) exp(-1*sin(5*pi*(x-y))^2); % periodic     
end

% choose points at which to sample
x = (0:.005:1);
n = length(x);


% covariance matrix
C = zeros(n,n);
for i=1:n
   for j=1:n
      C(i,j) = k (x(i), x(j)) ;
   end
end

% sample from gaussian process at this points
u = randn(n,1);
[A, S , B ] = svd(C);
z = A *sqrt(S)*u;


% plot
figure(2); hold on;
plot(x, z, '.-');
axis([0, 1, -2, 2]);



end

============ IN 2D ========

function GP_2d

kernel=3;
switch kernel
    case 1; k = @(x,y) 1*x'*y; % linear
    case 2; k = @(x,y) 1*min(x,y); % brownian motion
    case 3; k = @(x,y) exp(-100*(x-y)'*(x-y)); % squared
    case 4; k = @(x,y) exp(-1*sqrt(x-y)'*(x-y)); % Ornistin   
    case 5; k = @(x,y) exp(-1*sin(5*pi*(x-y))^2 ); % periodic     
end

% choose points at which to sample
points = (0:0.05:1)';
[U,V] = meshgrid(points,points);
x = [U(:) V(:)]';
n = size(x,2);


% covariance matrix
C = zeros(n,n);
for i=1:n 
   for j=1:n
      C(i,j) = k (x(:,i), x(:,j)) ;
   end
end

% sample from gaussian process at this points
u = randn(n,1);
[A, S , B ] = svd(C);
z = A*sqrt(S)*u;


% plot
figure(2); clf;
Z = reshape(z,sqrt(n) , sqrt(n));
surf(U,V,Z);


end

Sunday, April 14, 2013

Ruby Tutorial


 Ruby Installation with graphics (painful)
==============================

* Install RVM for Ruby -
\curl -#L https://get.rvm.io | bash -s stable --autolibs=3 --ruby

* Install TK from ActiveTcl - http://www.activestate.com/activetcl

* Run
rvm reinstall 2.0.0 --enable-shared --enable-pthread --with-tk --with-tcl

Recommended Sites
==================

1. From author (Yukihiro Matsumore)

http://rubymonk.com/

2. Basic
http://www.tutorialspoint.com/ruby/ruby_variables.htm


Interactive Tutorial
==============
http://tryruby.org/

Run ruby
=========
1. Using interpreter

irb ( start interpreter)
load "fname.rb" ( load the file)
ruby "fname.rb" ( run the file)

2. Using ruby command

ruby "fname.rb" ( run the file)

3. Type of Variables & Mehod
====================


@instanceVariable  
@@classVariable
$GlobalVariable


4. instanceMethod

def fnc
...
end

5. ClassMethod
=============
def self.fnc
...
end


6. Anonymous/Block 
================
  3.times { puts "hello"}

 arr = [1,2,3,4]
 arr.each { puts "arr"}
 arr.each { |cur|  puts cur}
   i = 50
  arr.each { |cur| if i> cur then puts i else puts cur  end}
   arr.each { |cur| puts cur*2}
   arr.map { |cur| cur*2}
   arr.any? { |cur| cur> 0}
   arr.inject { |acc,cur| acc+cur}
   arr.select { |cur| cur> 3}


Sending block as parameter(NO need to capture block in callee) and run block yield
=============================================================

class Foo
  def initialize(max)
    @max = max
  end

  def silly(i)
    yield(i,5) + yield(@max,@max) # yield will execute block
  end
end

f = Foo.new(1000)
f.silly(10) {|a,b|   a + b}  # block just double values

Proc like block but returns object
=======================

c = [3,5,7,9]
 c = a.map { |x| lambda{ |y|  x>y}  }
c[0].call 33


Loop
========
 i = 5
j = 9
(0..i).each do |curI|
    print curI.to_s + "\n"
    (curI..j).each do |curJ|
        print curJ.to_s
    end
end



Dynamic dispatching
================

class Point
  attr_accessor :x, :y

  def initialize(x,y)
    @x = x
    @y = y
  end
  def distFromOrigin
    Math.sqrt(@x * @x  + @y * @y) # uses instance variables directly
  end
  def distFromOrigin2
    Math.sqrt(x * x + y * y) # uses getter methods # CALL THE GETTER METHOD FROM POLARPOINT . BECOZ IT IS A FUNCTION CALL AND DUE TO DYNAMIC DISPATCH
        # IT WILL START SEARCHING METHOD FROM SUB-CLASS
  end

end


class PolarPoint < Point
  # Interesting: by not calling super constructor, no x and y instance vars
  # In Java/C#/Smalltalk would just have unused x and y fields
  def initialize(r,theta)
    @r = r
    @theta = theta
  end
  def x
    @r * Math.cos(@theta)
  end
  def y
    @r * Math.sin(@theta)
  end
  def x= a
    b = y # avoids multiple calls to y method
    @theta = Math.atan2(b, a)
    @r = Math.sqrt(a*a + b*b)
    self
  end
  def y= b
    a = x # avoid multiple calls to y method
    @theta = Math.atan2(b, a)
    @r = Math.sqrt(a*a + b*b)
    self
  end
  def distFromOrigin # must override since inherited method does wrong thing
    @r
  end
  # inherited distFromOrigin2 already works!!
end

# the key example
pp = PolarPoint.new(4,Math::PI/4)
pp.x
pp.y
pp.distFromOrigin2



Tuesday, February 26, 2013

Haskell tutorial


Important link for tutorial
===================

http://www.haskell.org/haskellwiki/Haskell_in_5_steps#Where_to_go_from_here

http://www.cs.nott.ac.uk/~gmh/book.html

http://rigaux.org/language-study/syntax-across-languages-per-language/Haskell.html

http://learnyouahaskell.com/chapters

You can change prompt by

:set prompt

0. how to run/quit haskell file
===============================
a. ghci  ( it will load Glasgow Haskell Compiler)
b. load file >  :l filename.hs
c. fncInsideFile parameter
d. :quit 


1.  bracket Issues in
=============
fnc(a)  // wrong

fnc a ;// right, parameters are space seperated


Parentesis
a. for tuple  -- let tup = ( 1, "sss")
b. (x:xs) -- x is first element, xs is rest elements

c.   for curry function/section. if you don't use () it will not work

-- correct
useSection
    = (/2)


-- Incorrect
useSection
    = /2


instead of using 1 + 2    use   (+) 1 2 

Square Bracket : for List -- let lst = [1,2]
-----------------------------------------------------------------
x  -- mean anything single/list
[x] -- mean a list with ONLY ONE element


let out = [ x | x  < -  [1..10] , x  < 3 ] -- List comprehension


Arrow issues in haskell
=================

< - means Belongs to set, Each element of list is generated iterratively

let b = [x | x < - [1..10 , x / 2 ==0] -- 4 quotient 2, remainder 0

- > means type conversion in function

testCurry x y
    = x + y

:type testCurry
(Num a) = > a -> a -> a


Tuple () VS list[]
====================

Tuple can combine any type of data

let myTubple = (1,2,"sd")

list combine only specific type of data with coercion
let myList = [1, 2.0 ]



List Concatenation
=========================================

concat two list using ++

 [1,2,3,4] ++ [9,10,11,12]
# [1,2,3,4,9,10,11,12]

 "hello" ++ " " ++ "world"
# "hello world"

concat one element with one list using :

 (x : xs)


list comprehension . Read each element one by one, Generate desired new list
=================================================

[ x | x < - [50..100], x `mod` 7 == 3]

[x*2 | x  < -  [1..10]]

[x*2 | x < -  [1..10], x*2  == 12]

rightAngle x
    = [ (a,b,c) | c < - [1..x], b < - [1..c], a < - [1..b], a ^ 2 + b ^ 2 == c ^ 2]



HASKELL CURRIED FUNCTION
======================

-- call as currySumRange  5 : sum 1 to X
currySumRange    x
    = foldr (+) 0 [1..x]

-- call as currySumList [2,3,9] : sum element of a list x
currySumList   
    = foldr (+) 0

-- call currySumTwo 4 5
currySumTwo x
    =  (+) x


Operation on element  Iteratively
=======================

1. List comprehension
2. Map  : For any function, applied for each elem -- return n result
3. Filter : For logical operation, applied for each elem -- return n result
4. Fold  : Any function, applied for each elem  --  return 1 result
1. List comprehension
--------------------------------------
let b = [x | x <  - [1..10 , x / 2 ==0] -- 1,2, ,,, 10 , select even

let a = [x | x < - "abc"] -- a , b , c

2. MAP
------------
{-
map takes a function and a list and applies that function to every element in the list, producing a new list
-}

-- map (+3) [1,5,3,1,6]
-- map (++ "!") ["BIFF", "BANG", "POW"]
-- map (replicate 3) [3..6]
-- map show [1,2,3]


3. FILTER
-----------------
{-
filter is a function that takes a predicate  and a list and then returns the list of elements that satisfy the predicate.
-}
-- filter (>3) [1,5,3,2,1,6,4,3,2,1]
-- filter (==3) [1,2,3,4,5]
-- filter (`elem` ['a'..'z']) "u LaUgH aT mE BeCaUsE I aM diFfeRent"

fIND ELEMENTS BELOW 10 AND DIVISIBLE BY 2

largest
    = (filter p [10,9..1])
    where
    p x = x `mod` 2 == 0



4. FOLD
===================

A fold takes  3 PAREMETER
  • a binary function, [binary function is applied between the starting value and the head/tail of the list.]
  • a starting value OF accumulator and
  • a list to fold up.

Then, the binary function is called again with the new accumulator and the now new first (or last) element, and so on.

To understand how foldl OR foldr works run them with scanr OR scanl, it will tell you every steps

scanl  (*) 1 [1,2,3,4,5] -- output  [1,1,2,6,24,120]
scanr (*) 1 [1,2,3,4,5] -- output  [120,120,60,20,5,1]

Tuple generation with ZIP
--------------------------------------------

-- (1,2) (2,3) ... (N, N+1)

zip [1..n] [2..n+1]

zip x tail(x)

-- (elem, elemIndex)

zip x [1..n]
      where n = length x


 Operator
========================

 list related
-------------------------

ALWAYS USE SQUARE BRACKET [] TO REPRESENT
list
set of element
conditional list etc

    x = [1..4]
    x = head [1,2,3,4]
    x = tail [1,2,3,4]
    x = init [1,2,3,4]
    x = last [1,2,3,4]
    x !! n -- find nth (0-based index)element of a list 
    length , sum ,  product
    reverse
    minimum    maximum   
    take n list -- take first n
    drop n list -- take last L-n




arithmetic
----------------------
let b = [x | x < - [1..10 , x `div` 2 ==0] -- 4,5 quotient 2, remainder ANY
let b = [x | x < - [1..10 , x / 2 ==0] -- 4 quotient 2, remainder 0

div -- how many times can be divided always return Int
/ -- original division return int/float



boolean
------------------------------

&& - binary and -- True && False
and - list element wise and -- and [True, False, True, True]

Type Checking
=================
If expr is an expression return type t , then it is written as

expr :: t

To check type of a fucniton
:type functionName

Available types: Char, String, int, Integer, Float

List contains single type  element

let lst = [1,2]
:type lst
lst :: [Integer]

Tuple contains multiple type of element

let tup = (1, "ab")
:type tup
tup :: (Integer, [Char])

String related
======================
take 2 ("abcdef") -- ab
drop 2 ("abcdef") - cdef


--Infinite sequence

repeat 5

CHECK EMPTY STRING
--------------------

isNull x
    = if length  x ==0 then True
        else False
myfnc x
    | null x  = []                     
    | otherwise = tail x


Function can be written in 2 different way a. guard b. pattern match
==========================================

1. GUARD
=======

useGuard x y
    | x/y > 1  = "less than"
    | x/y == 1 = "equal"
    | otherwise = "Greater than"



2. PATTERN
=============
usePattern x y
    = if x > y then "Greater"
        else if x == y then "Equal"
            else "Less"



WHERE
=======
useWhere x y
    | tmp < 1  = "less than"
    | tmp == 1 = "equal"
    | otherwise = "Greater than"
    where tmp = x/y

 findPos c x
    = [ b |    (a,b) <- a="=" br="" c="" n="" nbsp="" x="" zip="">        where  n = length x

Indentation under "where" matters like python


PATTERN VS GUARD
================

msumPattern :: Int - > Int
msumPattern n
    = if n ==0 then 0 else ( n + msumPattern (n-1) )


msumGuard :: Int - > Int
msumGuard n
    | n ==0 = 0
    | otherwise = ( n + msumGuard (n-1) )


RECUSION
========

1. To call recursive function, in each line function should be defined seperately
for each type of parameter.

# for qsort parameter type can vary null, or list, so two line

q2Sort []     = []
q2Sort (x:xs) = q2Sort ys ++ [x] ++ q2Sort zs
           where
              ys = [a | a <- a="" br="" x="" xs="">              zs = [b | b <- b="" xs="">  x]

# for replication parameter type always fixed, so one line

myReplicate n x
    | n <= 0    = []
    | otherwise = x: ( myReplicate (n-1) x   )



ANONYMOUS CLASS / LAMDA
========================
Both of these are same

    map (+3) [1,6,3,2]


\ (parameters) ->
           function definition

-- take 1 parameter
    map (\ y -> y+3) [1,6,3,2]
-- take 2 parameter
    map (\(y ,x) -> x+y+3) [ (1, 5) , (7,8) ]




-- sumAllFold [3,7,1]
sumAllFold x
    = foldr (acc curHead -> acc + curHead) 0 x


-- findElemFold 5 [1,3,7,5,6]
findElemFoldl n y
    = foldl (\acc curHead -> if curHead == n then True else acc) False y

findElemFoldr n y
    = foldl (\curHead acc -> if curHead == n then True else acc) False y




if - else
===============
MUST BE 3 PART if - else - then

usePattern x y
    = if x > y then "Greater"
        else if x == y then "Equal"
            else "Less"


For loop
===========

for i=1 to 5
   for j=1 to 3

 [ (x,y) |  x < - [1..4] , y < - [1..3] ]


for i=1 to 5
   for j=i+1 to 3

 [ (x,y) |  x < - [1..4] , y < - [x+1 ..3] ]



    INTERACTIVE Program
====================



getChar   - putChar
getLine    - putStr  - putStrLn

To use multiple action use do , do is indentation sensitive

actMyGetline :: IO String
actMyGetline
    = do   
        a <- getChar
        if a=='\n'   
            then
                return []
            else
                do
                    as <- actMyGetline
                    return (a:as)

show converts anything to string like toString()

actStrLen
    = do
        putStr "enter string:"
        x <- getLine
        putStr x
        putChar '\n'
        putStr ("len is:" ++ show (  length x ) )


Types

1.  RENATE EXISTING TYPE : type
2.  CREATE NEW TYPE LIKE STRUCTURE IN C/C++ : data


data BookInfo = Book Int String [String]        deriving (Show)
data MagazineInfo = Magazine Int String [String]    deriving (Show)

myInfo = Book 9780135072455 "Algebra of Programming"          ["Richard Bird", "Oege de Moor"]

weapons =  Book 173 "Use of Weapons" ["Iain M. Banks"]


IO
=========

() is an empty tuple (pronounced “unit”), indicating that there is no return value from putStrLn

getLine  ,  putStrLn
getChar ,  putChar


name2reply :: String - > String
name2reply name =
    "Pleased to meet you, " ++ name ++ ".\n" ++
    "Your name contains " ++ charcount ++ " characters."
    where charcount = show (length name)

main :: IO ()
main = do
       putStrLn "Greetings once again.  What is your name?"
       inpStr <- getLine
       let outStr = name2reply inpStr
       putStrLn outStr
 
 


Wednesday, February 13, 2013

Ghostscript pdf merge split



1. Cut range of page/ split  pages from pdf
=========================

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=3 -dLastPage=7 -sOutputFile=out.pdf   in.pdf


2. Merge multiple pdfs
================

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=out.pdf in1.pdf in2.pdf

Sunday, January 27, 2013

p value bonferroni correction



http://www.aaos.org/news/aaosnow/apr12/research7.asp

For example, a researcher is testing 20 hypotheses simultaneously, with a critical P value of 0.05. In this case, the following would be true:
  • P (at least one significant result) = 1 – P (no significant results)
  • P (at least one significant result) = 1 – (1-0.05)20
  • P (at least one significant result) = 0.64
Thus, performing 20 tests on a data set yields a 64 percent chance of identifying at least one significant result, even if all of the tests are actually not significant. Therefore, while a given α may be appropriate for each individual comparison, it may not be appropriate for the set of all comparisons.


http://www.fon.hum.uva.nl/praat/manual/Bonferroni_correction.html


In general, if we have k independent significance tests at the α level, the probability p that we will get no significant differences in all these tests is simply the product of the individual probabilities: (1 - α)k. For example, with α = 0.05 and k = 10 we get p = 0.9510 = 0.60. This means, however, we now have a 40% chance that one of these 10 tests will turn out significant, despite each individual test only being at the 5% level. In order to guarantee that the overall significance test is still at the α level, we have to adapt the significance level α′ of the individual test.
This results in the following relation between the overall and the individual significance level:
(1 - α′)k = 1 - α.
This equation can easily be solved for α′:
α′ = 1 - (1-α)1/k,
which for small α reduces to:
α′ = α / k
This is a very simple recipe: If you want an overall significance level α and you perform k individual tests, simply divide α by k to obtain the significance level for the individual tests.

Thursday, January 24, 2013

linux basic system admin tutorial from IBM


install software pre-compiled or from source
-----------------------------------------------------------------------

http://www.ibm.com/developerworks/linux/library/l-roadmap9/


Managing shared library
-----------------------------------------------
http://www.ibm.com/developerworks/linux/library/l-lpic1-v3-102-3/index.html

Basis System admin
-------------------------------------

http://www.ibm.com/developerworks/training/kp/l-kp-command/index.html

http://www.ibm.com/developerworks/linux/tutorials/l-basics/

All type of  work for System admin in linux
-------------------------------------------------------------------

http://www.ibm.com/developerworks/linux/library/l-lpic1-v3-map/index.html


all type of linux problem solution
---------------------------------------------------------------

http://www.ibm.com/developerworks/linux/library

Wednesday, January 23, 2013

C/C++ basic tutorial



Memory allocation stack vs heap
------------------------------------------------------

http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/

Wednesday, January 2, 2013

Ghost script manipulate pdf files


1. Merger pdf files
==================

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=combinedpdf.pdf -dBATCH 1.pdf 2.pdf 3.pdf