예제 #1
0
def test_gh_2466():
    row = np.array([0, 0])
    col = np.array([0, 1])
    val = np.array([1, -1])
    A = scipy.sparse.coo_matrix((val, (row, col)), shape=(1, 2))
    b = np.asarray([4])
    lsqr(A, b)
예제 #2
0
def test_b_shapes():
    # Test b being a scalar.
    A = np.array([[1.0, 2.0]])
    b = 3.0
    x = lsqr(A, b)[0]
    assert_almost_equal(norm(A.dot(x) - b), 0)

    # Test b being a column vector.
    A = np.eye(10)
    b = np.ones((10, 1))
    x = lsqr(A, b)[0]
    assert_almost_equal(norm(A.dot(x) - b.ravel()), 0)
예제 #3
0
    def solve_lsqr(self, b, rho=None, v=None, x_init=None, options=None):
        """Solve ||K*x - b||^2_2 + (rho/2)||x-v||_2^2.
        """

        # Add additional linear terms for the rho terms
        sizev = 0
        if rho is not None:
            vf = v.flatten() * np.sqrt(rho / 2.0)
            sizeb = self.K.input_size
            sizev = np.prod(v.shape)
            b = np.hstack((b, vf))

        input_data = np.zeros(self.K.input_size)
        output_data = np.zeros(self.K.output_size + sizev)

        def matvec(x, output_data):
            if rho is None:
                # Traverse compgraph
                self.K.forward(x, output_data)
            else:
                # Compgraph and additional terms
                self.K.forward(x, output_data[0:0 + sizeb])
                np.copyto(output_data[sizeb:sizeb + sizev], x * np.sqrt(rho / 2.0))

            return output_data

        def rmatvec(y, input_data):
            if rho is None:
                self.K.adjoint(y, input_data)
            else:
                self.K.adjoint(y[0:0 + sizeb], input_data)
                input_data += y[sizeb:sizeb + sizev] * np.sqrt(rho / 2.0)

            return input_data

        # Define linear operator
        def matvecComp(x): return matvec(x, output_data)

        def rmatvecComp(y): return rmatvec(y, input_data)

        K = LinearOperator((self.K.output_size + sizev, self.K.input_size),
                           matvecComp, rmatvecComp)

        # Options
        if options is None:
            # Default options
            return lsqr(K, b)[0]
        else:
            if not isinstance(options, lsqr_options):
                raise Exception("Invalid LSQR options.")
            return lsqr(K, b, atol=options.atol, btol=options.btol,
                        show=options.show, iter_lim=options.iter_lim)[0]
예제 #4
0
파일: utils.py 프로젝트: tansey/gfl
def create_tf_plateaus(k, data, edges, plateau_size, plateau_vals, plateaus=None):
    if k == 0:
        return create_plateaus(data, edges, plateau_size, plateau_vals, plateaus=plateaus)
    D = matrix_from_edges(edges)
    Dkminus1 = get_delta(D, k-1)
    if k % 2 == 0:
        plateaus = create_plateaus(data, edges, plateau_size, plateau_vals)
        data[:] = lsqr(Dkminus1, data)[0]
    else:
        # TODO: not a very principled way of creating odd-k plateaus, but it looks pretty
        p = np.zeros(Dkminus1.shape[0])
        plateaus = create_plateaus(p, edges, plateau_size, plateau_vals)
        data[:] = lsqr(Dkminus1, p)[0]
    return plateaus
예제 #5
0
    def _fit(self, X, y):
        # For regularisation, we need to solve the equation
        # (X^T.X + alpha*I) w == X^T y for w
        N, M = X.shape

        if self._m == 'svd':
            # Using SVD and the Woodbury matrix identity, we get the
            # following equation:
            U, s, Vh = svd(X, full_matrices=False)
            invpart = np.diag(1 / (1 + self.alpha * s**(-2)))
            self.w = np.dot(np.eye(M) - np.dot(Vh.T, np.dot(invpart, Vh)),
                            np.dot(X.T, y)) / self.alpha

        elif self._m == 'solve':
            # uses the scipy implementation of solve
            self.w = solve(np.dot(X.T, X) + self.alpha*np.eye(M),
                           np.dot(X.T, y), sym_pos=True)

        elif self._m == 'inverse':
            # very inefficient, for comparison of numerical stability
            # solves by direct computation of the inverse of (X^T . X)
            self.w = np.dot(inv(np.dot(X.T, X) + self.alpha*np.eye(M)),
                            np.dot(X.T, y))

        # alternatively, there are iterative methods to 'solve'
        # X w == y directly
        elif self._m == 'lsqr':
            self.w, = lsqr(X, y, damp=self.alpha)

        else:
            error("Method not implemented")
예제 #6
0
    def fit(self, X, y):
        """
        Fit linear model.

        Parameters
        ----------
        X : numpy array or sparse matrix of shape [n_samples,n_features]
            Training data
        y : numpy array of shape [n_samples]
            Target values
        Returns
        -------
        self : returns an instance of self.
        """
        X = safe_asarray(X)
        y = np.asarray(y)

        X, y, X_mean, y_mean, X_std = self._center_data(
            X, y, self.fit_intercept, self.normalize, self.copy_X)

        if sp.issparse(X):
            if hasattr(sp_linalg, 'lsqr'):
                out = sp_linalg.lsqr(X, y)
                self.coef_ = out[0]
                self.residues_ = out[3]
            else:
                # DEPENDENCY: scipy 0.7
                self.coef_ = sp_linalg.spsolve(X, y)
                self.residues_ = y - safe_sparse_dot(X, self.coef_)
        else:
            self.coef_, self.residues_, self.rank_, self.singular_ = \
                    linalg.lstsq(X, y)

        self._set_intercept(X_mean, y_mean, X_std)
        return self
예제 #7
0
def balance_triangulation(triangulation):
    if GRAPH not in triangulation.keys():
        refresh_graph(triangulation)
    if HULL not in triangulation.keys():
        refresh_hull(triangulation)
    points = triangulation[VERTICES]
    graph = triangulation[GRAPH]
    cannot_move = [
        triangulation[SRC_POINT_MARKERS][i] or triangulation[HULL][i]
        for i in range(len(points))
    ]
    A = np.zeros((2 * len(points), 2 * len(points)))
    b = np.zeros(2 * len(points))
    for p_idx, p in enumerate(points):
        row = 2 * p_idx
        n_neighbours = len(graph[p_idx])
        A[row][row] = 1
        A[row + 1][row + 1] = 1
        p_mean = [0, 0]
        for neighbour in graph[p_idx]:
            A[row][2 * neighbour] = -1. / n_neighbours if not cannot_move[
                neighbour] else 0
            A[row +
              1][2 * neighbour +
                 1] = -1. / n_neighbours if not cannot_move[neighbour] else 0
            p_mean[0] += points[neighbour][0] / n_neighbours
            p_mean[1] += points[neighbour][1] / n_neighbours
        b[row] = p_mean[0] - p[0]
        b[row + 1] = p_mean[1] - p[1]
    answer = lsqr(A, b)[0]
    points[:] = [
        [points[i][0] + answer[2 * i], points[i][1] +
         answer[2 * i + 1]] if not cannot_move[i] else points[i]
        for i in range(len(points))
    ]
