DEMO: Postulates#

import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import widgets
from ipywidgets.widgets import interact, interactive

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

Vectors#

x = np.array([1, 2])

y = np.array([-2, 1])

z = np.array([1+2j, 3-1j])

Operations with vectors

  • Try mulitiplying by number

  • try dot product using x @ y or np.dot(x, y)

def plot_vector(x, y):

    fig, ax = plt.subplots(figsize=(6, 6))
    
    ax.quiver(0, 0, x, y, angles='xy', scale_units='xy', scale=1, color='r')
    
    ax.set_xlim([-10, 10])
    ax.set_ylim([-10, 10])
    ax.axvline(x=0, color='grey', lw=1)
    ax.axhline(y=0, color='grey', lw=1)
    ax.grid(True, which='both')
    
    plt.show()

interact(plot_vector, x=(-10, 10, 1), y=(-10, 10, 1))
<function __main__.plot_vector(x, y)>

Matrices#

A = np.array([ [1, 2],
               [3, 4]])

Operations with Matrices

  • Try mulitiplying by number

  • try dot product with vectors using A @ x or np.dot(A, x)

def matrix_transform(a=1, b=0, c=0, d=1):
    matrix = np.array([[a, b], [c, d]])
    vector = np.array([[5], [5]])
    transformed_vector = np.dot(matrix, vector)
    
    fig, ax = plt.subplots(figsize=(6, 6))
    
    # Original vector (blue)
    ax.quiver(0, 0, vector[0, 0], vector[1, 0], angles='xy', scale_units='xy', scale=1, color='b', label="Original Vector")
    
    # Transformed vector (red)
    ax.quiver(0, 0, transformed_vector[0, 0], transformed_vector[1, 0], angles='xy', scale_units='xy', scale=1, color='r', label="Transformed Vector")
    
    ax.set_xlim([-10, 10])
    ax.set_ylim([-10, 10])
    ax.axvline(x=0, color='grey', lw=1)
    ax.axhline(y=0, color='grey', lw=1)
    ax.grid(True, which='both')
    ax.legend()
    
    plt.show()

widgets.interact(matrix_transform, 
                 a=widgets.IntSlider(min=-5, max=5, value=1, description="a"),
                 b=widgets.IntSlider(min=-5, max=5, value=0, description="b"),
                 c=widgets.IntSlider(min=-5, max=5, value=0, description="c"),
                 d=widgets.IntSlider(min=-5, max=5, value=1, description="d"))
<function __main__.matrix_transform(a=1, b=0, c=0, d=1)>

Solve linear euqation by matrix inversion#

# Define the coefficients matrix A and the right-hand side vector b
A = np.array([[2, 3],
              [1, -2]])

b = np.array([7, 1])

# Calculate the inverse of matrix A
A_inv = np.linalg.inv(A)

# Solve for the unknown vector x using matrix inversion: x = A_inv * b
x = np.dot(A_inv, b)

print("Solution using matrix inversion:")
print("x =", x)
Solution using matrix inversion:
x = [2.42857143 0.71428571]

Compute eigenvalues and eigenvectors#

# Define the coefficients matrix A and the right-hand side vector b
# vary coeficients
A = np.array([[1, -3],
              [1, -2]])

# Calculate the eigenvalues and eigenvectors of A
eigenvalues, eigenvectors = np.linalg.eig(A)
print('eigvals:')
print(eigenvalues)
print('eigvecs:')
print(eigenvectors)
eigvals:
[-0.5+0.8660254j -0.5-0.8660254j]
eigvecs:
[[0.8660254+0.j   0.8660254-0.j  ]
 [0.4330127-0.25j 0.4330127+0.25j]]