Notebook 19 – Math 2121, Fall 2020

Below are functions and examples covering the most important algorithms from the whole course.

8.1 μs
486 μs
DEFAULT_TOLERANCE
1.0e-9
40.0 ns
rowop_swap (generic function with 1 method)
111 μs
RREF (generic function with 2 methods)
81.6 μs
pivot_positions (generic function with 2 methods)
56.4 μs
solve_linear_system (generic function with 2 methods)
59.1 μs
column_space_basis (generic function with 2 methods)
42.2 μs
null_space_basis (generic function with 2 methods)
72.2 μs
is_diagonalizable (generic function with 1 method)
28.9 μs
gram_schmidt_process (generic function with 1 method)
90.8 μs
orthogonal_projection (generic function with 1 method)
40.7 μs
least_squares_solution (generic function with 2 methods)
44.5 μs
pseudoinverse (generic function with 2 methods)
67.9 μs
random_matrix (generic function with 2 methods)
47.2 μs

Variable names in Pluto

Pluto notebooks do not allow you to redefine variables names, so a variable can only be assigned once. You won't have this problem if you just import this notebook into Julia without using Pluto, which you can do by opening Julia in your terminal from the directory that contains this file and then running

include("19_Math2121_Fall2020.jl")

To make it easier to extend the examples in this notebook, the variable names used below all begin with underscores, like _A, _b, etc., so as not to crowd the usual namespace of variables. There is no need to continue this convention if you add to this notebook.

9.8 μs

Numerical tolerance

Warning: it is a tricky problem to determine when floating point operations should give the value 0.0. Many of the numerical algorithms above depend on a tolerance parameter tol that we use to determine when a floating point x number is zero, by checking if abs(x) < tol. Whether or not you get sensible output from these functions may depend on the value of tol. You can set this manually by passing in a value for tol. For example:

RREF(A) # uses default tolerance

RREF(A, 1e-10) # uses tol = 1e-10

That said, most of the time we don't have to worry about this parameter.

By default tol is set to 1.0e-9.

23.7 μs

Matrix operations

4.0 μs
_A
4×4 Array{Float64,2}:
  0.377141  -6.76723  5.62943    7.26387
  2.51621    1.98108  0.956934  -0.534678
  2.2948    -3.02732  4.62686    4.53796
 -0.785845  -4.35937  2.60611    4.05582
68.1 μs
4×4 Transpose{Float64,Array{Float64,2}}:
  0.377141   2.51621    2.2948   -0.785845
 -6.76723    1.98108   -3.02732  -4.35937
  5.62943    0.956934   4.62686   2.60611
  7.26387   -0.534678   4.53796   4.05582
46.0 ns
207 ns
4
290 ns
4
204 ns
_b
4×1 Array{Int64,2}:
  0
  2
  2
 -2
3.6 μs
_c
4×1 Array{Int64,2}:
 -2
  2
  1
  2
3.3 μs
4×1 Array{Float64,2}:
 -16.803342999999998
   6.945391
  -5.8768449999999985
 -11.618156999999998
19.3 μs
4×1 Array{Int64,2}:
 -2
  4
  3
  0
4.4 μs
4×1 Array{Int64,2}:
  0
  4
  2
 -4
101 ms
4×4 Diagonal{Int64,Array{Int64,1}}:
 0  ⋅  ⋅   ⋅
 ⋅  2  ⋅   ⋅
 ⋅  ⋅  2   ⋅
 ⋅  ⋅  ⋅  -2
5.0 ms
4×4 Array{Float64,2}:
  0.377141  -6.76723  5.62943    7.26387
  2.51621    1.98108  0.956934  -0.534678
  2.2948    -3.02732  4.62686    4.53796
 -0.785845  -4.35937  2.60611    4.05582
49.0 ns
2×2 Array{Float64,2}:
 2.51621   1.98108
 2.2948   -3.02732
