You are on page 1of 5

1

Python with Linear Algebra:


2D

G V V Sharma∗

Contents and can be obtained as


!
0 1
1 Line 1 n= m (6)
−1 0
! !
0 1 3
2 Altitudes of a Triangle 2 = (7)
−1 0 5
! !
3 Circumcircle 3 0×3+1×5 5
= = (8)
−1 × 3 + 0 × 5 −3
4 Medians of a Triangle 3 1.3 Verify (5).
Solution:
5 Incircle 4 !T !   3!
T 5 3
n m= = 5 −3 (9)
−3 5 5
Abstract—This manual introduces matrix computations
using python and the properties of a triangle. = 5 × 3 + (−3) × 5 = 0 (10)
1.4 Find the equation of AB.
Solution: The desired equation is obtained as
1 Line
x = A + λ (B − A) (11)
! !
1.1 Let 2 3
−2
!
1
!
4
! =− +λ (12)
A= ,B = ,C = , (1) 2 5
−2 3 −1
1.5 Draw the line AB
Find the direction vector and the normal vector Solution: The following code plots AB in Fig.
for AB 1.5
Solution: The direction vector of AB is
#Plotting AB
m=B−A (2) import numpy as np
! !
1 −2 import matplotlib.pyplot as plt
= − (3)
3 −2
1 − (−2)
!
3
! #if using termux
= = (4) import subprocess
3 − (−2) 5
import shlex
1.2 Find the normal vector of AB. #end if
Solution: The normal vector n is defined as
nT m = 0 (5)
A = np.array([−2,−2])
B = np.array([1,3])
*The author is with the Department of Electrical Engineering, len =10
Indian Institute of Technology, Hyderabad 502285 India e-mail:
gadepall@iith.ac.in. All content in this manual is released under GNU
GPL. Free and open source. x AB = np.zeros((2,len))
2

3 B 3 B AB
BC
CA
2 2

1 1

y
0 0

C
−1 −1

A A
−2 −2
−2.0 −1.5 −1.0 −0.5 0.0 0.5 1.0 −2 −1 0 1 2 3 4
x

Fig. 1.5 Fig. 1.6

lam = np.linspace(0,1,len) 1.7 Find the equation of the line in terms of the
for i in range(len): normal vector.
temp1 = A + lam[i]∗(B−A) Solution: The desired equation is
x AB[:,i]= temp1.T
nT (x − A) = 0 (13)
plt.plot(x AB[0,:],x AB[1,:],label=’$AB$’)     2!
=⇒ 5 −3 x = − 5 −3 = −4 (14)
plt.grid() # minor 2
1.8 Verify that the above equation can be obtained
plt.plot(A[0], A[1], ’o’)
by
plt.text(A[0] ∗ (1 + 0.1), A[1] ∗ (1 − 0.1) , ’
A’) nT (x − B) = 0 (15)
plt.plot(B[0], B[1], ’o’)
plt.text(B[0] ∗ (1 − 0.2), B[1] ∗ (1) , ’B’) 1.9 Find the equations of BC and CA.

#if using termux 2 Altitudes of a Triangle


plt.savefig(’../figs/draw line.pdf’) 2.1 In ∆ABC, Let P be a point on BC such that
plt.savefig(’../figs/draw line.eps’) AP ⊥ BC. Then AP is defined to be an altitude
subprocess.run(shlex.split(”termux−open ../ of ∆ABC.
figs/draw line.pdf”)) 2.2 Find the equation of AP.
#else Solution: The normal vector of AP is B − C.
#plt.show() From (12), the equation of AP is
1.6 Draw △ABC. (B − C)T (x − A) = 0 (16)
Solution: The following codes yields the de-     2!
sired plot in Fig. 1.6 =⇒ −3 4 x = − −3 4 = −2 (17)
2
https://raw.githubusercontent.com/gadepall/
school/master/linalg/2D/python 2d/codes/ 2.3 Find the equation of the altitude BQ.
coeffs.py Solution: The desired equation is
(C − A)T (x − B) = 0 (18)
https://raw.githubusercontent.com/gadepall/     1!
school/master/linalg/2D/python 2d/codes/ =⇒ 6 1 x = 6 1 =9 (19)
3
draw triangle.py
2.4 Find the equation of the altitude CR.
3

3 Circumcircle
3 B AB 3.1 Let A, B and C be points on a circle with centre
BC O and radius r.
CA
2 AP 3.2 Find O.
BQ
CR Solution: The equation of the circle is
1
kx − Ok = R (21)
y

0 =⇒ kx − Ok2 = (x − O)T (x − O) = R2 (22)

−1
C From (20),

A kA − Ok2 − kB − Ok2 = 0 (23)


