Skip to content

Instantly share code, notes, and snippets.

@ofan666
Created February 21, 2012 11:11
Show Gist options
  • Select an option

  • Save ofan666/1875903 to your computer and use it in GitHub Desktop.

Select an option

Save ofan666/1875903 to your computer and use it in GitHub Desktop.

Revisions

  1. ofan666 revised this gist Mar 9, 2012. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions TDMAsolver.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    import numpy as np
    try:
    import numpypy as np # for compatibility with numpy in pypy
    except:
    import numpy as np # if using numpy in cpython

    ## Tri Diagonal Natrix Algorithm(a.k.a Thomas algorithm) solver
    ## Tri Diagonal Matrix Algorithm(a.k.a Thomas algorithm) solver
    def TDMAsolver(a, b, c, d):
    '''
    TDMA solver, a b c d can be NumPy array type or Python list type.
  2. ofan666 revised this gist Mar 9, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions TDMAsolver.py
    Original file line number Diff line number Diff line change
    @@ -7,18 +7,18 @@ def TDMAsolver(a, b, c, d):
    refer to http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
    '''
    nf = len(a) # number of equations
    ac, bc, cc, dc = map(np.copy, (a, b, c, d)) # copy the array
    ac, bc, cc, dc = map(np.array, (a, b, c, d)) # copy the array
    for it in xrange(1, nf):
    mc = ac[it]/bc[it-1]
    bc[it] = bc[it] - mc*cc[it-1]
    dc[it] = dc[it] - mc*dc[it-1]

    xc = np.copy(ac)
    xc[nf-1] = dc[nf-1]/bc[nf-1]
    xc = ac
    xc[-1] = dc[-1]/bc[-1]

    for il in xrange(nf-2, -1, -1):
    xc[il] = (dc[il]-cc[il]*xc[il+1])/bc[il]

    del ac, bc, cc, dc # delete variables from memory
    del bc, cc, dc # delete variables from memory

    return xc
  3. ofan666 revised this gist Feb 25, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TDMAsolver.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ def TDMAsolver(a, b, c, d):
    bc[it] = bc[it] - mc*cc[it-1]
    dc[it] = dc[it] - mc*dc[it-1]

    xc = np.arange(nf, dtype=np.float)
    xc = np.copy(ac)
    xc[nf-1] = dc[nf-1]/bc[nf-1]

    for il in xrange(nf-2, -1, -1):
  4. ofan666 revised this gist Feb 25, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TDMAsolver.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ def TDMAsolver(a, b, c, d):
    bc[it] = bc[it] - mc*cc[it-1]
    dc[it] = dc[it] - mc*dc[it-1]

    xc = np.arange(nf, dtype=np.float64)
    xc = np.arange(nf, dtype=np.float)
    xc[nf-1] = dc[nf-1]/bc[nf-1]

    for il in xrange(nf-2, -1, -1):
  5. ofan666 revised this gist Feb 25, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TDMAsolver.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ def TDMAsolver(a, b, c, d):
    bc[it] = bc[it] - mc*cc[it-1]
    dc[it] = dc[it] - mc*dc[it-1]

    xc = np.arange(nf)
    xc = np.arange(nf, dtype=np.float64)
    xc[nf-1] = dc[nf-1]/bc[nf-1]

    for il in xrange(nf-2, -1, -1):
  6. ofan666 revised this gist Feb 22, 2012. No changes.
  7. ofan666 created this gist Feb 21, 2012.
    24 changes: 24 additions & 0 deletions TDMAsolver.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import numpy as np

    ## Tri Diagonal Natrix Algorithm(a.k.a Thomas algorithm) solver
    def TDMAsolver(a, b, c, d):
    '''
    TDMA solver, a b c d can be NumPy array type or Python list type.
    refer to http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
    '''
    nf = len(a) # number of equations
    ac, bc, cc, dc = map(np.copy, (a, b, c, d)) # copy the array
    for it in xrange(1, nf):
    mc = ac[it]/bc[it-1]
    bc[it] = bc[it] - mc*cc[it-1]
    dc[it] = dc[it] - mc*dc[it-1]

    xc = np.arange(nf)
    xc[nf-1] = dc[nf-1]/bc[nf-1]

    for il in xrange(nf-2, -1, -1):
    xc[il] = (dc[il]-cc[il]*xc[il+1])/bc[il]

    del ac, bc, cc, dc # delete variables from memory

    return xc