Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Numerieke primitievenยถ

Scalairenยถ

a=5a = 5
5
int64
()
ndarray
a=3.0a = 3.0
3.0
float64

Vectorenยถ

a=[5812]\pmb{a} = \begin{bmatrix} 5 \cr 8 \cr 12 \end{bmatrix}
[ 5  8 12]
(3,)

Representatie als een kolom-vector (een matrix met 1 kolom):

array([[ 5], [ 8], [12]])

Representatie als een rij-vector (een matrix met 1 rij):

aT=[5812]\pmb{a}^T = \begin{bmatrix} 5 & 8 & 12 \end{bmatrix}
array([[ 5, 8, 12]])

Transpositie terug naar kolom-vector:

array([[ 5], [ 8], [12]])

Matricesยถ

B=[1.02.03.07.01.02.0]\pmb{B} = \begin{bmatrix} 1.0 & 2.0 & 3.0 \cr 7.0 & 1.0 & 2.0 \end{bmatrix}
b: [[1. 2. 3.]
 [7. 1. 2.]]
b shape: (2, 3)
BT\pmb{B}^T
[[1. 7.]
 [2. 1.]
 [3. 2.]]

Tensorsยถ

C=[[1.02.03.07.01.02.0],[5.01.03.02.09.06.0]]\pmb{C} = \begin{bmatrix} \begin{bmatrix} 1.0 & 2.0 & 3.0 \cr 7.0 & 1.0 & 2.0 \end{bmatrix}, \cr \begin{bmatrix} 5.0 & 1.0 & 3.0 \cr 2.0 & 9.0 & 6.0 \end{bmatrix} \end{bmatrix}
c: [[[1. 2. 3.]
  [7. 1. 2.]]

 [[5. 1. 3.]
  [2. 9. 6.]]]
c shape: (2, 2, 3)
array([[[1., 2.], [3., 7.]], [[1., 2.], [5., 1.]], [[3., 2.], [9., 6.]]])
[[[[0.22733602 0.31675834 0.79736546]
   [0.67625467 0.39110955 0.33281393]]

  [[0.59830875 0.18673419 0.67275604]
   [0.94180287 0.24824571 0.94888115]]

  [[0.66723745 0.09589794 0.44183967]
   [0.88647992 0.6974535  0.32647286]]]


 [[[0.73392816 0.22013496 0.08159457]
   [0.1598956  0.34010018 0.46519315]]

  [[0.26642103 0.8157764  0.19329439]
   [0.12946908 0.09166475 0.59856801]]

  [[0.8547419  0.60162124 0.93198836]
   [0.72478136 0.86055132 0.9293378 ]]]


 [[[0.54618601 0.93767296 0.49498794]
   [0.27377318 0.45177871 0.66503892]]

  [[0.33089093 0.90345401 0.25707418]
   [0.33982834 0.2588534  0.35544648]]

  [[0.00502233 0.62860454 0.28238271]
   [0.06808769 0.61682898 0.17632632]]]


 [[[0.30438839 0.44088681 0.15020234]
   [0.21792886 0.47433312 0.47636886]]

  [[0.25523235 0.29756527 0.27906712]
   [0.26057921 0.48276159 0.21197904]]

  [[0.4956306  0.24626133 0.83848265]
   [0.18013059 0.86215629 0.17829944]]]


 [[[0.75053133 0.6111204  0.20915503]
   [0.75987242 0.24926057 0.08557173]]

  [[0.61805672 0.53696833 0.63452671]
   [0.17437411 0.24816449 0.68482298]]

  [[0.08087165 0.8750736  0.42869438]
   [0.6183942  0.3131055  0.17896286]]]]

Element-gewijse operatiesยถ

a=[5812]\pmb{a} = \begin{bmatrix} 5 \cr 8 \cr 12 \end{bmatrix}
a+5\pmb{a} + 5
a: [ 5  8 12]
a+5: [10 13 17]
aร—5\pmb{a} \times 5
a x 5: [25 40 60]
b=[123]\pmb{b} = \begin{bmatrix} 1 \cr 2 \cr 3 \end{bmatrix}
a+b\pmb{a} + \pmb{b}
a + b: [ 6 10 15]

Let op voor dimensies!

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[16], line 2
      1 b = np.array([1, 2, 3, 4])
----> 2 print(f"a + b: {a + b}")

