17 March, 2010

sed oddities under OSX

instead of "sed -i 's/from/to/g' file" as before
use

sed -i "" -e 's/from/to/g' file

04 March, 2010

Crouching Python Hidden Matlab

Slowly creeping towards toward the inevitable public option - however as one user put it (on one of the innumerable online discussion boards, beating this topic to death) - Matlab is annoyingly powerful. And as another user rightly noted - the only way to really get a measure of how good one is versus the other is to try them both out.
There are many important considerations ranging from readability to universality, from availability of libraries and toolboxes to performance.
Concerning the latter aspect, it appears (I might be wrong) that most of speed comparisons are performed on Matlab that uses just in time acceleration (JIT) vs Python that uses something experimental or not at all. There seem to be several ongoing efforts to speed things up: Google is working or something called "Unladen Swallow" in hopes of accelerating most python codes five-fold. There is also Psyco, whom google searches related to python acceleration bring up most of the time. Some are skeptical that Psyco will work with NumPy. Then there is Cython (formerly known as Pyrex) which [seamlessly] fuses C++ with Pyhon, thereby drastically accelerating the slow bits. Not having tried any of these out I am leaning toward Cython. Of course there is also PyCuda, that brings GPU's powers into play and supposedly does wonders to otherwise sluggish codes (which however may be mostly pertinent to problems involving ginormous matrices).
For a summary of sorts scroll look here.

03 March, 2010

nice programs for OSX

  • Apparently you don't have to run the CPU-hogging Activity Monitor in order to keep an eye on system activity in OSX. I just installed istatMenu from iSlayer and iLove it. They have a couple of other goodies there. Comely and and free.

  • SweetFM - a prettier and much more functional alternative to the native Last.FM client. Most important qualities that set it apart from the rest - global shortcuts allow pausing/unpausing/skipping forward etc the songs, "loving" and "banning" them, without having to bring up the interface. And it's much less cumbersome than the native.

21 July, 2009

Single colon

You can use the colon to initialize a member when a method is called. It's usually used to initialize constants in the constructor.

Example:


#include

class object {
const int m_member;
public:
object(const int member);
int getMember();
};

object::object(const int member) : m_member(member) {
}

int object::getMember() {
return m_member;
}

int main(int argc, char *argv[]) {
object o(10);
std::cout << o.getMember() << std::endl;
return 0;
}

typedef

typedef и с чем его едят

typedef
int Apple;
typedef int Orange;
Apple coxes;
Orange jaffa;
...
coxes++;
...
if (jaffa == 10)
...

Static

  • On the difference between static global and static

Static global has file scope and it can't be accessedoutside of the file. While global has program scope and can be accessed outside of file in which it has been defined using extern keyword. A program can be spread across two or more files... so, a global variable can be accessed by all the files.. where as, a static global variable can be accessed only bythe file in which it is declared...static means permanent and hence the value in static global variable is shared by all the functions in that file...

  • Static class variable

    #include
    using namespace std;
    class shared {
    static int a;
    int b;
    public:
    void set(int i, int j) {
    a=i;

    b=j;
    }
    void show();
    } ;
    int shared::a;
    void shared::show()
    {
    cout << "This is static a: " <<>
    cout << "\nThis is non-static b: " <<>
    cout << "\n"; } int main() { shared x, y; x.set(1, 1); x.show(); y.set(2, 2); y.show(); x.show(); return 0; }


Usage and effect of a static data member

  • static variable in a function

#include

int func1(){
static long lfunc=1;

printf("%s : lfunc = %d\n ",__func__,lfunc);

lfunc++;
return 0;
}


int main(){

func1();
func1();
func1();

return 0;
}


Output:
make static && ./static
g++ static.cpp -o static
func1 : lfunc = 1
func1 : lfunc = 2
func1 : lfunc = 3


  • Static methods
A static method defined in a class does not need an object in order to be called.

20 July, 2009

Useful C++ tricks

from http://prokutfaq.byethost15.com

Odd or even?


bool isOdd(int num){
return num&1?true:false;
}


If the number is odd then it's least significant bit (LSB) is set i.e. it is 1. If it is even then it is 0. When you bitwise and (&) this number with 1, the result would be either 1 if the number is odd or zero if it is even.

Passing 2 dimensional array

Example:
/* This program demonstrates passing a 2 dimensional array of size 4 * 4 */

void myFunction(int(*)[4]);

int main()
{
int a[4][4];
myFunction(a);
return 0;
}

void myFunction(int (*myArray)[4])
{
/* Here you can access the content of the argument passed like any other 2-D array */
/* Example: myArray[2][2]=10; */
}


Passing 3 dimensional array

Example:
/* This program demonstrates passing a 3 dimensional array of size 4 * 4 * 4 */

void myFunction(int(*)[4]);

int main()
{
int a[4][4][4];
myFunction(a);
return 0;
}

void myFunction(int (*myArray)[4][4])
{
/* Here you can access the content of the argument passed like any other 3-D array */
/* Example: myArray[2][2][2]=10; */
}

And an inetresting bit about changing particular bits

22 September, 2008

show-paren-mode style

(setq show-paren-style 'expression) in .emacs

And much more from the emacs blog

12 September, 2008

if all else (lsof, fuser) fails

Here is a brute force method to eject a stubborn external drive
% hdiutil eject -force device_name