Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9101209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:04:02+00:00 2026-06-17T01:04:02+00:00

I am using scipy.sparse.linalg.cg to solve a large, sparse linear system, and it works

  • 0

I am using scipy.sparse.linalg.cg to solve a large, sparse linear system, and it works fine, except that I would like to add a progress report, so that I can monitor the residual as the solver works. I’ve managed to set up a callback, but I can’t figure out how to access the current residual from inside the callback. Calculating the residual myself is possible, of course, but that is a rather heavy operation, which I’d like to avoid. Have I missed something, or is there no efficient way of getting at the residual?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:04:03+00:00Added an answer on June 17, 2026 at 1:04 am

    The callback is only sent xk, the current solution vector. So you don’t have direct access to the residual. However, the source code shows resid is a local variable in the cg function.

    So, with CPython, it is possible to use the inspect module to peek at the local variables in the caller’s frame:

    import inspect
    import numpy as np
    import scipy as sp
    import scipy.sparse as sparse
    import scipy.sparse.linalg as splinalg
    import random
    
    def report(xk):
        frame = inspect.currentframe().f_back
        print(frame.f_locals['resid'])
    
    N = 200
    A = sparse.lil_matrix( (N, N) )
    for _ in xrange(N):
        A[random.randint(0, N-1), random.randint(0, N-1)] = random.randint(1, 100)
    
    b = np.random.randint(0, N-1, size = N)
    x, info = splinalg.cg(A, b, callback = report)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large matrix that I would like to convert to sparse CSR
I'm using scipy.linalg to solve a matrix equation A*x = b The following code
I am using Scipy to construct a large, sparse (250k X 250k) co-occurrence matrix
Problem I would like to compute the following using numpy or scipy: Y =
I have a large csv of similarities between keywords that I would like to
I am working on an FEM project using Scipy. Now my problem is, that
If I use scipy.linalg.solve (which I believe calls LAPACK's gesv function) on a ~12000
So, I'm doing some Kmeans classification using numpy arrays that are quite sparse-- lots
Using scipy.stats.norm to generate a random sample, then running that through scipy.stats.normaltest produces wildly
I am using scipy intensively for various purposes. I have noticed that the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.