예제 #8
0
def test_Fredholm1(par):
    """Dot-test and inversion for Fredholm1 operator"""
    np.random.seed(10)

    _F = np.arange(par["nsl"] * par["nx"] * par["ny"]).reshape(
        par["nsl"], par["nx"], par["ny"]
    )
    F = _F - par["imag"] * _F

    x = np.ones((par["nsl"], par["ny"], par["nz"])) + par["imag"] * np.ones(
        (par["nsl"], par["ny"], par["nz"])
    )

    Fop = Fredholm1(
        F,
        nz=par["nz"],
        saveGt=par["saveGt"],
        usematmul=par["usematmul"],
        dtype=par["dtype"],
    )
    assert dottest(
        Fop,
        par["nsl"] * par["nx"] * par["nz"],
        par["nsl"] * par["ny"] * par["nz"],
        complexflag=0 if par["imag"] == 0 else 3,
    )
    xlsqr = lsqr(Fop, Fop * x.ravel(), damp=1e-20, iter_lim=30, show=0)[0]
    xlsqr = xlsqr.reshape(par["nsl"], par["ny"], par["nz"])
    assert_array_almost_equal(x, xlsqr, decimal=3)
예제 #9
0
    def solve_matrix_free(self, data, sphere, alpha=0.0, scale=True, fista=False, lsqr=True, lsmr=False):
        '''
            data = [vis_arr, n_freq, n_pol]
        '''
        logger.info("Solving Visabilities nside={}".format(sphere.nside))
        t0 = time.time()

        frequencies = [self.frequency]
        logger.info("frequencies: {}".format(frequencies))

        A = DiSkOOperator(self.u_arr, self.v_arr, self.w_arr, data, frequencies, sphere)
        Apre = DirectImagingOperator(self.u_arr, self.v_arr, self.w_arr, data, frequencies, sphere)
        d = data.flatten()

        #u,s,vt = spalg.svds(A, k=min(A.shape)-2) 
        #logger.info("t ={}, s={}".format(time.time() - t0, s))      
        if fista:
            sky, niter =  pylops.optimization.sparsity.FISTA(A, d, tol=1e-3, niter=2500, alpha=alpha, show=True)
        if lsqr:
            sky, lstop, itn, r1norm, r2norm, anorm, acond, arnorm, xnorm, var = spalg.lsqr(A, data, damp=alpha)
            logger.info("Matrix free solve elapsed={} x={}, stop={}, itn={} r1norm={}".format(time.time() - t0, sky.shape, lstop, itn, r1norm))      
        if lsmr:
            x0 = Apre*d

            sky, info = pylops.optimization.leastsquares.NormalEquationsInversion(A, Regs=None, data=d, x0=x0,
                                                             epsI = alpha,
                                                             returninfo=True)
            #logger.info("Matrix free solve elapsed={} x={}, stop={}, itn={} r1norm={}".format(time.time() - t0, sky.shape, lstop, itn, r1norm))      
            #logger.info("A M={} N={}".format(A.M, A.N))

            #sky, lstop, itn, normr, mormar, morma, conda, normx = spalg.lsmr(A, data, damp=alpha)
            #logger.info("Matrix free solve elapsed={} x={}, stop={}, itn={} normr={}".format(time.time() - t0, sky.shape, lstop, itn, normr))      
        #sky = np.abs(sky)
        sphere.set_visible_pixels(sky, scale)
        return sky.reshape(-1,1)
예제 #10
0
    def adjoint_derivative(dx, dy, ds, **kwargs):
        """Applies adjoint of derivative at (A, b, c) to perturbations dx, dy, ds

        Args:
            dx: NumPy array representing perturbation in `x`
            dy: NumPy array representing perturbation in `y`
            ds: NumPy array representing perturbation in `s`

        Returns:
            (`dA`, `db`, `dc`), the result of applying the adjoint to the
            perturbations; the sparsity pattern of `dA` matches that of `A`.
        """
        dw = -(x @ dx + y @ dy + s @ ds)
        dz = np.concatenate(
            [dx, D_proj_dual_cone.rmatvec(dy + ds) - ds,
             np.array([dw])])
        if np.allclose(dz, 0):
            r = np.zeros(dz.shape)
        else:
            r = splinalg.lsqr(cone_lib.transpose_linear_operator(M), dz,
                              **kwargs)[0]

        # dQ is the outer product of pi_z and r. Instead of materializing this,
        # the code below only computes the entries needed to compute dA, db, dc
        values = pi_z[cols] * r[rows + n] - pi_z[n + rows] * r[cols]
        dA = sparse.csc_matrix((values, (rows, cols)), shape=A.shape)
        db = pi_z[n:n + m] * r[-1] - pi_z[-1] * r[n:n + m]
        dc = pi_z[:n] * r[-1] - pi_z[-1] * r[:n]
        return dA, db, dc
예제 #11
0
def run_test(data_low, data_high, x_low, x_high):
    """Generates a test problem and solves it using Scipy's LSQR"""
    _proc.start_processes()

    start_time = time.time()
    A, b, x_real = generate_data(data_low, data_high, x_low, x_high)
    print("generate_data(...) run time:", time.time() - start_time, "seconds")

    _proc.end_processes()

    print("Size of A =", A.shape)
    print()

    start_time = time.time()
    lsmr_result = linalg.lsmr(A, b, maxiter=1000)

    print("linalg.lsmr(A, b) run time:", time.time() - start_time, "seconds")
    print("Number of iterations =", lsmr_result[2])

    x_lsmr = numpy.array([lsmr_result[0]]).T
    print("sum(|x_real - x_lsmr|) =", numpy.sum(numpy.abs(x_real - x_lsmr)))
    print()

    start_time = time.time()
    lsqr_result = linalg.lsqr(A, b, iter_lim=1000)

    print("linalg.lsqr(A, b) run time:", time.time() - start_time, "seconds")
    print("Number of iterations =", lsqr_result[2])

    x_lsqr = numpy.array([lsqr_result[0]]).T
    print("sum(|x_real - x_lsqr|) =", numpy.sum(numpy.abs(x_real - x_lsqr)))
예제 #12
0
def test_Block(par):
    """Dot-test and inversion for Block operator
    """
    np.random.seed(10)
    G11 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype(par['dtype'])
    G12 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype(par['dtype'])
    G21 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype(par['dtype'])
    G22 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype(par['dtype'])

    x = np.ones(2 * par['nx']) + par['imag'] * np.ones(2 * par['nx'])

    Bop = Block([[
        MatrixMult(G11, dtype=par['dtype']),
        MatrixMult(G12, dtype=par['dtype'])
    ],
                 [
                     MatrixMult(G21, dtype=par['dtype']),
                     MatrixMult(G22, dtype=par['dtype'])
                 ]],
                dtype=par['dtype'])
    assert dottest(Bop,
                   2 * par['ny'],
                   2 * par['nx'],
                   complexflag=0 if par['imag'] == 0 else 3)

    xlsqr = lsqr(Bop, Bop * x, damp=1e-20, iter_lim=500, show=0)[0]
    assert_array_almost_equal(x, xlsqr, decimal=3)
예제 #13
0
def dcpf(B, Pbus, Va0, ref, pv, pq):
    """Solves a DC power flow.

    Solves for the bus voltage angles at all but the reference bus, given the
    full system C{B} matrix and the vector of bus real power injections, the
    initial vector of bus voltage angles (in radians), and column vectors with
    the lists of bus indices for the swing bus, PV buses, and PQ buses,
    respectively. Returns a vector of bus voltage angles in radians.

    @see: L{rundcpf}, L{runpf}

    @author: Carlos E. Murillo-Sanchez (PSERC Cornell & Universidad
    Autonoma de Manizales)
    @author: Ray Zimmerman (PSERC Cornell)
    @author: Richard Lincoln
    """
    pvpq = matrix(r_[pv, pq])

    ## initialize result vector
    Va = copy(Va0)

    ## update angles for non-reference buses
    A=B[pvpq.T, pvpq]
    d = det(A.todense())
    if(math.fabs(d) > 0.2):
        Va[pvpq] = spsolve(A, (Pbus[pvpq] - B[pvpq.T, ref] * Va0[ref]).transpose())
    else:
        Va[pvpq] = lsqr(A, (Pbus[pvpq] - B[pvpq.T, ref] * Va0[ref]).transpose())[0]

    return Va
