Wednesday, November 11, 2009

iTheremin will be on Make: Tokyo Meeting 04

Hi, it is NISHIO Hirokazu. I just made a new promotion movie of my iPhone software: iTheremin. You can control iTheremin without touch.


I and other members of Hacker's Cafe will exhibit on Make: Tokyo Meeting 04. Some belongs to Hacker's Cafe group, others Team Lab group and I Tsuden-Mitoh groups. We are too active to be organized in a shape, ;-)

I'll demonstrate the software on Make: Tokyo Meeting 04. Meet me!

Friday, October 16, 2009

Re: Python vs Clojure

Hi, it is NISHIO Hirokazu, a Python hacker.

In Python vs Clojure – Evolving « Best in Class I found a code:



What a mess!

Why you need icross. In Clojure it was written as (for [x (range 100 1000) y (range 100 1000)] (* x y)). It can be written (x * y for x in range(100, 1000) for y in range(100, 1000)) in Python too!

Why you need digits_from_num. In Clojure it was written as (= (seq s) (reverse s)). It can be written list(s) == list(reversed(s)) in Python too!!

And Why you need mul. In Clojure it was (* x y). Why you need new multiply function which takes arbitrary number of arguments?? Why didn't you write just (x * y)?

Finally, I concluded the messy python code is equivalent to the below:

print max(s for s in (x * y
for x in range(111, 1000)
for y in range(y, 1000))
if list(str(s)) == list(reversed(str(s))))


It is shorter than Clojure.



P.S. matz reported the original version of Python code takes 12.2 seconds, my version takes 4.9 seconds (both on Python2.6). And his ruby code (see first post) takes only 2.8 seconds on Ruby1.8.7 and 0.9 seconds on Ruby1.9.

http://twitter.com/yukihiro_matz/status/4901341641
http://twitter.com/yukihiro_matz/status/4901791131
http://twitter.com/yukihiro_matz/status/4901849726

Friday, October 9, 2009

My CG was selected as an excellent work on ASIAGRAPH 2009

Hi, I'm NISHIO Hirokazu. Today I announce a good news (for me!)

My CG artwork was selected as an excellent work by ASIAGRAPH 2009 committee. It will be displayed on 10/22-25 at Odaiba, Tokyo. See below, "Virtual Star" is mine. It shows recursive birth of 5 children of rings.


For more informations: http://www.asiagraph.jp/public/index.html

Friday, October 2, 2009

printed with EPSON's PX-5002

Hi, it is NISHIO Hirokazu. I was present at a blogger event yesterday. It is about EPSON's brandnew printer for professionals PX-5002. It was nice experience to print own graphics out and make a displayable object.





Cool! The printer has 3 grayish inks so it is good to print gray-scale image.

Professional photographer NAKAI Seiya said it is not strange if the display is sold in IKEA.



Here is a movie, but unfortunately compression made it dirty. I'm looking for a chance to display my works.

Monday, July 6, 2009

iPhone Camera and its motion blur

Hi, it is NISHIO Hirokazu. I bought iPhone 3GS yesterday! I found the response of camera is very slow. It is nice! You can make interesting pattern of motion bulr very easily.




Ivanhoe said... "A former colleague of mine used to play a lot with his CRT TV and video camera, making a lot of interesting patterns : http://chabin.laurent.free.fr/camera/ccd.htm" Oh! That's nice! I'm very interested in turing patterns.

Monday, June 29, 2009

SAKETOBA: Japanese redvines

Hi, it is NISHIO Hirokazu, today I'll write about a Japanese traditional food.


Do you know Red Vines or Twizzlers? I know it is a popular candy in US, however it is not popular in Japan. Most of Japanese don't like its liquorice flavor. They say "It smells strange! It is too chewy! What's this lengthy food! Is it food?"


Today I realized SAKETOBA is such in Japan. It is very chewy. And it is made from salmons! I don't feel it smells strange, but I can imagine a lot of foreign people feel it strange. SAKETOBA is a salmon jerky. SAKE means salmon (you may know SAKE means alcoholic drink. The word has different accent.) and TOBA means swarm in Ainu; an ethnic group indigenous to northern part of Japan (Hokkaido). It is nice tidbits to go with drink.
Ivanhoe said... "I can tell you about some snack food that feels strange for a foreigner : dried squids ! Chewy and salted, it is not bad, but the first time I was shown it I wondered if I was really supposed to eat it..."

Yes, I love dried squids too!

Tuesday, June 16, 2009

How to calculate Bezier curves' bounding box

Hi, it is NISHIO Hirokazu. Today I wanted to make a lot of Bezier curves same size. And I got it.

Here is what I want to draw:


To calculate bounding box of cubic Bezier seems easy, especially you know its parametric form. See Bézier curve - Wikipedia, the free encyclopedia

However, there are some pitfall. The derivative of Bezier equation is usually quadratic equation but not always. Solutions of the derivative may out of range, etc.

I publish following source code under MIT License. Feel free to use it.

def calc_box(start, curves):
P0 = start
bounds = [[P0[0]], [P0[1]]]

