Labels

Recent Posts

Tuesday, March 19, 2013

Py Girl 3rd try

Py Girl  impersonate (or perhaps it counts as cosplay) Haruhi Suzumiya.


 Anime style.


Black white manga style.

Blender renderd and edited in gimp


Tuesday, March 12, 2013

Project Euler Problem 198

Original post in Chinese: 2008/06/project-euler-198.html

Project Euler Problem 198 asek you to find all ambiguous numbers between 0~0.01 with denominator no more than 10^8, where by definition, a real number x ambiguous, if there is at least one denominator bound m for which x possesses two best rational approximations. For example, let x=9/40 and m=6, then 1/5 and 1/4 are both best rational approximation of x with denominator no more than 6. Clearly, an ambiguous number is necessarily rational.
This seems to be difficult if you don't have certain number theory background knowledge. On the other hand, if you do have the certain knowledge, you may think 10^8 is kind of too small for a Euler project problem. IIRC, you can find a nice algorithm in Knuth's The Art of Computer Programming.
However, it is pretty straightforward to solve it by recursion using Haskell:

f a b l m | a*b>l || (a==1 && b > m)  = 0
         | otherwise = (f a c l m)+1+(f c b l m) where c=a+b
main = putStrLn $ show result
      where result= (f 1 100 l2 m) + 49+l2-m
      l = 10^8
      l2 = l `div` 2
      m = floor (sqrt (fromIntegral l2)
As easy as 7 lines of code, yet the code is a pretty straightforward translation of the mathematics behind it. However, using Python to solve it is a totally different story. The above recursion exceeds the limit of numbers of function calls in python. The following is a loop based solution:

def p198(M=10**8, z=100, cnt=0):
 M2=M/2
 a=m=int(M2**0.5)
 stack=range(z,m)
 while stack:
     b=stack[-1]
     if a*b>M2:
         a=stack.pop()
     else:
         cnt+=1
         stack.append(a+b)
 return cnt+M2-z/2
The code is directly copy from the code I gave on Project Euler website, so it does not look good.
With the help of psyco, it is fairly fast, but is not very readable by human and can not be considered as a straightforward interpretation of the mathematics behind the problem.
On the contrary, doing some easy tasks in Haskell is not so elegant. For example, suppose we want to find the integer part of the square root of an integer n, and then print it on the screen. Doing this task in python is as simple as you can imagine.
print int(x**0.5).
To do this in Haskell, you will have to convert the integer n to a float point number, then take square root, then take floor, then covert it to an integer, and then convert it to a text before it is ready to print on screen.
putStrLn.show $ (floor . sqrt) (fromIntegral n)
If there are a lot of float point numbers and integers involved, then you probably need to do the coversion every here and there in your code. I know that those conversions exist for a good reason, however, sometimes you just fell like to do a quick and simple computation.
Sure Haskell can make complicate tasks simple and make simple tasks complicate.

Sunday, March 10, 2013

Py Girl, 2nd try

Looks a bit better than the first try, but still not quite there. 
 It is created using blender + freestyle like before. I also use gimp to auto balance the color. The original blender rendered image can be found below.
The basic idea of this image is inspired by Nüwa and Zhao Ling'er, as suggested by Jenny JS Liang.
The background image features a landscape painting of Fan Kuan.
The .blend  file can be downloaded at  Blend Swap.

Blender Renderting Time (On computers available to me)

This a simple test of  of the rendering time of  blender internal render.
I was curious how Xoom or Azure compares to my desktops and laptops. However, running blender in the chrooted ubuntu of  my xoom causes a segment fault ( for both the ubuntu blender and a 2.66 blender compiled by me).

The test .blend file is my Python Girl video.  The video has 800 blender rendered frames (frame 600 - frame 1400). But in this test, only one of every 4 frames between frame 1250 and frame 1346 are rendered. We measure the difference between the modified time of 1250.png and 1346.png.

The blender used in this test is freestyle branch. On windows,  a 32bit executable is used. 64 bit binaries are used on linux and OSX.
The followings are the results:
  • Core i7-2700 Ubuntu 12.10: 19min 16sec
  • Azure extra large 8 core Ubuntu 12.10:  32min 45sec
  • Core 2 Quads Q9500 Win7: 53min 8 sec
  • Macbook Air Core i5-3317u: 60min
  • Win8 Core 2 Duo T5600: about 3 hours (I had a train to catch, so unable to finish the rendering)

The segment fault on  Xoom could be caused by kernel incompatibility or because Xoom does not have enough RAM. But compiling blender on Xoom took like 10 hours while compiling on my Core i7 desktop took only 10-20 minutes.
If possible, it would be interesting to run the same test on an ipad, iphone, EC2,  raspberry Pi, or high end android phone like HTC One, Butterfly, or may padfone infinity, S4.

Wednesday, March 6, 2013

The Life of Py : Girl Py Debut

 

Girl Py or Python Girl is an anthropomorphic character of Python programming language. The original purpose of this character is to promote PyCon Taiwan.
Most tools used to create Girl Py are python related.
The main tool is Blender with freestyle, which is no doubt a python related 3d rendering and modeling tools.
The body of the character is created using Makehuman. Makehuman contains a lot of Python code, in fact, the version I used is the pure python branch. Makehuman is designed for modeling realistic human bodies instead of manga style ones. Fotunately, makehuman support customized skin and target. I made a manga skin and manga target for it. The result of first draft looks like the following.
The hair color is actually black, but the light was too strong, so it looks very pink.
The face generated by my manga target is kind of ugly and scary, but does not look that bad with smooth modifer.
After playing with makehuman and blender awhile, I was getting more familier with these tools and improve the target a bit.
It looks less scary and more smooth now but still ugly.
After adding some manga style material, it looks like the following
Still not good. Because of the time constraint, I decided to model the face by blender directly instead. Using the models list at the end of the video as reference and inspiration, the result is the following:
It looks like this in Blender's opengl view
The python code featured in the video is PyLottery of PyCon Taiwan 2012, which generates an animated snake to fetch random lottery balls.
Other software used in this project are OpenShot and gimp, all python related.
The next image is the credit at the end of the video

PyCon Taiwan 2013 is Calling for proposals, you are welcome to submit your proposals.