Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Friday, 10 May 2013

rc.lua

Looked inside Awesome config, it is funny:
-- Standard awesome library
require("awful")
require("beautiful")

Wednesday, 24 April 2013

Beautify your math code. Part I


I've discovered few typical mistakes that people often do in matlab/gnu octave/python code. Maybe mistake is strong word, not mistakes, but suboptimal usage of environment capabilities (yes, I know mistake sounds better :)). Since I am familiar with python more that mathab, I'll provide few snippets written on python with numpy and scipy libraries, but idea remains the same in matlab also.

Grid functions.


We need setup geometry and some initial data:

import numpy as np
from scitools.numpyutils import meshgrid

h = 0.1
L = H = 1.
x = np.arange(0, L, h)
y = np.arange(0, H, h)
u_analytical = lambda x, y: 2*x + np.exp(y)
 
Ugly way to fill function values on a grid:
u1 = np.zeros( (len(x), len(y) ) )
for i, x_val in enumerate( x ):
    for j, y_val in enumerate( y ):
        u1[i][j] = u_analytical(x_val, y_val)
 
Good way to fill function values on a grid:
(xx, yy) = meshgrid(x, y, sparse=False, indexing='ij')
u1_ = u_analytical(xx, yy) 

Sparse matrices

First you should use sparse matrices, they rock! Second you should carefully choose sparse matrix format. As example why it should be done I'll quote documentation:

Each sparse format has certain advantages and disadvantages. For instance, adding new non-zero entries to a lil_matrix is fast, however changing the sparsity pattern of a csr_matrix requires a significant amount of work. On the other hand, operations such as matrix-vector multiplication and matrix-matrix arithmetic are much faster with csr_matrix than lil_matrix. A good strategy is to construct matrices using one format and then convert them to another that is better suited for efficient computation.
It is not very hard to choose correct sparse matrix format, especially in numerical computations, where in most cases you know all valuable information about your matrix before computation.

The saddest thing is that I saw bad written code in examples given by teachers in their numerical courses.

Thursday, 4 April 2013

Private virtual methods

During review of my patch, reviewer mentioned that  private virtual methods make very little sense.  Then I've asked few more people and all of them have similar opinion that it is better not to use them.

But I think private virtual method make code more clear in following case: if we do not need to  invoke virtual function from derived classes, but only customize  the behaviour, then function can be private.

And we immediately get profit: only by looking on base class definition we can gain information about  class behaviour.

UPD
Something mixed up in my head :) By making virtual function in base class private we only forbid to derived classes call base version of virtual function.

Monday, 31 December 2012

SICP about Lisp

Lisp obeys the convention that every expression has a value. This convention, together with the old reputation of Lisp as an inefficient language, is the source of quip by Alan Perlis (paraphrasing Oscar Wilde) that "Lisp programmers knows the value of everything but the cost of nothing".

From Hal Abelson's, Jerry Sussman's and Julie Sussman's Structure and Interpretation of Computer Programs