-1

enter image description here

How can I create this matrix using python ?

I've already created S , T , X ,W ,Y and Z as well as the first and the last line of L.

Rami Henia
  • 11
  • 1
  • The question here is better written than the question on https://stackoverflow.com/questions/5842903/block-tridiagonal-matrix-python in that it shows an example of the desired matrix. It is clearly asking for something different than a matrix with three scalar diagonals. It is a https://en.wikipedia.org/wiki/Block_matrix#Block_tridiagonal_matrices and might have some guidance in this neglected answer to the other question: https://stackoverflow.com/a/50241451/1653571 – Dave X Mar 14 '23 at 04:44

1 Answers1

0

Something like this (it's a draft!). Make a class that stores 3 lists (diagonal, upper diagonal and lower diagonal) and expose a way of editing those values.

class TBMatrix:
    def __init__(self,size):
        self._size = size #Has to be square I guess?
        self._diagonal = [None for i in range(0,size)]
        self._upper_diagonal = [None for i in range(0,size - 2)]
        self._lower_diagonal = [None for i in range(0,size - 2)]
    def get(self,row,col):
        if row == col:
            return self._diagonal[row]
        if row == col - 1:
            return self._lower_diagonal[col]
        if row == col + 1:
            return self._upper_diagonal[row]
        return 0 #or None, if you want a matrix that contains objects 
    def set(self,row,col,value):
        if row == col:
            self._diagonal[row] = value
        elif row == col - 1:
            self._lower_diagonal[col] = value
        elif row == col + 1:
            self._upper_diagonal[row] = value
        else:
            #No effect, maybe you want to throw an exception?
            pass

This is a quick draft, you'll need to do a bunch of checks to make sure there is no trying to assign an index outside of the list sizes. But this should get you started.

Another alternative is to override the __getitem__ and __setitem__ to return a row full of 0's or None except where it need to hold a spot for self._diagonal, self._upper_diagonal and self._lower_diagonal. But it just seems more complicated.

excalibur1491
  • 1,201
  • 2
  • 14
  • 29