for c in curves:
P1, P2, P3 = (
(c[0], c[1]),
(c[2], c[3]),
(c[4], c[5]))

bounds[0].append(P3[0])
bounds[1].append(P3[1])

for i in [0, 1]:
f = lambda t: (
(1-t)**3 * P0[i]
+ 3 * (1-t)**2 * t * P1[i]
+ 3 * (1-t) * t**2 * P2[i]
+ t**3 * P3[i])

b = 6 * P0[i] - 12 * P1[i] + 6 * P2[i]
a = -3 * P0[i] + 9 * P1[i] - 9 * P2[i] + 3 * P3[i]
c = 3 * P1[i] - 3 * P0[i]

if a == 0:
if b == 0:
continue
t = -c / b
if 0 < t < 1:
bounds[i].append(f(t))
continue

b2ac = b ** 2 - 4 * c * a
if b2ac < 0:
continue
t1 = (-b + sqrt(b2ac))/(2 * a)
if 0 < t1 < 1: bounds[i].append(f(t1))
t2 = (-b - sqrt(b2ac))/(2 * a)
if 0 < t2 < 1: bounds[i].append(f(t2))

P0 = P3

x = min(bounds[0])
w = max(bounds[0]) - x
y = min(bounds[1])
h = max(bounds[1]) - y
return (x, y, w, h)



Blaze Boy asked me about the structure, thank you. for each c in curves is 6-tuples: P1, P2, P3 = ((c[0], c[1]), (c[2], c[3]), (c[4], c[5])) P0 and P3 are terminal points of each bezier curves.

Wednesday, June 3, 2009

Fractal aurora(Koch curve)

Hi, it is NISHIO Hirokazu. I wondered how we see if there is aurora in Koch curve. So I wrote a python script run on Blender. Let see it(click to enlarge):



The script is as follow. It is not beautifil because I'm just a beginner of Blender. A lot of lines are copy of sample.


from Blender.Mathutils import Vector
from Blender import *
from math import sin, cos, pi

class Turtle(object):
def __init__(self):
self.x = 0.0
self.y = 0.0
self.dirx = 1.0
self.diry = 0.0
self.vert2d_list = [(0.0, 0.0)]
self.faces = []

def forward(self, mag):
self.x += self.dirx * mag
self.y += self.diry * mag
self.vert2d_list.append((self.x, self.y))

def rot(self, rad):
self.dirx, self.diry = (
self.dirx * cos(rad) - self.diry * sin(rad),
self.dirx * sin(rad) + self.diry * cos(rad))

def build_mesh(self, z=0.0, name="Mesh"):
verts3d = [Vector(x, y, z) for (x, y) in self.vert2d_list]
me = Mesh.New(name + str(z)) # create a new mesh
me.verts.extend(verts3d) # add vertices to mesh
me.faces.extend(self.faces) # add faces to the mesh (also adds edges)
return me

def deg(x):
return 2 * pi / 360 * x

def makeHalo(x):
mat = Material.New('Halo') # create a new Material called 'newMat'
mat.rgbCol = [0.7 * x, 0.7, 0.1] # change its color
mat.setAlpha(0.3 * (1 - x)) # mat.alpha = 0.2 -- almost transparent
#mat.emit = 0.7 # equivalent to mat.setEmit(0.8)
#mat.mode |= Material.Modes.ZTRANSP # turn on Z-Buffer transparency
#mat.setAdd(0.8) # make it glow
mat.setMode('Halo') # turn 'Halo' "on" and all others "off"
return mat

def koch(level=1, unit=1):
if level == 0:
turtle.forward(unit)
else:
koch(level - 1, unit)
turtle.rot(deg(60))
koch(level - 1, unit)
turtle.rot(deg(-120))
koch(level - 1, unit)
turtle.rot(deg(60))
koch(level - 1, unit)


turtle = Turtle()
koch(4, 0.6)

for i in range(100):
ob = Object.New('Mesh', 'myObj') # link mesh to an object
m = turtle.build_mesh(i / 5.0)
m.materials += [makeHalo(i / 100.0)]
ob.link(m)

sc = Scene.GetCurrent() # link object to current scene
sc.link(ob)

Saturday, April 18, 2009

bug repellent talisman

Hi, it is NISHIO Hirokazu. I'm attending Hackathon with Spa and monjudoh found a talisman of bug repellent at nearby shrine.



Originally the talisman is for farmers, but of course, we also suffer from bugs. It works! :-)

Tuesday, April 14, 2009

Can you see what the diagram means?

Hi, it's NISHIO Hirokazu. Today I translate one of my 'quiz' entry. It is for programmers who know a functional programming language.

See following image.

  • What do "black-centered squares" mean?
  • What do "white-centered squares" mean?
  • What do ">"-like shape mean?
  • What do "+"-like shape mean?




Answers:

