Re-uploaded as plain files.

Put attached files in /tto folder at same level as /pyinstaller-develop 
folder.  Create .pyd with cythonize.py.  Generate --onedir executable using 
command line options in 'make.txt' from within /tto folder.  Run executable 
from /dist.

The sample example is from 
http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html#still-too-slow-preconditioning.

Works in python interpreter; works with pyd files; doesn't work with 
pyinstaller - see run-time "ImportError cannot import name Random" message. 
Note that source files do not directly use random or random.Random.

Environment: windows XP/7, python 2.7.4, numpy 1.7.1, scipy 0.12, cython 
0.18, pyinstaller 2.0, with and without virtualenv.

-- 
You received this message because you are subscribed to the Google Groups 
"PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pyinstaller?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


python ..//pyinstaller-develop//pyinstaller.py --noconfirm --log-level=ERROR 
--onedir --console --nowindowed tto.spec

Attachment: tto.spec
Description: Binary data

import other


if __name__ == '__main__':
    other.solve()
## usage: python setup.py build_ext --inplace

import numpy

from distutils.core import setup
# from distutils.extension import Extension
from Cython.Distutils.extension import Extension
from Cython.Distutils import build_ext


ext_modules = [Extension("other", ["other.pyx"])]

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules,
    include_dirs=[numpy.get_include()]
    )

# http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html

import numpy as np
from scipy.optimize import root
from scipy.sparse import spdiags, kron
from scipy.sparse.linalg import spilu, LinearOperator
from numpy import cosh, zeros_like, mgrid, zeros, eye


# parameters
nx, ny = 75, 75
hx, hy = 1./(nx-1), 1./(ny-1)

P_left, P_right = 0, 0
P_top, P_bottom = 1, 0

def get_preconditioner():
    """Compute the preconditioner M"""
    diags_x = zeros((3, nx))
    diags_x[0,:] = 1/hx/hx
    diags_x[1,:] = -2/hx/hx
    diags_x[2,:] = 1/hx/hx
    Lx = spdiags(diags_x, [-1,0,1], nx, nx)

    diags_y = zeros((3, ny))
    diags_y[0,:] = 1/hy/hy
    diags_y[1,:] = -2/hy/hy
    diags_y[2,:] = 1/hy/hy
    Ly = spdiags(diags_y, [-1,0,1], ny, ny)

    J1 = kron(Lx, eye(ny)) + kron(eye(nx), Ly)

    # Now we have the matrix `J_1`. We need to find its inverse `M` --
    # however, since an approximate inverse is enough, we can use
    # the *incomplete LU* decomposition

    J1_ilu = spilu(J1)

    # This returns an object with a method .solve() that evaluates
    # the corresponding matrix-vector product. We need to wrap it into
    # a LinearOperator before it can be passed to the Krylov methods:

    M = LinearOperator(shape=(nx*ny, nx*ny), matvec=J1_ilu.solve)
    return M

def solve(preconditioning=True):
    """Compute the solution"""
    count = [0]

    def residual(P):
        count[0] += 1

        d2x = zeros_like(P)
        d2y = zeros_like(P)

        d2x[1:-1] = (P[2:]   - 2*P[1:-1] + P[:-2])/hx/hx
        d2x[0]    = (P[1]    - 2*P[0]    + P_left)/hx/hx
        d2x[-1]   = (P_right - 2*P[-1]   + P[-2])/hx/hx

        d2y[:,1:-1] = (P[:,2:] - 2*P[:,1:-1] + P[:,:-2])/hy/hy
        d2y[:,0]    = (P[:,1]  - 2*P[:,0]    + P_bottom)/hy/hy
        d2y[:,-1]   = (P_top   - 2*P[:,-1]   + P[:,-2])/hy/hy

        return d2x + d2y + 5*cosh(P).mean()**2

    # preconditioner
    if preconditioning:
        M = get_preconditioner()
    else:
        M = None

    # solve
    guess = zeros((nx, ny), float)

    sol = root(residual, guess, method='krylov',
               options={'disp': True,
                        'jac_options': {'inner_M': M}})
    print 'Residual', abs(residual(sol.x)).max()
    print 'Evaluations', count[0]

    return sol.x

def solve():
    sol = solve(preconditioning=True)

Attachment: other.pyx
Description: Binary data

Reply via email to