def lineaire_combinaties(): # pylint: disable=C0103 RNG().set(1) u = random_tensor(r"\vec u", ret=True, details=False) v = random_tensor(r"\vec v", ret=True, details=False) w = random_tensor(r"\vec w", 2, ret=True, details=False) x = random_tensor(r"\vec x", 2, ret=True, details=False) y = random_tensor(r"\vec y", 4, ret=True, details=False) z = random_tensor(r"\vec z", 4, ret=True, details=False) display(Markdown("<hr>")) latex_bmatrix(3*u, r"3\vec{u}", details=False) latex_bmatrix(-5*v, r"-5\vec{v}", details=False) latex_bmatrix(v/2, r"\frac{1}{2} \vec{v}", details=False) latex_bmatrix(3*w + 4*x, r"3\vec{w} + 4\vec{x}", details=False) latex_bmatrix(8*y-z/2, r"8\vec{y} - \frac{1}{2}\vec{z}", details=False)
def random_tensor(label=None, size=None, singular=DONT_CARE,\ interval=None, ret=False, details=True): # pylint: disable=R0913 def generate_tensor(size, interval): if not interval: interval = (-20, 20) if size and isinstance(size, int): size = (size, 1) elif not size or not isinstance(size, tuple): size = (np.random.randint(2, 6), 1) return np.random.randint(interval[0], interval[1], size=size) candidate = generate_tensor(size, interval) while (singular == DEGENERATE and np.linalg.det(candidate) != 0)\ or (singular == NONDEGENERATE and np.linalg.det(candidate) == 0): candidate = generate_tensor(size, interval) latex_bmatrix(candidate, label, details=details) return candidate if ret else None
def negatieven_en_sommen(): # pylint: disable=C0103 RNG().set(0) u = random_tensor(r"\vec u", ret=True, details=False) v = random_tensor(r"\vec v", ret=True, details=False) w = random_tensor(r"\vec w", 3, ret=True, details=False) x = random_tensor(r"\vec x", 3, ret=True, details=False) y = random_tensor(r"\vec y", 5, ret=True, details=False) z = random_tensor(r"\vec z", 5, ret=True, details=False) display(Markdown("<hr>")) latex_bmatrix(-u, r"-\vec u", details=False) latex_bmatrix(-v, r"-\vec v", details=False) latex_bmatrix(-w + x, r"-\vec w + \vec x", details=False) latex_bmatrix(-y-z, r"- \vec y - \vec z", details=False)
def inverses(): # pylint: disable=C0103 RNG().set(6) def fr_matrix(M, divisor=1, label=None): def fraction(n): n = int(n) gcd = np.gcd(n,divisor) s = '' if divisor*n > 0 else '-' n = abs((n/gcd).round(0).astype(int)) d = abs((divisor/gcd).round(0).astype(int)) if d == 1: return s+str(n) else: return f"{s}\\frac{{{n}}}{{{d}}}" if len(M.shape) > 2: raise ValueError('bmatrix can at most display two dimensions') lines = str(M).replace("[", "").replace("]", "").splitlines() if label: result = [label + " = "] else: result = [""] result += [r"\begin{bmatrix}"] result += [" " + " & ".join(map(fraction, l.split())) + r"\\" for l in lines] result += [r"\end{bmatrix}"] display(Math("\n".join(result))) adj = lambda M: np.array(((M[1][1], -M[0][1]),(-M[1][0], M[0][0]))) M = random_tensor(r"\textbf{M}", (2,2), singular=NONDEGENERATE, ret=True, details=False) det = np.linalg.det(M).round(0).astype(int) latex_bmatrix(adj(M), r"$\text{adj}(\mathbf{M})", details=False) display(Markdown(f"$\\text{{det}}(\\mathbf{{M}}) = {det}$")) fr_matrix(adj(M), det, r"$\mathbf{M}^{-1}") display(Markdown("<hr>")) N = random_tensor(r"\textbf{N}", (2,2), singular=DEGENERATE, ret=True, details=False) latex_bmatrix(adj(N), r"$\text{adj}(\mathbf{N})", details=False) display(Markdown(f"$\\text{{det}}(\\mathbf{{N}}) = {np.linalg.det(N).round(0).astype(int)}$")) display(Markdown(r"$\mathbf{N}^{-1} = \bot$")) display(Markdown("<hr>")) O = random_tensor(r"\textbf{O}", (2,2), singular=NONDEGENERATE, ret=True, details=False) det = np.linalg.det(O).round(0).astype(int) latex_bmatrix(adj(O), r"$\text{adj}(\mathbf{O})", details=False) display(Markdown(f"$\\text{{det}}(\\mathbf{{O}}) = {det}$")) fr_matrix(adj(O), det, r"$\mathbf{O}^{-1}")
def matrix_producten(): # pylint: disable=C0103 RNG().set(4) u = random_tensor(r"\vec u", 3, ret=True, details=False) RNG().consume_entropy(0x02, -0x14, 0x14) M = random_tensor(r"\mathbf{M}", (3,2), ret=True, details=False) N = random_tensor(r"\mathbf{N}", (2,3), ret=True, details=False) O = random_tensor(r"\mathbf{O}", (2,2), ret=True, details=False) display(Markdown("<hr>")) latex_bmatrix(O.dot(N.dot(u)), r"\mathbf{O} (\mathbf{N} \vec u)", details=False) latex_bmatrix(O.dot(N).dot(u), r"(\mathbf{O} \mathbf{N}) \vec u", details=False) latex_bmatrix(O.dot(N), r"\mathbf{O} \mathbf{N}", details=False) display(Markdown("<hr>")) display(Markdown(r"$\mathbf{O}\mathbf{M} = \bot$")) latex_bmatrix(O.dot(O), r"\mathbf{O} \mathbf{O}", details=False) display(Markdown(r"$\mathbf{N}\mathbf{N} = \bot$")) latex_bmatrix(N.dot(M), r"\mathbf{N} \mathbf{M}", details=False) display(Markdown(r"$\mathbf{N}\mathbf{O} = \bot$")) latex_bmatrix(M.dot(N), r"\mathbf{M} \mathbf{N}", details=False) display(Markdown(r"$\mathbf{M}\mathbf{M} = \bot$")) latex_bmatrix(M.dot(O), r"\mathbf{M} \mathbf{O}", details=False)
def matrix_vector(): # pylint: disable=C0103 RNG().set(4) u = random_tensor(r"\vec u", 3, ret=True, details=False) v = random_tensor(r"\vec v", 2, ret=True, details=False) M = random_tensor(r"\mathbf{M}", (3,2), ret=True, details=False) N = random_tensor(r"\mathbf{N}", (2,3), ret=True, details=False) O = random_tensor(r"\mathbf{O}", (2,2), ret=True, details=False) RNG().set(2).consume_entropy(0x06, -0x14, 0x14) pa = random_tensor(r"\vec {p_a}", 2, ret=True, details=False) pb = random_tensor(r"\vec {p_b}", 2, ret=True, details=False) qa = random_tensor(r"\vec {q_a}", 4, ret=True, details=False) qb = random_tensor(r"\vec {q_b}", 4, ret=True, details=False) display(Markdown("<hr>")) latex_bmatrix(M.dot(v), r"\mathbf{M}\vec{v}", details=False) display(Markdown(r"$\mathbf{M}\vec{u} = \bot$")) display(Markdown(r"$\mathbf{N}\vec{v} = \bot$")) latex_bmatrix(N.dot(u), r"\mathbf{N}\vec{u}", details=False) latex_bmatrix(O.dot(N.dot(u)), r"\mathbf{O} (\mathbf{N} \vec u)", details=False) display(Markdown("<hr>")) P = np.hstack((pa,pb)) Q = np.hstack((qa,qb)) latex_bmatrix(P, r"\mathbf{P}", details=False) latex_bmatrix(Q, r"\mathbf{Q}", details=False) display(Markdown("<hr>")) latex_bmatrix(P.dot(np.array((3,4))).reshape(2,1), r"\mathbf{P} \begin{bmatrix}3 \\ 4\end{bmatrix}", details=False) latex_bmatrix(Q.dot(np.array((8,-0.5))).reshape(4,1), r"\mathbf{Q} \begin{bmatrix}8 \\ -\frac{1}{2}\end{bmatrix}", details=False)