If you have learned Haskell you should have been confused by those rules.


  • The first 3 lines mean monad laws.
    1. (return x) >>= f == f x
    2. m >>= return == m
    3. (m >>= f) >>= g == m >>= (\x -> f x >>= g)

  • Black-centered squares mean monads. (Monad m) => m a
  • White-centered squares --- if you put a black circle in them, they become black-centered squares --- mean functions which take something as argument and return a monad. (Monad m) => a -> m b
  • Fully white squares mean return :: (Monad m) => a -> m a
  • ">"-like shapes mean (>>=) :: (Monad m) => m a -> (a -> m b) -> m b.
    It have a black-centered square(monad) on its left-side and a white-centered square(function) on its right side.


You see ">"-like shape on the third line have a white-centered square on its left. A black circle x was removed from a monad (f x >>= g). So it means (\x -> f x >>= g). It seem like (f >>= g) but it cause type error.

It is a little unclear because it is not symmetrical. But if you remove all black circles, those have same structure as 1 * x = x, x * 1 = x, (x * y) * z = x * (y * z)

Now let's see the last 3 lines.

  • The last 3 lines mean MonadPlus's law
    1. mzero >>= f == mzero
    2. m >>= (\x -> mzero) == mzero
    3. mzero `mplus` m == m `mplus` mzero == m

  • Fully black square means mzero.
  • "+"-like shape means mplus.
  • A black square with white hole, in the other word "A fully black square which was removed a black circle", means (\x -> mzero)


If you remove all black circles again, you'll see these 3 lines have same structure as 0 * x = 0, x * 0 = 0, x + 0 = 0 + x = x.

This figure is very beautiful because it shows mathematical structure behind haskell.



Want to know more about me? Please visit http://www.nishiohirokazu.org/

Monday, March 16, 2009

Ruby's Struct v.s. Python's namedtuple

Hi, it is NISHIO Hirokazu. 9 years ago I met to Ruby's Struct. It is nice.


irb(main):018:0> FooBar = Struct.new(:foo, :bar)
=> FooBar
irb(main):019:0> FooBar.new(1, 2)
=> #<struct FooBar foo=1, bar=2>
irb(main):020:0> _.foo
=> 1


So I wrote its Python version.


>>> def Struct(*keys):
class _Struct(object):
def __init__(self, *values):
self.__dict__.update(zip(keys, values))
return _Struct


>>> Struct("foo", "bar")
<class '__main__._Struct'>
>>> FooBar = Struct("foo", "bar")
>>> FooBar(1, 2)
<__main__._Struct object at 0x01494E90>
>>> _.foo
1


Today I regret to inform the code is obsolete. In python2.6 we can use collections.namedtuple:


>>> from collections import namedtuple
>>> namedtuple("MyClass", "foo bar")
<class '__main__.MyClass'>
>>> _(1, 2)
MyClass(foo=1, bar=2)
>>> _.foo
1

Friday, February 13, 2009

[Python] To raise exception doesn't take extra overhead on call

Hi, it is NISHIO Hirokazu. Today I'll write about Python tips. My friend said "Does a function which raise exceptions take extra overhead on call?" on Twitter. I don't think so, but I wrote a code:


import timeit

print timeit.Timer("foo(1)", setup="""
def foo(x):
if x:
return None
else:
return x
""").repeat()

print timeit.Timer("bar(1)", setup="""
def bar(x):
if x:
return None
else:
raise NotImplementedError
""").repeat()


The formar function doesn't raise any exceptions. The latter does. The timeit module measures time to run given code 1000000 times, and repeat it 3 times. The result is below:


[0.24111700057983398, 0.22863888740539551, 0.22955012321472168]
[0.23151803016662598, 0.23359298706054688, 0.2297508716583252]


I concluded there is no extra overhead between them.

Thursday, January 1, 2009

HATSU-MOUDE(the first shrine visit of the year)

Happy New Year! Today most of Japanese go to nearby JIN-JA(shrine, holy place of Japanese Shinto). Most of Japanese is not devout, they don't have deep religious feeling, so this is the first and the last visit of them. It is just an annual event.

SHIN-BUTSU-SHU^GO^ (word for word 'God-Buddha-Fusion') is a character of Japanese religion. Because of our traditional religion (Japanese Shinto) is a polytheism so when Buddhism came in Japan Buddha was considered as 'another god in the neighbor country'. It made Japanese tolerance of other religions. Most of Japanese celebrate Christmas, visit a Shinto shrine on new year day and mourn the deceaded in Buddhism style.

The number of people who visited shrine in the first three day is 98.18 millions (in 2008, according to National Police Agency). The most visited shrine is MEIJI-JINGU^ in Tokyo, 3.17 millions. MEIJI-JINGU^ is the shrine dedicated to the memory of the Meiji Emperor. I and my family always visit ISHI-KIRI-TSURUGI-YA Shrine (means stone-cut-sword-arrow) because it is the nearest. It is dedicated to NIGIHAYAHI god, lived before the first emperor come to here. But I didn't know it till yesterday. It is not important for me.

See also: Shinbutsu shūgō - Wikipedia, the free encyclopedia and Hatsumōde - Wikipedia, the free encyclopedia.