[SciPy-user] compare scipy and matlab

oyster lepto.python at gmail.com
Mon Jun 8 04:19:12 EDT 2009


first of all, I must make it clear that I don't want to stir up any
battle between languages! Never!

what I want to know is the truth and availabilty to use scipy in
serious works. Because I have met scipy bug but it has been fixed
soon; and now I met 2(maybe 1) bugs with sympy, so I regard
opensoftware(or any software?) with some suspicion.

the following is written by lac. I hope he will not charge me for my post ;)

####### speed #########

# python
>>> def xperm(L):
    if len(L) <= 1:
        yield L
    else:
        for i in range(len(L)):
            for p in xperm(L[:i]+L[i+1:]):
                yield [L[i]] + p

>>> def test(i):
    for p in xperm(map(lambda x:x+1, range(i))): pass;

>>> import timeit
>>> timeit.Timer('test(5)','from __main__ import test').timeit()

1263.9717730891925

# MatLab - timeit.m
function [ret] = timeit(times, repeat)
ret = zeros(1,repeat);
for i = 1:repeat
tic();
for j = 1:times
    perms([1:5]);
end
ret(i) = toc();
end

>> timeit(1000000,3)

ans =

  468.0005  449.7487  466.5325


####### correctness #######


# MatLab

>> A = hilb(256);

>> [L,U,P] = lu(A);

>> norm(P*A - L*U)/norm(A)

ans =

    3.2428e-017

>> [Q,R] = qr(A)

>> norm(Q)

ans =

    1.0000




# SciPy

>>> import numpy, scipy

>>> Hilb = lambda N: scipy.mat( [ [ 1.0/(i + j + 1) for j in range(N) ] for i in range(N) ] )

>>> A = Hilb(256)

>>> P, L, U = scipy.linalg.lu(A)

>>> scipy.linalg.norm( scipy.mat(P)*A - scipy.mat(L)*scipy.mat(U) ) / scipy.linalg.norm(A)
0.56581114936540999

>>> Q, R = scipy.linalg.qr(A)

>>> scipy.linalg.norm(scipy.mat(Q))
16.000000000000011



More information about the SciPy-User mailing list