ValueError: operands could not be broadcast together with shapes (3,) (4,) 
aโŠ™b\pmb{a} \odot \pmb{b}
a * b: [ 5 16 36]
A=[123456]\pmb{A} = \begin{bmatrix} 1 & 2 \cr 3 & 4 \cr 5 & 6 \end{bmatrix}
A+5\pmb{A}+5
A: [[1 2]
 [3 4]
 [5 6]]
A + 5: [[ 6  7]
 [ 8  9]
 [10 11]]
Aร—5\pmb{A} \times 5
A x 5: [[ 5 10]
 [15 20]
 [25 30]]
B=[533190]\pmb{B} = \begin{bmatrix} 5 & 3 \cr 3 & 1 \cr 9 & 0 \end{bmatrix}
A+B\pmb{A}+\pmb{B}
A: [[1 2]
 [3 4]
 [5 6]]
B: [[5 3]
 [3 1]
 [9 0]]
A + B: [[ 6  5]
 [ 6  5]
 [14  6]]
AโŠ™B\pmb{A} \odot \pmb{B}
A * B: [[ 5  6]
 [ 9  4]
 [45  0]]
AT+b\pmb{A}^T + \pmb{b}
A: [[1 2]
 [3 4]
 [5 6]]
b: [1 2 3]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[23], line 1
----> 1 print(A + b)

ValueError: operands could not be broadcast together with shapes (3,2) (3,) 
[[2 5 8]
 [3 6 9]]
ATโŠ™b\pmb{A}^T \odot \pmb{b}
[[ 1  6 15]
 [ 2  8 18]]
