恭祝各位新年好!
Monthly Archives: December 2007
为什么笔记本上没有FM调频收音机?
上周买了一台昂达的MP4( VX979+)作为圣诞礼物送给老婆。26万色的2.4寸TFT液晶屏(320x240分辨率),支持各种常见的音乐和视频格式,视频无需转换可直接播放,支持RM和RMVB格式,可播放Flash,提供了几款小游戏,还有FM调频立体声收音机。2GB闪存,可扩充至多4GB的TF卡。
如果你不是“村”里人,你可能想象不到,它的价格只有380元人民币,是的,只大概50美金!
我在想,为什么作为个人多媒体中心的笔记本电脑,却没有FM调频收音机呢?
Ruby 1.9 is released!
Y combinator in Python
在lambda系统中实现递归,需要借用Y组合算子(combinator)。下面是Python中的一个实作。
Y = lambda F: (lambda f: F(lambda x: f(f)(x))) (lambda f: F(lambda x: f(f)(x))) F = lambda f: (lambda x: 1 if x==0 else x*f(x-1)) fact = Y(F) print fact(10)
感觉这个Y combinator实在是令人头晕目眩,不知所以。具体的推导过程和相关理论,请参见刘未鹏先生的宏文,以及负暄琐话相关的blog(1、2)。读了几遍,仍然只是朦朦胧胧地明白了一点点,特别是后面的对角线,再读、再读... ...
史上第二难的初等几何题

原题参见http://thinkzone.wlonk.com/MathFun/Triangle.htm。第二题果然简单些,有了第一题的经验,不多时便解出x为30度。如果需要提示信息,可以参见本篇blog的评论。建议你先不要看提示,自己想想看 ![]()
My first invention disclosure got approved!
After several months since I submitted my first invention disclosure (SUN080023), I saw there was $200 for "patent bonus" on my payroll of last month. And later received a notification email from the attorney, said my invention disclosure "was reviewed, and will not file a patent application".
While I'm not sure, if "reviewed" means "reviewed and approved"? After several rounds of email communication with the attorney, I confirmed, yes, it's approved, and that's why I got the bonus.
I heard that the "idt tool" will support checking status (such as PENDING, REJECTED, APPROVED, READY-TO-PATENT etc). And this would happen in the next 6 months.
史上最难的初等几何题

原题参见http://thinkzone.wlonk.com/MathFun/Triangle.htm。Simford给出了一个完整的分步解。我在其中也贡献了比较关键的一步,不过之后想偏了,跑去证明相似三角形去了。
建议你先不要看答案,自己想想看 ![]()
Python: mmap and array
我需要在Python程序中存取一个很大的数组,数组的每一项是(int, int, float, int)的记录。如果直接用list来存放,占据的内存巨大(因为不仅所有这些数都是对象,且tuple本身也是对象)。Python提供了一个array模块,以更有效地存取数字值,但是它只支持单一的数据类型,例如你无法创建这样的array对象:a = array.array('2lfl')。
我想到了存放在文件中,并用mmap的方式来访问。除了mmap,我不知道Python中是否还有其他方法可以得到一块raw的内存。且mmap在性能和效率上,有一定的优越性。最后,辗转得到了下面的代码:
class MMArray:
__file = __mem = None
__realsize = __capsize = 0
def __init__(self, type='B', fname=None, capsize=1024*1024):
self.__elmsize = struct.calcsize(type)
if not fname:
fno, self.__fname = tempfile.mkstemp("-mmarray", "pyslm-")
self.__file = os.fdopen (fno, "w+")
self.__enlarge(capsize)
else:
self.fromfile(fname)
def fromfile(self, fname):
if not os.path.exists(fname):
raise "The file '%s' does not exist!"
fsize = os.path.getsize(fname)
if fsize == 0:
raise "The size of file '%s' is zero!" % fname
if self.__mem: self.__mem.close()
if self.__file: self.__file.close()
self.__file = open (fname, "r+")
self.__mem = mmap.mmap(self.__file.fileno(), fsize)
self.__realsize = self.__capsize = fsize/self.__elmsize
def tofile(self, fname):
if fname == self.__file.name:
raise "Can not dump the array to currently mapping file!"
tf = open(fname, "w+")
bsize = self.__realsize * self.__elmsize
tf.write (self.__mem[:bsize])
tf.close()
def __enlarge(self, capsize):
if self.__capsize >= capsize:
return
self.__capsize = capsize
self.__file.seek(self.__elmsize * self.__capsize - 1)
self.__file.write('')
self.__file.flush()
if (self.__mem): self.__mem.close()
self.__mem = mmap.mmap(self.__file.fileno(), self.__file.tell())
def __del__ (self):
bsize = self.__realsize * self.__elmsize
self.__file.truncate (bsize)
self.__file.close()
if self.__mem: self.__mem.close()
os.remove(self.__fname)
def __getitem__(self, idx):
if idx < 0 or idx >= self.__realsize:
raise IndexError
return self.__access(idx)
def __setitem__(self, idx, buf):
if idx < 0 or idx >= self.__realsize:
raise IndexError
if type(buf) != type("") or len(buf) != self.__elmsize:
raise "Not a string, or the buffer size is incorrect!"
self.__access(idx, buf)
def __access (self, idx, buf=None):
start = idx * self.__elmsize
end = start + self.__elmsize
if not buf: return self.__mem[start:end]
self.__mem[start:end] = buf
def size(self):
return self.__realsize
def append(self, buf):
if type(buf) != type("") or len(buf) != self.__elmsize:
raise "Not a string, or the buffer size is incorrect!"
if self.__realsize >= self.__capsize:
self.__enlarge(self.__capsize*2)
self.__access(self.__realsize, buf)
self.__realsize += 1
def __iter__(self):
for i in xrange(0, self.__realsize):
yield self.__access(i)
def truncate(self, tsize):
if self.__realsize >= tsize:
self.__realsize = tsize
当然,还有许多要改进的地方,例如支持从尾部索引(即index<0),以及slicing等等。