Feeds:
Posts
Comments

column sums of a matrix

what a weird topic to talk about. i don’t even know if this piece
of code is any good. it will probably serve to remind me of the
usage of certain functions etc at a later date. may be it will
scare somebody off and may be some kind stranger will correct my
mistakes and point me to enlightened realms. all this does is to
compute the column sums of a matrix. notice that we maintain our
distance from the for and while loops. i have been admonished
myriads of time for not using the standard library functions so i
have avoided them here. what can i say i simply love the lambda
functions
over functors. although the compiler ultimately uses
functors behind the scenes but i love how clean and separated the
code looks.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>

int main(){
  // matrix
  std::vector<std::vector<int> > a = {{0,0,0}, {0,0,0}, {0,0,0}};
// c++0x allows initialization lists

  // randomly initialize using lambda expressions
  std::for_each(a.begin(), a.end(), [](std::vector<int>& i){
      std::generate(i.begin(), i.end(), [](){return rand()%10;});
    });

  // display using lamda expressions
  std::for_each(a.begin(), a.end(), [](std::vector<int>& i){
      std::copy(i.begin(), i.end(), std::ostream_iterator<int>(std::cout, "\t"));
      std::cout << std::endl;
    });

  // zero vector
  std::vector<int> zeros = {0,0,0};
  
  // column sums
  std::vector<int> csums = std::accumulate(a.begin(), a.end(), zeros, 
[](std::vector<int>& i, std::vector<int>& j)->std::vector<int>{
std::transform(i.begin(), i.end(), j.begin(), i.begin(), std::plus<int>());
return i;
});

  // display the column sums using ostream_iterators
  std::copy(csums.begin(), csums.end(), std::ostream_iterator<int>(std::cout, "\t"));

  return 0;
}

i now explain bits and parts of the mess

  // randomly initialize
  std::for_each(a.begin(), a.end(), [](std::vector<int>& i){
      std::generate(i.begin(), i.end(), [](){return rand()%10;});
    });

the above initializes the matrix a. since each element of a is itself a vector i use a lambda function that initializes the individual vectors. the lambda is as follows:

[](std::vector<int>& i){std::generate(i.begin(), i.end(), [](){return rand()%10;});}

the actual adding of columns is done using the std::accumulate() library function

  // column sums
  std::vector<int> csums = std::accumulate(a.begin(), a.end(), zeros, 
[](std::vector<int>& i, std::vector<int>& j)->std::vector<int>{
std::transform(i.begin(), i.end(), j.begin(), i.begin(), std::plus<int>());
return i;
});

that uses the lamda

[](std::vector<int>& i, std::vector<int>& j)->std::vector<int>{
std::transform(i.begin(), i.end(), j.begin(), i.begin(), std::plus<int>());
return i;
}

whenever possible one should use the standard library tools provided. std::mismatch() returns a pair of iterators to the first mismatch. examples of this are abundant on the WWW but i wanted to find all the mismatches and could not find an example online, hence this post. i don’t think the code is elegant so please let me know better versions of my code.

#include <iostream>
#include <vector>
#include <algorithm>
 
int main()
{
  std::vector<unsigned> a = {1,2,3,4,5,6};
  std::vector<unsigned> b = {1,2,33,4,55,66};
 
  // find the first mismatch
  auto p = std::mismatch(a.begin(), a.end(), b.begin());

  while(p.first != a.end())
    {
      std::cout << *p.first << std::endl;
      std::cout << *p.second << std::endl;
      p = std::mismatch(++p.first, a.end(), ++p.second);
    }
 
return 0;
}

havent’ tried the above on list container but i believe list iterators support the increment operator.

don’t forget to compile the above using -std=c++0x

guys when i run a code i get a lot of text dump on the console so i m in the habbit of issuing

“tmux clearhist && ./a.out”

so that when i scroll back i only see dump pertaining to only the current execution of a.out . turns out that this interacts weirdly with the standard input buffer. for a simple code such as the follows:

#include <iostream>
#include <string>
int main()
{
  std::string s;
  while(std::getline(std::cin,s))
    {
      std::cout << s << std::endl;
    }
return 0;
}

does not work at all with “tmux clearhist && ./a.out”.

by “does not work” i mean the program does not wait for any input and simply proceeds to completion. at first i thought that probably the standard input buffer has whitespaces or newlines sitting which i needed to clear before doing an input, but that did not work either.

so currently i have resorted to first clearing up history and then running my executable instead of chaining them together as one code. or one can use

tmux clearhist >/dev/null 2>&1 </dev/null && ./a.out

i have always defined matrices of two dimensions as a vector of a vector container as follows

vector< vector <double> > v; // note the distance between the two right angle brakets just before v

however i recently learned that c++0x allows one to get rid of the extra space between the right angle brackets as in the following

vector< vector <double> > v_old_style; 
vector< vector <double>> v_new_style; 

however emacs 23 breaks indentation when faced with this format

i also use 3 and 4 dimensional tensor like objects for which i would much like the following:

vector < vector < vector < vector <double> > > > v1; // don't like it
vector<vector<vector<vector<double>>>> v2; // like this a lot 

for me at least the saving in horizontal space matters when i am comparing code side by side … also its quite easy to figure out the dimensionality of the tensor at one glance.

