Python: vectorizing a structured linear system solveComparing algorithms for tridiagonal linear systems solutionSparse, underdetermined system of linear equationsEfficient solver for a symmetric tridiagonal system where the upper/lower diagonals are offsetHow to obtain a convergent solution iteratively for a linear system of equations?How to make this matrix efficiently?What is a good way to solve the following linear system? (repeatedly)Extending the Frobenius inner product to all matrix inner products$LU$ Factorization of a nonsingular matrix with a particular patternEfficient algorithm for solving linear system with symmetric near-tridiagonal matrix?Neumann boundary conditions for the upwind scheme applied to the advection equation (Python)

How could empty set be unique if it could be vacuously false

Mathematically modelling RC circuit with a linear input

macOS: How to take a picture from camera after 1 minute

Is the continuity test limit resistance of a multimeter standard?

Should I include an appendix for inessential, yet related worldbuilding to my story?

Text alignment in tikzpicture

Value matching with NA - missing values - using mutate

Can Hunter's Mark be moved after Silence has been cast on a character?

Should the party get XP for a monster they never attacked?

Am I legally required to provide a (GPL licensed) source code even after a project is abandoned?

Is there any proof that high saturation and contrast makes a picture more appealing in social media?

Justifying Affordable Bespoke Spaceships

Why does independence imply zero correlation?

I found a password with hashcat, but it doesn't work

QGIS. Polygon doesn't repeat itself

How do I remove this inheritance-related code smell?

Why is it easier to balance a non-moving bike standing up than sitting down?

How can a warlock learn from a spellbook?

How to work with PETG? Settings, caveats, etc

Extending prime numbers digit by digit while retaining primality

Has a life raft ever been successfully deployed on a modern commercial flight?

Where should a runway for a spaceplane be located?

Why isn't my calculation that we should be able to see the sun well beyond the observable universe valid?

What triggered jesuits' ban on infinitesimals in 1632?



Python: vectorizing a structured linear system solve


Comparing algorithms for tridiagonal linear systems solutionSparse, underdetermined system of linear equationsEfficient solver for a symmetric tridiagonal system where the upper/lower diagonals are offsetHow to obtain a convergent solution iteratively for a linear system of equations?How to make this matrix efficiently?What is a good way to solve the following linear system? (repeatedly)Extending the Frobenius inner product to all matrix inner products$LU$ Factorization of a nonsingular matrix with a particular patternEfficient algorithm for solving linear system with symmetric near-tridiagonal matrix?Neumann boundary conditions for the upwind scheme applied to the advection equation (Python)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5












$begingroup$


Overview



I am looking for a way to solve a structured linear system in Python without using a for loop (preferably using vectorization, if possible).



Background



Consider the following linear system:
beginalign
beginpmatrix
E_0 \
F_1 & E_1 \
& F_2 & E_2 \
&& ddots & ddots \
&&& F_K-1 & E_K-1
endpmatrix
beginpmatrix
x_0 \ x_1 \ x_2 \ vdots \ x_K-1
endpmatrix
=
beginpmatrix
b_0 \ b_1 \ b_2 \ vdots \ b_K-1
endpmatrix
endalign

where $E_i, F_i in mathbbR^n times n$, and
$x_i, b_i in mathbbR^n$ for $i = 0, ldots, K-1$



Further, the $E_i$ are invertible for $i = 0, ldots, K-1$.



Then this system can be solved through forward substitution:



Solve $E_0 x_0 = b_0$



for $i = 1, ldots, K-1$:
Solve $E_i x_i = b_i - F_i x_i-1$



My Current Implementation



The block matrices $E_i$ and $F_i$ are available by calling Ek(i) and Fk(i).



Currently $x$ and $b$ are shaped as a numpy arrays with shape $K times n$ so that x[k] gives $x_k$, and so forth.



import numpy as np
from scipy.sparse.linalg import spsolve

# define K and n, create b and initialize x

x[0] = spsolve(Ek(0), b[0])
for i in range(1, K):
x[i] = spsolve(Ek(i), b[i] - Fk(i) @ x[i-1])


Can this be vectorized? I would like to not use a for loop here since they are quite slow in Python.










share|cite|improve this question











