conorjh

Just another WordPress.com site

Archive for the category “Programming”

Using emacs/emerge to merge in mercurial

add what’s below to your .hgrc file


[ui]
merge = emacs

[merge-tools]
emacs.args = -q --eval "(emerge-files-with-ancestor nil \"$local\" \"$other\" \"$base\" \"$output\" nil 'kill-emacs)"

http://mercurial.selenic.com/wiki/MergingWithEmacs

Advertisements

Vectors and constructors with rand()

So here is a silly mistake I just made; say Foo is a class whose constructor includes setting a variable randomly and you want a population of Foos, then

vector foos(n,foo);

isn’t what you want, this only calls the Foo constructor once and then makes n copies of the result, so the random value is the same for each instance! You need

vector foos;
for(int i=0;i<n;++)
{
Foo foo;
foos.push_back(foo);
}

Relational functions, classes and sort in C++

If you are defining your own relational function for use in sort, why do it have
to be a global function, rather than a class method.

Why is it that this works:


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

using namespace std;

bool comp(const int & x,const int & y){return x>y;}
class Foo
{
public:

Foo(vector<int> a){this->a=a;sort(this->a.begin(),this->a.end(),comp);}
void print(){for(unsigned int i=0;i<a.size() ;++i){cout<<a[i]<<" ";}cout<<endl;}
private:
vector<int> a;
};

int main()
{
vector<int> a(3,3);
a[1]=1;
a[2]=2;

Foo foo(a);
foo.print();
}

when what's below doesn't.


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

using namespace std;

class Foo
{
public:
bool comp(const int & x,const int & y){return x>y;}
Foo(vector<int> a){this->a=a;sort(this->a.begin(),this->a.end(),comp);}
void print(){for(unsigned int i=0;i<a.size() ;++i){cout<<a[i]<<" ";}cout<<endl;}
private:
vector<int> a;
};

int main()
{
vector<int> a(3,3);
a[1]=1;
a[2]=2;

Foo foo(a);
foo.print();
}

Another question is, if the objects being compared, the object you have a vector of, are themselves a class, why can't you just overload the "<" operator in the class?

Declaring two pointers.

It’s

Foo * bar1, * bar2;

note the extra star. Makes sense when you think about it.

Code for calculating spike metrics

I have started a project on sourceforge

http://sourceforge.net/p/spikemetrics

to store the c++ spike train metric code that was used for the paper with Thomas Kreutz:

On the efficient calculation of van Rossum distances.

The van Rossum metric measures the distance between two spike trains. Measuring a single van Rossum distance between one pair of spike trains is not a computationally expensive task, however, many applications require a matrix of distances between all the spike trains in a set or the calculation of a multi-neuron distance between two populations of spike trains. Moreover, often these calculations need to be repeated for many different parameter values. An algorithm is presented here to render these calculation less computationally expensive, making the complexity linear in the number of spikes rather than quadratic.

The paper is still under review.

Dev-C++ is back!

I am about to start teaching C++ again so I need an IDE to recommend to students using windows, I was pleased to see Dev-C++ has been updated again at last!

http://sourceforge.net/projects/orwelldevcpp/

gcc optimizations.

Today I spent trying to compare how fast different algorithms for calculating the van Rossum metric run; I ran sample code compiled using the -O3 flag in gcc. I have never known how these optimization settings work, but today I was profiling the code using gprof and was amazed to see how in-line the code is when optimized, the length of the gprof output is determined in part by how many function calls there are, the output is 163 lines with -O3 and 1042 without.

Post Navigation