IronPython + Mathematica .NET/Link

とりあえず動かしてみただけですが.

Mathematica .NET バインディングである .NET/Link 1.2.1 for Windows を使用しています.
以下の環境で動作を確認しました.

ソースコードとランタイム・ライブラリはこちら.
http://www.dwahan.net/nyaruru/hatena/pyNETLink.zip



これだけだとなんなので,Python の多倍長精度整数も試してみました.

>>> import clr
>>> from System.Diagnostics import *
>>> import System
>>> from System import *
>>> def Timing(func):
...     s = Stopwatch()
...     s.Start()
...     func()
...     s.Stop()
...     return `s.Elapsed.TotalMilliseconds / 1000` + " Second"

こんな感じで計算時間計測用の関数を定義してみます.

>>> print Timing(lambda :((2**1024)-1)**1024)
3.5747699 Second

とりあえず (21024-1)1024 を計算してみました.結果はこのように約 3.6 秒.Lambda 便利.
ついでに Mathematica の計算時間も見てみましょう.
予め ipy.exe のフォルダに Wolfram.NETLink.dll を置いておきます.

>>> clr.AddReference("Wolfram.NETLink")
>>> from Wolfram.NETLink import *
>>>
>>> mk = MathKernel()
>>> mk.Compute( "Timing[ (2^1024 - 1)^1024 ][[1]]" )
>>> print mk.Result
0.047 Second

さすがに Mathematica 速いと.
(22048-1)2048 も実験.

>>> print Timing(lambda :((2**2048)-1)**2048)
56.1609654  Second

>>> mk.Compute( "Timing[ (2^2048 - 1)^2048 ][[1]]" )
>>> print mk.Result
0.266 Second

おまけで Mathematica による 10,000,000 の階乗.昔自分でも同じ計算を試してみたことがありますが,Mathematica のこの計算速度はバケモノじみてます.いやいや,これぐらいなら自分でもという方はぜひお試しあれ.

>>> mk.Compute( "Timing[ 10000000! ][[1]]" )
>>> print mk.Result
145.594 Second