예제 #14
0
def lsqr_cp(aa, bb, guess=None, **kw):
    # implement two speed-ups:
    # 1. "column preconditioning": make sure each column of aa has the same
    #    norm
    # 2. allow guesses

    # column preconditioning is important (substantial speedup), and has
    # been implemented directly in fit_once.

    # allow guesses: solving Ax = b is the same as solving A(x-x*) = b-Ax*.
    # => A(dx) = b-Ax*.  So we can solve for dx instead, then return dx+x*.
    # This improves speed if we reduce the tolerance.
    from scipy.sparse import linalg

    if guess is not None:
        bb2 = bb - aa.dot(guess)
        if 'btol' in kw:
            fac = numpy.sum(bb**2.)**(0.5)/numpy.sum(bb2**2.)**0.5
            kw['btol'] = kw['btol']*numpy.clip(fac, 0.1, 10.)
    else:
        bb2 = bb.copy()

    normbb = numpy.sum(bb2**2.)
    bb2 /= normbb**(0.5)
    par = linalg.lsqr(aa, bb2, **kw)
    # for some reason, everything ends up as double precision after this
    # or lsmr; lsqr seems to be better
    # par[0][:] *= norm**(-0.5)*normbb**(0.5)
    par[0][:] *= normbb**0.5
    if guess is not None:
        par[0][:] += guess
    par = list(par)
    par[0] = par[0].astype('f4')
    par[9] = par[9].astype('f4')
    return par
예제 #15
0
def test_FFT3D(par):
    """Dot-test and inversion for FFTND operator for 3d signal
    """
    decimal = 4 if np.real(np.ones(1, par['dtype'])).dtype == np.float32 else 8

    dt, dx, dy = 0.005, 5, 2
    t = np.arange(par['nt']) * dt
    f0 = 10
    nfft1 = par['nt'] if par['nfft'] is None else par['nfft']
    nfft2 = par['nx'] if par['nfft'] is None else par['nfft']
    nfft3 = par['ny'] if par['nfft'] is None else par['nfft']
    d = np.outer(np.sin(2 * np.pi * f0 * t), np.arange(par['nx']) + 1)
    d = np.tile(d[:, :, np.newaxis], [1, 1, par['ny']])
    d = d.astype(par['dtype'])

    FFTop = FFTND(dims=(par['nt'], par['nx'], par['ny']),
                  nffts=(nfft1, nfft2, nfft3),
                  sampling=(dt, dx, dy))
    assert dottest(FFTop, nfft1*nfft2*nfft3,
                   par['nt']*par['nx']*par['ny'],
                   complexflag=2, tol=10**(-decimal))

    D = FFTop * d.flatten()
    dadj = FFTop.H*D # adjoint is inverse for fft
    dinv = lsqr(FFTop, D, damp=1e-10, iter_lim=100, show=0)[0]

    dadj = np.real(dadj).reshape(par['nt'], par['nx'], par['ny'])
    dinv = np.real(dinv).reshape(par['nt'], par['nx'], par['ny'])

    assert_array_almost_equal(d, dadj, decimal=decimal)
    assert_array_almost_equal(d, dinv, decimal=decimal)
예제 #16
0
def test_FFT_1dsignal(par):
    """Dot-test and inversion for FFT operator for 1d signal
    """
    decimal = 4 if np.real(np.ones(1, par['dtype'])).dtype == np.float32 else 8

    dt = 0.005
    t = np.arange(par['nt']) * dt
    f0 = 10
    x = np.sin(2 * np.pi * f0 * t)
    x = x.astype(par['dtype'])
    nfft = par['nt'] if par['nfft'] is None else par['nfft']
    
    FFTop = FFT(dims=[par['nt']], nfft=nfft, sampling=dt,
                real=par['real'], engine=par['engine'], dtype=par['dtype'])

    # FFT with real=True cannot pass dot-test neither be inverted correctly,
    # see FFT documentation for a detailed explanation. We thus test FFT.H*FFT
    if par['real']:
        FFTop = FFTop.H * FFTop
        assert dottest(FFTop, par['nt'], par['nt'],
                       complexflag=0, tol=10**(-decimal), verb=True)
    else:
        assert dottest(FFTop, nfft, par['nt'],
                       complexflag=2, tol=10**(-decimal), verb=True)
        assert dottest(FFTop, nfft, par['nt'],
                       complexflag=3, tol=10**(-decimal), verb=True)

    y = FFTop * x
    xadj = FFTop.H * y  # adjoint is same as inverse for fft
    xinv = lsqr(FFTop, y, damp=1e-10, iter_lim=10, show=0)[0]

    assert_array_almost_equal(x, xadj, decimal=decimal)
    assert_array_almost_equal(x, xinv, decimal=decimal)
def solve(A, b, method, tol=1e-3):
    """ General sparse solver interface.

    method can be one of
    - spsolve_umfpack_mmd_ata
    - spsolve_umfpack_colamd
    - spsolve_superlu_mmd_ata
    - spsolve_superlu_colamd
    - bicg
    - bicgstab
    - cg
    - cgs
    - gmres
    - lgmres
    - minres
    - qmr
    - lsqr
    - lsmr
    """

    if method == 'spsolve_umfpack_mmd_ata':
        return spla.spsolve(A, b, use_umfpack=True, permc_spec='MMD_ATA')
    elif method == 'spsolve_umfpack_colamd':
        return spla.spsolve(A, b, use_umfpack=True, permc_spec='COLAMD')
    elif method == 'spsolve_superlu_mmd_ata':
        return spla.spsolve(A, b, use_umfpack=False, permc_spec='MMD_ATA')
    elif method == 'spsolve_superlu_colamd':
        return spla.spsolve(A, b, use_umfpack=False, permc_spec='COLAMD')
    elif method == 'bicg':
        res = spla.bicg(A, b, tol=tol)
        return res[0]
    elif method == 'bicgstab':
        res = spla.bicgstab(A, b, tol=tol)
        return res[0]
    elif method == 'cg':
        res = spla.cg(A, b, tol=tol)
        return res[0]
    elif method == 'cgs':
        res = spla.cgs(A, b, tol=tol)
        return res[0]
    elif method == 'gmres':
        res = spla.gmres(A, b, tol=tol)
        return res[0]
    elif method == 'lgmres':
        res = spla.lgmres(A, b, tol=tol)
        return res[0]
    elif method == 'minres':
        res = spla.minres(A, b, tol=tol)
        return res[0]
    elif method == 'qmr':
        res = spla.qmr(A, b, tol=tol)
        return res[0]
    elif method == 'lsqr':
        res = spla.lsqr(A, b, atol=tol, btol=tol)
        return res[0]
    elif method == 'lsmr':
        res = spla.lsmr(A, b, atol=tol, btol=tol)
        return res[0]
    else:
        raise Exception('UnknownSolverType')
예제 #18
0
 def solve_iteration(self, Xdr):
     if self.svd_invA==None:
         self.lsqr_result = linalg.lsqr(self.A, Xdr)
         self.x = self.lsqr_result[0].reshape((self.NDoF()/3,3))
     else:
         svd_x = scipy.dot(self.svd_invA, Xdr)
         self.x = svd_x.reshape((self.NDoF()/3,3))