$endgroup$


















    5












    $begingroup$


    Overview



    I am looking for a way to solve a structured linear system in Python without using a for loop (preferably using vectorization, if possible).



    Background



    Consider the following linear system:
    beginalign
    beginpmatrix
    E_0 \
    F_1 & E_1 \
    & F_2 & E_2 \
    && ddots & ddots \
    &&& F_K-1 & E_K-1
    endpmatrix
    beginpmatrix
    x_0 \ x_1 \ x_2 \ vdots \ x_K-1
    endpmatrix
    =
    beginpmatrix
    b_0 \ b_1 \ b_2 \ vdots \ b_K-1
    endpmatrix
    endalign

    where $E_i, F_i in mathbbR^n times n$, and
    $x_i, b_i in mathbbR^n$ for $i = 0, ldots, K-1$



    Further, the $E_i$ are invertible for $i = 0, ldots, K-1$.



    Then this system can be solved through forward substitution:



    Solve $E_0 x_0 = b_0$



    for $i = 1, ldots, K-1$:
    Solve $E_i x_i = b_i - F_i x_i-1$



    My Current Implementation



    The block matrices $E_i$ and $F_i$ are available by calling Ek(i) and Fk(i).



    Currently $x$ and $b$ are shaped as a numpy arrays with shape $K times n$ so that x[k] gives $x_k$, and so forth.



    import numpy as np
    from scipy.sparse.linalg import spsolve

    # define K and n, create b and initialize x

    x[0] = spsolve(Ek(0), b[0])
    for i in range(1, K):
    x[i] = spsolve(Ek(i), b[i] - Fk(i) @ x[i-1])


    Can this be vectorized? I would like to not use a for loop here since they are quite slow in Python.










    share|cite|improve this question











    $endgroup$














      5












      5








      5





      $begingroup$


      Overview



      I am looking for a way to solve a structured linear system in Python without using a for loop (preferably using vectorization, if possible).



      Background



      Consider the following linear system:
      beginalign
      beginpmatrix
      E_0 \
      F_1 & E_1 \
      & F_2 & E_2 \
      && ddots & ddots \
      &&& F_K-1 & E_K-1
      endpmatrix
      beginpmatrix
      x_0 \ x_1 \ x_2 \ vdots \ x_K-1
      endpmatrix
      =
      beginpmatrix
      b_0 \ b_1 \ b_2 \ vdots \ b_K-1
      endpmatrix
      endalign

      where $E_i, F_i in mathbbR^n times n$, and
      $x_i, b_i in mathbbR^n$ for $i = 0, ldots, K-1$



      Further, the $E_i$ are invertible for $i = 0, ldots, K-1$.



      Then this system can be solved through forward substitution:



      Solve $E_0 x_0 = b_0$



      for $i = 1, ldots, K-1$:
      Solve $E_i x_i = b_i - F_i x_i-1$



      My Current Implementation



      The block matrices $E_i$ and $F_i$ are available by calling Ek(i) and Fk(i).



      Currently $x$ and $b$ are shaped as a numpy arrays with shape $K times n$ so that x[k] gives $x_k$, and so forth.



      import numpy as np
      from scipy.sparse.linalg import spsolve

      # define K and n, create b and initialize x

      x[0] = spsolve(Ek(0), b[0])
      for i in range(1, K):
      x[i] = spsolve(Ek(i), b[i] - Fk(i) @ x[i-1])


      Can this be vectorized? I would like to not use a for loop here since they are quite slow in Python.










      share|cite|improve this question











      $endgroup$




      Overview



      I am looking for a way to solve a structured linear system in Python without using a for loop (preferably using vectorization, if possible).



      Background



      Consider the following linear system:
      beginalign
      beginpmatrix
      E_0 \
      F_1 & E_1 \
      & F_2 & E_2 \
      && ddots & ddots \
      &&& F_K-1 & E_K-1
      endpmatrix
      beginpmatrix
      x_0 \ x_1 \ x_2 \ vdots \ x_K-1
      endpmatrix
      =
      beginpmatrix
      b_0 \ b_1 \ b_2 \ vdots \ b_K-1
      endpmatrix
      endalign

      where $E_i, F_i in mathbbR^n times n$, and
      $x_i, b_i in mathbbR^n$ for $i = 0, ldots, K-1$



      Further, the $E_i$ are invertible for $i = 0, ldots, K-1$.



      Then this system can be solved through forward substitution:



      Solve $E_0 x_0 = b_0$



      for $i = 1, ldots, K-1$:
      Solve $E_i x_i = b_i - F_i x_i-1$



      My Current Implementation



      The block matrices $E_i$ and $F_i$ are available by calling Ek(i) and Fk(i).



      Currently $x$ and $b$ are shaped as a numpy arrays with shape $K times n$ so that x[k] gives $x_k$, and so forth.



      import numpy as np
      from scipy.sparse.linalg import spsolve

      # define K and n, create b and initialize x

      x[0] = spsolve(Ek(0), b[0])
      for i in range(1, K):
      x[i] = spsolve(Ek(i), b[i] - Fk(i) @ x[i-1])


      Can this be vectorized? I would like to not use a for loop here since they are quite slow in Python.







      linear-algebra python numpy






      share|cite|improve this question















      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited Jun 11 at 12:30









      GertVdE

      5,4301535




      5,4301535










      asked Jun 11 at 5:53









      Nathaniel KroegerNathaniel Kroeger

      877




      877




















          2 Answers
          2






          active

          oldest

          votes


















          6












          $begingroup$

          In your explanation, you solve the large problem using forward substitution. This implies that you are solving your large problem successively: you first need $x_i-1$ before you can solve for $x_i$. This means that you need to loop, there is no way to avoid this.



          However, for each subproblem, you use the spsolve routine, dedicated to solving sparse systems. Why? Are the submatrices $E_i$ really sparse? What is the order of magnitude of n? What sparse storage format do you use for the $E$ and $F$ matrices?



          Another approach would be to consider your full matrix as a banded matrix and use scipy.linalg.solve_banded. If you have numpy linked to an optimized BLAS/LAPACK like MKL, this might also prove to be quite fast.



          Could you elaborate a bit more on your problem (sizes, structure of the $E$ and $F$ matrices)? And if you feel up to it, make the comparison between the forward substitution approach (using a for loop) and the banded matrix approach and post it here...






          share|cite|improve this answer









          $endgroup$




















            0












            $begingroup$

            I think the way to go is to use some library code at that point. Vectorization is quite a low-level (hardware near) optimization which seems a bit unnatural to tackle via python. Search for Lapack or BLAS vectorized operations. including them into your code might end up in a couple of extra lines, but will potentially spare you the misery of doing it by hand.scipy.linalg.lapack)



            Also, before you think about vectorization, consider whether there are operations which you can run thread-parallel. Depending on your machine, that might already give you significant performance increase.



            I'm no expert on performance in python, is there a chance of calling your substitution recursively might give a performance boost?






            share|cite|improve this answer









            $endgroup$













              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "363"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fscicomp.stackexchange.com%2fquestions%2f32861%2fpython-vectorizing-a-structured-linear-system-solve%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6












              $begingroup$

              In your explanation, you solve the large problem using forward substitution. This implies that you are solving your large problem successively: you first need $x_i-1$ before you can solve for $x_i$. This means that you need to loop, there is no way to avoid this.



              However, for each subproblem, you use the spsolve routine, dedicated to solving sparse systems. Why? Are the submatrices $E_i$ really sparse? What is the order of magnitude of n? What sparse storage format do you use for the $E$ and $F$ matrices?



              Another approach would be to consider your full matrix as a banded matrix and use scipy.linalg.solve_banded. If you have numpy linked to an optimized BLAS/LAPACK like MKL, this might also prove to be quite fast.



              Could you elaborate a bit more on your problem (sizes, structure of the $E$ and $F$ matrices)? And if you feel up to it, make the comparison between the forward substitution approach (using a for loop) and the banded matrix approach and post it here...






              share|cite|improve this answer









              $endgroup$

















                6












                $begingroup$

                In your explanation, you solve the large problem using forward substitution. This implies that you are solving your large problem successively: you first need $x_i-1$ before you can solve for $x_i$. This means that you need to loop, there is no way to avoid this.



                However, for each subproblem, you use the spsolve routine, dedicated to solving sparse systems. Why? Are the submatrices $E_i$ really sparse? What is the order of magnitude of n? What sparse storage format do you use for the $E$ and $F$ matrices?



                Another approach would be to consider your full matrix as a banded matrix and use scipy.linalg.solve_banded. If you have numpy linked to an optimized BLAS/LAPACK like MKL, this might also prove to be quite fast.



                Could you elaborate a bit more on your problem (sizes, structure of the $E$ and $F$ matrices)? And if you feel up to it, make the comparison between the forward substitution approach (using a for loop) and the banded matrix approach and post it here...






                share|cite|improve this answer









                $endgroup$















                  6












                  6








                  6





                  $begingroup$

                  In your explanation, you solve the large problem using forward substitution. This implies that you are solving your large problem successively: you first need $x_i-1$ before you can solve for $x_i$. This means that you need to loop, there is no way to avoid this.



                  However, for each subproblem, you use the spsolve routine, dedicated to solving sparse systems. Why? Are the submatrices $E_i$ really sparse? What is the order of magnitude of n? What sparse storage format do you use for the $E$ and $F$ matrices?



                  Another approach would be to consider your full matrix as a banded matrix and use scipy.linalg.solve_banded. If you have numpy linked to an optimized BLAS/LAPACK like MKL, this might also prove to be quite fast.



                  Could you elaborate a bit more on your problem (sizes, structure of the $E$ and $F$ matrices)? And if you feel up to it, make the comparison between the forward substitution approach (using a for loop) and the banded matrix approach and post it here...






                  share|cite|improve this answer









                  $endgroup$



                  In your explanation, you solve the large problem using forward substitution. This implies that you are solving your large problem successively: you first need $x_i-1$ before you can solve for $x_i$. This means that you need to loop, there is no way to avoid this.



                  However, for each subproblem, you use the spsolve routine, dedicated to solving sparse systems. Why? Are the submatrices $E_i$ really sparse? What is the order of magnitude of n? What sparse storage format do you use for the $E$ and $F$ matrices?



                  Another approach would be to consider your full matrix as a banded matrix and use scipy.linalg.solve_banded. If you have numpy linked to an optimized BLAS/LAPACK like MKL, this might also prove to be quite fast.



                  Could you elaborate a bit more on your problem (sizes, structure of the $E$ and $F$ matrices)? And if you feel up to it, make the comparison between the forward substitution approach (using a for loop) and the banded matrix approach and post it here...







                  share|cite|improve this answer












                  share|cite|improve this answer



                  share|cite|improve this answer










                  answered Jun 11 at 12:48









                  GertVdEGertVdE

                  5,4301535




                  5,4301535























                      0












                      $begingroup$

                      I think the way to go is to use some library code at that point. Vectorization is quite a low-level (hardware near) optimization which seems a bit unnatural to tackle via python. Search for Lapack or BLAS vectorized operations. including them into your code might end up in a couple of extra lines, but will potentially spare you the misery of doing it by hand.scipy.linalg.lapack)



                      Also, before you think about vectorization, consider whether there are operations which you can run thread-parallel. Depending on your machine, that might already give you significant performance increase.



                      I'm no expert on performance in python, is there a chance of calling your substitution recursively might give a performance boost?






                      share|cite|improve this answer









                      $endgroup$

















                        0












                        $begingroup$

                        I think the way to go is to use some library code at that point. Vectorization is quite a low-level (hardware near) optimization which seems a bit unnatural to tackle via python. Search for Lapack or BLAS vectorized operations. including them into your code might end up in a couple of extra lines, but will potentially spare you the misery of doing it by hand.scipy.linalg.lapack)



                        Also, before you think about vectorization, consider whether there are operations which you can run thread-parallel. Depending on your machine, that might already give you significant performance increase.



                        I'm no expert on performance in python, is there a chance of calling your substitution recursively might give a performance boost?






                        share|cite|improve this answer









                        $endgroup$















                          0












                          0








                          0





                          $begingroup$

                          I think the way to go is to use some library code at that point. Vectorization is quite a low-level (hardware near) optimization which seems a bit unnatural to tackle via python. Search for Lapack or BLAS vectorized operations. including them into your code might end up in a couple of extra lines, but will potentially spare you the misery of doing it by hand.scipy.linalg.lapack)



                          Also, before you think about vectorization, consider whether there are operations which you can run thread-parallel. Depending on your machine, that might already give you significant performance increase.



                          I'm no expert on performance in python, is there a chance of calling your substitution recursively might give a performance boost?






                          share|cite|improve this answer









                          $endgroup$



                          I think the way to go is to use some library code at that point. Vectorization is quite a low-level (hardware near) optimization which seems a bit unnatural to tackle via python. Search for Lapack or BLAS vectorized operations. including them into your code might end up in a couple of extra lines, but will potentially spare you the misery of doing it by hand.scipy.linalg.lapack)



                          Also, before you think about vectorization, consider whether there are operations which you can run thread-parallel. Depending on your machine, that might already give you significant performance increase.



                          I'm no expert on performance in python, is there a chance of calling your substitution recursively might give a performance boost?







                          share|cite|improve this answer












                          share|cite|improve this answer



                          share|cite|improve this answer










                          answered Jun 11 at 10:02









                          MPIchaelMPIchael

                          1117




                          1117



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Computational Science Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fscicomp.stackexchange.com%2fquestions%2f32861%2fpython-vectorizing-a-structured-linear-system-solve%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                              Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                              Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림