Matrix manipulation in scheme

Here’s the code I wrote for matrix manipulation in scheme. It use lists.

Procedure that creates new square identity matrix:

(define (make-matrix n)
  (let outter ((i n) (result '()))
    (if (= i 0)
        result
        (outter (- i 1) 
                (cons 
                 (let inner ((j n) (row '()))
                   (if (= j 0)
                       row
                       (inner (- j 1) (cons (if (= i j) 1 0) row))))
                 result)))))

Procedure that return nth element of the list, which is the same as nth row of the matrix:

(define (nth list n)
  (let iter ((n n) (result list))
    (if (= n 0)
        (car result)
        (iter (- n 1)
              (cdr result)))))

(define matrix-row nth)

Procedure that return nth column of the matrix:

(define (matrix-col M n)
  (let iter ((i (length M)) (result '()))
    (if (= i 0)
        result
        (iter (- i 1)
              (cons (nth (nth M (- i 1)) n) result)))))

Procedure for multiplication of two matrices:

(define (matrix-mul N M)
  (let rows ((i (length N)) (result '()))
    (if (= i 0)
        result
        (rows (- i 1)
              (cons
               (let cols ((j (length (car M))) (row '()))
                 (if (= j 0)
                     row
                     (cols
                      (- j 1)
                      (cons (reduce + (map *
                                           (matrix-row N (- i 1))
                                           (matrix-col M (- j 1))))
                            row))))
               result)))))

For above procedure you will need reduce procedure:

(define (reduce fun lst)
  (let iter ((result (car lst)) (lst (cdr lst)))
    (if (null? lst)
        result
        (iter (fun result (car lst)) (cdr lst)))))

Procedure for multiplication of vector and matrix:

(define (matrix-vector-mul v M)
  (car (matrix-mul (list v) M)))

Procedure for transpose the matrix:

(define (matrix-transpose M)
  (if (null? (car M))
      '()
      (cons (map car M)
            (matrix-transpose (map cdr M)))))

Tail recursive procedure for transpose the matrix:

(define (matrix-transpose M)
  (let iter ((M M) (result '()))
    (if (null? (car M))
        result
        (iter (map cdr M) (append result (list (map car M)))))))

Procedure that calculate the sum of two matrices:

(define (matrix-sum N M)
  (let iter ((N N) (M M) (result '()))
    (if (or (null? N) (null? M))
        (reverse result)
        (iter (cdr N) 
              (cdr M)
              (cons (map + (car N) (car M)) result)))))

Shorter version of the above:

(define (matrix-sum N M)
  (map (lambda (nrow mrow) (map + nrow mrow)) N M))

Usage:

You can use those procedures like this:

(define M1 '((1 2 3) (2 3 4) (3 2 1)))
(define M2 (make-matrix 3))

(write (matrix-mul M1 M2))
(newline)
(write (matrix-mul M1 '((2 3 1) (1 2 1) (1 3 1))))
(newline)
(write (matrix-sum M1 M2))
(newline)
(write (matrix-vector-mul '(2 3 1) M1)

,

Leave a comment

666 the number of the beast tweets on smashing magazine

I spot number of the beast tweets on smashing magazine article “Desktop Wallpaper Calendar: April 2011″

666 the number of the beast tweets

Leave a comment

Really wicked web application source code hiding

I saw this video on YouTube from DefCon Conference.

I go to samy website, and it’s web application which look like Microsoft Windows. So first think I do to see how it is build. I look at source code and it’s look like there is no code at all, but in the middle (line 281) there is this

<script>/*
No source for you!
*//
</script>

Before and after script tags are only empty lines (\n), I check the end of the line 283 and there is following code (line breaks and indentations are mine):

/.source.replace(/.{7}/g,function(w){document.write(
    String.fromCharCode(parseInt(w.replace(/ /g,'0').
                                 replace(/	/g,'1'),2)))});

And thats it. So where is the real code? And the answer is this:

  1. He encode all characters in html as binary and replace zeros as space and ones as tabulations. there is no string, but in 283 line there is this: "*//" which is the end of multi-line comment and beginning of Regular Expression (which look like simple closing comment)
  2. He get value of the string representation of the RegEx using source field
  3. replace all whitespace with '0' and '1'
  4. convert it to decimal
  5. get the value of characters encoded and print all of that out

Pretty clever.

Of course you can get generated code from a menu “View Generated Source” from WebDeveloper toolbar in Firefox or see the the DOM in Firebug.

, , ,

Leave a comment

jQuery splitter – split container

This is my latest jquery plugin — “splitter” which splits content vertically or horizontally with movable element between them, that allow to change the proportion of two element. You can get it from github. The demo is here.

, ,

21 Comments

Concatenate files with progress bar on GNU/Linux

When I download files form rapidshare or other file hosting sites I occasionally download movies that are splited into parts, and to watch the movie they need to be concatenated.

I came up with this bash function which I put in my .bashrc file:

function concat() {
  files=${@:1:$(($#-1))}
  cat $files | pv -s $(du -cb $files | tail -n1 | cut -d $'\t' -f1) > ${@: -1}
}

If you have files like Movie.avi.001…Movie.avi.020, to concatenate these files just type:

concat Movie.avi.* Movie.avi

And you’ll see progress bar when output file is created.

, ,

Leave a comment

Convert pdf to jpg for free on windows using open source tools

To convert pdf to jpg on windows you can use existing open source tools ImageMagic and GhostScript.

Get and Install latest ImageMagic from:

http://www.imagemagick.org/script/binary-releases.php#windows

Ensure that you install with OLE support (for scripting with VBScript)

Get and install latest GPL version of Ghostcript from sourceforge

http://sourceforge.net/projects/ghostscript/files/GPL%20Ghostscript/

Unfortunately there is no GUI for ImageMagic but you can use script below, just save it in pdf2jpg.vbs – to run the script just drag all pdf files that you want to convert on script icon. The resulting images will have the same name but with jpg extension and they will be placed in the same directory as pdf files.

Set imageMagick = CreateObject("ImageMagickObject.MagickImage.1")
For Each name in Wscript.Arguments
       pdf = Replace(name, ".pdf", ".jpg")
       imageMagick.Convert name, "-resize", 650, "-quality", 80, pdf
next

With ImageMagic you can do lots of cool stuff like create, edit, compose, convert bitmap images, scale, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

For more information see command line and COM+ ImageMagickObject documentations.

Instead of VBScript you can use other languages.

, ,

1 Comment

[Ruby] Wyświetlanie definicji ze “Słownika Wyrazów Obcych Kopalinskiego”

To jest kod skryptu w Ruby do wyświetlania definicji ze “Słownika Wyrazów Obcych Kopalińskiego”. Pobierz plik kopalinski.rb.

Żeby użyć tego skryptu musisz mieć zainstalowanego Ruby-ego.

W terminalu wpisz:

kopalinski.rb Słownik

Skrypt wyświetli definicje słowa “Słownik”. Jeśli w słowniku Kopalińskiego nie ma jakiegoś słowa skrypt nie wyświetla nic.

,

3 Comments

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: