Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Table of Contents

Cvičení 6

06a

Life

life.txt:

a = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
     [0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
     [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
   ]


def step(x, b):
    for r in range(len(b)):
        for s in range(len(b[0])):
            cnt=0
            for dr in (-1,0,1):
                for ds in range(-1,2):
                    rr=r+dr
                    ss=s+ds
                    cnt+=x[rr%len(b)][ss%(len(b[0]))]
            cnt-=x[r][s]
            if (cnt==2 and x[r][s]>0) or cnt==3:
                b[r][s]=1
            else:
                b[r][s]=0
    return b, x

b=[ [0]*len(a[0]) for i in a ]
import time
for s in range(50):
    print('------------ STEP', s, '------------')
    for i in a:
        print(''.join('X' if p!=0 else ' ' for p in i))
    time.sleep(0.4)
    a, b =step(a, b)

06b

Gaussova eliminace

gauss.py

m=[[12,-7,3, 26],
   [4 ,5,-6, -5],
   [-7 ,8,9, 21]]

def maximum(m, s):
    maximum_index = s
    max_value = abs(m[s][s])
    for j in range(s+1,len(m)):
        if abs(m[j][s])>max_value:
            max_value = abs(m[j][s])
            maximum_index = j
    return maximum_index

def set(m, s):
    ind = maximum(m, s)
    if ind!=s:
        m[ind],m[s] = m[s],m[ind]

def do_line(m,s):
    set(m,s)
    if m[s][s]!=0:
        div = m[s][s]
        for j in range(len(m[s])):
            m[s][j]=m[s][j]/div
        print("Krok 1")
        for i in m:
            print(*i,sep='\t')

        for r in range(len(m)):
            if r!=s:
                x = m[r][s]
                for j in range(len(m[r])):
                    m[r][j]=m[r][j]-x*m[s][j]
        print("Krok 2")
        for i in m:
            print(*i,sep='\t')
        print()
        return True
    else:
        return False

def Gauss_elim(m):
    for s in range(len(m)):
        if not do_line(m,s):
            return False
    return True

from fractions import Fraction

m_fraction = [list(map(Fraction,v)) for v in m]

Gauss_elim(m)

for i in m:
    print(*i,sep='\t')

print("Zlomky - Fraction")

Gauss_elim(m_fraction)

for i in m_fraction:
    print(*i,sep='\t')

courses/b3b33alp/cviceni/kratka_videa/c06.txt · Last modified: 2021/11/09 10:05 by stepan