Monday, May 21, 2012

Vertical xtick Labels in Matlab graphs

This was taken from the mailing list entry available at: https://mailman.cae.wisc.edu/pipermail/help-octave/2009-July/036066.html
--------------------------------

here is a script that illustrates how to rotate the xtick label and put them vertically.

clear
## init demo plot
clf
plot(1:4);
xtick=[1 2 3 4];
set(gca,'xtick',xtick);
xticklabel=["a";"b";"c";"d"];
set(gca,'xticklabel',xticklabel);

## get position of current xtick labels
h = get(gca,'xlabel');
xlabelstring = get(h,'string');
xlabelposition = get(h,'position');

## construct position of new xtick labels
yposition = xlabelposition(2);
yposition = repmat(yposition,length(xtick),1);

## disable current xtick labels
set(gca,'xtick',[]);

## set up new xtick labels and rotate
hnew = text(xtick, yposition, xticklabel);
set(hnew,'rotation',90,'horizontalalignment','right');

Saturday, May 19, 2012

Xor Range without LOOP

I am lazy, and I wont be able to explain it more clearly than him. Hence, I lifted the content directly from there, to have all the coding tricks in my blog for my reference. 
----------------------
HOW to get Xor between numbers from 1 to 300000000
you will get TLE if you try to use Loop for all numbers
Question was asked on the codeforces site. Click Here to see the discussion 

jacob answer
Let's introduce
 f(x) = x ^ (x-1) ^ (x-2) ^ ... ^ 1
Then anwer would be  
f(end) ^ f(start - 1)


Let's now learn to calculate f(x) fast enough. First, notice that f(4*k - 1) = 0 (provable by induction). So to calculate f(x) you only need to take at most 4 last numbers and xor them.
Finally,
f(4*k) = 4*k
f(4*k + 1) = 1
f(4*k + 2) = 4*k + 3
f(4*k + 3) = 0 


Expressions "at most 4 last numbers" and "only last 4 numbers" are different. In fact, you need to take exactly (x+1)%4 last numbers.

Conclusion:Use this function 


int f(int n){  
switch(n%4){  
case 0: return n;  
case 1: return 1;  
case 2: return n+1;  
default: return 0;  
}  
}  


Saturday, April 7, 2012

Iterative, recursive and heap algorithms to generate permutations

A very good article on iterative and recursive algorithms for generating permutations of an array is here:
http://www.cut-the-knot.org/do_you_know/AllPerm.shtml

