Saturday, December 27, 2008

Embedding Python

I've finally written my first embedded Python program! The following is basically a "hello world" program that demonstrates the basics of writing a C program that interacts with Python. It starts the embedded Python environment, parses some input (which is stored in the __main__ namespace) and prints out that input directly from C.

#include <Python.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
PyObject *globals, *list, *name, *item, *mp;
Py_ssize_t i, n;

Py_Initialize();
PyRun_SimpleString("a = 'some text'; b = 123.456");
mp = PyImport_AddModule("__main__");
list = PyObject_Dir(mp);
n = PyList_Size(list);
printf("n=%d\n", n);
for(i = 0; i < n; i++) {
name = PyList_GetItem(list, i);
item = PyObject_GetAttr(mp, name);
if(PyString_Check(item))
printf("list[%d] (string): '%s' = '%s'\n", i, PyString_AsString(name), PyString_AsString(item));
else if(PyFloat_Check(item))
printf("list[%d] (float): '%s' = %g\n", i, PyString_AsString(name), PyFloat_AsDouble(item));
}
Py_Finalize();

return 0;
}


To compile this program, you'll need to do something like
gcc embed_python.c -I/usr/include/python2.5 -lpython2.5

Thursday, December 18, 2008

Modern physics course at Stanford



Apparently Stanford University has published a series of modern physics lecture videos (taught by Prof. Leonard Susskind) on Youtube. Although this is a continuing studies course, it is definitely graduate level, but taught in a very friendly manner -- a very good supplement for even graduate students of physics.

The rest of the videos are available via this link.

Monday, December 15, 2008

SI vs. Gaussian units

This is a major source of confusion for many physics students, including me. The following link is an article that explains the differences quite clearly, citing some of the historical reasons for having two popular unit systems in use at the same time.

S.I. versus c.g.s. Units by Jason Harlow

Wednesday, December 3, 2008

Multi-threaded programming

This is something I've been curious about for a long time but haven't really had the chance to look into. Thanks to this tutorial at LLNL, I've discovered that it's actually quite easy to do simple multi-threading with POSIX threads -- all you really need are basic C programming skills and a basic concept of how processes are managed by a POSIX operating system. The API is easy to learn, and there is MUCH less overhead compared to multi-host parallel programming. On SMP machines, it is probably much more powerful than using a full parallel enviornment like MPI.

Monday, October 20, 2008

blunder blunder

一天之內連出兩個大包。

美國總統大選的通訊投票,忘記帶來新竹。

雙聖的晚餐,浪費掉了。

我好想哭。



後記:
晚餐很神奇的救了回來,感謝阿尼借的摩托車
歐巴馬先生,我的票寄出去了,加油!

Wednesday, October 15, 2008

研究所

來新竹一個月了。這學期修課總共十二學分,其中九學分包括恆星天文物理、電動力學和微波物理都很不容易拿。會讓自己負擔這麼重,一方面其實是給自己挑戰:我要是無法克服這邊的課程,之後就算成功申請出國一定也會水土不服;另一方面也是這些東西真的是有實用價值,不論是天文或是工程上都是很重要的知識。

除了最後兩年實驗室專題做的還不錯之外,我大學真的念得很糟。這是我一直不願意面對卻一直讓我非常沒自信,也阻礙我繼續求學的事情;因此來新竹之前就下定決心絕不能再像大學一樣把研究所搞砸。但最近卻越來越覺得奇怪—我以前大學到底是怎麼混過來的?也許是我現在課真的比較重,但最近一個月我每天我都覺得我大學從來沒真的讀過書;甚至現在用的讀書方法也都是在中研院當兩年助理培養出來的—這種感覺真的很糟,因為越來越覺得以前申請不到學校根本就是活該。也許這麼想有點過於激烈,但那種感覺的真實性毋庸置疑。

無論如何,我無疑的已經到了十字路口。堅持著理想不斷走下去,才有那麼一點機會能夠達成夢想

Rings a bell


Loneliness is a terrible price to pay for independence.
--Anonymous


Not sure where I saw this, but I can REALLY identify with what it says.

Friday, March 7, 2008

Standard predefined macros


Came across this by accident today -- I guess I would've known about this stuff and saved a whole lot of trouble in the past year if I took a proper course in C programming. Oh well.

These are really useful macros for debugging and other purposes in writing C programs, for example the __FILE__ and __LINE__ macros will provide the location of a statement, and it is done completely automatically by the preprocessor, which saves a WHOLE lot of work for debugging -- exactly what SPARX needs.

Once again, RTFM...