1.6 μs
4.2 μs
3.5 μs
-3.0273244999999993
235 ns
_square_matrix
3×3 Array{Int64,2}:
 1  2  3
 3  4  2
 0  1  0
35.2 ms
7.0
19.0 μs
5
233 ns
3×6 Array{Int64,2}:
 1  2  3  1  0  0
 3  4  2  0  1  0
 0  1  0  0  0  1
29.7 μs
3×6 Array{Float64,2}:
 1.0  0.0  0.0  -0.285714   0.428571  -1.14286
 0.0  1.0  0.0   0.0        0.0        1.0
 0.0  0.0  1.0   0.428571  -0.142857  -0.285714
129 ms
3×3 Array{Float64,2}:
 -0.285714   0.428571  -1.14286
 -0.0        0.0        1.0
  0.428571  -0.142857  -0.285714
49.5 μs
3×3 Array{Int64,2}:
  7  13   7
 15  24  17
  3   4   2
6.4 μs

Row reduction

5.5 μs
4×4 Array{Float64,2}:
  0.377141  -6.76723  5.62943    7.26387
  2.51621    1.98108  0.956934  -0.534678
  2.2948    -3.02732  4.62686    4.53796
 -0.785845  -4.35937  2.60611    4.05582
40.0 ns
4×4 Array{Float64,2}:
  1.0  0.0   0.991744   0.606026
 -0.0  1.0  -0.776596  -1.03962
  0.0  0.0   0.0        0.0
  0.0  0.0   0.0       -0.0
92.7 ms
37.5 ms
4×2 Array{Float64,2}:
  0.377141  -6.76723
  2.51621    1.98108
  2.2948    -3.02732
 -0.785845  -4.35937
62.4 ms
4×2 Array{Float64,2}:
 -0.991744  -0.606026
  0.776596   1.03962
  1.0        0.0
  0.0        1.0
69.3 ms
4×2 Array{Float64,2}:
 0.0           0.0
 0.0           1.11022e-16
 8.88178e-16   0.0
 4.44089e-16  -8.88178e-16
18.5 μs
2
81.0 μs
false
24.1 μs

Solving linear systems

3.4 μs
_M
4×4 Array{Float64,2}:
  -3.57174   -7.35301   -3.42201   3.81875
   5.69388    6.33517   -1.96969   1.70079
   7.69715    3.05182  -10.2608   10.2693
 -11.0038   -13.5657     1.98354  -1.37461
12.9 μs
_test_vec
4×1 Array{Float64,2}:
  -5.958647520884568
   5.114630507983956
   2.4275068057374454
 -10.96086766946625
8.1 μs
_test_sol
4×1 Array{Float64,2}:
 -0.007336740627503224
  0.8139325807742661
  0.0
  0.0
43.4 ms
5.0 μs
_dim_nul_M
2
11.8 μs
_other_sol
4×1 Array{Float64,2}:
 0.04221002984635365
 0.7791793775967624
 0.06048291925849458
 0.03362373291973686
18.2 μs
6.3 μs
_vec_not_in_column_space
17.8 μs
39.0 ns

Complex numbers

5.9 μs
5 + 6im
203 ns
5
208 ns
6
300 ns
5 - 6im
271 ns
9 + 8im
222 ns
8 + 34im
195 ns
1.6 + 0.7im
224 ns
7.810249675906654
899 ns
true
962 ns
_z
2 + 3im
198 ns
2×2 Array{Int64,2}:
 2  -3
 3   2
28.0 ms

Eigenvalues and eigenvectors

2.4 μs
_B
4×4 Array{Float64,2}:
  6.0118   -5.45765   -5.14928  -0.975021
  2.02876   0.464118   4.78987  -3.90178
 -4.63644  -6.61134   -3.49888  -9.10147
 10.7833    4.37568   -1.54777  13.5531
14.8 μs
_C
4×4 Array{Int64,2}:
 1  2  3  4
 0  1  2  3
 0  0  1  2
 0  0  0  1