예제 #19
0
def doFlattening(mesh, quadIdx):
    n = mesh.VPos.shape[0] # N x 3
    k = np.array(quadIdx).shape[0]
    I = []
    J = []
    V = []
    anchors = np.array([[0, 0, 0],
                        [0, 1, 0],
                        [1, 1, 0],
                        [1, 0, 0]])
    # Build sparse Laplacian Matrix coordinates and values
    for i in range(n):
        vertex = mesh.vertices[i]
        neighbors = mesh.vertices[i].getVertexNeighbors()
        indices = map(lambda x: x.ID, neighbors)

        if i in quadIdx:
            I = I + [i]
            J = J + [i]
            V = V + [WEIGHT]
        else:    
            z = len(indices)
            I = I + ([i] * (z + 1)) # repeated row
            J = J + indices + [i] # column indices and this row
            V = V + ([-1 / float(z)] * z) + [1] # negative weights divided by degree and row degree

    L = sparse.coo_matrix((V, (I, J)), shape=(n, n)).tocsr()
    delta = np.zeros((n, 3))
    delta[np.array(quadIdx), :] = WEIGHT * anchors

    # update mesh vertices with least-squares solution
    for i in range(3):
        mesh.VPos[:, i] = lsqr(L, delta[:, i])[0]

    return mesh
예제 #20
0
 def solve_iteration(self, Xdr):
     if self.svd_invA == None:
         self.lsqr_result = linalg.lsqr(self.A, Xdr)
         self.x = self.lsqr_result[0].reshape((self.NDoF() / 3, 3))
     else:
         svd_x = scipy.dot(self.svd_invA, Xdr)
         self.x = svd_x.reshape((self.NDoF() / 3, 3))
예제 #21
0
def equivalent_resistance(g,v1,v2):
    if v1 == v2:
        return 0
    vind = g.new_vertex_property('long')
    c = 0
    for v in g.vertices():
        vind[v] = c
        c += 1
    G = laplacian(g, index=vind)
    n1 = vind[v1]
    n2 = vind[v2]
    r = G[n2]
    s = r.shape[1]
    diff = csr_matrix((np.append([-1],r.data),np.append([n1],r.indices), np.append(np.full(n2+1,r.indptr[0]),np.full(s-n2,r.indptr[1]+1))),shape=G.shape)
    diff += diff.transpose()
    diff -= csr_matrix(([G[n2,n2]],([n2],[n2])),shape=G.shape)
    Gnn = G - diff
    I = np.zeros(s)
    I[n2] = 1
    ans = lsqr(Gnn,I)[0]
    res = -ans[n1]/ans[n2]
    if res < 0:
        return np.inf
    else:
        return res
    def fit(self, show_max_performance = False, logFile = None, loss_tolerance = 1e-6,
            iteration_limit = 50000, damp_coeff=0.0, topK = 300, add_zeros_quota = 0.0, normalize_similarity = False):


        self.logFile = logFile
        self.normalize_similarity = normalize_similarity

        self.add_zeros_quota = add_zeros_quota
        self.topK = topK

        self._generateTrainData_low_ram()


        commonFeatures = self.ICM[self.row_list].multiply(self.ICM[self.col_list])

        linalg_result = linalg.lsqr(commonFeatures, self.data_list, show = False, atol=loss_tolerance, btol=loss_tolerance,
                          iter_lim = iteration_limit, damp=damp_coeff)

        # res = linalg.lsmr(commonFeatures, self.data_list, show = False, atol=loss_tolerance, btol=loss_tolerance,
        #                   maxiter = iteration_limit, damp=damp_coeff)



        self.D_incremental = linalg_result[0].copy()
        self.D_best = linalg_result[0].copy()
        self.epochs_best = 0

        self.loss = linalg_result[3]


        self._compute_W_sparse()
예제 #23
0
def equivalent_resistances(g,node_pairs):
    vind = g.new_vertex_property('long')
    c = 0
    for v in g.vertices():
        vind[v] = c
        c += 1
    G = laplacian(g, index=vind)
    Rmap = {}
    for v1,v2 in node_pairs:
        n1 = vind[v1]
        n2 = vind[v2]
        if n1 == n2:
            continue
        r = G[n2]
        s = r.shape[1]
        diff = csr_matrix((np.append([-1],r.data),np.append([n1],r.indices), np.append(np.full(n2+1,r.indptr[0]),np.full(s-n2,r.indptr[1]+1))),shape=G.shape)
        diff += diff.transpose()
        diff -= csr_matrix(([G[n2,n2]],([n2],[n2])),shape=G.shape)
        Gnn = G - diff
        I = np.zeros(s)
        I[n2] = 1
        ans = lsqr(Gnn,I)[0]
        res = -ans[n1]/ans[n2]
        if res < 0:
            Rmap[(v1,v2)] = np.inf
        else:
            Rmap[(v1,v2)] = res
    return Rmap
예제 #24
0
    def _solve_lsqr(self, A, B, **kwargs):
        """
        Call scipy lsqr

        Parameters
        ----------
        A : rectangular sparse matrix
        B : vector

        Returns
        -------

        """

        lsqrargs = {}
        # lsqrargs['tol'] = 1e-12
        if 'iter_lim' in kwargs:
            logger.info("Using %i maximum iterations" % kwargs['iter_lim'])
            lsqrargs['iter_lim'] = kwargs['iter_lim']
        if 'damp' in kwargs:
            logger.info("Using damping coefficient")
            lsqrargs['damp'] = kwargs['damp']
        if 'atol' in kwargs:
            logger.info('Using a tolerance of %f' % kwargs['atol'])
            lsqrargs['atol'] = kwargs['atol']
        if 'btol' in kwargs:
            logger.info('Using btol of %f' % kwargs['btol'])
            lsqrargs['btol'] = kwargs['btol']
        if 'show' in kwargs:
            lsqrargs['show'] = kwargs['show']
        if 'conlim' in kwargs:
            lsqrargs['conlim'] = kwargs['conlim']
        return sla.lsqr(A, B, **lsqrargs)[0]
예제 #25
0
def test_well_conditioned_problems():
    # Test that sparse the lsqr solver returns the right solution
    # on various problems with different random seeds.
    # This is a non-regression test for a potential ZeroDivisionError
    # raised when computing the `test2` & `test3` convergence conditions.
    n = 10
    A_sparse = scipy.sparse.eye(n, n)
    A_dense = A_sparse.toarray()

    with np.errstate(invalid="raise"):
        for seed in range(30):
            rng = np.random.RandomState(seed + 10)
            beta = rng.rand(n)
            beta[beta == 0] = 0.00001  # ensure that all the betas are not null
            b = A_sparse * beta[:, np.newaxis]
            output = lsqr(A_sparse, b, show=show)

            # Check that the termination condition corresponds to an approximate
            # solution to Ax = b
            assert_equal(output[1], 1)
            solution = output[0]

            # Check that we recover the ground truth solution
            assert_array_almost_equal(solution, beta)

            # Sanity check: compare to the dense array solver
            reference_solution = np.linalg.solve(A_dense, b).ravel()
            assert_array_almost_equal(solution, reference_solution)
예제 #26
0
파일: reference.py 프로젝트: sarahjkim/sgdf
    def poisson_blend(self, source, mask, tinyt, maximum=False):
        """
        Blends the source image into the target using a mask.

        Args:
            source (2D float np.array): Values of source image at relevant blending pixels;
                    same size as mask array (may be smaller than source image)
            mask (2D bool np.array): Mask with 1 (True) values for source pixels
            tinyt (2D float np.array): Values of target image at relevant blending pixels;
                    same size as mask array (may be smaller than target image)

        Returns:
            (2D float np.array): Channel of modified target image with section of source image

        """
        mask_dilated = self.dilate(mask)
        mask_border = mask ^ mask_dilated

        A4 = self.construct_A4(source)
        t_prime = A4.dot(tinyt.ravel())
        s_prime = A4.dot(source.ravel())

        b = t_prime.copy()
        if maximum:
            max_prime = np.maximum(s_prime, t_prime)
            b = self.set_b(b, mask.ravel(), max_prime)
        else:
            b = self.set_b(b, mask.ravel(), s_prime)
        tinyt_values = np.concatenate([tinyt.ravel(), tinyt.ravel(), tinyt.ravel(), tinyt.ravel()])
        b = self.set_b(b, mask_border.ravel(), tinyt_values)

        A4 = self.construct_A4(source, mask_border=mask_border)
        imh, imw = source.shape
        v = lsqr(A4, b)[0]
        return v.reshape((imh, imw)).clip(0, 1)
예제 #27
0
파일: test_ffts.py 프로젝트: mrava87/pylops
def test_FFT2D_random_real(par):
    shape = par["shape"]
    dtype, decimal = par["dtype_precision"]
    ifftshift_before = par["ifftshift_before"]
    engine = par["engine"]

    x = np.random.randn(*shape).astype(dtype)

    # Select an axis to apply FFT on. It can be any integer
    # in [0,..., ndim-1] but also in [-ndim, ..., -1]
    # However, dimensions cannot be repeated
    axes = _choose_random_axes(x.ndim, n_choices=2)

    FFTop = FFT2D(
        dims=x.shape,
        dirs=axes,
        ifftshift_before=ifftshift_before,
        real=True,
        dtype=dtype,
        engine=engine,
    )
    x = x.ravel()
    y = FFTop * x

    # Ensure inverse and adjoint recover x
    xadj = FFTop.H * y  # adjoint is same as inverse for fft
    xinv = lsqr(FFTop, y, damp=0, iter_lim=10, show=0)[0]
    assert_array_almost_equal(x, xadj, decimal=decimal)
    assert_array_almost_equal(x, xinv, decimal=decimal)

    # Dot tests
    nr, nc = FFTop.shape
    assert dottest(FFTop, nr, nc, complexflag=0, tol=10**(-decimal))
    assert dottest(FFTop, nr, nc, complexflag=2, tol=10**(-decimal))
예제 #28
0
    def derivative(dA, db, dc, **kwargs):
        """Applies derivative at (A, b, c) to perturbations dA, db, dc

        Args:
            dA: SciPy sparse matrix in CSC format; must have same sparsity
                pattern as the matrix `A` from the cone program
            db: NumPy array representing perturbation in `b`
            dc: NumPy array representing perturbation in `c`

        Returns:
           NumPy arrays dx, dy, ds, the result of applying the derivative
           to the perturbations.
        """
        dQ = sparse.bmat(
            [[None, dA.T, np.expand_dims(dc, -1)],
             [-dA, None, np.expand_dims(db, -1)],
             [-np.expand_dims(dc, -1).T, -np.expand_dims(db, -1).T, None]])
        # can ignore w since w = 1
        rhs = dQ @ pi_z
        if np.allclose(rhs, 0):
            dz = np.zeros(rhs.size)
        else:
            dz = splinalg.lsqr(M, rhs, **kwargs)[0]
        du, dv, dw = np.split(dz, [n, n + m])
        dx = du - x * dw
        dy = D_proj_dual_cone @ dv - y * dw
        ds = D_proj_dual_cone @ dv - dv - s * dw
        return -dx, -dy, -ds
예제 #29
0
def test_VStack(par):
    """Dot-test and inversion for VStack operator"""
    np.random.seed(0)
    G1 = np.random.normal(0, 10, (par["ny"], par["nx"])).astype(par["dtype"])
    G2 = np.random.normal(0, 10, (par["ny"], par["nx"])).astype(par["dtype"])
    x = np.ones(par["nx"]) + par["imag"] * np.ones(par["nx"])

    Vop = VStack(
        [MatrixMult(G1, dtype=par["dtype"]), MatrixMult(G2, dtype=par["dtype"])],
        dtype=par["dtype"],
    )
    assert dottest(
        Vop, 2 * par["ny"], par["nx"], complexflag=0 if par["imag"] == 0 else 3
    )

    xlsqr = lsqr(Vop, Vop * x, damp=1e-20, iter_lim=300, atol=1e-8, btol=1e-8, show=0)[
        0
    ]
    assert_array_almost_equal(x, xlsqr, decimal=4)

    # use numpy matrix directly in the definition of the operator
    V1op = VStack([G1, MatrixMult(G2, dtype=par["dtype"])], dtype=par["dtype"])
    assert dottest(
        V1op, 2 * par["ny"], par["nx"], complexflag=0 if par["imag"] == 0 else 3
    )

    # use scipy matrix directly in the definition of the operator
    G1 = sp_random(par["ny"], par["nx"], density=0.4).astype("float32")
    V2op = VStack([G1, MatrixMult(G2, dtype=par["dtype"])], dtype=par["dtype"])
    assert dottest(
        V2op, 2 * par["ny"], par["nx"], complexflag=0 if par["imag"] == 0 else 3
    )
예제 #30
0
    def run_lsqr(self,logdamp,scale=True,**kwargs):
        # scale the log(damp) into a damping
        if logdamp is None:
            damp=0.
        else:
            damp=10.**logdamp
            if scale:
                damp*=self.frob   # scale the damping by frobenius

        print('[info]Running LSQR with l={}'.format(damp))
        
        t1=default_timer()
        r=ssl.lsqr(self.A,self.bi,damp=damp,calc_var=True,**kwargs)
        
        #atol=atol,btol=btol,conlim=conlim,iter_lim=maxiter)#
        
                   #,show=show,
        print(r[1],r[2])
        t2=default_timer()
        dt=(t2-t1)/60.    # in minutes

        # take the target out
        if self.target is not None:
            r=(r[0]+self.target,*r[1:])
            
        # package the outputs
        r=LSQRResult(*r,r[-1].copy(),damp/self.frob)
        
        # update the Lcurve
        self.lcurve.append(r.r1norm,r.xnorm,r.logdamp)
        

        return r
예제 #31
0
    def backward(self, x, tau):
        # (1 + tau A^T A)^-1(x + tau A^T b)
        # which amounts to
        #   min_y ||A y - b||^2_F + tau * || y - x ||

        # TODO solve the dual when we have fat matrix

        if hasattr(self.A, 'A') and type(self.A.A) is _np.ndarray:
            # self.A is a dense matrix
            # we can pre-factorize the system using cholesky decomposition
            # and then quickly re-solve the system
            if self._solve_backward is None or self._solve_backward_tau != tau:
                from scipy.linalg import cho_factor, cho_solve

                A = self.A.A
                H = tau * A.T.dot(A) + _np.eye(A.shape[1])
                self._solve_backward = partial(cho_solve, cho_factor(H))
                self._solve_backward_tau = tau

            return self._solve_backward(x + tau * self.A.rmatvec(self.b))

        else:
            from scipy.sparse.linalg import lsqr, LinearOperator

            def matvec(y):
                return y + tau * self.A.rmatvec(self.A.matvec(y))
            x, istop, itn, r1norm, r2norm, anorm, acond, arnorm, xnorm, var = \
                lsqr(LinearOperator((self.A.shape[1], self.A.shape[1]), matvec, matvec),
                     x + tau * self.A.rmatvec(self.b))
            return x
예제 #32
0
def test_HStack(par):
    """Dot-test and inversion for HStack operator with numpy array as input
    """
    np.random.seed(0)
    G1 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype('float32')
    G2 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype('float32')
    x = np.ones(2 * par['nx']) + par['imag'] * np.ones(2 * par['nx'])

    Hop = HStack([G1, MatrixMult(G2, dtype=par['dtype'])], dtype=par['dtype'])
    assert dottest(Hop,
                   par['ny'],
                   2 * par['nx'],
                   complexflag=0 if par['imag'] == 0 else 3)

    xlsqr = lsqr(Hop, Hop * x, damp=1e-20, iter_lim=300, show=0)[0]
    assert_array_almost_equal(x, xlsqr, decimal=4)

    # use numpy matrix directly in the definition of the operator
    H1op = HStack([G1, MatrixMult(G2, dtype=par['dtype'])], dtype=par['dtype'])
    assert dottest(H1op,
                   par['ny'],
                   2 * par['nx'],
                   complexflag=0 if par['imag'] == 0 else 3)

    # use scipy matrix directly in the definition of the operator
    G1 = sp_random(par['ny'], par['nx'], density=0.4).astype('float32')
    H2op = HStack([G1, MatrixMult(G2, dtype=par['dtype'])], dtype=par['dtype'])
    assert dottest(H2op,
                   par['ny'],
                   2 * par['nx'],
                   complexflag=0 if par['imag'] == 0 else 3)
예제 #33
0
def test_well_conditioned_problems():
    # Test that sparse the lsqr solver returns the right solution
    # on various problems with different random seeds.
    # This is a non-regression test for a potential ZeroDivisionError
    # raised when computing the `test2` & `test3` convergence conditions.
    n = 10
    A_sparse = scipy.sparse.eye(n, n)
    A_dense = A_sparse.toarray()

    with np.errstate(invalid='raise'):
        for seed in range(30):
            rng = np.random.RandomState(seed + 10)
            beta = rng.rand(n)
            beta[beta == 0] = 0.00001  # ensure that all the betas are not null
            b = A_sparse * beta[:, np.newaxis]
            output = lsqr(A_sparse, b, show=show)

            # Check that the termination condition corresponds to an approximate
            # solution to Ax = b
            assert_equal(output[1], 1)
            solution = output[0]

            # Check that we recover the ground truth solution
            assert_allclose(solution, beta)

            # Sanity check: compare to the dense array solver
            reference_solution = np.linalg.solve(A_dense, b).ravel()
            assert_allclose(solution, reference_solution)
def get_transformations_and_masks(triangulation, average_landmarks, original_landmarks):
    transformations = {}
    masks = {}
    for idx, triangle in enumerate(triangulation.simplices):
        orig_verts = np.take(original_landmarks, triangle, axis=0).astype('int32')
        average_verts = np.take(average_landmarks, triangle, axis=0).astype('int32')

        A = lil_matrix((6,6))
        b = np.zeros((6, 1))
        equation = 0
        for idx2, vert in enumerate(orig_verts):
            A[equation, :] = [vert[0], vert[1], 1, 0, 0, 0]
            A[equation+1, :] = [0, 0, 0, vert[0], vert[1], 1]
            b[equation] = average_verts[idx2][0]
            b[equation+1] = average_verts[idx2][1]
            equation += 2

        A = A.tocsr()
        results = linalg.lsqr(A, b, atol=1e-13, btol=1e-13)
        v = results[0]
        M = v.reshape((2,3))
        transformations[idx] = M

        mask = get_image_mask(900, 1200, orig_verts)
        masks[idx] = mask
    return transformations, masks
예제 #35
0
def test_Convolve3D(par):
    """Dot-test and inversion for ConvolveND operator
    """
    # 3D on 3D
    Cop = ConvolveND(par['nz'] * par['ny'] * par['nx'],
                     h=h3,
                     offset=par['offset'],
                     dims=[par['nz'], par['ny'], par['nx']],
                     dtype='float32')
    assert dottest(Cop, par['nz'] * par['ny'] * par['nx'],
                   par['nz'] * par['ny'] * par['nx'])

    x = np.zeros((par['nz'], par['ny'], par['nx']))
    x[int(par['nz'] / 2 - 3):int(par['nz'] / 2 + 3),
      int(par['ny'] / 2 - 3):int(par['ny'] / 2 + 3),
      int(par['nx'] / 2 - 3):int(par['nx'] / 2 + 3)] = 1.
    x = x.flatten()
    y = Cop * x
    xlsqr = lsqr(Cop, y, damp=1e-20, iter_lim=400, show=0)[0]
    # due to ringing in solution we cannot use assert_array_almost_equal
    assert np.linalg.norm(xlsqr - x) / np.linalg.norm(xlsqr) < 2e-1

    # 3D on 4D (only modelling)
    Cop = ConvolveND(par['nz'] * par['ny'] * par['nx'] * par['nt'],
                     h=h3,
                     offset=par['offset'],
                     dims=[par['nz'], par['ny'], par['nx'], par['nt']],
                     dirs=[0, 1, 2],
                     dtype='float32')
    assert dottest(Cop, par['nz'] * par['ny'] * par['nx'] * par['nt'],
                   par['nz'] * par['ny'] * par['nx'] * par['nt'])
예제 #36
0
	def solveFunctionWithConstraints(self, constraints, deltaCoords, g, overwriteRows = None):
		#TODO: Implement overwriteRows
		(I, J, V) = self.getLaplacianSparseMatrixCoords(overwriteRows, LaplaceBeltramiWeightFunc)
		NVerts = len(self.vertices)
		NConstraints = g.shape[0]
		Y = deltaCoords.shape[1]
		for i in range(NConstraints):
			constraint = constraints[i]
			for elem in constraint:
				(index, val) = elem
				I.append(i + NVerts)
				J.append(index)
				V.append(val)
		N = NVerts + NConstraints
		M = NVerts
		b = deltaCoords
		if g.shape[0] > 0:
			b = np.append(deltaCoords, g, axis = 0)
		I = np.array(I)
		J = np.array(J)
		V = np.array(V)
		#print "V.shape = %s, N = %i, M = %i"%(V.shape, N, M)
		A = sparse.coo_matrix((V, (I, J)), shape=(N,M)).tocsr()
		ret = np.zeros((NVerts, Y))
		for i in range(Y):
			print "Solving column %i..."%i
			thisColumn = lsqr(A, b[:, i])[0]
			ret[:, i] = thisColumn
		return ret
예제 #37
0
    def _calculate(self, graph1, graph2, i, j):
        if self.is_training and (self.res[j, i] > 0 or self.res[i, j] > 0):
            return

        # norm1, norm2 - normalized adjacency matrixes
        # norm1 = _norm(graph1)
        # norm2 = _norm(graph2)
        norm1 = graph1
        norm2 = graph2

        # if graph is unweighted, W_prod = kron(a_norm(g1)*a_norm(g2))
        w_prod = kron(lil_matrix(norm1), lil_matrix(norm2))
        starting_prob = np.ones(w_prod.shape[0]) / (w_prod.shape[0])
        stop_prob = starting_prob

        # first solve (I - lambda * W_prod) * x = p_prod
        A = identity(w_prod.shape[0]) - (w_prod * self._lmb)
        x = lsqr(A, starting_prob)

        self.res[i, j] = stop_prob.T.dot(x[0])

        if self.is_training:
            self.res[j, i] = self.res[i, j]

        print 'Kernel: ', i, ', ', j, self.res[i, j]
예제 #38
0
    def fit(self, X, y):
        """
        Fit linear model.

        Parameters
        ----------
        X : numpy array or sparse matrix of shape [n_samples,n_features]
            Training data
        y : numpy array of shape [n_samples]
            Target values
        Returns
        -------
        self : returns an instance of self.
        """
        X = safe_asarray(X)
        y = np.asarray(y)

        X, y, X_mean, y_mean, X_std = self._center_data(X, y,
                self.fit_intercept, self.normalize, self.copy_X)

        if sp.issparse(X):
            if hasattr(sp_linalg, 'lsqr'):
                out = sp_linalg.lsqr(X, y)
                self.coef_ = out[0]
                self.residues_ = out[3]
            else:
                # DEPENDENCY: scipy 0.7
                self.coef_ = sp_linalg.spsolve(X, y)
                self.residues_ = y - safe_sparse_dot(X, self.coef_)
        else:
            self.coef_, self.residues_, self.rank_, self.singular_ = \
                    linalg.lstsq(X, y)

        self._set_intercept(X_mean, y_mean, X_std)
        return self
예제 #39
0
    def backward(self, x, tau):
        # (1 + tau A^T A)^-1(x + tau A^T b)
        # which amounts to
        #   min_y ||A y - b||^2_F + tau * || y - x ||

        # TODO solve the dual when we have fat matrix

        if hasattr(self.A, 'A') and type(self.A.A) is _np.ndarray:
            # self.A is a dense matrix
            # we can pre-factorize the system using cholesky decomposition
            # and then quickly re-solve the system
            if self._solve_backward is None or self._solve_backward_tau != tau:
                from scipy.linalg import cho_factor, cho_solve

                A = self.A.A
                H = tau * A.T.dot(A) + _np.eye(A.shape[1])
                self._solve_backward = partial(cho_solve, cho_factor(H))
                self._solve_backward_tau = tau

            return self._solve_backward(x + tau * self.A.rmatvec(self.b))

        else:
            from scipy.sparse.linalg import lsqr, LinearOperator

            def matvec(y):
                return y + tau * self.A.rmatvec(self.A.matvec(y))
            x, istop, itn, r1norm, r2norm, anorm, acond, arnorm, xnorm, var = \
                lsqr(LinearOperator((self.A.shape[1], self.A.shape[1]), matvec, matvec),
                     x + tau * self.A.rmatvec(self.b))
            return x
예제 #40
0
def poisson_blend(s, s_mask, tinyt, t, tinyt_topleft, maximum=False):
    s_inside = inside(s_mask)
    s_border = s_mask & ~s_inside
    s_outside = ~s_inside

    A4 = construct_A4(s)
    t_prime = A4.dot(tinyt.ravel())
    s_prime = A4.dot(s.ravel())

    b = t_prime.copy()
    if maximum == True:
        max_prime = np.maximum(s_prime, t_prime)
        b = set_b(b, s_inside.ravel(), max_prime)
    else:
        b = set_b(b, s_inside.ravel(), s_prime)
    tinyt_values = np.concatenate([tinyt.ravel(), tinyt.ravel(), tinyt.ravel(), tinyt.ravel()])
    b = set_b(b, s_border.ravel(), tinyt_values)

    A4 = construct_A4(s, s_border=s_border)
    imh, imw = s.shape
    v = lsqr(A4, b)[0]
    out = v.reshape((imh, imw))

    tttly, tttlx = tinyt_topleft
    tty, ttx = tinyt.shape
    t[tttly:tttly + tty, tttlx:tttlx + ttx] = out
    return t
예제 #41
0
    def _solve_sym_nonposdef(self, Q, q):
        # since Q is indefinite, i.e., the function is linear along the eigenvectors
        # correspondent to the null eigenvalues, the system has not solutions, so we
        # will choose the one that minimizes the residue, i.e. the least-squares solution
        # see more @ https://docs.scipy.org/doc/scipy/reference/sparse.linalg.html#solving-linear-problems

        # bad numerical solution: does not exploit the symmetricity of Q, waiting for `symmlq` in scipy
        if self.sym_nonposdef_solver == 'lsqr':

            x = lsqr(Q, -q)[0]

        else:

            # `min ||Ax - b||` is formally equivalent to solve the linear system:
            #                           A^T A x = A^T b

            Q, q = np.inner(Q, Q), Q.T.dot(q)

            if self.sym_nonposdef_solver == 'minres':

                x = minres(Q, -q)[0]

            else:

                raise TypeError(
                    f'{self.sym_nonposdef_solver} is not an allowed solver, '
                    f'choose one of `minres` or `lsqr`')

        return x
예제 #42
0
    def infer_scene(self, data):
        """
        Take data and a current belief about the PSF; infer the scene for
        this image given the PSF.

        ## Arguments

        * `data` (numpy.ndarray): The data.

        ## Returns

        * `new_scene` (numpy.ndarray): The scene implied by this particular
          data alone.

        ## Note

        This method has been deprecated by `get_dlds` and the proper
        stochastic gradient update. I'm keeping this here for now just in
        case.

        """
        # Infer scene and return
        data_vector = np.append(data.flatten(), np.zeros(self.scene.size))
        results = lsqr(self.get_psf_matrix(L2=True), data_vector)

        new_scene = results[0].reshape(self.scene.shape)

        return new_scene
예제 #43
0
 def __truediv__(self, y, niter=100):
     if self.explicit is True:
         if sp.sparse.issparse(self.A):
             # use scipy solver for sparse matrices
             xest = spsolve(self.A, y)
         elif isinstance(self.A, np.ndarray):
             # use scipy solvers for dense matrices (used for backward
             # compatibility, could be switched to numpy equivalents)
             if self.A.shape[0] == self.A.shape[1]:
                 xest = solve(self.A, y)
             else:
                 xest = lstsq(self.A, y)[0]
         else:
             # use numpy/cupy solvers for dense matrices
             ncp = get_array_module(y)
             if self.A.shape[0] == self.A.shape[1]:
                 xest = ncp.linalg.solve(self.A, y)
             else:
                 xest = ncp.linalg.lstsq(self.A, y)[0]
     else:
         if isinstance(y, np.ndarray):
             # numpy backend
             xest = lsqr(self, y, iter_lim=niter)[0]
         else:
             # cupy backend
             ncp = get_array_module(y)
             xest = cgls(self,
                         y,
                         x0=ncp.zeros(int(self.shape[1]), dtype=self.dtype),
                         niter=niter)[0]
     return xest
예제 #44
0
파일: face.py 프로젝트: karla3jo/menpo-old
 def lsqr_heat_phi_about_lm(self, landmark_key):
   geo = self.geodesics_about_lm(landmark_key, method='heat')
   div_X = geo['div_X']
   L_c = self._cache['laplacian_cotangent']
   lsqr_solution = sparse_linalg.lsqr(L_c, div_X, show=True)
   phi_lsqr = lsqr_solution[0]
   phi_lsqr = phi_lsqr - phi_lsqr[self.landmarks[landmark_key]]
   return phi_lsqr
예제 #45
0
def compute_lsqr(s):
    imh, imw = s.shape
    A2 = construct_A2(s)
    b = A2.dot(s.ravel())
    v = lsqr(A2, b)[0]
    return v.reshape((imh, imw))
    
    
예제 #46
0
def extract_w_paralell(img, mlist):
    '''
    :param img: <fits>
    :param mlist: <list> one element of the csr_matrix
    :return: <ndarray> result of lsqr
    '''
    x = lsqr(mlist, img)
    return x[0]
예제 #47
0
def doFlattening(mesh, quadIdxs):
    if len(quadIdxs) != 4:
        print "please select 4 points"
        return
    L = getLaplacianMatrixHelp(mesh, quadIdxs, umbrellaWeight, True)
    delta = np.zeros((len(mesh.vertices), 3))
    delta[quadIdxs, :] = [[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]]
    for col in range(3):
        mesh.VPos[:, col] = lsqr(L, delta[:, col])[0]
예제 #48
0
def extract_w_paralell(img, mlist):
    '''
    :param img: <fits>
    :param mlist: <list> one element of the csr_matrix
    :return: <ndarray> result of lsqr
    '''
    from scipy.sparse.linalg import lsqr
    x = lsqr(mlist, img)
    return x[0]
예제 #49
0
def solveLaplacianMesh(mesh, anchors, anchorsIdx):
    N = len(mesh.vertices)
    K = len(anchorsIdx)
    L = getLaplacianMatrixUmbrella(mesh, anchorsIdx)
    #L = getLaplacianMatrixCotangent(mesh, anchorsIdx)
    delta = np.array(L.dot(mesh.VPos))
    for i in range(0, K):
        delta[i+N, :] = anchors[i]
    for j in range(3):
        mesh.VPos[:, j] = lsqr(L, delta[:, j])[0]
예제 #50
0
파일: bsplines.py 프로젝트: caomw/r4r
    def interpolate(self,t,pts):
        """
        Initialize control points by interpolation.
        """
        A = self.assemble_interpolation_matrix(t) 
        
        for i in range(0,self.d):

            result = lsqr(A,pts[i])
            self.cp[i,:] = result[0]
예제 #51
0
 def beginDeformationTransfer(self):
     resultFrames = np.empty([self.NFrames, self.NVertices, 3])  # this is result array to fill in
     resultFrames[0, :, :] = self.warpedFrames[0, :, :]
     origOldVPos4 = self.getVPos4(self.origFrames[0, :, :], self.ITris)  # old VPos with extra NFaces vectors
     warpedOldVPos4 = self.getVPos4(self.warpedFrames[0, :, :], self.ITris)
     for i in range(1, self.NFrames):
         # 1 orig: get newVPos4
         origNewVPos4 = self.getVPos4(self.origFrames[i, :, :], self.ITris)
         # 2 orig: use old and new VPos4 to get S-matrix which shape is 3 x 3NFaces
         S = self.getSMatrix(origOldVPos4, origNewVPos4, self.Tris4)
         # 3 warped: use old VPos4 to get A (coefficient) sparse matrix which shape is 3NFaces x NVertices
         A = self.getAMatrix(warpedOldVPos4, self.Tris4)
         origOldVPos4 = origNewVPos4
         warpedOldVPos4[:, 0] = lsqr(A, S[0, :])[0]
         warpedOldVPos4[:, 1] = lsqr(A, S[1, :])[0]
         warpedOldVPos4[:, 2] = lsqr(A, S[2, :])[0]
        # print "new VPos4 shape:", warpedOldVPos4[np.arange(self.NVertices), :].shape
         resultFrames[i, :, :] = warpedOldVPos4[np.arange(self.NVertices), :]
     self.warpedVideo.Frames = resultFrames
예제 #52
0
def depths(mask, normals):
    """Reconstructs the depths from normals.
    
    Args:
        normals: width x height x 3 array
    """
    width, height, three = normals.shape
    assert three == 3
    m = dok_matrix((width*height*2, width*height), dtype=float)
    b = np.zeros(width*height*2, dtype=float)
    log.debug('maximal shape: %s', m.shape)
    row = 0
    coords = ConsistentBimap()
    for x in range(width):
        for y in range(height):
            if not mask[x,y]: continue
            elif not (mask[x+1,y] and mask[x,y+1] and mask[x-1,y] and mask[x,y-1]):
                continue
            else:
                # n_z (z(x+1, y) - z(x, y)) = -n_x
                m[row, coords[(x+1,y)]] = 1
                m[row, coords[(x,y)]] = -1
                b[row] = normals[x,y,X]/normals[x,y,Z]
                row += 1

                # n_z (z(x, y+1) - z(x, y)) = -n_y
                m[row, coords[(x,y+1)]] = 1
                m[row, coords[(x,y)]] = -1
                b[row] = normals[x,y,Y]/normals[x,y,Z]
                row += 1

    # Now we know how many pixels are used and we restrict the matrix to the
    # rows needed.
    m_p = dok_matrix((row+1, coords.i), dtype=float)

    for (x,y), v in m.items():
        try:
            m_p[x,y] = v
        except Exception as e:
            log.error('error at (%s, %s)', x, y)
            raise
    # normalization
    m_p[row,0] = 1
    m_p = m_p.tocsr()
    b = b[:row+1]
    log.debug('actual shape: %s', m_p.shape)
    s = lsqr(m_p, b, atol=1e-3, btol=1e-6, show=True)
    z_p = s[0]
    z_p = normalize(z_p)
    z = np.zeros((width, height))
    for row,(x,y) in coords.r.items():
        z[x,y] = z_p[row]
    log.debug('z(0,0) = %s', z[0,0])
    return z
예제 #53
0
def doFlattening(mesh, quadIdxs):
    N = mesh.VPos.shape[0]
    L = getLaplacianMatrixUmbrella(mesh, [])
    vertices = [[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]]
    delta = np.zeros((N, 3))
    for i in range(4):
        L[quadIdxs[i], :] = 0
        L[quadIdxs[i], quadIdxs[i]] = 1
        delta[quadIdxs[i], :] = vertices[i]
    for j in range(3):
        mesh.VPos[:, j] = lsqr(L, delta[:, j])[0]
예제 #54
0
def solveLaplacianMesh(VPos, ITris, anchorsIdx, anchors):
    N = VPos.shape[0]
    L = getLaplacianMatrixCotangent(VPos, ITris, anchorsIdx)
    delta = L.dot(VPos)
    delta[N:, :] = anchors
    sio.savemat("System.mat", {"L":L, "delta":delta})
    VPosNew = np.zeros((N, 3))
    for k in range(3):
        print "Solving Laplacian mesh coordinates %i of %i..."%(k+1, 3)
        VPosNew[:, k] = lsqr(L, delta[:, k])[0]
    return VPosNew
예제 #55
0
def aper_extract(model_map, wcols, img):
    from scipy.sparse.linalg import lsqr

    n0 = model_map.total_fibers
    n1 = img.shape[1]
    rss = numpy.zeros((n0, n1))
    for key, val in wcols.items():
        yl = img[:, key]
        res = lsqr(val, yl)
        rss[:, key] = res[0]

    return rss
예제 #56
0
파일: heating.py 프로젝트: bluecube/heating
    def learn(self, state_iterator):

        print("building the input matrix from states")
        A, b = self._build_equations(state_iterator)

        print("shape: A: {}, b: {}".format(A.shape, b.shape))

        print("converting A to CSR format")
        A = A.tocsr()

        print("least squares")
        self.x = lsqr(A, b)[0]
예제 #57
0
파일: bsplines.py 프로젝트: caomw/r4r
 def interpolate(self,A,t,pts):
     """
     Initialize control points by interpolation.
     """
     if(len(pts)!=self.d):
         print "Dimension mismatch..."
         return
     
     for i in range(0,self.d):
         result = lsqr(A,pts[i])
         
         self.cp[i,:,:] = np.reshape(result[0],(self.cp.shape[1],self.cp.shape[2]))                
 def lamed(self):
     """lamed, the Phoenician name for lambda. (As the word lambda is 
     reserved in Python.)"""
     if self._lamed is None:
         eH = powerflow(self.ev, self.extY)
         resid = self.eb - eH
         rresid = resid[self.reindr2eindr]
         
         out = spsl.lsqr(self.C, rresid, atol=1e-14, btol=1e-14)
         # assert out[3] < 1e-8
         self._lamed = np.matrix(out[0]).T
     return self._lamed
예제 #59
0
def _solve_lsqr(X, y, alpha, max_iter=None, tol=1e-3):
    n_samples, n_features = X.shape
    coefs = np.empty((y.shape[1], n_features))

    # According to the lsqr documentation, alpha = damp^2.
    sqrt_alpha = np.sqrt(alpha)

    for i in range(y.shape[1]):
        y_column = y[:, i]
        coefs[i] = sp_linalg.lsqr(X, y_column, damp=sqrt_alpha[i], atol=tol, btol=tol, iter_lim=max_iter)[0]

    return coefs