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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:25:35+00:00 2026-06-17T12:25:35+00:00

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

  • 0

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.

I have been using Maple but I would like to set these up and solve them in python now, maybe using numpy.linalg.solve (or any other better method). I actually only want the value of M(n-1). For example, when n=2 the answer should be M(1) = 4, I believe. This is because the equations become

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

Therefore

M(1)/2 = 1+1

and so

M(1) = 4. 

If I want to plug in n=50, say, how can you set up this system of simultaneous equations in python so that numpy.linalg.solve can solve them?

Update The answers are great but they use dense solvers where the system of equations is sparse. Posted follow up to Using scipy sparse matrices to solve system of equations .

  • 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-17T12:25:36+00:00Added an answer on June 17, 2026 at 12:25 pm

    Updated: added implementation using scipy.sparse


    This gives the solution in the order N_max,...,N_0,M_max,...,M_1.

    The linear system to solve is of the shape A dot x == const 1-vector.
    x is the sought after solution vector.
    Here I ordered the equations such that x is N_max,...,N_0,M_max,...,M_1.
    Then I build up the A-coefficient matrix from 4 block matrices.

    Here’s a snapshot for the example case n=50 showing how you can derive the coefficient matrix and understand the block structure. The coefficient matrix A is light blue, the constant right side is orange. The sought after solution vector x is here light green and used to label the columns. The first column show from which of the above given eqs. the row (= eq.) has been derived:
    enter image description here

    As Jaime suggested, multiplying by n improves the code. This is not reflected in the spreadsheet above but has been implemented in the code below:

    Implementation using numpy:

    import numpy as np
    import numpy.linalg as linalg
    
    
    def solve(n):
        # upper left block
        n_to_M = -2. * np.eye(n-1) 
    
        # lower left block
        n_to_N = (n * np.eye(n-1)) - np.diag(np.arange(n-2, 0, -1), 1)
    
        # upper right block
        m_to_M = n_to_N.copy()
        m_to_M[1:, 0] = -np.arange(1, n-1)
    
        # lower right block
        m_to_N = np.zeros((n-1, n-1))
        m_to_N[:,0] = -np.arange(1,n)
    
        # build A, combine all blocks
        coeff_mat = np.hstack(
                              (np.vstack((n_to_M, n_to_N)),
                               np.vstack((m_to_M, m_to_N))))
    
        # const vector, right side of eq.
        const = n * np.ones((2 * (n-1),1))
    
        return linalg.solve(coeff_mat, const)
    

    Solution using scipy.sparse:

    from scipy.sparse import spdiags, lil_matrix, vstack, hstack
    from scipy.sparse.linalg import spsolve
    import numpy as np
    
    
    def solve(n):
        nrange = np.arange(n)
        diag = np.ones(n-1)
    
        # upper left block
        n_to_M = spdiags(-2. * diag, 0, n-1, n-1)
    
        # lower left block
        n_to_N = spdiags([n * diag, -nrange[-1:0:-1]], [0, 1], n-1, n-1)
    
        # upper right block
        m_to_M = lil_matrix(n_to_N)
        m_to_M[1:, 0] = -nrange[1:-1].reshape((n-2, 1))
    
        # lower right block
        m_to_N = lil_matrix((n-1, n-1))
        m_to_N[:, 0] = -nrange[1:].reshape((n-1, 1))
    
        # build A, combine all blocks
        coeff_mat = hstack(
                           (vstack((n_to_M, n_to_N)),
                            vstack((m_to_M, m_to_N))))
    
        # const vector, right side of eq.
        const = n * np.ones((2 * (n-1),1))
    
        return spsolve(coeff_mat.tocsr(), const).reshape((-1,1))
    

    Example for n=4:

    [[ 7.25      ]
     [ 7.76315789]
     [ 8.10526316]
     [ 9.47368421]   # <<< your result
     [ 9.69736842]
     [ 9.78947368]]
    

    Example for n=10:

    [[ 24.778976  ]
     [ 25.85117842]
     [ 26.65015984]
     [ 27.26010007]
     [ 27.73593401]
     [ 28.11441922]
     [ 28.42073207]
     [ 28.67249606]
     [ 28.88229939]
     [ 30.98033266]  # <<< your result
     [ 31.28067182]
     [ 31.44628982]
     [ 31.53365219]
     [ 31.57506477]
     [ 31.58936225]
     [ 31.58770694]
     [ 31.57680467]
     [ 31.560726  ]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fixed array of constant integer values about 300 items long (Set
I have a pointer to array of fixed size integer elements. After populating that
I have three fixed width integer types: typedef int16_t TABCellManagedDataKey; typedef int16_t TABCellManagedDataIndex; typedef
This is a follow up to How to set up and solve simultaneous equations
I want to have a dictionary that assigns a value to a set of
I have a class that encapsulates some arithmetic, let's say fixed point calculations. I
Problem: I have a struct of a fixed size that I'm trying to marshal.
let's say I have a huge set of non-overlapping rectangle with integer coordinates, who
I have a for loop which gives a given integer sequence, for fixed parameters
I have a carousel that I can change the location of with an integer

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.