Wednesday, March 5, 2008

安全背帶扣不安全

安全背帶扣有多安全?如果買到瑕疵品,那真的一點都不安全。就在剛剛,我的寶貝fender差點從我懷中摔到地上,因為應該要很安全的安全背帶扣竟然鬆脫了!


吉他的安全背帶扣,靠的就是圖中左手邊三個環狀的東西夾住背帶,再靠右手邊柱狀物的卡榫卡住吉他上的釘子。

結果柱狀物上的卡榫竟然縮進去了,真的是瑕疵品!有在用這玩意兒的人要小心呀!

Saturday, February 23, 2008

Radio-SkyPipe Strip Chart Program - results


Radio-SkyPipe是一個使用音效卡當A/D轉換器的 (Win32) 程式,安裝非常的簡單,也不太需要做什麼特別的設定就可以使用了。他的功能也非常的直覺:就是從音效卡的麥克風輸入端讀取類比訊號(聲音)再依時間紀錄成相對單位的數位訊號 -- 看來如果要轉換成有意義的單位,還是得像一般的電波觀測一樣做calibration了。

Wednesday, February 20, 2008

天線到了!


整個計畫進行的比想像中快,而且比想像中更簡單(唉)。就在今早,高中生電波望遠鏡動手做所要使用的衛星接收天線送到了所上。整組設備包括天線、12GHz降頻器及power meter據說大約才NT$4,000!基本上只要把power meter接上降頻器就已經可以使用了 -- 可是這樣實在也太簡單了,所以大家已經在討論要如何把計畫變的好玩/複雜一點:像是用赤道儀取代天線的支架(其實那是個喇叭架,就是那種戶外表演會用的),或是自行製作降頻器/接收機等等。超期待啊!

SlackWiki

For some reason the Slackware (unofficial) wiki didn't turn up in my searches until fairly recently -- all you Slackware users, go contribute!

Tuesday, February 19, 2008

Radio-SkyPipe Strip Chart Program

A/D部分有初步的方向了--目前計畫是用這個程式來將power meter的聲音轉成數位訊號。等試過了再把結果放上來。

Project Slackware

之前在家裡架了老半天的伺服器經過了分割區掛點事件後,最近經過百般折騰總算是真的要復活了。學到了上次ReiserFS因為暴力重開機而半毀的教訓後,這次在檔案系統的選擇上還是走保守路線使用ext3,並且同時架好了兩台機器:主伺服器 (voyager) 提供blog, wiki, Trac等服務,而實驗用機 (pegasus) 則是在對voyager進行任何更動前做實驗用,以免安裝/設定失敗造成伺服器服務中斷。兩台機器目前都是用Slackware Linux 12。

Slackware 12由於使用最新的2.6.21 kernel,對於硬體的支援度相當高,基本上安裝後不需做什麼設定就可以使用了。但在設定pegasus時遇到了一段小插曲:開機後dhcp一直啟動失敗,僅顯示網卡的MAC address卻一直沒有出現dhcp server所分配的IP address。奇怪的是啟動失敗卻沒有任何錯誤訊息(我想這應該要寫bug report給Slackware維護者了)。最後在軟體設定無法解決的情況下猜到有可能是硬體衝突,並在更換網卡的PCI插槽後才順利啟動。

接下來就是冗長的軟體安裝及設定了…請拭目以待。

Monday, February 18, 2008

電波天文望遠鏡動手做

這是所上最近要和建中合辦的一個活動 -- 讓高中生實際動手製作電波望遠鏡。受限於經費,雖然無法製作出真正專業級的望遠鏡(天線>6m + 頻率>100GHz = NT$10,000,000+),但我覺得想出這個點子的人真的超棒啊!讓高中生有機會跳脫考試用的「標準教材」,見識一下真正的科學是什麼 -- 我真希望我當年也有這樣的機會。但話說回來,要參加這個活動的門檻真的超高!必須要是 1) 前一學期學業成績為居全班前三名 2) 數理資優班學生 3) 獲自然科教師推薦 -- 以我高中混的程度,如果當時有這樣的活動我應該幾乎沒有機會參加了吧。

這個計畫的主要內容就是要用現成的~20GHz衛星天線實做一個電波望遠鏡。由於前端的接收機等已有現成,主要需要動手做的應該是後端資料擷取的部分 -- 但由於使用的power meter全為類比顯示(指針跟聲音 -- 真的就是用喇叭放出來),A/D部分的處理似乎頗有挑戰性。讓我們拭目以待吧!

Big Bang!

The beginning of a new blog and a new life.