34.4 ms
true
142 ms
false
3.4 ms
46.1 μs
55.2 μs
4×4 Array{Complex{Float64},2}:
 -0.172547+0.0im  -0.0297456+0.418441im  -0.0297456-0.418441im   0.260016+0.0im
 -0.734333+0.0im   -0.671807-0.0im        -0.671807+0.0im       -0.289454+0.0im
  0.495264+0.0im    0.118236+0.491568im    0.118236-0.491568im  -0.359243+0.0im
  0.430925+0.0im   0.0936165-0.329111im   0.0936165+0.329111im   0.848264+0.0im
46.1 μs
4×4 Array{Complex{Float64},2}:
 -0.435685+0.0im  -0.160302+0.442395im   -0.160302-0.442395im     0.761407+0.0im
  0.215249+0.0im   0.591921-0.0im         0.591921+0.0im        0.00637559+0.0im
  0.711081+0.0im   0.440853+0.198845im    0.440853-0.198845im    -0.246823+0.0im
  0.508144+0.0im   0.437822-0.0513943im   0.437822+0.0513943im    0.599414+0.0im
59.8 μs
22.4 μs
4×4 Array{Float64,2}:
 1.0  -1.0           1.0          -1.0
 0.0   1.11022e-16  -1.11022e-16   1.11022e-16
 0.0   0.0           1.2326e-32   -1.2326e-32
 0.0   0.0           0.0           1.36846e-48
22.5 μs
4.1 ms
2.830578330796744e-14
1.1 s
_symmetric
4×4 Array{Float64,2}:
 12.0236   -3.42889    -9.78571    9.80823
 -3.42889   0.928236   -1.82147    0.473901
 -9.78571  -1.82147    -6.99775  -10.6492
  9.80823   0.473901  -10.6492    27.1062
2.6 μs
_eigenvectors
4×4 Array{Float64,2}:
 0.323252   0.234151   0.793089   0.4601
 0.196796   0.918942  -0.341333  -0.0175587
 0.911403  -0.24589   -0.12072   -0.307098
 0.161629  -0.200641  -0.489826   0.832881
85.4 μs
4×4 Array{Float64,2}:
  1.0          -7.95206e-18   1.78588e-16   5.29273e-17
 -7.95206e-18   1.0           9.42052e-17  -2.45753e-17
  1.78588e-16   9.42052e-17   1.0          -2.18036e-16
  5.29273e-17  -2.45753e-17  -2.18036e-16   1.0
14.6 μs

Inner products and orthogonal projections

2.3 μs
3.4641016151377544
691 ns
2
453 ns
_basis
5×3 Array{Float64,2}:
 -2.43045    2.55144  -6.17487
 -3.3509     9.80676   4.91765
  0.676911  -5.97453  -7.44102
 -6.03631    5.55339  -7.68659
 -0.555015  -2.87016  -7.55904
13.8 μs
3
24.6 μs
_orthogonal_basis
5×3 Array{Float64,2}:
 -2.43045   -0.80474  -2.92988
 -3.3509     5.17955  -0.557834
  0.676911  -5.03979  -0.0171806
 -6.03631   -2.78207   1.61207
 -0.555015  -3.63658  -1.35563
9.9 μs
3×3 Array{Float64,2}:
 54.3388        1.29626e-14  -2.6544e-14
  1.29626e-14  73.8394       -7.95571e-15
 -2.6544e-14   -7.95571e-15  13.3321
6.1 μs
_orthonormal_basis
5×3 Array{Float64,2}:
 -0.32971    -0.0936508  -0.802416
 -0.454575    0.602765   -0.152776
  0.0918283  -0.5865     -0.00470532
 -0.818872   -0.32376     0.441503
 -0.0752921  -0.423203   -0.37127