(d - g).shape: (5, 3, 2, 3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[28], line 1
----> 1 print((d - h).shape)

ValueError: operands could not be broadcast together with shapes (5,3,2,3) (3,3,2,5) 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[29], line 1
----> 1 print((d - j).shape)

ValueError: operands could not be broadcast together with shapes (5,3,2,3) (3,30) 

Dot productยถ

a=[732]Tb=[281]TaTb=โ€ฆ\begin{aligned} \pmb{a} &= \begin{bmatrix}7 & 3 & 2\end{bmatrix}^T \cr \pmb{b} &= \begin{bmatrix}2 & 8 & 1\end{bmatrix}^T \cr \pmb{a}^T\pmb{b} &= \ldots \end{aligned}
a: [7 3 2]
b: [2 8 1]
a'b: 40
a'b: 40
a=[4.61.15.38.210.1]Tb=[6.11.53.57.92.8]TaTb=โ€ฆ\begin{aligned} \pmb{a} &= \begin{bmatrix}4.6 & 1.1 & 5.3 & 8.2 & 10.1\end{bmatrix}^T \cr \pmb{b} &= \begin{bmatrix}6.1 & 1.5 & 3.5 & 7.9 & 2.8\end{bmatrix}^T \cr \pmb{a}^T\pmb{b} &= \ldots \end{aligned}
a: [ 4.6  1.1  5.3  8.2 10.1]
b: [6.1 1.5 3.5 7.9 2.8]
a'b: 141.32
a=[6127]Tb=[221]TaTb=โ€ฆ\begin{aligned} \pmb{a} &= \begin{bmatrix}6 & 1 & 2 & 7\end{bmatrix}^T \cr \pmb{b} &= \begin{bmatrix}2 & 2 & 1\end{bmatrix}^T \cr \pmb{a}^T\pmb{b} &= \ldots \end{aligned}
a: [6 1 2 7]
b: [2 2 1]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[33], line 5
      3 print(f"a: {a}")
      4 print(f"b: {b}")
----> 5 print(f"a'b: {np.dot(a, b)}")

ValueError: shapes (4,) and (3,) not aligned: 4 (dim 0) != 3 (dim 0)
A=[332714759283]b=[4281]TAb=โ€ฆ\begin{aligned} \pmb{A} &= \begin{bmatrix} 3 & 3 & 2 & 7 \cr 1 & 4 & 7 & 5 \cr 9 & 2 & 8 & 3 \end{bmatrix} \cr \pmb{b} &= \begin{bmatrix}4 & 2 & 8 & 1\end{bmatrix}^T \cr \pmb{A}\pmb{b} &= \ldots \end{aligned}
A: [[3 3 2 7]
 [1 4 7 5]
 [9 2 8 3]]
b: [4 2 8 1]
Ab: [ 41  73 107]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[35], line 2
      1 # Mind the order!
----> 2 print(f"bA: {np.dot(b, A)}")

ValueError: shapes (4,) and (3,4) not aligned: 4 (dim 0) != 3 (dim 0)
A=[6.13.71.04.35.13.33.93.77.98.2]b=[3.02.2]TAb=โ€ฆ\begin{aligned} \pmb{A} &= \begin{bmatrix} 6.1 & 3.7 \cr 1.0 & 4.3 \cr 5.1 & 3.3 \cr 3.9 & 3.7 \cr 7.9 & 8.2 \cr \end{bmatrix} \cr \pmb{b} &= \begin{bmatrix}3.0 & 2.2\end{bmatrix}^T \cr \pmb{A}\pmb{b} &= \ldots \end{aligned}
A: [[6.1 3.7]
 [1.  4.3]
 [5.1 3.3]
 [3.9 3.7]
 [7.9 8.2]]
b: [3.  2.2]
Ab: [26.44 12.46 22.56 19.84 41.74]
A=[332714759283]B=[62172275]AB=โ€ฆ\begin{aligned} \pmb{A} &= \begin{bmatrix} 3 & 3 & 2 & 7 \cr 1 & 4 & 7 & 5 \cr 9 & 2 & 8 & 3 \end{bmatrix} \cr \pmb{B} &= \begin{bmatrix} 6 & 2 \cr 1 & 7 \cr 2 & 2 \cr 7 & 5 \end{bmatrix} \cr \pmb{A}\pmb{B} &= \ldots \end{aligned}
A: [[3 3 2 7]
 [1 4 7 5]
 [9 2 8 3]]
B: [[6 2]
 [1 7]
 [2 2]
 [7 5]]
AB: [[74 66]
 [59 69]
 [93 63]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[38], line 2
      1 # Mind the order!
----> 2 print(f"BA: {np.dot(B, A)}")

ValueError: shapes (4,2) and (3,4) not aligned: 2 (dim 1) != 3 (dim 0)

Bereken het gemiddelde van de reeks 0 tot en met 113 aan de hand van een dot product.

a: [0 1 2 3 4 5 6 7 8 9] ...
w: [0.00877193 0.00877193 0.00877193 0.00877193 0.00877193 0.00877193
 0.00877193 0.00877193 0.00877193 0.00877193] ...
a'w: 56.5
a=[53292]Tโˆฃโˆฃaโˆฃโˆฃ2=โ€ฆ\begin{aligned} \pmb{a} &= \begin{bmatrix}5 & 3 & 2 & 9 & 2\end{bmatrix}^T \cr ||\pmb{a}||_2 &= \ldots \end{aligned}
||a||: 11.090536506409418
||a||: 11.090536506409418

Hoe groot is de cosine similarity tussen:

a=[4.61.15.38.210.1]Tb=[6.11.53.57.92.8]T\begin{aligned} \pmb{a} &= \begin{bmatrix}4.6 & 1.1 & 5.3 & 8.2 & 10.1\end{bmatrix}^T \cr \pmb{b} &= \begin{bmatrix}6.1 & 1.5 & 3.5 & 7.9 & 2.8\end{bmatrix}^T \cr \end{aligned}
cos(theta): 0.8633164353920106

Speciale matricesยถ

Maak een diagonale matrix:

diag(3.3,6.0,7.1,5.4)diag(3.3, 6.0, 7.1, 5.4)
array([[3.3, 0. , 0. , 0. ], [0. , 6. , 0. , 0. ], [0. , 0. , 7.1, 0. ], [0. , 0. , 0. , 5.4]])

Maak een idenditeitsmatrix:

I67\pmb{I}_{67}
array([[1., 0., 0., ..., 0., 0., 0.], [0., 1., 0., ..., 0., 0., 0.], [0., 0., 1., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 1., 0., 0.], [0., 0., 0., ..., 0., 1., 0.], [0., 0., 0., ..., 0., 0., 1.]], shape=(67, 67))

Transformeer de volgende matrix in (a) een upper triangular matrix en (b) een lower triangular matrix:

[3327147585529283]\begin{bmatrix} 3 & 3 & 2 & 7 \cr 1 & 4 & 7 & 5 \cr 8 & 5 & 5 & 2 \cr 9 & 2 & 8 & 3 \end{bmatrix}
array([[3, 3, 2, 7], [0, 4, 7, 5], [0, 0, 5, 2], [0, 0, 0, 3]])
array([[3, 0, 0, 0], [1, 4, 0, 0], [8, 5, 5, 0], [9, 2, 8, 3]])

Ga na of deze matrix (a) een orthogonale matrix is en (b) een rotatiematrix is. $$ \pmb{A} =

[0.70710678โˆ’0.707106780.00.00.707106780.707106780.00.00.00.01.00.00.00.00.01.0]\begin{bmatrix} 0.70710678 & -0.70710678 & 0.0 & 0.0 \cr 0.70710678 & 0.70710678 & 0.0 & 0.0 \cr 0.0 & 0.0 & 1.0 & 0.0 \cr 0.0 & 0.0 & 0.0 & 1.0 \end{bmatrix}

$$

Orthogonality: True
Rotation matrix: True

Systeem van lineaire vergelijkingenยถ

Gegeven het volgende stelsel:

14=2ฮฒ1+ฮฒ2+3ฮฒ318=ฮฒ1+4ฮฒ2+2ฮฒ312=3ฮฒ1+2ฮฒ2+ฮฒ3\begin{align} 14 &= 2\beta_1 + \beta_2 + 3\beta_3 \cr 18 &= \beta_1 + 4\beta_2 + 2\beta_3 \cr 12 &= 3\beta_1 + 2\beta_2 + \beta_3 \cr \end{align}

wat is de oplossing voor ฮฒ1\beta_1, ฮฒ2\beta_2 en ฮฒ3\beta_3?

  1. Matrix notatie

y=[141812],X=[213142321],b=[ฮฒ1ฮฒ2ฮฒ3]\mathbf{y} = \begin{bmatrix} 14 \\ 18 \\ 12 \end{bmatrix}, \quad \mathbf{X} = \begin{bmatrix} 2 & 1 & 3 \\ 1 & 4 & 2 \\ 3 & 2 & 1 \end{bmatrix}, \quad \mathbf{b} = \begin{bmatrix} \beta_1 \\ \beta_2 \\ \beta_3 \end{bmatrix}
  1. Inverse van X\pmb{X}

Xโˆ’1=1det(X)[(x22x33โˆ’x23x32)โˆ’(x12x33โˆ’x13x32)(x12x23โˆ’x13x22)โˆ’(x21x33โˆ’x23x31)(x11x33โˆ’x13x31)โˆ’(x11x23โˆ’x13x21)(x21x32โˆ’x22x31)โˆ’(x11x32โˆ’x12x31)(x11x22โˆ’x12x21)]\mathbf{X}^{-1} = \frac{1}{det(\pmb{X})} \begin{bmatrix} (x_{22}x_{33} - x_{23}x_{32}) & -(x_{12}x_{33} - x_{13}x_{32}) & (x_{12}x_{23} - x_{13}x_{22}) \cr -(x_{21}x_{33} - x_{23}x_{31}) & (x_{11}x_{33} - x_{13}x_{31}) & -(x_{11}x_{23} - x_{13}x_{21}) \cr (x_{21}x_{32} - x_{22}x_{31}) & -(x_{11}x_{32} - x_{12}x_{31}) & (x_{11}x_{22} - x_{12}x_{21}) \end{bmatrix}
detโก(X)=2(4โ‹…1โˆ’2โ‹…2)โˆ’1(1โ‹…1โˆ’2โ‹…3)+3(1โ‹…2โˆ’4โ‹…3)=2(4โˆ’4)โˆ’1(1โˆ’6)+3(2โˆ’12)=0+5โˆ’30=โˆ’25\begin{align} \det(\mathbf{X}) &= 2(4 \cdot 1 - 2 \cdot 2) - 1(1 \cdot 1 - 2 \cdot 3) + 3(1 \cdot 2 - 4 \cdot 3) \cr &= 2(4 - 4) - 1(1 - 6) + 3(2 - 12) = 0 + 5 - 30 = -25 \end{align}
Xโˆ’1=1โˆ’25[05โˆ’105โˆ’7โˆ’1โˆ’10โˆ’17]=[0โˆ’0.20.4โˆ’0.20.280.040.40.04โˆ’0.28]\begin{align} \mathbf{X}^{-1} &= \frac{1}{-25} \begin{bmatrix} 0 & 5 & -10 \\ 5 & -7 & -1 \\ -10 & -1 & 7 \end{bmatrix} \cr &= \begin{bmatrix} 0 & -0.2 & 0.4 \\ -0.2 & 0.28 & 0.04 \\ 0.4 & 0.04 & -0.28 \end{bmatrix} \end{align}
det(X) = -25.0
X-1 = [[ 0.   -0.2   0.4 ]
 [-0.2   0.28  0.04]
 [ 0.4   0.04 -0.28]]
  1. Oplossing

b=Xโˆ’1y\pmb{b} = \pmb{X}^{-1}\pmb{y}
b: [1.2  2.72 2.96]
y: [14 18 12]
Xb: [14. 18. 12.]