Skip to content

Making sense of a mess with the kaleidoscope transform

May 1, 2013

One of my daughter’s aunts submitted the first photo for a Springbok Puzzle contest and Springbok has chosen to display it on their Facebook fan page. See the firs picture below.

The second picture is with my favorite tiling transformation applied to the first one. (Made with the image functions of Mathematica.) In mine opinion the transformed image emphasizes the qualities of the first image since it brings some order and sense to the mess.




Caterpillar writing of the kanji “butterfly”

March 12, 2013

A resent discussion over the quote

Just when the caterpillar thought the world was over, it turned into a butterfly.

reminded me of a haiku by Bashō

kochoo ni mo narade aki furu namushi kana

The translation to English I like the most is

Deep into autumn
and this caterpillar
still not a butterfly

Remembering this haiku gave me the idea to write the kanji for “butterfly” with the caterpillar pen within the iPad app Sumo Paint.


I even wrote — or more precisely drew — the original haiku in Japanese using the iPad app Zen Brush. (Well the result is not that great, but still I wanted to share the idea …)


Below are some variations of the above images. First, thicker caterpillars:


Second, a Percolator-ed version of the haiku writing with Zen Brush:

Brewed in Percolator

Markov Chains and N-Grams in Mathematica

November 8, 2012

While reading the book “Programming in Lua” I reached the chapter where it is described an implementation of a random text generation algorithm using Markov chains and N-grams. I implemented a similar algorithm 10 years ago. My first encounter with this algorithm was when I was a teenager — I read a description of it in Martin Gardner‘s column in the Russian translation of “Scientific American.”
Instead of digging out that old implementation, I decided to write a new one from scratch in Mathematica and that turned out to be surprisingly easy.
The easiness came from the available implementations of multi-dimensional sparse arrays (SparseArray) and weighted random sampling (RandomSample). The code I wrote can be run with N-grams of any N; the implementation in the book “Programming in Lua” 2nd ed. is for 2-grams — see Chapter 10.2 of the book.

Below are given screenshots of texts generated by the algorithm using Darwin’s “The Origin of Species.” (Also readily available within Mathematica.) From them you can see that the 5-gram generated text makes more sense than the 2-gram one. All 4 randomly generated texts start from the same place in the book.

Here is the Mathematica code:

(* The argument numberOfPreviousWords is for the size of the N-grams. *)
(* The argument numberOfWords is for the size of the randomly generated text. *)
Options[MakeRandomText] = {"StartNGram" -> Automatic};
MakeRandomText[text_String, numberOfPreviousWords_Integer, 
   numberOfWords_Integer, opts : OptionsPattern[]] :=
  Module[{textWords, words, PickWord, wordToIndexRules, 
     indexToWordRules, ntuples, markovMat, inds, randomTextWords, 
     startNGram, startNGramOpt = OptionValue["StartNGram"]},
    textWords = StringSplit[text];
    words = Union[textWords];
    wordToIndexRules = Dispatch[Thread[words -> Range[Length[words]]]];
    indexToWordRules = Dispatch[Thread[Range[Length[words]] -> words]];
    PickWord[inds_] := 
     RandomSample[Normal[markovMat[[Sequence @@ inds]]] -> words, 1][[1]];
    PickWord[ss : {_String ..}] := PickWord[ss /. wordToIndexRules];
    ntuples = Partition[textWords, numberOfPreviousWords + 1, 1]; 
    markovMat = 
      Table[Length[words], {numberOfPreviousWords + 1}]];
     inds = Apply[Sequence, t /. wordToIndexRules];
     markovMat[[inds]] = markovMat[[inds]] + 1,
     {t, ntuples}];
    startNGram = 
     If[NumberQ[startNGramOpt] && startNGramOpt <= Length[ntuples], 
      ntuples[[startNGramOpt]], RandomSample[ntuples, 1][[1]]];
    randomTextWords = 
     Nest[Append[#, PickWord[Take[#, -numberOfPreviousWords]]] &, 
      Most@startNGram, numberOfWords];
    StringJoin @@ Riffle[randomTextWords, " "]
    ] /; 2 <= numberOfPreviousWords <= 5;

My first entry for the One-liner competition at WTC 2012

October 23, 2012

My first entry for the one-liner competition at the Wolfram Technology Conference 2012 makes wallpaper autostereograms. (See
Using the correct pupil focus you should see six black balls in front of a black wall, six red balls in front of a black wall with circular holes, and a red wall with circular holes in front of five black balls/disks. When you enter the gallery if you focus correctly on one of the images you will see the rest without refocusing.

You can also try to focus on the slide show. (I can focus easily on the slideshow, but this might be because I know what I am going to see and because of my high resolution screen…) Because the slideshow images are smaller the number of perceived balls / disks increases with 1. For example, apparent seven balls are seen on the first image.

This slideshow requires JavaScript.


Evaluating several times this code would produce the images with the red balls and the red wall:
{u, v}=Point/@GatherBy[RandomReal[1,{8000,2}],Norm[#-.5]<.33&];

My third entry for the One-liner competition at WTC 2012

October 22, 2012

Here is a screenshot of my third entry for the one-liner competition at the Wolfram Technology Conference 2012. I took the TIGER shapefile data that gives the geometry edges of various geo-objects in the Champaign county, replaced the edges with polygons, and sharpened the result image.

That third entry did not get mentioned in any of the categories of the competition (“dishonorable mention” , “honorable mention” , “winners” ).

Won the second place in the One-liner competition at the Wolfram Technology Conference 2012

October 20, 2012

I was a speaker at the Wolfram Technology Conference 2012. I presented two talks and I also submitted three entries for the One-liner contest. The contest is to write Mathematica code 140 characters long or less that would impress the judges. I won the second place with one of my submissions called “Translating Chinese characters into a folk-like melody”, which should have been named “Translating Chinese characters into a Bulgarian folk-like melody”, but I didn’t want to hint to the judges who is the contestant. (There were only two Bulgarians at the conference.)

Here are the two character sequences I used


in order to produce the main voice in the this composition of mine:

Further details follow. I transposed the original melody to start from E not from F because this made the note specification for SoundNote to be shorter. Not short enough, though. So that is why I did the remapping from strings to integers — SoundNote can take integers as note specifications. With this approach, though, I wasn’t able to specify pauses and chords. So I prolonged one of the notes in the middle of the melody and changed the power chord at the end to be a single note in a lower octave.

Moku Hanga Distortions

August 7, 2012

Here is another take on moku hanga distortions with the iPad app Percolator.