−2
−2 −1 0 1 2 3 4
x
=⇒ (A − O)T (A − O)
Fig. 2.9 − (B − O)T (B − O) = 0 (24)
which can be simplified as
2.5 Find the point of intersection of AP and BQ. kAk2 − kBk2
(A − B)T O = (25)
Solution: (15) and (17) can be stacked together 2
into the matrix equation Similarly,
! !
−3 4 −2 kBk2 − kCk2
6 1
x=
9
(20) (B − C)T O = (26)
2
The following code computes the point of The following code computes O using the
intersection. above two equations.
https://raw.githubusercontent.com/gadepall/
https://raw.githubusercontent.com/gadepall/
school/master/linalg/2D/python 2d/codes/
school/master/linalg/2D/python 2d/codes/
circumcentre.py
orthocentre.py
3.3 Find the radius R.
2.6 Find the point of intersection of and BQ and 3.4 Plot the circumcircle of △ABC.
CR. Comment. Solution: The following code plots Fig. 3.4
2.7 Find P
Solution: The following code finds the re- https://raw.githubusercontent.com/gadepall/
quired points. school/master/linalg/2D/python 2d/codes/
circumcircle.py
https://raw.githubusercontent.com/gadepall/
school/master/linalg/2D/python 2d/codes/
alt foot.py 4 Medians of a Triangle
4.1 Find the coordinates of D, E and F of the
2.8 Find Q and R. mid points of AB, BC and CA respectively for
2.9 Draw AP, BQ and CR and verify that they meet ∆ABC.
at a point H. 4.2 Find the equations of AD, BE and CF. These
Solution: The following code plots the alti- lines are the medians of ∆ABC
tudes in Fig. 2.9 4.3 Find the point of intersection of AD and CF.
4.4 Verify that G is the point of intersection of
https://raw.githubusercontent.com/gadepall/ BE, CF as well as AD, BE. G is known as the
school/master/linalg/2D/python 2d/codes/ centroid of ∆ABC.
alt draw.py 4.5 Graphically show that the medians of ∆ABC
meet at the centroid.
4

From (32) and (33)


3 B AB nT I = nT B + λnT n (37)
BC
2
CA
circumcircle
=⇒ nT (I − B) = λknk2 (38)

1
nT (I − B)
=⇒ r = |λ| knk = (39)
0
knk
O
y

C from (35). Letting


−1
A
n
−2 kn1 k = , (40)
knk
−3 r = nT1 (I − B) (41)
−4 −2 0 2 4 5.4 Find I.
x
Solution: Since r = IU = IV = IW, from (40),
Fig. 3.4 T
n (I − B) = nT (I − C) = nT (I − A) (42)
1 2 3

where n2 , n3 are unit normals of CA, AB re-


4.6 Verify that spectively. (41) can be expressed as
A+B+C
G= (27) nT1 (I − B) = k1 nT2 (I − C) (43)
3
nT2 (I − C) = k2 nT3 (I − A) (44)
5 Incircle
where k1 , k2 = ±1. The above equations can be
5.1 Consider a circle with centre I and radius r that expressed as the matrix equation
lies within △ABC and touches BC, CA and AB !
at U, V and W respectively.  T nT1 B − k1 nT2 C
n1 − k1 n2 n2 − k2 n3 I = T
5.2 Show that IU ⊥ BC. n2 C − k2 nT3 A
Solution: Let x1 , x2 be two points on the circle (45)
such that x1 x2 k BC. Then 5.5 Show that I lies inside △ABC for k1 = k2 = 1
2
kx1 − Ik − kx2 − Ik = 0 2
(28) 5.6 Compute I and r.

x1 + x2
 Solution:
=⇒ (x1 − x2 )T −I =0 (29)
x + 2 https://raw.githubusercontent.com/gadepall/
1 x2 
school/master/linalg/2D/python 2d/codes/
=⇒ (B − C)T −I =0 (30)
2 incentre.py
For x1 = x2 = U, x1 x2 merges into BC and the
above equation becomes 5.7 Plot the incircle of △ABC
Solution: The following code plots the incircle
(B − C)T (U − I) = 0 =⇒ OD ⊥ BC (31) in Fig. 5.7
5.3 Find an expression for r if I is known. https://raw.githubusercontent.com/gadepall/
Solution: Let n be the normal vector of BC. school/master/linalg/2D/python 2d/codes/
The equation for BC is then given by incircle.py

nT (x − B) = 0 (32)
=⇒ nT (U − B) = 0 (33)
since U lies on BC. Since IU ⊥ BC,
I = U + λn (34)
=⇒ I − U = λn (35)
or r = kI − Uk = |λ| knk (36)
5

3 B AB
BC
CA
2 incircle

1
y

I
0

C
−1

A
−2
−2 −1 0 1 2 3 4
x

Fig. 5.7

You might also like