星期一 十二月 10, 2007

triangle

原题参见http://thinkzone.wlonk.com/MathFun/Triangle.htm。第二题果然简单些,有了第一题的经验,不多时便解出x为30度。如果需要提示信息,可以参见本篇blog的评论。建议你先不要看提示,自己想想看 :)

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.

星期三 十二月 05, 2007

triangle

原题参见http://thinkzone.wlonk.com/MathFun/Triangle.htmSimford给出了一个完整的分步解。我在其中也贡献了比较关键的一步,不过之后想偏了,跑去证明相似三角形去了。

建议你先不要看答案,自己想想看 :)

星期一 十二月 03, 2007

我需要在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('\0')
        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等等。

星期日 十一月 18, 2007

python内建的dict(字典)类使用的是hash算法,因此它的key不是有序的。而C++中的std::map或std::set使用的是平衡二叉树(通常为红黑树),其key是有序的。在网上搜了搜,找到了一个用C和pyrex混合实现的红黑树模块,python-rbtree

我编写了一个极简单的测试程序,在Solaris x86 + python 2.4.4平台上运行,分别使用dict和rbtree,插入两百万个记录(key是3个整型,value是1个整型,你大概猜到我在干什么了吧 :))。且在dict插入完之后,调用dict.keys().sort()对其key进行排序(也就是快排)。比较的结果是,两种方法使用的内存相当(大概在200M左右)。但是hash算法的速度要快一倍以上。当记录个数增加到五百万个时,结果还是差不多──即内存使用相当,hash算法快一倍。

至少在这个数量级上,内建的dict性能更佳。我还尝试了另一个纯Python的红黑树实现--RBTree.py,结果令人失望,在记录个数比较多的情况下,似乎根本无法得到正确的结果。

结论,python中的dict是可信赖的!
Jserv(黃敬群)是名满两岸的自由软件开发者,是我崇拜的青年才俊、技术偶像之一。我虽然痴长了几岁,但是在Linux和自由软件开发方面却是后学晚辈。下面是台湾的同业者对Jserv的访问,读后直觉得自己光阴虚度了 :$。

自由軟體開發者 Jserv 訪問 (上)
自由軟體開發者 Jserv 訪問 (下)

记得我最初了解到Jserv,是2002或03年,读到他的一篇关于Qt/Embedded本地化的文章。后来逐步看到他在浏览器、嵌入式系统、图形库、Java虚拟机、输入法、Linux内核、实时系统等各个方面的成绩,对他的技术能力真是钦佩不已。而且Jserv还是一位兼具诗人气质、十分感性的青年,从他许多的blog中,可以看到他对人生许多富有哲理性的思考。

后来虽然在网络上有一些联络,但对他个人却了解不多。这两篇访问,正好满足了我的好奇... ...

希望他将Lab的事业拓展到大陆来。

星期二 十一月 13, 2007

Google released Android SDK preview today, have a look at the feature overview video. From the application development demonstration, and technical presentations (video 1, 2, and 3), you can see that Java (as a programming language) is almost everywhere, but on a different virtual machine--Dalvik Virtual Machine, a register-based (typically JVM is stack-based) virtual machine that optimized for mobile devices. Another interesting thing is, it supports OpenGL ES 1.0 specification, and the "Surface Manager" could seamlessly composite 2D and 3D graphic layers from multiple applications.

Here is the architecture of Android platform (click to enlarge):

You may download the SDK here.

星期三 十一月 07, 2007

经朋友的推荐,我去买了一本早已畅销多时的书──《货币战争》。一口气读完了,真是心惊肉跳。

原来英格兰银行是私有银行啊,连美联储也不例外(也有很多人对此 进行了反驳),而这两国的政府居然无权发行货币,需要将国家的赋税作为抵押从银行借贷,再由银行来发行。书中还描写了这些国际银行家是如何操纵国际金融 市场(甚至环保都不放过),来牟取巨额利润的。

然后去豆瓣上看了看大家的书评,有推崇备至的,也有不屑一顾的。不过对我这种金融门外汉来说,读过一遍还是很有收获的。

人类获得自由和解放的路途还很遥远啊!

本书作者的blog:http://currencywar.blog.hexun.com

This blog copyright 2009 by yongsun