====== Cvičení 5: 2D pole ====== ==== Dvojrozměrné pole ==== * Ukázka "pole polí": x = [[0, 1], ['a', True], [1, 1.0, 2, 4]] >>> len(x) 3 >>> len(x[0]) 2 >>> x[1][1] True >>> type(x) >>> type(x[0]) * Vytvoření nulové matice o rozměrech 2x3: x = [] for i in range(2): x.append([0] * 3) >>> print(x) [[0, 0, 0], [0, 0, 0]] ==== Výpis matice ==== * Napište funkci ''printMatrix'', která vypíše matici zadanou 2D polem * Prvky budou odděleny mezerou a každý řádek bude vypsán na nový řádek * Kromě prvků matice nebude vypsán žádný jiný znak ==== Stopa matice ==== * Napište funkci pro výpočet [[ https://en.wikipedia.org/wiki/Trace_(linear_algebra) | stopy ]] matice ==== Násobení vektoru a matice ==== * Napište funkci ''multiVecMat(v,m)'', která vypočte součin vektoru $v \in \mathbb{R}^{N}$ a matice $m \in \mathbb{R}^{N \times N}$. * Pokud nesouhlasí rozměry matice a vektoru, pak funkce vrací ''None''. * Vypočtete výsledek násobení $m \cdot v$: m=[[0,0,1],[0,1,0],[1,0,0]] v=[2, 4, 6] ==== Načtení 2D pole ze souboru ==== * Načtení matice uložené v souboru. Na každém řádku v souboru je uložen jeden řádek matice. Čísla jsou oddělena mezerou. * Vstupní soubor (matice.txt) 1 2 3 4 0 5 7 -1 2 * Soubor matice.txt musí být "viditelný" pro váš skript (pokud ne, je třeba zadat absolutní cestu k souboru). pole=[] f=open('matice.txt','rt') for line in f: pole.append(list(map(int, line.split()))) f.close() print(pole) * Program vypíše [[1,2,3],[4,0,5],[7,-1,2]] ==== Načtení zakódovaného obrázku ze souboru ==== * Stáhněte si soubor {{ :courses:b3b33alp:cviceni:encoded.txt |encoded.txt}}, jenž obsahuje zakódovaný obrázek. Každý řádek obsahuje různý počet dvojic **znak+číslo** (např. ''#2'') oddělených mezerou. Pro zobrazení obrázku se musí tyto dvojice prvně dekódovat. * Pročtete si a porozumějte tomutou kódu a poté ho spusťte. ## | ------------------ Functions definition ------------------ | # [You should understand this function] # Function loading given file def loadFile(filename): mat = [] # create empty list f = open(filename, 'rt') # open file with name 'filename' in 'read' and 'text' modes for line in f: # iterate all lines in the file # parse single line from file: # 1) strip: remove spaces and new line characters from beginning and end of the line # 2) split: split line by spaces, returns list # 3) map: maps each element to string # 4) list: convert output of map to list of strings lst = list(map(str, line.strip().split(' '))) mat.append(lst) # append list to our 'mat' variable f.close() return mat # [You don't need to understand this function] # Function decoding our file def decode(mat): mat_decoded = [] # create empty output list for lst in mat: # for each list in 'mat' (list of lists) decoded = '' # initialize the decoded string to empty value for s in lst: # for each encoded value in list 'lst' char = s.rstrip('0123456789') # cut the numbered part of our string out, example: #2 -> # num = s[len(char):] # retrieve the cut part, example: 2 if char == 's': # space is encoded as 's' char = ' ' # replace 's' by space character decoded = decoded + int(num) * char # decode and store into 'decoded' variable mat_decoded.append(decoded) # append single decoded line into our output list return mat_decoded # return output list ## | -------------- Python starts processing here ------------- | encoded = loadFile('encoded.txt') # load the encoded file decoded = decode(encoded) # decode the encoded file for line in decoded: # print our decoded file print(line) ===== Témata k procvičení ===== Implementujte následující úlohy: * Funkce, která v matici najde všechny záporné hodnoty, vraťte seznam jejich indexů * Funkce pro násobení dvou matic * Funkce pro výpočet determinantu matice ===== Domácí úkol =====