I shall shamelessly lift the algorithms listed in the page, for my reference here in the blog.
(Just for reference, another good algorithm is described at http://introcs.cs.princeton.edu/java/23recursion/Permutations.java.html)
Recursive algorithm for Permutations
It's executed with a call visit(0). Global variable level is initialized to -1 whereas every entry of the array Value is initialized to 0.
  void visit(int k)
  {
    level = level+1; Value[k] = level;    // = is assignment
    if (level == N)  // == is comparison
      AddItem();     // to the list box
    else
      for (int i = 0; i < N; i++)
        if (Value[i] == 0)
          visit(i);
    
    level = level-1; Value[k] = 0;
  }

  void AddItem()
  {
  // Form a string from Value[0], Value[1], ... Value[N-1].
  // At this point, this array contains one complete permutation.
  // The string is added to the list box for display.
  // The function, as such, is inessential to the algorithms.
  }

Lexigraphic order to find the next permutation
Permutation f precedes a permutation g in the lexicographic (alphabetic) order iff for the minimum value of k such that f(k)≠ g(k), we have f(k) < g(k). Starting with the identical permutation f(i) = i for all i, the second algorithm generates sequentially permutaions in the lexicographic order. The algorithm is described in [Dijkstra, p. 71].
  
  void getNext()
  {
    int i = N - 1;
    while (Value[i-1] >= Value[i]) 
      i = i-1;

    int j = N;
    while (Value[j-1] <= Value[i-1]) 
      j = j-1;
  
    swap(i-1, j-1);    // swap values at positions (i-1) and (j-1)

    i++; j = N;
    while (i < j)
    {
      swap(i-1, j-1);
      i++;
      j--;
    }
  }
B. Heap's algorithm 
Heap's short and elegant algorithm is implemented as a recursive method HeapPermute [Levitin, p. 179]. It is invoked with HeapPermute(N).
  
void HeapPermute(int n)
{
  if (n == 1) 
    AddItem();
  else {
    for (int i = 0; i < n; i++) 
      HeapPermute(n-1);
      if (n % 2 == 1)  // if n is odd
        swap(0, n-1);
      else            // if n is even
        swap(i, n-1);
}

Monday, March 12, 2012

MongoDB and Python

For my project I had to store and operate on a large amount of data. I was mostly using dictionaries and lists in Python. Since the storing entire dictionary/list in memory was impossible, I was looking for alternatives. The obvious fall back option was to use disk storage. Persistent storage databases like MongoDB allow directly storing python objects on disk.

To install MongoDB on windows, I followed the quick tutorial provided on the MongoDB site: http://www.mongodb.org/display/DOCS/Quickstart+Windows. Don't forget to check the --dbpath directive while running mongod.exe, as it helps to specify the database path.

Being new to MongoDB, and having prior experience with SQL, it is good to know how the two approaches differ. The tutorial provided at http://www.mongodb.org/display/DOCS/Tutorial, gave a very good overview of how MongoDB works.

To setup Python to interact with MongoDB, follow the instructions here: http://www.mongodb.org/display/DOCS/Python+Language+Center

To learn about setting some fields unique (apart from the _id field), and to create indexes to boost the search latency, read tutorials here http://www.mongodb.org/display/DOCS/Indexes#Indexes-unique%3Atrue.

The book titled "MongoDB and Python:Patterns and Processes for the Popular Document-Oriented Database" provided all the needed python commands to interact with the database. It was very helpful.

Tuesday, December 13, 2011

Setting up HAVP (with ClamAV) + Squid to work as Secure Proxy

There are two ways in which HAVP can be made to interact with Squid (obtained from http://kokikode.wordpress.com/2010/03/14/configuring-squid-havpclamav-in-ubuntu-reviews/).
1. First way is to have the HAVP as Parent proxy for squid.
               Transparent Proxy*
               192.168.0.253:3128
                      ||                    [eth1]
                      ||                      ||
 [Intranet]--------[Squid]-+-[HAVP]--------[Internet]
     ||                        ||
   [eth0]                  Parent Proxy
192.168.0.0/24            127.0.0.1:8080
                               ||
                               ||
                            [ClamAV]
2. Second is to make Squid Proxy as the Parent proxy for HAVP.
                 [ClamAV]
                     ||
                     ||
               Transparent Proxy*
               192.168.0.253:8080
                     ||                     [eth1]
                     ||                       ||
 [Intranet]--------[HAVP]-+-[Squid]--------[Internet]
     ||                        ||
   [eth0]                  Parent Proxy
192.168.0.0/24            127.0.0.1:3128

There are advantages and disadvantages for each (as described in http://kokikode.wordpress.com/2010/03/14/configuring-squid-havpclamav-in-ubuntu-reviews/)

I prefer Method-1, because the proxy only caches the content deemed safe.

Creating Method-1 Setup:
STEP1: Install Squid Proxy
The steps are outlined in my previous post.
STEP2: Install ClamAV.
I tried installing ClamAV using the command:
sudo apt-get install clamav
When performing signature update using
sudo freshclam
I obtained some warning saying ClamAV engine is out of date. So I decided to install ClamAV from the source code. The latest stable source code can be obtained from http://www.clamav.net/lang/en/download/sources/.

Before installing ClamAV, a new user and groupid needs to be created. The default userid and groupid are "clamav". They can be added using the commands:
groupadd clamav
useradd -g clamav clamav
Untar the downloaded source code using the command:
tar -xzvf clamav-0.97.3.tar.gz

Move into the newly created directory and try to perform "configure" with any needed options (No need to specify any options unless userid and groupid are different from default, clamav-milter needs to be installed or new experimental features need to be added). Before we actually install, it would be good to check for previous versions of clamav and remove them. The commands to install are:
cd clamav-0.97.3
./configure
make
sudo apt-get remove --purge clamav
sudo make uninstall
sudo make install

Before updating clamav, it is good to edit the clamd.conf freshclam.conf files located in /usr/local/etc folder. One line containing "Example" needs to be commented and any other entries need to be uncommented as per need. The config files are self explanatory. However, one issue could arise. The log files and signature directories used by freshclam and clamd, specified in the .conf files, might not have required file permissions to be created/edited. You can manually create the file and folder and edit their permissions using "chmod/chown" command.

Update the antivirus signatures and try scanning a simple file.
sudo freshclam
clamscan -v filename

STEP3: Install HAVP
The instructions given in http://kokikode.wordpress.com/2010/03/04/configuring-squid-havpclamav-in-ubuntu-example-1/ provide some guidance for the installation.

This command was installing an older version of HAVP.
sudo apt-get install havp

Hence, get the latest source code of HAVP from http://www.server-side.de/download.htm.

Install HAVP using the regular commands given below. They also add required userid and groupid "havp", allowing me to skip adding them manually.
tar -xvzf havp-0.92a.tar.gz
cd havp-0.92a
./configure
make
sudo make install

Add the below line in /usr/local/squid/etc/squid.conf
cache_peer 127.0.0.1 parent 8080 0 no-query no-digest no-netdb-exchange default

Next havp.config needs to be edited. This config file is located in /usr/local/etc/havp/havp.config. Most of the entries are self explanatory in the config file, however the configuration provided in http://kokikode.wordpress.com/2010/03/04/configuring-squid-havpclamav-in-ubuntu-example-1/ can be used as a reference.

Two major steps need to be performed before successfully running HAVP. The first one is set to set appropriate permissions for the files used by HAVP. In my case, I enumerated the files and folders needing permission change by looking at the error messages. The INSTALL file provided in the extracted havp folder (havp-0.92a) provides the following commands.
Make sure the directories you are using have correct permissions:

  # chown havp /var/tmp/havp /var/log/havp /var/run/havp
  # chmod 700 /var/tmp/havp /var/log/havp /var/run/havp

The second major step is to ensure the scan file system has mandatory locks enabled. The INSTALL file indicates how this could be done. The file /etc/fstab indicates the file systems which are mounted when booting. In my case I just had the root file system (/). So, as indicated in the INSTALL file, I used the following command to activate mandatory locks.
mount -o remount,mand /
The "mand" option could be similarly added to any other file system used by HAVP for scanning (maybe the file system specified for SCANTEMPFILE in havp.config?). To make the "mand" change permanent, we need to add the "mand" option in the fstab file, for the mount root file system entry. The entry in my case looks like:
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/host/ubuntu/disks/root.disk /               ext4    loop,mand,errors=remount-ro 0       1

We can then start havp using the command:
/usr/local/sbin/havp
We could also start HAVP as user "havp" using the command:
sudo -u havp /usr/local/sbin/havp

STEP 4: Testing the setup.
Try configuring the browser to the squid proxy ip and port 3128, and try browsing few webpages. To test if HAVP is working properly, try visiting this link http://www.eicar.org/download/eicarcom2.zip. It should through a HAVP-ACCESS DENIED PAGE (shown below).

References:
1. http://volatile-minds.blogspot.com/2008/06/installing-clamav-latest-from-source.html
2. http://wiki.clamav.net/bin/view/Main/InstallFromSource
3. http://informatix.or.id/willy/installing-clamav-from-source.php

Friday, December 9, 2011

Setting up Squid Proxy server

Squid could be installed from the Packet Manager, like Synaptic, on Ubuntu. However, most of the times, the latest squid version won't be available from packet managers. We can download the latest source code from http://www.squid-cache.org/Versions/.

I followed the instructions given here: http://www.technologytricks.com/install-squid-proxy-server/ to install Squid. While initializing the cache using the command:
/usr/local/squid/sbin/squid -z

I received the following error:
WARNING: Cannot write log file: /usr/local/squid/var/logs/cache.log/usr/local/squid/var/logs/cache.log: Permission denied

Following instructions given here:http://vonroeschlaub.com/kurt/server.html, I modified the permissions for the folder /usr/local/squid/var/logs, and all its contents using the commands
user@ubuntu:/usr/local/squid/var$ sudo chmod a+w logs
user@ubuntu:/usr/local/squid/var/logs$ sudo chmod a+w * 

You can then test Squid in debugging mode using the command:
sudo /usr/local/squid/sbin/squid -NCd1

"-NCd1" is used for starting it in debugging mode to look for any further errors. I had to use "sudo", because it threw an error as shown below:
2011/12/09 15:44:18| Accepting  HTTP connections at [::]:3128, FD 12.
2011/12/09 15:44:18| HTCP Disabled.
2011/12/09 15:44:18| /usr/local/squid/var/run/squid.pid: (13) Permission denied
FATAL: Could not write pid file
Aborted

Using "sudo" should make it run successfully in debug mode, like below:
2011/12/09 15:45:29| Accepting  HTTP connections at [::]:3128, FD 12.
2011/12/09 15:45:29| HTCP Disabled.
2011/12/09 15:45:29| Squid plugin modules loaded: 0
2011/12/09 15:45:29| Ready to serve requests.

You can then verify if squid is working from the browser. Just go to the connection settings and fill in the IP address of your machine and port 3128 for the proxy and try to browse a url. After browsing few urls, check the access logs in squid/var/logs folder to verify that squid did indeed see the url requests. If you are accessing the proxy from a subnet other than the one listed on your squid.conf file, you should get an "ACCESS DENIED" page. To allow proper browsing, probably the squid.conf needs to be edited further and squid needs to be restarted to make use of the updated configuration file.

Saturday, November 12, 2011

Resetting wireless interface in Ubuntu

Most often in Ubuntu, the wireless stops working. Either it shows
Wireless Networks - Disconnected
and does not display any wireless connections available, or the wireless connection speed slows down drastically and resets quite frequently. This happens when the laptop wakes from a sleep or I try to unlock the screen after a brief period of inactivity.

One way to get out of this situation without rebooting is to restart the wireless lan interface (wlan0) using the commands:
sudo ifdown wlan0
sudo ifup wlan0

(or)

sudo ifconfig wlan0 down
sudo ifconfig wlan0 up

Reference:
1. http://askubuntu.com/questions/33818/lost-wireless-connection-and-detection