Wednesday, December 30, 2015

Math formulae:.........

Good Math softwares, websites: Matlab, Mathematica, Maple, Symbolab, CAS, Wolfram
PEMDAS: Parenthesis, Exponential, Multiplication, Division, Addition, Subtraction
BODMAS: Brackets, Orders (powers and roots), Division and Multiplication, Addition and Subtraction
Identities(Additive, Associative,Multiplicative, Distributive, Commutative, polynomial)

(a+b)^2 = a^2 + 2ab + b^2
(a+b)(c+d) = ac + ad + bc + bd
#Difference of squares
a^2 - b^2 = (a+b)(a-b)
#Sum and Difference of Cubes
a^3 (+-) b^3 = (a (+-) b)(a^2(-+) ab + b^2)
#Quadratic Formula
ax^2 + bx + c = 0 then x = ( -b (+-)sqrt(b^2 - 4ac) ) / 2a
------------------------------
########Mensuration########
pi = 3.141
#Circle
Circumference = 2Pi r
Area = Pi r^2

Length of arc
    = theta (in degree)  (Pi/180)  r
    = theta (in radian) r

Area of Sector:
= (theta/360) Pi r^2  
=((theta/(2Pi)) Pi r^2

#Surface Area formulae
Cube = 6a^2
Cylinder = 2pi r^2 + 2pi rh
Sphere = 4 pi r^2

#Volume formulae
cube = a^3
rectangular prism = abc
cylinder = pi r^2h
cone = (1/3)pi r^2h
sphere = (4/3) pi r^3

########Trigonometry########
sin(q) = opposite / hypotenuse = p/h
cos(q) = base / hypotenuse = b/h
sin(90°) = 1
cos(90°) = 0

Language: C#.......

C# (C sharp) is an object-oriented, structured language, as part of .NET initiative
C# is based on C and C++, resembles Java.
Its case-sensitive, ends with ;, execution starts with main method, class name can be different from file name
Online tool
http://www.tutorialspoint.com/compile_csharp_online.php
C# source code file has extension .cs
 IDE: Visual Studio (VS), Visual C# 2010 Express, Visual Web Developer
Compiling (Mono C# compiler)
mcs file.cs -out file.exe
Executing
mono file.exe
using is a keyword
namespace is a collection of classes
class has many methods behavior)
There is one main method.
--------------------------
using System.IO;
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}
Hello, World! 
---------
using System;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
          Console.WriteLine("Hello, World");
         Console.ReadKey();
      }
   }
}
Hello, World 
--------------------------

Language: C (1).........

Extension is .c
gcc -o file.c
c file.c
-------------------------
#Hello World C program (first compile, then execute)
#include <stdio.h>

int main(){
   /* My first program in C */
   printf("Hello, World! \n");
 
   return 0;
}
gcc -o main *.c 
Hello, World!
-------------------------

Language: LISP (1).......

Lisp (LISt Processing) is a very old language (1958).
; for line commenting
script extension is .lisp
CLisp is common lisp.
terpri function outputs a newline to output-stream. (TERminate PRInt line)
...........................................
String printing
(write-line "Autumn leaves make me happy")
(write-line "I love picking them.")
(write-line "His authored a book titled \"Eternal peace.\"")
Autumn leaves make me happy                                                                     
I love picking them                                                                             
His authored a book titled "Eternal peace."
---------------------
Case-sensitive comparison
(write (string= "hi" "Hi"))
(terpri)
(write (string> "hi" "Hi"))
(terpri)
NIL                                                                                                                           
0
---------------------
String case conversion
(write-line (string-upcase "winter is almost here."))
(write-line (string-capitalize "sun sets very early."))
WINTER IS ALMOST HERE.                                                                
Sun Sets Very Early. 

(write-line (string-downcase "PLUMERIA FLOWER IS VERY FRAGRANT."))
(write-line (string-capitalize "seaweeds are algae."))
plumeria flower is very fragrant.                                                     
Seaweeds Are Algae.
---------------------
Stripping the spaces
(write-line (string-trim "   Its a cold night   "))
(write-line (string-left-trim "       Owls are hooting."))
(write-line (string-right-trim "Bats are flying    "))
(write-line (string-trim " "I"    I an scared."))

---------------------
Finding length of string
(write (length "Chaparral flora"))
(terpri)
; pull all characters except the number 1 character
(write-line (subseq "Chaparral" 1))
; pull the character only at index 1
(write (char "Chaparral" 1))
15                                                                                                              
haparral                                                                                                        
#\h                                                                                                             
---------------------
Sorting the strings (ascending, descending order)
(write (sort (vector "milk" "egg" "fruits" "vegetables") #'string<))
(terpri)
(write (sort (vector "milk" "egg" "fruits" "vegetables") #'string<))
(terpri)
#("egg" "fruits" "milk" "vegetables") 
#("vegetables" "milk" "fruits" "egg") 
---------------------
Joining strings (order of strings vary based on < or > symbol used).
(write(merge'vector(vector "tulip" "hibiscus")(vector "rose" "jasmine")#'string<))
(terpri)
(write(merge'vector(vector "tulip" "hibiscus")(vector "rose" "jasmine")#'string>))
#("rose" "jasmine" "tulip" "hibiscus")                                                              
#("tulip" "rose" "jasmine" "hibiscus")  
---------------------
Reversing a string
(write-line (reverse "Calley pear blooms are spectacular."))

Joining strings
(write-line (concatenate 'string "I like hiking." "Nature fascinates me."))
---------------------

Language: Ruby (1).......

Ruby
Script extension is .rb
Used online IDE http://www.tutorialspoint.com/execute_ruby_online.php
--------------------------------
# Hello World
puts "Hello World!";
Hello World!
# The Hello Class
class Hello
 
   def initialize( name )
      @name = name.capitalize
   end

   def salute
      puts "Hello #{@name}!"
   end
end

# Create a new object
h = Hello.new("Ruby")

# Output "Hello Ruby!"
h.salute
Hello Ruby! 
--------------------------------


Language: Scala (1)......

Scala (Scalabale Language) is a Java-based, hybrid language that integrates features of object-oriented and functional languages. Its popular among application developers.
Here, every value is object and every function is a value.
So, every function is an object.
Migration between Scala and java is easy, as they share same run-time platform.
download Scala from http://www.scala-lang.org/downloads
#HelloWorld as script
object HelloWorld {
     def main(args: Array[String]) {
      println("Hello, world!") // prints Hello World
   }
}

#Execution
scalac HelloWorld.scala
scala HelloWorld

Scala has about 50 keywords e.g. val, var
//One line comment
/*Multi-line comment*/

#Package declaration
package com.liftcode.stuff

#Package import
import scala.xml._

#Single class import from a Package
import scala.collection.mutable.HashMap

#Multiple class import from a Package
import scala.collection.immutable.{TreeMap, TreeSet}

#Script Test.scala
object Test {
   def main(args: Array[String]) {
      println("Hello\tWorld\n\n" );
   }


#Execution of the above script
scalac Test.scala   
scala Test  

IT (15): Machine learning...........

#Issues with big data
Capturing data
Curation
Storage
Searching
Sharing
Transfer
Analysis
Presentation

Machine learning: Programming the system to behave in more customized manner with experience. Machine learning is mostly used in the fields of  language processing, forecasting (e.g., stock market trends), pattern recognition, data mining, games, robotics.

Machine learning techniques : recommendation, classification, and clustering (recommendation by search engines, pattern finding, modelling).

Hidden Markov Models (HMMs) model sequential data in many fields such as text/speech processing and signal analysis. 
Support vector machines (SVMs) are supervised learning models with associated learning algorithms that analyze data used for classification and regression analysis.

Mining of big data by regular algorithms poses issues, that's when machine learning comes into picture to better reflect trends. Apache Mahout is used for scalable machine learning algorithms. Parallel run can enable handling of such huge data.
Mahout (driver) framework coupled with Hadoop (elephant) infrastructure can solve the issue. MapReduce clustering implementations : k-means, fuzzy k-means, Canopy...








IT (12): Keyboard Shortcuts...........

#Copy
Ctrl C
#Paste
Ctrl V
#Cut
Ctrl X
#Save
Ctrl S
#Execute
F5
#Suspend a running process and put it in the background (undo an action)
Ctrl Z
#Switching between open apps
Alt Tab
#Closing or exiting the active app
Alt F4

Sunday, December 27, 2015

Language: BASH.....

#Script showing username, date, user_id and current directory
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current directory `pwd`"

#Script giving execution permission and executing
chmod 755 file.sh
./file.sh
#To find reverse of a number
# sh script.sh 3487
#!/bin/bash
if [ $# -ne 1 ]
then
    echo "Usage: $0   number"
    echo "       I will find reverse of given number"
    echo "       For eg. $0 0123, I will print 3210"
    exit 1
fi

n=$1
rev=0
sd=0

while [ $n -gt 0 ]
do
    sd=`expr $n % 10`
    rev=`expr $rev \* 10  + $sd`
    n=`expr $n / 10`
done
    echo  "Reverse number is $rev"

#Pipe the calculated value to bc command
echo 7.56 + 2.453 | bc

# Show value of pi up to 15 digits  (for it pi package must be installed)
pi 15

Saturday, December 26, 2015

IT (8): Spark..................

Spark
Hadoop handles big data and their analysis. However, the data processing is slow due to MapReduce attributes like replication, serialization and disk IO
To speed up the Hadoop-based analysis, Spark was developed. So, Hadoop is just one of the implementation of Spark.
Apache Spark is a very fast cluster computing technology.
Its functions encompass batch applications, iterative algorithms, streaming, interactive queries etc. 
It makes an application fast by storing data in memory and minimizing the number of read/write operations to the disk.
Apart from the ‘Map’ and ‘reduce’, other advantages of Spark include .compatibility to SQL queries, machine learning, streaming data, and graph algorithms etc.
Spark data structure in called RDD (Resilient Distributed Datasets)
Spark can be installed in scala language.
###################################################

IT (3): Data Structure (Runtime complexity and algorithm approaches).......

Data Structure: Systematic organization of data, so as to optimize its usage
Data structure ought to be correct and should have low time complexity (less time usage, which can be achieved by less number of operations) and low space complexity (less memory usage).
The efficiency is a major criteria to tackle searching huge data size, maintain processor speed and cater to numerous user queries simultaneously.
Data structures  implements ADTs (stacks, queues, priority queue, tree, graph)
Interface: Operations (like, traversing, searching, insertion, deletion, sorting, merging)
Implementation: Architecture of data structure
Execution cases: worst, average and best (desirable)
A priori analysis:  theoretical analysis (before experiment)
A posterior analysis:  empirical analysis (after experiment)
Asymptomatic notations: Used to calculate run time complexity of an algorithm
Big Oh (O): Upper bound
Omega: Lower bound
Theta: Both upper and lower bound
Program run complexity can be constant, logarithmic, linear, nlogn, quadratic, cubic, polynomial, exponential 

Greedy approach: Achieves immediate optimization, but expensive for global scale
e.g. Travelling salesman problem, job scheduling problem

Divide and conquer approach: Consists of 3 steps i.e. divide/break, conquer/solve, merge/combine
e.g. Merge sort, Quick sort, Binary search

Dynamic programming approach: Almost like Divide and Conquer, though in stead of merging, memorization occurs
e.g. Fibonacci number series, project scheduling

Greedy algorithm (local optimization is objective) versus dynamic programming algorithm (global optimization is objective)

Built-in data:integer, boolean, float, character, string
Derived data: list, array, stack, queue
Linked List is a chain of data items. Each node of linked list contains an element and a pointer to next node. (types: simple (only forward navigation), doubly (both forward and backward navigation) and circular (last item points towards first item and first points towards last). Its used for insertion, deletion, display, search and remove.
Recursion: When a module or function calls itself
To prevent recursive function to go indefinitely, criteria followed are base criteria and progressive approach (to make the call closer to base criteria).
Stack frame keeps track of the recursion-related parameters.
While iteration and recursion do the same job, the latter is much efficient than the former. But, based on implementation, their time complexity varies. Space complexity of recursion is higher.

ADT: Abstract data type
Common ADT e.g. stacks (piles of things, vertical) and queues (sequence of things, horizontal).
-------------------------------------------------
Stacks(LIFO)
Insert or store: push
Remove or access: pop
Other stack-related functionalities are peek(), isFull(), isEmpty(), which furnishes information about the top element, full and empty status, respectively.
Top pointer indicates the outermost element.
Size can be fixed or dynamic
This ADT is implemented with array, structure, linked list
Common algorithm structure for each function is begin, return and end.
-------------------------------------------------
Queue(FIFO)
Insert or store: enqueue
Remove or access: dequeue
Other stack-related functionalities are peek(), isFull(), isEmpty(), which furnishes information about the top element, full and empty status, respectively.
Top pointer indicates the outermost element
Size can be fixed or dynamic
This ADT is implemented with array, structure, linked list
Common algorithm structure for each function is begin, return and end.
-------------------------------------------------
peek() returns an int
isFull() and isEmpty() return boolean value
-------------------------------------------------
Linear Search
Search engine moves along data sequence till the required element is found.
For each item in the sequence, if item ==value, return index of the item.
Run-time complexity O(n).
-------------------------------------------------
Binary Search
Search engine compares the value with middle element of the sequence.
Array must be sorted.
If value is larger, left half of array is discarded
I value is smaller, right half of array is discarded.
Very fast algorithm based on divide and conquer with run-time complexity O(logn).
#low, mid, high are the index of the elements
mid = low + (high - low) / 2
low = mid + 1

mid = low + (high - low) / 2
*Interpolation search, based on finding the position of required value is an improved form of binary search, with a run-time complexity of O(log(logn)).
#All the search methods return the index of the element.
-------------------------------------------------
Data structures: Array (simple list), record (tuple or struct), Hash table, Binary Tree, Graphs
Data structures can be linear (Array, List) or non-linear (Binary tree, B tree, Heaps, Tree)
Array: Bitmap, Dynamic array, Lookup table, matrix, sorted array
List: Linked list, skip list, doubly linked list
Hash table (hash map): To store data array, where each data element has its unique index. The knowledge of index makes data insertion and search fast. Hashing technique is used to generate the unique indices for the key values. Linear probing is used to find if a duplicate index has been generated. Key operations are search, insertion and deletion, all of which are based on computation of hash code and from it index finding.
Heap: binary heap, Fibonacci heap
Binary tree: binary search tree, AVL tree, red-black tree, scapegoat tree, T tree, splay tree
Sorting algorithms 
Sorting: Arranging data in a particular order (to make data search faster). Tow important aspect is in-place and stability.
Sorting can be in-place (no extra memory needed e.g. bubble sort, insertion sort, selection sort) or not in-place (extra memory needed e.g. merge sort).
Sorting can be stable (does not change sequence of original elements e.g. ) or not stable (does not change sequence of original elements e.g. ).
Bubble sort: Its simple algorithm that relies on swapping a pair of un-ordered numbers. Unsuitable for large data set as iterations number will be too large. Worst case complexity is O(n^2). Its in-place.
Insertion sort: A sorted sub-list is maintained and elements are inserted in proper position in the sub-list.. Unsuitable for large data set as iterations number will be too large. Worst case complexity is O(n^2). Its in-place.
Insertion sort: Array is divided into LHS sorted and RHS unsorted. Smallest element from unsorted part is put at proper position in sorted part. Unsuitable for large data set as iterations number will be too large. Worst case complexity is O(n^2). Its in-place.
--------------------------------
Merge sort: Sorting based on divide and conquer. The array is divided into equal parts (till units), followed by sorting and their combination. Worst case complexity is O(nlogn). Its not in-place.
Shell sort: Its an improved version of insertion sort. It sorts widely spaced elements followed by less widely spaced elements. The intervals are calculated by Knuth's formula. Divide and sort is the approach taken. Worst case complexity is O(n). Its in-place.
Quick sort: The right-most element is made pivot and based on it the array is divided into sub-arrays. Recursive sorting is done. Worst case complexity is O(nlogn). Its in-place.
***********************************
Graphs: Picture where objects are vertices and their connectors are edges
Depth first search (DFS) and Breadth first search (BFS) algorithms traverse the graph in depth and breadth-ward fashion, respectively.
Tree: Picture where nodes are linked by edges.(A root, paths, parent nodes , child nodes, leaf nodes)
One tree can have sub trees and many levels. elation between nodes can be parent-child or siblings.
Visiting: Checking the value of a node
Traversing: Passing through nodes (in order, pre order, post order)
A node has data part and references to its left and right child nodes.
Creation of a tree by insertion of an element and search by traversing.
Binary Tree: The tree-shaped data structure where each node can have at most 2 children.
Its merits include fast search as in ordered array and fast insertion/deletion  as in linked list.
LHS node has value less than parent whereas RHS node has value higher than parent.
element search starts from root (the head node) and depending on element to be searched, LHS or RHS sub tree is explored.

A binary search tree (BST) has worst case complexity is O(n), which is same with linear search. Data in sorted order leads to worst performance as tree becomes unbalanced with LHS and RHS height being stark. 
To address the lacuna with regular BST, other improved BST trees  have been developed.
AVL tree ensures that difference between LHS and RHS is not more than 1. To maintain balance, it performs 4 kinds of rotations (left, right, left-right, right-left).
Heap is a special type of balanced BST where root node key is compared with its children and arranged accordingly.
It can result in Min Heap (where value of root node is less than or equal to either of its children) or Max Heap (where value of root node is greater than or equal to either of its children).
#Tower of Hanoi is algorithm with formula (2^n)-1, where number of discs are n.
#Fibonacci Series is tye algorithm to generate  subsequent number by adding two previous numbers. 
-------------------------------------------
#Using the link http://www.tutorialspoint.com/compile_c_online.php
-------------------------------------------
#Hello World C program (first compile, then execute)
#include <stdio.h>

int main(){
   /* My first program in C */
   printf("Hello, World! \n");
 
   return 0;
}
gcc -o main *.c 
Hello, World!
-------------------------------------------
#Insertion of an element to an array (start; j = n; n = n+1; loop when  j >= k; array[j+1] =array[j]; j = j-1; array[k] =item; stop
#include <stdio.h>
main() {
   int init_array[] = {1,3,9};
   int item = 3, k = 4, n = 3;
   int i = 0, j = n;
 
   printf("Array elements were :\n");
   for(i = 0; i<n; i++) {
      printf("init_array[%d] = %d \n", i, init_array[i]);
   }
   n = n + 1;
   while( j >= k){
      init_array[j+1] = init_array[j];
      j = j - 1;
   }
   init_array[k] = item;
   printf("Updated array elements are:\n");
   for(i = 0; i<n; i++) {
      printf("init_array[%d] = %d \n", i, init_array[i]);
   }
}
Array elements were :                                                                                       
init_array[0] = 1                                                                                           
init_array[1] = 3                                                                                           
init_array[2] = 9                                                                                           
Updated array elements are:                                                                                 
init_array[0] = 1                                                                                           
init_array[1] = 3                                                                                           
init_array[2] = 9                                                                                           
init_array[3] = 4                                                                                           
-------------------------------------------
#Deletion of an element to an array (start; j = k; loop when  j < n; array[j-1] =array[j]; j = j+1; n = n-1; stop
#include <stdio.h>
main() {
   int init_array[] = {5,7,8};
   int k = 1, n = 3;
   int i, j;
 
   printf("The original array elements are :\n");

   for(i = 0; i<n; i++) {
      printf("init_array[%d] = %d \n", i, init_array[i]);
   }
 
   j = k;

   while( j < n){
      init_array[j-1] = init_array[j];
      j = j + 1;
   }

   n = n -1;
 
   printf("The array elements after deletion :\n");
for(i = 0; i<n; i++) {
      printf("init_array[%d] = %d \n", i, init_array[i]);
   }
}
The original array elements are :                                                                
init_array[0] = 5                                                                                
init_array[1] = 7                                                                                
init_array[2] = 8                                                                                
The array elements after deletion :                                                              
init_array[0] = 7                                                                                
init_array[1] = 8                                                                                
-------------------------------------------
#Search of an element to an array (start; j = 0; loop when  j < n; array[j] =item then print j, item; otherwise j = j+1; print j, item; stop
#include <stdio.h>
main() {
   int init_array[] = {1,7,9};
   int item = 7, n = 3;
   int i = 0, j = 0;
   printf("The original array elements are :\n");

   for(i = 0; i<n; i++) {
      printf("init_array[%d] = %d \n", i, init_array[i]);
   }
   while( j < n){
 if( init_array[j] == item ){
         break;
      }
      j = j + 1;
   }
   printf("Found element %d at position %d\n", item, j+1);

}
Original array elements are :                                                                    
init_array[0] = 1                                                                                
init_array[1] = 7                                                                                
init_array[2] = 9                                                                                
Found element 7 at position 2                                                                    
-------------------------------------------
Doubly linked list (implemented in C)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

struct node {
   int data;  int key;
   struct node *next;    struct node *prev;
};

//this link always point to first Link
struct node *head = NULL;

//this link always point to last Link
struct node *last = NULL;
struct node *current = NULL;

//is list empty
bool isEmpty(){
   return head == NULL;
}

int length(){
   int length = 0;
   struct node *current;

   for(current = head; current != NULL; current = current->next){
      length++;
   }
return length;
}

//display the list in from first to last
void displayForward(){

   //start from the beginning
   struct node *ptr = head;

   //navigate till the end of the list
   printf("\n[ ");

   while(ptr != NULL){      
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }

   printf(" ]");
}

//display the list from last to first
void displayBackward(){

   //start from the last
   struct node *ptr = last;

   //navigate till the start of the list
   printf("\n[ ");

   while(ptr != NULL){  

      //print data
      printf("(%d,%d) ",ptr->key,ptr->data);

      //move to next item
      ptr = ptr ->prev;
      printf(" ");
   }

   printf(" ]");
}


//insert link at the first location
void insertFirst(int key, int data){

   //create a link
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;

   if(isEmpty()){
      //make it the last link
      last = link;
   }else {
      //update first prev link
      head->prev = link;
   }

   //point it to old first link
   link->next = head;

   //point first to new first link
   head = link;
}

//insert link at the last location
void insertLast(int key, int data){

   //create a link
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;

   if(isEmpty()){
      //make it the last link
      last = link;
   }else {
      //make link a new last link
      last->next = link;  
      //mark old last node as prev of new link
      link->prev = last;
   }

   //point last to new last node
   last = link;
}

//delete first item
struct node* deleteFirst(){

   //save reference to first link
   struct node *tempLink = head;

   //if only one link
   if(head->next == NULL){
      last = NULL;
   }else {
      head->next->prev = NULL;
   }

   head = head->next;
   //return the deleted link
   return tempLink;
}

//delete link at the last location

struct node* deleteLast(){
   //save reference to last link
   struct node *tempLink = last;

   //if only one link
   if(head->next == NULL){
      head = NULL;
   }else {
      last->prev->next = NULL;
   }

   last = last->prev;

   //return the deleted link
   return tempLink;
}

//delete a link with given key

struct node* delete(int key){

   //start from the first link
   struct node* current = head;
   struct node* previous = NULL;

   //if list is empty
   if(head == NULL){
      return NULL;
   }

   //navigate through list
   while(current->key != key){
      //if it is last node

      if(current->next == NULL){
         return NULL;
      }else {
         //store reference to current link
         previous = current;

         //move to next link
         current = current->next;          
      }
   }

   //found a match, update the link
   if(current == head) {
      //change first to point to next link
      head = head->next;
   }else {
      //bypass the current link
      current->prev->next = current->next;
   }  

   if(current == last){
      //change last to point to prev link
      last = current->prev;
   }else {
      current->next->prev = current->prev;
   }

   return current;
}

bool insertAfter(int key, int newKey, int data){
   //start from the first link
   struct node *current = head;

   //if list is empty
   if(head == NULL){
      return false;
   }

   //navigate through list
   while(current->key != key){

      //if it is last node
      if(current->next == NULL){
         return false;
      }else {        
         //move to next link
         current = current->next;          
      }
   }
   //create a link
   struct node *newLink = (struct node*) malloc(sizeof(struct node));
   newLink->key = key;newLink->data = data;

   if(current == last) {
      newLink->next = NULL; last = newLink;
   }else {
      newLink->next = current->next; current->next->prev = newLink;
   }

   newLink->prev = current; current->next = newLink;
   return true;
}
main() {
   insertFirst(1,10); insertFirst(2,20); insertFirst(3,30);

   printf("\nList (First to Last): "); displayForward();

   printf("\nList (Last to first): "); displayBackward();
 
   printf("\nList , after deleting first record: ");
   deleteFirst();displayForward();

   printf("\nList , after deleting last record: ");
   deleteLast();displayForward();

   printf("\nList , insert after key(2) : ");
   insertAfter(2,5, 11);displayForward();

   printf("\nList  , after delete key(2) : ");
   delete(2);displayForward();
  printf("\n");
}
List (First to Last):                                                                 
[ (3,30) (2,20) (1,10)  ]                                                             
List (Last to first):                                                                 
[ (1,10)  (2,20)  (3,30)   ]                                                          
List , after deleting first record:                                                   
[ (2,20) (1,10)  ]                                                                    
List , after deleting last record:                                                    
[ (2,20)  ]                                                                           
List , insert after key(2) :                                                          
[ (2,20) (2,11)  ]                                                                    
List  , after delete key(2) :                                                         
[ (2,11)  ]                                                                           
--------------------------------------------------

Wednesday, December 23, 2015

Language: R (Examples)......

#Using datasets to find distance matrix
#Applying hierarchical clustering
------------------------------------------- --
d <- dist(as.matrix(mtcars))
hc <- hclust(d)            
plot(hc)


----------------------------------------------
d <- dist(as.matrix(euro))
hc <- hclust(d)              
plot(hc)

----------------------------------------------
d <- dist(as.matrix(Orange))
hc <- hclust(d)              
plot(hc)

----------------------------------------------
#Package for marketing and micro-econometrics applications.
install.packages("bayesm")
library("bayesm")
library(bayesm)
data(rivers)
str(rivers) 

Monday, December 21, 2015

Language: R (Datasets)....

Package: datasets
datasets is an inbuilt package with numerous data sets (A-Z, great for practice.
e.g. airquality, mtcars, trees, rivers, iris, volcano, quakes, states
Moidified from https://stat.ethz.ch/R-manual/R-devel/library/datasets/
Each data frame has certain number of observations and variables
----------------------------------------------
#data frame airquality (154 obs and 6 variables)
require(graphics)
pairs(airquality, panel = panel.smooth, main = "airquality data")

#data frame BOD (6 rows and 2 columns)
require(stats)
# simplest form of fitting a first-order model to these data
fm1 <- nls(demand ~ A*(1-exp(-exp(lrc)*Time)), data = BOD,
   start = c(A = 10, lrc = log(.30)))
coef(fm1)
fm1
# using the plinear algorithm
fm2 <- nls(demand ~ (1-exp(-exp(lrc)*Time)), data = BOD,
   start = c(lrc = log(.30)), algorithm = "plinear", trace = TRUE)
# using a self-starting model
fm3 <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD)
summary(fm3)
#data frame DNase (166 rows and 3 columns)
require(stats); require(graphics)
coplot(density ~ conc | Run, data = DNase,
       show.given = FALSE, type = "b")
coplot(density ~ log(conc) | Run, data = DNase,
       show.given = FALSE, type = "b")
## fit a representative run
fm1 <- nls(density ~ SSlogis( log(conc), Asym, xmid, scal ),
    data = DNase, subset = Run == 1)
## compare with a four-parameter logistic
fm2 <- nls(density ~ SSfpl( log(conc), A, B, xmid, scal ),
    data = DNase, subset = Run == 1)
summary(fm2)
anova(fm1, fm2)
#data frame states (Facts of USA states)
state.abb
state.area
state.center
state.division
state.name
state.region

state.x77
-------------------------------------------------------
Data frames can be very big, in millions of rows and columns and thousands of groups.
data.table, plyr is not an in-built, but additional package to handle such big data.

Language: R (Various plots)...

Plots can be many types:
Density plots (histograms and kernel density plots), dot plots, bar charts (simple, stacked, grouped), line charts, pie charts (simple, annotated, 3D), boxplots (simple, notched, violin plots, bagplots) and scatter plots (simple, with fit lines, scatterplot matrices, high density plots, and 3D plots).
The Advanced Graphs section describes how to customize and annotate graphs, and covers more statistically complex types of graphs.
Customization and annotation of graphs is possible.
#Keywords: col,  h, lty, pch, bg, fg, ann,type, par
par is used to set or query graphical parameters
ann is default annotation
#Colors: green, blue, gray
require(datasets)
require(grDevices); require(graphics)
----------------------------------------------------------
Following snippets are modified form of codes from 
https://stat.ethz.ch/R-manual/R-devel/library/graphics/demo/graphics.R
#Plots random numbers
x <- stats::rnorm(100)
opar <- par(bg = "white")
plot(x, ann = FALSE, type = "n")
abline(h = 0, col = gray(.90))
lines(x, col = "green4", lty = "dotted")
points(x, bg = "green", pch = 21)
title(main = "Plotting",
      xlab = "Labelling",
      col.main = "red", col.lab = yellow(.8),
      cex.main = 1.2, cex.lab = 1.0, font.main = 4, font.lab = 3)


#Plots graph using the dataset package e.g mtcar
require(datasets)
require(graphics)
pairs(mtcars, main = "mtcars data")
coplot(mpg ~ disp | as.factor(cyl), data = mtcars,
       panel = panel.smooth, rows = 1)
#Plots a pie chart
par(bg = "yellow")
pie(rep(1,12), col = rainbow(12), radius = 0.8)
title(main = "Spectrum", cex.main = 1.2, font.main = 4)
title(xlab = "(Raincow colors)",
      cex.lab = 0.8, font.lab = 3)

#Plots a pie chart . The pie used here is a function
cake.bake <- c(0.17, 0.37, 0.26, 0.18, 0.2, 0.12)
names(cake.bake) <- c("Orange", "Apple",
     "Grapes", "Raspberry", "Coconut","Other")
pie(cake.bake,
    col = c("orange","yellow","limegreen","red","white","pink"))
title(main = "Favorite cake bake", cex.main = 1.8, font.main = 1)
title(xlab = "(varieties)", cex.lab = 1, font.lab = 3)
#Creates box plot
par(bg="white")
n <- 15
g <- gl(n, 100, n*100)
x <- rnorm(n*100) + sqrt(as.numeric(g))
boxplot(split(x,g), col="pink", notch=FALSE)
title(main="Box plots", xlab="Clustered", font.main=3, font.lab=1)
#Graphs the area between time-distance plot
par(bg="lightblue")
n <- 100
x <- c(0,cumsum(rnorm(n)))
y <- c(0,cumsum(rnorm(n)))
xx <- c(0:n, n:0)
yy <- c(x, rev(y))
plot(xx, yy, type="n", xlab="Time elapsed", ylab="Distance covered")
polygon(xx, yy, col="yellow")

title("Brownian Movement")

#Trend plot
x <- c(0.5, 0.7, 1.3, 2.6, 3.1, 4.6, 4.9, 5.2, 5.6)
par(bg="pink")
plot(x, type="n", axes=FALSE, ann=FALSE)
usr <- par("usr")
rect(usr[1], usr[3], usr[2], usr[4], col="yellow", border="magenta")
lines(x, col="red")
points(x, pch=21, bg="orange", cex=1.5)
axis(2, col.axis="blue", las=1)
axis(1, at=1:12, lab=month.abb, col.axis="blue")
box()
title(main= "Population boom", font.main=4, col.main="red")
title(xlab= "2015", col.lab="red")

#Graphs the area between histogram
par(bg="skyblue")
x <- rnorm(100)
hist(x, xlim=range(-3, 3, x), col="grey", main="")
title(main="Normal Curve", font.main=2)

#Scatter plot using a data set (here Iris data)
pairs(iris[1:4], main="Iris flower data", font.main=4, pch=19)
pairs(iris[1:4], main=" Iris flower data", pch=21,
       bg = c("red", "green3", "blue")[unclass(iris$Species)])

#Conditioning plot (quakes data)
par(bg="pink")
coplot(lat ~ long | depth, data = quakes, pch = 21, bg = "pur")
par(opar)


#Contour or topography plot (volcano data)
x <- 10*1:nrow(volcano)
y <- 10*1:ncol(volcano)
lev <- pretty(range(volcano), 10)
par(bg = "white")
pin <- par("pin")
xdelta <- diff(range(x))
ydelta <- diff(range(y))
xscale <- pin[1]/xdelta
yscale <- pin[2]/ydelta
scale <- min(xscale, yscale)
xadd <- 0.5*(pin[1]/scale - xdelta)
yadd <- 0.5*(pin[2]/scale - ydelta)
plot(numeric(0), numeric(0),
     xlim = range(x)+c(-1,1)*xadd, ylim = range(y)+c(-1,1)*yadd,
     type = "n", ann = FALSE)
usr <- par("usr")
rect(usr[1], usr[3], usr[2], usr[4], col="yellow")
contour(x, y, volcano, levels = lev, col="red", lty="solid", add=TRUE)
box()
title("A Topographic Map", font= 4)
title(xlab = "Meters North", ylab = "Meters West", font= 3)
mtext("10 Meter Contour Spacing", side=3, line=0.35, outer=FALSE,
      at = mean(par("usr")[1:2]), cex=0.7, font=3)

IT (6): git.......


CVS: Concurrent Versions System
Git is more flexible, reliable than other version control system, though it is more complex as well.
Use of git:
Revision control is needed to modify code and sync, so that labmates can use the latest one. Its less clumsy than mailing or something like that.

Configuration management to easily diagnose and fix bugs

Revision history enables reverting back. It should be clean and followable.
Public branch: Commit message to inform others of the revision done.
Private branch: Push for self-work, mutable
Create a branch off Master
Work
Merge the corrected branch to Master


Working directory to repository: add, commit, delete
Repository to working directory : checkout, update, revert
Bilateral: diff, status, log

After..reset, rebase...........when its clean enough private and public branch should be merged.
-----------------------------------
git add: Saving changes before committing
git branch: Creating another version in the same repository
git checkout: Leaving old commits and navigating among existing branches
git clean: Removing untracked files from an working directory
git clone: Making an copy of existing git repository
git commit: After changes are made, they are shown in history
git commit --amend: The latest commit can be amended
git config: Configuring git as per requirement (needed only one, just after installing Git)
git fetch: It downloads a branch and its related files from a repository
git init:Used to put a project under a repository
git log: Enables visiting revision history
git merge: Opposite of branching as it merges different branches
git pull: Downloads a branch from a remote repository and merges with current branch
git push: Sends a branch to another repository (opposite of fetch)
git rebase: Allows  minimizing merge commits
gite rebase -i: rebasing in interactive mode
git reflog: Keeping track of updates
git remote: For shortcut-based remote connection
git reset: Operating only on tracked files; allows removal of changes that has been pushed to public branch
git revert: It nullifies a commit command
git status: Shows present state of the working directory

*Above 20 git commands to manipulate project and its revision control
*Fetch and pull are similar..
*Opposite of fetch is push
*Opposite of branch is merge
-----------------------------------
In order of use: git init, git clone, git config, 

#Project started at central repository
ssh <user>@<host>
cd  repository
git init --bare my_project.git

#Project copied from central repository to local machine
git clone <repository> <directory>
git clone ssh://Seema@example.com  my_project.git
cd my_project

# Git configuration
git config --global user.name "Seema"
git config --global user.email <email_id>
---------------------
git checkout -b private_banc
touch file
git add file
git commit