Successive over-relaxation python

111323: <p>import numpy as np</p>
<pre><code class="lang-auto">def sor_solver(A, b, omega, initial_guess, convergence_criteria):
    """
    This is an implementation of the pseudo-code provided in the Wikipedia article.
    Arguments:
        A: nxn numpy matrix.
        b: n dimensional numpy vector.
        omega: relaxation factor.
        initial_guess: An initial solution guess for the solver to start with.
        convergence_criteria: The maximum discrepancy acceptable to regard the current solution as fitting.
    Returns:
        phi: solution vector of dimension n.
    """
    phi = initial_guess[:]
    residual = np.linalg.norm(np.matmul(A, phi) - b) #Initial residual
    while residual &gt; convergence_criteria:
        for i in range(A.shape[0]):
            sigma = 0
            for j in range(A.shape[1]):
                if j != i:
                    sigma += A[i][j] * phi[j]
            phi[i] = (1 - omega) * phi[i] + (omega / A[i][i]) * (b[i] - sigma)
        residual = np.linalg.norm(np.matmul(A, phi) - b)
        print('Residual: {0:10.6g}'.format(residual))
    return phi

residual_convergence = 1e-8
omega = 0.5 #Relaxation factor

A = np.matrix([[4, -1, -6, 0],
              [-5, -4, 10, 8],
              [0, 9, 4, -2],
              [1, 0, -7, 5]])

b = np.matrix([2, 21, -12, -6])

initial_guess = np.zeros(4)

phi = sor_solver(A, b, omega, initial_guess, residual_convergence)
print(phi)
</code></pre>
<p>I’m actually don’t know why this is don’t work… I have an error like:<br>
Traceback (most recent call last):<br>
File “sol.py”, line 42, in <br>
phi = sor_solver(A, b, omega, initial_guess, residual_convergence)<br>
File “sol.py”, line 22, in sor_solver<br>
sigma += A[i][j] * phi[j]<br>
File “C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\matrixlib\defmatrix.py”, line 195, in <strong>getitem</strong><br>
out = N.ndarray.<strong>getitem</strong>(self, index)<br>
IndexError: index 1 is out of bounds for axis 0 with size 1</p>

KFrank: <p>Hi Софья!</p>
<p>This is specifically a pytorch forum, so it is probably not the best<br>
place to ask your question.  I’m not really sure where the best place<br>
would be, but maybe stackoverflow would be a better (more general)<br>
fit.</p>
<p>However …</p>
<aside class="quote no-group quote-modified" data-username="111323" data-post="1" data-topic="82900" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111323/48/12367_2.png" class="avatar"> 111323:</div>
<blockquote>
<pre><code class="lang-auto">A = np.matrix([[4, -1, -6, 0],
              [-5, -4, 10, 8],
              [0, 9, 4, -2],
              [1, 0, -7, 5]])
</code></pre>
<p>…</p>
<pre><code class="lang-auto">  File "sol.py", line 22, in sor_solver
    sigma += A[i][j] * phi[j]
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\matrixlib\defmatrix.py", line 195, in __getitem__
    out = N.ndarray.__getitem__(self, index)
IndexError: index 1 is out of bounds for axis 0 with size 1
</code></pre>
</blockquote>
</aside>
<p><code>A</code> is a numpy <code>matrix</code> so you need to index it as <code>A[i, j]</code> (rather than<br>
as <code>A[i][j]</code>).</p>
<p>For example, try running this script:</p>
<pre data-code-wrap="python"><code class="lang-python">import numpy as np
A = np.matrix([[4, -1, -6, 0],
              [-5, -4, 10, 8],
              [0, 9, 4, -2],
              [1, 0, -7, 5]])
A[1, 1]
A[1][1]
</code></pre>
<p>Best.</p>
<p>K. Frank</p>