32leaves.net

Playing around with Markov algorithms

This is just something quick I wanted to for about a week now: an interpreter for Markov algorithms. So I wrote two of them, one in Ruby to get the idea how to do this and one in Javascript to have it on the web. Both are not particularly beautiful written but get the job done. Click this neat little link to get to the online version or have a look at the Ruby code (as you can see the production system and input word are “hard coded”). In both versions the production system removes the trailing zeros from the input word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
E = 0x00
P = [
    { "a1" => "1a" },
    { "a0" => "0a" },
    { "0a" => "a" },
    { "a" => [E] },
    { E => "a" }
]
I = "0101101001110110000"


word = I
while true do
    ruleToRun = nil
    P.each {|rule|
        rule = rule.to_a[0]
        score = word.index(rule[0])
        if (!score.nil? || rule[0] == E) && ruleToRun.nil?
            ruleToRun = rule       
        end
    }
   
    break if ruleToRun.nil?
    shouldStopAfterThisRule = ruleToRun[1].is_a? Array
    ruleToRun[1] = ruleToRun[1][0] if shouldStopAfterThisRule
    oldword = word
    if ruleToRun[0] == E
        word = ruleToRun[1] + word;
    elsif ruleToRun[1] == E
        word = word.sub(ruleToRun[0], "")
    else
        word = word.sub(ruleToRun[0], ruleToRun[1])
    end
    puts "#{ruleToRun[0]} -> #{(shouldStopAfterThisRule ? "." : "")}#{ruleToRun[1].to_s}: #{oldword} => #{word}"
    break if shouldStopAfterThisRule
end
puts word

Live coding in Ruby

After I’ve been to the 25C3 and listened to quite a lot of electronic music there, I wanted to do something with visualization again. And while looking for a cool 3D engine for C++ (like Irrlicht), I stumbled uppon fluxus. But Scheme is really not my programming language (at least not yet :) ), so I wrote something similiar in Java using JOGL and JRuby.

Tonight I got the first visualization running on live data being broadcasted by my iTunes using OSC. Some source code and more teasers will follow. Meanwhile, have a look at the screenshot:

Btw. just to have that meantioned here, JRuby is extremely fast (at least the way I’m using it). The guys who wrote that awesome library did a wonderfull job. The next steps will be most likely add some more features (e.g. make the OSC stuff easier) and start playing arround with the SuperColider.

WikiCircle

Alright, after looking thru my previous projects I stumbled appon some neat little script. It was one of my first experiments with graph searching and visualization. You just have to provide a topic, the script will search it on Wikipedia and try to find the most releated topics. Those topics (and their releationships) are then displayed in what I call a WikiCircle.
Ok, to be honest I mostly developed it because it looks cool and I wanted to play arround with visualization of interconnected data.

WikiCircle output

WikiCircle output

If you want to play around with it, you can download it here. It’s all published unter a Attribution-ShareAlike 3.0 Unported. Ahh, and you need ImageMagick

MimeTex ruby bindings

Did you ever wish you could render Tex code using Ruby? Now you can. Ok, it’s not entirly Ruby, but at least we got some bindings now.

I took the code from MimeTex, modified it a bit and wrote some Ruby bindings for it.
So how can I get this, you might ask?

Just install the GEM that that you can download here, using

1
gem install mimetexrb-1.0.0.gem

and you’re ready to go.

Have a look at the following example on how to use it:

1
2
3
4
5
6
7
8
require 'rubygems'
require 'mimetexrb'

renderer = MimeTex::Render.new(4)  # 4 is the font size
raw_gif = renderer.render('\sqrt{\frac{1}{2}}*\vec{a}')
File.open("output.gif", "a") { |f|
  f.print(raw_gif)
}

This code will produce \sqrt{\frac{1}{2}}*\vec{a}.
In Rails you can use send_data to send the image directly to the user.
Ahhm, just to metion this, the code that does the magic is kinda hackish. I’m really no C programmer.

Hope you enjoy this little gadget. If you have any questions feel free to contact me.

Fork me on GitHub