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 9143871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:07:50+00:00 2026-06-17T10:07:50+00:00

This is a follow up to How to set up and solve simultaneous equations

  • 0

This is a follow up to How to set up and solve simultaneous equations in python but I feel deserves its own reputation points for any answer.

For a fixed integer n, I have a set of 2(n-1) simultaneous equations as follows.

M(p) = 1+((n-p-1)/n)*M(n-1) + (2/n)*N(p-1) + ((p-1)/n)*M(p-1)

N(p) = 1+((n-p-1)/n)*M(n-1) + (p/n)*N(p-1)

M(1) = 1+((n-2)/n)*M(n-1) + (2/n)*N(0)

N(0) = 1+((n-1)/n)*M(n-1)

M(p) is defined for 1 <= p <= n-1. N(p) is defined for 0 <= p <= n-2. Notice also that p is just a constant integer in every equation so the whole system is linear.

Some very nice answers were given for how to set up a system of equations in python. However, the system is sparse and I would like to solve it for large n. How can I use scipy’s sparse matrix representation and http://docs.scipy.org/doc/scipy/reference/sparse.linalg.html for example instead?

  • 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-17T10:07:51+00:00Added an answer on June 17, 2026 at 10:07 am

    I wouldn’t normally keep beating a dead horse, but it happens that my non-vectorized approach to solving your other question, has some merit when things get big. Because I was actually filling the coefficient matrix one item at a time, it is very easy to translate into COO sparse matrix format, which can efficiently be transformed to CSC and solved. The following does it:

    import scipy.sparse
    
    def sps_solve(n) :
        # Solution vector is [N[0], N[1], ..., N[n - 2], M[1], M[2], ..., M[n - 1]]
        n_pos = lambda p : p
        m_pos = lambda p : p + n - 2
        data = []
        row = []
        col = []
        # p = 0
        # n * N[0] + (1 - n) * M[n-1] = n
        row += [n_pos(0), n_pos(0)]
        col += [n_pos(0), m_pos(n - 1)]
        data += [n, 1 - n]
        for p in xrange(1, n - 1) :
            # n * M[p] + (1 + p - n) * M[n - 1] - 2 * N[p - 1] +
            #  (1 - p) * M[p - 1] = n
            row += [m_pos(p)] * (4 if p > 1 else 3)
            col += ([m_pos(p), m_pos(n - 1), n_pos(p - 1)] +
                    ([m_pos(p - 1)] if p > 1 else []))
            data += [n, 1 + p - n , -2] + ([1 - p] if p > 1 else [])
            # n * N[p] + (1 + p -n) * M[n - 1] - p * N[p - 1] = n
            row += [n_pos(p)] * 3
            col += [n_pos(p), m_pos(n - 1), n_pos(p - 1)]
            data += [n, 1 + p - n, -p]
        if n > 2 :
            # p = n - 1
            # n * M[n - 1] - 2 * N[n - 2] + (2 - n) * M[n - 2] = n
            row += [m_pos(n-1)] * 3
            col += [m_pos(n - 1), n_pos(n - 2), m_pos(n - 2)]
            data += [n, -2, 2 - n]
        else :
            # p = 1 
            # n * M[1] - 2 * N[0] = n
            row += [m_pos(n - 1)] * 2
            col += [m_pos(n - 1), n_pos(n - 2)]
            data += [n, -2]
        coeff_mat = scipy.sparse.coo_matrix((data, (row, col))).tocsc()
        return scipy.sparse.linalg.spsolve(coeff_mat,
                                           np.ones(2 * (n - 1)) * n)
    

    It is of course much more verbose than building it from vectorized blocks, as TheodorosZelleke does, but an interesting thing happens when you time both approaches:

    enter image description here

    First, and this is (very) nice, time is scaling linearly in both solutions, as one would expect from using the sparse approach. But the solution I gave in this answer is always faster, more so for larger ns. Just for the fun of it, I also timed TheodorosZelleke’s dense approach from the other question, which gives this nice graph showing the different scaling of both types of solutions, and how very early, somewhere around n = 75, the solution here should be your choice:

    enter image description here

    I don’t know enough about scipy.sparse to really figure out why the differences between the two sparse approaches, although I suspect heavily of the use of LIL format sparse matrices. There may be some very marginal performance gain, although a lot of compactness in the code, by turning TheodorosZelleke’s answer into COO format. But that is left as an exercise for the OP!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I follow this tutorial online exactly but somehow it's giving me errors. Saying there
I follow this rule but some of my colleagues disagree with it and argue
I tried to follow this but the default modelbinder let my array null on
For a fixed integer n , I have a set of 2(n-1) simultaneous equations
I have looked at past questions on this but I can't solve my problem
Follow-to this question: The Dual Nature of svn:ignore Is there any way to accomplish
I have this follow code in my javascript. I call this function when the
I just follow this DIHQuickStart , try to import data to solr from mysql.
Sorry to follow this topic http://stackoverflow.com/questions/11044825/javascript-run-on-ie-7 I want to use document.GetElementByClassName ON INTERNET EXPLORE
Trying to follow this example. (Section String sorting...) Is there anything obvious that would

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.