i don’t know how to fix this :( help me please

[UPDATE] 03Jan2011 : Please note that the following works ONLY because a and b are “sorted” already

say we have two sets

a = [1,2,34]

and

b = [2,4]

and i want to perform the set difference operation:

a = a/b OR a = a-b

that is remove from a the elements that are also present in b.

then is the following c++ solution valid?

 a.erase(set_difference(a.begin(), a.end(), b.begin(), b.end(), a.begin()), a.end());

i noticed that the following

 set_difference(a.begin(), a.end(), b.begin(), b.end(), a.begin());

changed a in to

[1,3,3,4]

and returned an iterator to the 3 to the left of 4. this remined me of the erase-remove idiom hence this post.

is it ok to do this?

i have always been writing my code as such

if(condition)
{
foo();
}
else
{
bar();
}

notice how the curly brace is on the next line.

however today it dawned upon me that this coding style is wasteful of vertical screen estate. an entire line is wasted. compare the above style to the following

if(condition){
foo();}
else{
bar();}

this decidedly looks ugly but may be useful if screen estate is scanty.

there have been some situations where i needed a custom grid on a 2-dimensional plot in gnuplot instead of a regularly placed grid. in order for me to set up a customized grid i have to first set up a customised xtics and/or ytics. gnuplot does provide a way of providing user preferred tics by manually typing them in at the gnuplot command prompt:

gnuplot> set xtics (1,2,3,4)

however that might get too tiresome for your digits. naturally one would want to employ the powerful text processors and stream editors that GNU/Linux provides. this is how i went about it:

assume that the data file(data.out, say) is as follows:

# some comments and blank lines

#      #x1	#x2 	  #x3

1      1.1	3.9	  4.5
2      2.7	6.1	  5.6
3      3.9	2.3	  3.4
       
4      3.5	4.3	  34.0
5      12.1	3.4	  15.9

i want to set the xtics to the values in column 2 and ytics to the values in column 3 say. first i conjure up the following shell script (ttf.sh, say):

#!/usr/bin/env bash

# set tic levels from file at the gnuplot prompt
# examples:
# gnuplot> `./tff.sh data.dat 1 xtics`
# gnuplot> `./tff.sh data.dat 2 ytics`

# dataFile from which tic levels will be read
dataFile=$1

# which column to use
column=$2

# xtics OR ytics 
whichTics=$3

sed -e '/^#/d' $dataFile | \
    sed -e '/^$/d' | \
    cut -f $column | \
    tr '\n' ',' | \
    sed -e "s/^/set $whichTics (/" | \
    sed -e 's/,$/)\n/'

and then in gnuplot carry out the following sequence of commands:

gnuplot> `./tff.sh data.out 2 xtics`
gnuplot> `./tff.sh data.out 3 ytics`
gnuplot> plot 'data.out' u 1:2 w p pt 13 ps 2

to produce the following plot:

it should be noted that some of the grid lines are hidden due to alignment with borders.

the shell script needs 3 inputs

  1. datafile name
  2. which column to pull from the datafile
  3. which tics to set the values to

then the whole command is sandwiched between couple of backtics which bring in the command substitution magic. one could very well do the following in bash:

$ ./tff.sh data.out 2 xtics > tmp

and then in gnuplot

gnuplot> load 'tmp'

but i prefer the backtics.

now the explanation of the wonderful sed magic. first we do a little bit of file cleaning/processing like removing comments and blank lines. these operations may not necessarily apply to your case or might need additional operations which is not that difficult a job. anyways on to the explanations now…

sed -e '/^#/d' $dataFile

simply deletes all lines starting with the # symbol. of course if the comment character is different one should change the code appropriately. ^ stands for the beginning of all lines and ^# means all lines starting with a # symbol. the `d’ command instructs sed to delete those lines. we then pipe (|) the output of this command to the next command

 
sed -e '/^$/d' 

which simply deletes all blank lines. since ^ represents the beginning of all lines and $ represents the end of all lines, ^$ then represents those lines with nothing in between ^ and $ i.e. blank lines. as always the `d’ command instructs sed to delete this line.

cut -f $column

next we cut out the required column from the datafile using the cut command. we assume here that the field separater is a TAB character. if some other field separater is being used that can be specified with the -d switch. do a `man cut` to find out more. now comes the all important part of the entire exercise, what we want is to transform the vertical stack of values:

1
2
3

to a horizontal comma separated list of values:

1,2,3,

notice the comma after 3.
we achieve this by using the tr command which we instruct to transform all the newline characters (`\n’) to the comma character (`,’). we do pick up an unwanted comma at the very end of the list which we get rid of and transformed to a right bracket ) like so:

sed -e 's/,$/)\n/'

this time we are using the subsitute command `s’ and asking sed to replace the last comma by a ). as you already know $ represents the end of the line so (,$) represents the last comma we substitute those with a right bracket ) and a newline character `\n’.
of course we had to prepend the “set xtics (” to the beginning of the list and that bit is done by the following:

sed -e "s/^/set $whichTics (/"

notice the double quotes instead of the single quotes. we use double quotes so that $whichTics is automatically expanded to the user supplied tics name.

Follow

Get every new post delivered to your Inbox.