4.4 ms
3×3 Array{Float64,2}:
  1.0          -6.35764e-17  3.31234e-16
 -6.35764e-17   1.0          2.36696e-16
  3.31234e-16   2.36696e-16  1.0
7.2 μs
_v
5×1 Array{Int64,2}:
  2
 -2
 -1
 -4
 -1
3.6 μs
_v_hat
5×1 Array{Float64,2}:
  0.9157012881291711
 -0.6344293450681221
 -0.19998600158597624
 -4.355743010034982
  0.3483621248361716
31.3 μs
5×4 Array{Float64,2}:
  1.0   0.0  0.0   2.82911
  0.0   1.0  0.0   1.27133
 -0.0  -0.0  1.0  -0.736532
  0.0   0.0  0.0  -0.0
  0.0   0.0  0.0   0.0
8.7 μs
3×1 Array{Float64,2}:
 1.3131522849785959e-15
 3.2528969620086696e-15
 5.634650153790826e-15
7.2 μs
4×5 Array{Float64,2}:
  1.0  0.0   0.991744   0.606026   0.914466
 -0.0  1.0  -0.776596  -1.03962    0.0978552
  0.0  0.0   0.0        0.0        0.0
  0.0  0.0   0.0       -0.0       -0.0
25.7 μs

Least-squares solutions

3.2 μs
_least_squares_solution
4×1 Array{Float64,2}:
 0.9144659461447051
 0.09785518382324851
 0.0
 0.0
5.2 ms
4×1 Array{Float64,2}:
 0.9144659461447051
 0.09785518382324851
 0.0
 0.0
18.7 μs
9.6 μs
_dim_nul
2
17.8 μs
_another_least_squares_solution
4×1 Array{Float64,2}:
 -0.31484149960282926
  1.493477642011315
  0.7712839971084404
  0.7662893290500663
20.7 μs
5.8 μs

Singular value decompositions

2.5 μs
64.0 ns
_X
4×5 Array{Float64,2}:
 -3.4233    7.59986    0.0224568  -8.46628    5.98055
 -4.28842  -0.502641   5.19409     1.47641    7.49157
  1.62312  -1.38475   -1.15415     1.33975   -2.83553
 -3.4536    0.860067   3.53104    -0.335714   6.03324
13.7 μs
SVD{Float64,Float64,Array{Float64,2}}
U factor:
4×4 Array{Float64,2}:
  0.718735   0.661076   -0.209989   0.0479817
  0.479036  -0.664983   -0.512437  -0.256378
 -0.247517   0.0170652  -0.62418    0.74084
  0.438953  -0.347108    0.551101   0.61897
singular values:
4-element Array{Float64,1}:
 16.002967090628864
 10.105428798819345
  1.1687268383051277e-15
  4.133566008353511e-16
Vt factor:
5×5 Array{Float64,2}:
 -0.401955   0.371292   0.271195  -0.365978   0.702202
  0.179619   0.498363  -0.463561  -0.637207  -0.313766
  0.537101  -0.502761   0.344718  -0.564462   0.145963
 -0.65493   -0.578057  -0.327772  -0.339399  -0.119548
  0.297923  -0.163886  -0.696634   0.161919   0.610628
49.5 ms
4×5 Array{Float64,2}:
 16.003   0.0     0.0          0.0          0.0
  0.0    10.1054  0.0          0.0          0.0
  0.0     0.0     1.16873e-15  0.0          0.0
  0.0     0.0     0.0          4.13357e-16  0.0
1.8 μs
5.393785051703609e-15
9.2 μs
_X_plus
5×4 Array{Float64,2}:
 -0.00630251  -0.023852    0.00652033  -0.0171951
  0.0492776   -0.0216802  -0.00490116  -0.00693373
 -0.0181451    0.0386225  -0.00497738   0.0233614
 -0.0581218    0.0309759   0.00458449   0.0118486
  0.0110118    0.0416671  -0.0113908    0.0300385
28.5 ms
8.3 μs