Creation a tridiagonal block matrix in python [duplicate]

2024/10/11 14:21:29

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.

Answer

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] = valueelif row == col - 1:self._lower_diagonal[col] = valueelif row == col + 1:self._upper_diagonal[row] = valueelse:#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.

https://en.xdnf.cn/q/118320.html

Related Q&A

Python tkinter checkbutton value always equal to 0

I put the checkbutton on the text widget, but everytime I select a checkbutton, the function checkbutton_value is called, and it returns 0.Part of the code is :def callback():file_name=askopenfilename(…

How does derived class arguments work in Python?

I am having difficulty understanding one thing in Python.I have been coding in Python from a very long time but theres is something that just struck me today which i struggle to understandSo the situat…

grouping on tems in a list in python

I have 60 records with a column "skillsList" "("skillsList" is a list of skills) and "IdNo". I want to find out how many "IdNos" have a skill in common.How …

How do I show a suffix to user input in Python?

I want a percentage sign to display after the users enters their number. Thankspercent_tip = float(input(" Please Enter the percent of the tip:")("%"))For example, before the user t…

Discord.py Self Bot using rewrite

Im trying to make a selfbot using discord.py rewrite. Im encountering issues when attempting to create a simple command. Id like my selfbot to respond with "oof" when ">>>test&q…

int to binary python

This question is probably very easy for most of you, but i cant find the answer so far.Im building a network packet generator that goes like this:class PacketHeader(Packet): fields = OrderedDict([(&quo…

Get aiohttp results as string

Im trying to get data from a website using async in python. As an example I used this code (under A Better Coroutine Example): https://www.blog.pythonlibrary.org/2016/07/26/python-3-an-intro-to-asyncio…

Waiting for a timer to terminate before continuing running the code

The following code updates the text of a button every second after the START button was pressed. The intended functionality is for the code to wait until the timer has stopped before continuing on with…

PySpark: how to resolve path of a resource file present inside the dependency zip file

I have a mapPartitions on an RDD and within each partition, a resource file has to be opened. This module that contains the method invoked by mapPartitions and the resource file is passed on to each ex…

Convert normal Python script to REST API

Here I have an excel to pdf conversion script. How can I modify it to act as a REST API? import os import comtypes.client SOURCE_DIR = D:/projects/python TARGET_DIR = D:/projects/python app = comtypes…