This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
6.sparse.jl
1263 lines (1056 loc) · 38.8 KB
/
6.sparse.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### A Pluto.jl notebook ###
# v0.19.19
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
end
# ╔═╡ 4b6307f7-af43-4990-a5c4-5be2b6b11364
using PlutoUI
# ╔═╡ e548acb6-bf53-11ed-1988-7325c1e2b481
using LinearAlgebra, SparseArrays
# ╔═╡ 0c8bf832-0cf8-4b56-b064-1dd177aceae8
using KrylovKit
# ╔═╡ 5b764297-3d06-4899-a8ae-f4378427a6e5
using Graphs # for generating sparse matrices
# ╔═╡ 8d0d3889-14c0-4e2e-b710-8830e2dab827
TableOfContents()
# ╔═╡ 2add4589-251b-47d0-a800-f0db7f204b61
html"""<button onclick="present()">present</button>"""
# ╔═╡ 83886259-6371-4a02-aedf-b84b413bc229
some_random_matrix = reshape(1:25, 5, 5)
# ╔═╡ 4f1e505c-170b-436f-a8ba-ef61c1d8943e
struct COOMatrix{T} <: AbstractArray{T, 2} # Julia does not have a COO data type
rowval::Vector{Int} # row indices
colval::Vector{Int} # column indices
nzval::Vector{T} # values
m::Int # number of rows
n::Int # number of columns
end
# ╔═╡ a205f6d4-8ecd-4103-9904-505c4ecc7b72
Base.size(coo::COOMatrix{T}) where T = (coo.m, coo.n)
# ╔═╡ 78e46965-d581-4f1b-9b53-283441526571
function Base.getindex(coo::COOMatrix{T}, i::Integer, j::Integer) where T
v = zero(T)
for (i2, j2, v2) in zip(coo.rowval, coo.colval, coo.nzval)
if i == i2 && j == j2
v += v2 # accumulate the value, since repeated indices are allowed.
end
end
return v
end
# ╔═╡ 4990344c-81e8-48cf-93c0-fa1460e0470b
md"# Overview
1. Sparse matrix representation.
* COOrdinate (COO) format
* Compressed Sparse Column/Row (CSC/CSR) format
2. Solving the dominant eigenvalue problem.
* Symmetric Lanczos process
* Anoldi process
"
# ╔═╡ 49a32422-2415-487e-b3be-28a0436bb552
md"# Sparse Matrices"
# ╔═╡ 4e017416-db9b-43b9-8fd5-657e9810e075
md"Recall that the elementary elimination matrix in Gaussian elimination has the following form."
# ╔═╡ 893f30df-9984-41e3-8353-c23d22730dcc
md"""
```math
M_k = \left(\begin{matrix}
1 & \ldots & 0 & 0 & 0 & \ldots & 0\\
\vdots & \ddots & \vdots & \vdots & \vdots & \ddots & \vdots\\
0 & \ldots & 1 & 0 & 0 & \ldots & 0\\
0 & \ldots & 0 & 1 & 0 & \ldots & 0\\
0 & \ldots & 0 & -m_{k+1} & 1 & \ldots & 0\\
\vdots & \ddots & \vdots & \vdots & \vdots & \ddots & \vdots\\
0 & \ldots & 0 & -m_{n} & 0 & \ldots & 1\\
\end{matrix}\right)
```
where $m_i = a_i/a_k$.
"""
# ╔═╡ 5cca8d2a-7b49-4fcd-bf84-748cb0d66eb0
md"The following cell is copied from notebook: `4.linearequation.jl`"
# ╔═╡ 50b753dc-7033-447b-9480-2e7e0a052f78
md"""
This representation requires storing $n^2$ elements, which is very memory inefficient since it has only $2n-k$ nonzero elements.
"""
# ╔═╡ cf51c362-3a4a-4f60-9123-2693b60e00fd
md"Let $A\in\mathbb{R}^{m\times n}$ be a sparse matrix, and ${\rm nnz}(A) \ll mn$ be the number of nonzero elements in $A$. Is there a universal matrix type that stores such sparse matrices efficiently?."
# ╔═╡ 78da9988-7cc0-4be7-9b8b-89e0029971d7
md"The answer is yes."
# ╔═╡ 6210e3be-fcd3-4682-9f45-8c8d1af9f99c
md"""
## COOrdinate (COO) format
"""
# ╔═╡ 30eb61aa-b03c-4bb7-8491-c6e411a378fe
md"""
The coordinate format means storing nonzero matrix elements into triples
```math
\begin{align}
&(i_1, j_1, v_1)\\
&(i_2, j_2, v_2)\\
&\vdots\\
&(i_k, j_k, v_k)
\end{align}
```
Quiz: How many bytes are required to store the matrix `demo_matrix` in the COO format?
"""
# ╔═╡ 3b1537ea-6176-4996-acd0-05071187f95c
md"We need to implement the [`AbstractArray` interfaces](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array)."
# ╔═╡ 3b72536e-c005-4224-ba9d-b157f1c38310
Base.size(coo::COOMatrix{T}, i::Int) where T = getindex((coo.m, coo.n), i)
# ╔═╡ f19a3128-c0a5-4529-b839-1e97fb969483
function elementary_elimination_matrix(A::AbstractMatrix{T}, k::Int) where T
n = size(A, 1)
@assert size(A, 2) == n
# create Elementary Elimination Matrices
M = Matrix{Float64}(I, n, n)
for i=k+1:n
M[i, k] = -A[i, k] ./ A[k, k]
end
return M
end
# ╔═╡ 64ac1cc6-6463-44f8-be88-54f9f52fc12b
demo_matrix = elementary_elimination_matrix(some_random_matrix, 3)
# ╔═╡ 33c4367d-2475-4cf5-8ac4-7ecd81642bbf
md"""
## Indexing a COO matrix
"""
# ╔═╡ 6a3e1a35-762a-4e1e-a706-310e21e618b3
md"Element indexing requires $O({\rm nnz}(A))$ time."
# ╔═╡ 8b6801b4-0ece-4484-a48f-f9435a868bc1
coo_matrix = COOMatrix([1, 2, 3, 4, 5, 4, 5], [1, 2, 3, 4, 5, 3, 3], [1, 1, 1, 1, 1, demo_matrix[4,3], demo_matrix[5, 3]], 5, 5)
# ╔═╡ 6f54c341-383a-4969-95d3-9e347862ddd2
# uncomment to show the result
sizeof(coo_matrix)
# ╔═╡ 0ea729be-40b2-41ab-991a-c87c8a79fbdf
md"""
## Multiplying two COO matrices
"""
# ╔═╡ 1365d99c-4baa-4f04-b5e3-a05fbb3a42c1
md"In the following example, we compute `coo_matrix * coo_matrix`."
# ╔═╡ cd21ceec-3ebd-459b-b28c-8942a885d4b6
function Base.:(*)(A::COOMatrix{T1}, B::COOMatrix{T2}) where {T1, T2}
@assert size(A, 2) == size(B, 1)
rowval = Int[]
colval = Int[]
nzval = promote_type(T1, T2)[]
for (i, j, v) in zip(A.rowval, A.colval, A.nzval)
for (i2, j2, v2) in zip(B.rowval, B.colval, B.nzval)
if j == i2
push!(rowval, i)
push!(colval, j2)
push!(nzval, v * v2)
end
end
end
return COOMatrix(rowval, colval, nzval, size(A, 1), size(B, 2))
end
# ╔═╡ 2e7b7227-e3c8-4235-aeea-610508e25432
coo_matrix * coo_matrix
# ╔═╡ 09fff797-5ee7-442c-90de-fb2eae4ede7c
demo_matrix ^ 2
# ╔═╡ 90193207-5ace-4987-94f5-747690777ab3
md"""
Yep!
"""
# ╔═╡ 3b43ee31-eb5a-4e62-81ba-6196e1ad8420
md"""
* Quiz 1: What is the time complexity of COO matrix `setindex!` (`A[i, j] += v`)?
* Quiz 2: What is the time complexity of COO matrix multiplication?
"""
# ╔═╡ 86dc94e1-726a-40c5-87fd-6f5ab43774da
md"""
## Compressed Sparse Column (CSC) format
"""
# ╔═╡ c5eb77b7-f1e6-45fc-88ef-cf4edf331223
md"""
A CSC format sparse matrix can be constructed with the `SparseArrays.sparse` function
"""
# ╔═╡ 39c9b4c3-469d-45dd-ab89-523c48764480
csc_matrix = sparse(coo_matrix.rowval, coo_matrix.colval, coo_matrix.nzval)
# ╔═╡ eff3b121-edaa-4dcd-b3f3-fbdfb061713d
md"It contains 5 fields"
# ╔═╡ 3133cc77-6647-45c5-b693-5eaa1b9528bc
fieldnames(csc_matrix |> typeof)
# ╔═╡ 04531119-a926-4e9e-9f4f-b0cfc655f637
md"""
The `m`, `n`, `rowval` and `nzval` have the same meaning as those in the COO format.
`colptr` is a integer vector of size $n+1$, the element of which points to the elements in `rowval` and `nzval`. Given a matrix $A \in \mathbb{R}^{m\times n}$ in the CSC format, the $j$-th column of $A$ is defined as
`A[rowval[colptr[j]:colptr[j+1]-1], j] := nzval[colptr[j]:colptr[j+1]-1]`
"""
# ╔═╡ 242a2046-fae4-4a45-9792-2ac06deb08a3
csc_matrix[:, 3]
# ╔═╡ 009dd53d-402f-44a4-9dec-087887ab74eb
md"The row indices of nonzero elements in the 3rd column."
# ╔═╡ a59bda82-78cd-44e3-ad23-f3a887a33ad7
rows3 = csc_matrix.rowval[csc_matrix.colptr[3]:csc_matrix.colptr[4]-1]
# ╔═╡ bb4a7db3-c437-4247-8ba5-8f752960f8eb
# or equivalently in Julia, we can use `nzrange`
csc_matrix.rowval[nzrange(csc_matrix, 3)]
# ╔═╡ 4cffe4df-c28a-4004-afc1-1652020d0aa7
md"The values of nonzero elements in the 3rd column."
# ╔═╡ e4b610d0-5247-4c6b-8f06-733504f8ebae
csc_matrix.nzval[csc_matrix.colptr[3]:csc_matrix.colptr[4]-1]
# ╔═╡ cb869bad-b17c-446e-860f-600e1054668d
md"""
## Indexing a CSC matrix
"""
# ╔═╡ a4ff9546-3dc4-463e-84b3-73303ced838d
md"""
The number of operations required to index an element in the $j$-th column of a CSC matrix is linear to the nonzero elements in the $j$-th column.
"""
# ╔═╡ 2a590b74-3ed1-44bf-87bc-c703a237211b
# I do not want to overwrite `Base.getindex`
function my_getindex(A::SparseMatrixCSC{T}, i::Int, j::Int) where T
for k in nzrange(A, j)
if A.rowval[k] == i
return A.nzval[k]
end
end
return zero(T)
end
# ╔═╡ 9296e387-fc25-495c-9299-564d7d512e72
my_getindex(csc_matrix, 4, 3)
# ╔═╡ 97d9d866-20bd-490f-b0e1-a722a3dedf29
md"""
## Multiplying two CSC matrices
"""
# ╔═╡ 27ebe6eb-5b47-400a-8e30-2dccafdaab24
md"""
Multiplying two CSC matrices is much faster than multiplying two COO matrices.
"""
# ╔═╡ 778b3d09-1b76-4ad0-bdd7-8cbd06918519
function my_matmul(A::SparseMatrixCSC{T1}, B::SparseMatrixCSC{T2}) where {T1, T2}
T = promote_type(T1, T2)
@assert size(A, 2) == size(B, 1)
rowval, colval, nzval = Int[], Int[], T[]
for j2 in 1:size(B, 2) # enumerate the columns of B
for k2 in nzrange(B, j2) # enumerate the rows of B
v2 = B.nzval[k2]
for k1 in nzrange(A, B.rowval[k2]) # enumerate the rows of A
push!(rowval, A.rowval[k1])
push!(colval, j2)
push!(nzval, A.nzval[k1] * v2)
end
end
end
return sparse(rowval, colval, nzval, size(A, 1), size(B, 2))
end
# ╔═╡ fa3f8189-85eb-4943-8347-83aa6572c2fe
my_matmul(csc_matrix, csc_matrix)
# ╔═╡ e8234fbd-61a8-4ff8-9d18-b974267fb062
csc_matrix^2
# ╔═╡ d7f295c4-ce02-46d0-9f14-5ec3d4f25ab3
md"""
Quiz: What is the time complexity of CSC matrix `setindex!` (`A[i, j] = v`)?
"""
# ╔═╡ fffdf566-3d54-4cbe-8543-bc88bd669f4c
md"# Large sparse eigenvalue problem"
# ╔═╡ f14621f1-a002-46c5-b0c0-15d008ab382c
md"""
## Dominant eigenvalue problem
"""
# ╔═╡ 3b75625c-2d92-4499-9662-ee1c7a173c30
md"One can use the power method to compute dominant eigenvalues (one having the largest absolute value) of a matrix."
# ╔═╡ 1981eb8f-e93b-44cb-b81d-6f8c7ed13ec1
function power_method(A::AbstractMatrix{T}, n::Int) where T
n = size(A, 2)
x = normalize!(randn(n))
for i=1:n
x = A * x
normalize!(x)
end
return x' * A * x', x
end
# ╔═╡ ec049390-f8fa-42d8-8e63-45af08b058d3
md"""
Since computing matrix-vector multiplication of CSC sparse matrix is fast, the power method is a convenient method to obtain the largest eigen value of a sparse matrix.
"""
# ╔═╡ a51ddacf-7e9f-4ab5-be9b-f2ed4e9952aa
md"The rate of convergence is dedicated by $|\lambda_2/\lambda_1|^k$."
# ╔═╡ cefb596e-2df5-44a3-bd6f-062e7ebdc0ed
md"""
By inverting the sign, $A\rightarrow -A$, we can use the same method to obtain the smallest eigenvalue.
"""
# ╔═╡ 50717c84-e8cf-4e19-b344-7d50b0b290ff
md"## The symmetric Lanczos process"
# ╔═╡ 339df51a-7623-405a-a590-adc337edc0be
md"""
Let $A \in \mathbb{R}^{n \times n}$ be a large symmetric sparse matrix, the Lanczos process can be used to obtain its largest/smallest eigenvalue, with faster convergence speed comparing with the power method.
"""
# ╔═╡ 014be843-e2d3-42c8-bbc9-bd66bbb7df93
md"""
## The Krylov subspace
"""
# ╔═╡ 2a548303-adb7-4189-bb82-2095f920a996
md"""
A Krylov subspace of size $k$ with initial vector $q_1$ is defined by
```math
\mathcal{K}(A, q_1, k) = {\rm span}\{q_1, Aq_1, A^2q_1, \ldots, A^{k-1}q_1\}
```
"""
# ╔═╡ b9c1e68a-634f-42e0-aa13-e84b4feb2a3a
md"""
The Julia package `KrylovKit.jl` contains many Krylov space based algorithms.
`KrylovKit.jl` accepts general functions or callable objects as linear maps, and general Julia
objects with vector like behavior (as defined in the docs) as vectors.
The high level interface of KrylovKit is provided by the following functions:
* `linsolve`: solve linear systems
* `eigsolve`: find a few eigenvalues and corresponding eigenvectors
* `geneigsolve`: find a few generalized eigenvalues and corresponding vectors
* `svdsolve`: find a few singular values and corresponding left and right singular vectors
* `exponentiate`: apply the exponential of a linear map to a vector
* `expintegrator`: [exponential integrator](https://en.wikipedia.org/wiki/Exponential_integrator)
for a linear non-homogeneous ODE, computes a linear combination of the `ϕⱼ` functions which generalize `ϕ₀(z) = exp(z)`.
"""
# ╔═╡ 8dc7e127-187e-48e5-9ed5-d7ecf196a40e
md"""
## Projecting a sparse matrix into a subspace
Given $Q\in \mathbb{R}^{n\times k}$ and $Q^T Q = I$, the following statement is always true.
```math
\lambda_1(Q^T_k A Q_k) \leq \lambda_1(A),
```
where $\lambda_1(A)$ is the largest eigenvalue of $A \in \mathbb{R}^{n\times n}$.
"""
# ╔═╡ 89bc81e9-e1ce-459f-bd28-511b534a14fc
md"""
## Lanczos Tridiagonalization
In the Lanczos tridiagonalizaiton process, we want to find a orthogonal matrix $Q^T$ such that
```math
Q^T A Q = T
```
where $T$ is a tridiagonal matrix
```math
T = \left(\begin{matrix}
\alpha_1 & \beta_1 & 0 & \ldots & 0\\
\beta_1 & \alpha_2 & \beta_2 & \ldots & 0\\
0 & \beta_2 & \alpha_3 & \ldots & 0\\
\vdots & \vdots & \vdots & \ddots & \vdots\\
0 & 0 & 0 & \beta_{k-1} & \alpha_k
\end{matrix}\right),
```
$Q = [q_1 | q_2 | \ldots | q_n],$ and ${\rm span}(\{q_1, q_2, \ldots, q_k\}) = \mathcal{K}(A, q_1, k)$.
"""
# ╔═╡ 03cb1b92-ba98-4065-8761-1a108f6cabfb
md"We have $Aq_k = \beta_{k-1}q_{k-1} + \alpha_k q_k + \beta_k q_{k+1}$, or equivalently in the recursive style
```math
q_{k+1} = (Aq_k - \beta_{k-1}q_{k-1} - \alpha_k q_k)/\beta_k.
```
"
# ╔═╡ a2d79593-fd1f-46ea-afe2-578f75e084cf
md"""
By multiplying $q_k^T$ on the left, we have
```math
\alpha_k = q_k^T A q_k.
```
Since $q_{k+1}$ is normalized, we have
```math
\beta_k = \|Aq_k - \beta_{k-1}q_{k-1} - \alpha_k q_k\|_2
```
"""
# ╔═╡ 0ada2d40-1052-42f1-9577-3d6e590efca4
md"""
If at any moment, $\beta_k = 0$, the interation stops due to convergence of a subspace. We have the following reducible form
```math
T(\beta_2 = 0) = \left(\begin{array}{cc|ccc}
\alpha_1 & \beta_1 & 0 & \ldots & 0\\
\beta_1 & \alpha_2 & 0 & \ldots & 0\\
\hline
0 & 0 & \alpha_3 & \ldots & 0\\
\vdots & \vdots & \vdots & \ddots & \vdots\\
0 & 0 & 0 & \beta_{k-1} & \alpha_k
\end{array}\right),
```
"""
# ╔═╡ 0906b965-f760-4f67-9ddc-498aeb99c3b8
md"## A naive implementation"
# ╔═╡ 166d4ddc-5e58-4210-a389-997c1ea82de2
function lanczos(A, q1::AbstractVector{T}; abstol, maxiter) where T
# normalize the input vector
q1 = normalize(q1)
# the first iteration
q = [q1]
Aq1 = A * q1
α = [q1' * Aq1]
rk = Aq1 .- α[1] .* q1
β = [norm(rk)]
for k = 2:min(length(q1), maxiter)
# the k-th orthonormal vector in Q
push!(q, rk ./ β[k-1])
Aqk = A * q[k]
# compute the diagonal element as αₖ = qₖᵀ A qₖ
push!(α, q[k]' * Aqk)
rk = Aqk .- α[k] .* q[k] .- β[k-1] * q[k-1]
# compute the off-diagonal element as βₖ = |rₖ|
nrk = norm(rk)
# break if βₖ is smaller than abstol or the maximum number of iteration is reached
if abs(nrk) < abstol || k == length(q1)
break
end
push!(β, nrk)
end
# returns T and Q
return SymTridiagonal(α, β), hcat(q...)
end
# ╔═╡ 37a68538-2fee-45e2-a1c4-653107f1e0de
md"""
## Example: using dominant eigensolver to study the spectral graph theory
"""
# ╔═╡ f75a35cd-d334-4741-8477-7555aec38e92
md"""
Laplacian matrix
Given a simple graph $G$ with $n$ vertices $v_{1},\ldots ,v_{n}$, its Laplacian matrix $L_{n\times n}$ is defined element-wise as
```math
L_{i,j}:={\begin{cases}\deg(v_{i})&{\mbox{if}}\ i=j\\-1&{\mbox{if}}\ i\neq j\ {\mbox{and}}\ v_{i}{\mbox{ is adjacent to }}v_{j}\\0&{\mbox{otherwise}},\end{cases}}
```
or equivalently by the matrix $L=D-A$, where $D$ is the degree matrix and A is the adjacency matrix of the graph. Since $G$ is a simple graph, $A$ only contains 1s or 0s and its diagonal elements are all 0s.
"""
# ╔═╡ cab1420b-fdf1-42d7-95fd-f37ef416bf49
md"""Theorem: The number of connected components in the graph is the dimension of the nullspace of the Laplacian and the algebraic multiplicity of the 0 eigenvalue.
"""
# ╔═╡ 294e1dea-4f2f-4731-9327-b1c04c82d2db
graphsize = 10
# ╔═╡ 273f9cbd-1885-4834-921d-60130c34c028
md"One can use the `Graphs.laplacian_matrix(graph)` to generate a laplacian matrix (CSC formated) of a graph."
# ╔═╡ 3b299d74-e209-4c1e-a831-fce40c4f43f4
lmat = laplacian_matrix(random_regular_graph(graphsize, 3))
# ╔═╡ 1b403168-63b7-4850-8d49-20869d00f71b
tri, Q = lanczos(lmat, randn(graphsize); abstol=1e-8, maxiter=100)
# ╔═╡ e11c71bb-a079-4748-9598-e6c49dfce7a7
eigen(tri).values
# ╔═╡ 95e3a70a-0c2a-4c87-a7c6-963b607c63ad
Q' * Q
# ╔═╡ 2a5118ab-c616-4b85-8ea5-737cd319f835
@bind graph_size Slider(10:2:200; show_value=true, default=10)
# ╔═╡ 05411446-466b-4c6c-bb8a-0caebb516770
let
graph = random_regular_graph(graph_size, 3)
A = laplacian_matrix(graph)
q1 = randn(graph_size)
tr, Q = lanczos(-A, q1; abstol=1e-8, maxiter=100)
# using function `KrylovKit.eigsolve`
@info "KrylovKit.eigsolve: " eigsolve(A, q1, 2, :SR)
@info Q' * Q
# diagonalize the triangular matrix obtained with our naive implementation
@info "Naive approach: " eigen(-tr).values
end;
# ╔═╡ 4c0f9641-f723-4880-9346-ba7899390d52
md"""NOTE: with larger `graph_size`, you should see some "ghost" eigenvalues """
# ╔═╡ e68e3e51-2084-45bf-a8d0-42e4f22d0aea
md"""
## Reorthogonalization
"""
# ╔═╡ 64ed8383-a449-4278-9cd9-ad317ce13574
md"""
Let $r_0, \ldots, r_{k-1} \in \mathbb{R}_n$ be given and suppose that Householder matrices $H_0, \ldots, H_{k-1}$ have been computed such that $(H_0\ldots H_{k- 1})^T [r_0\mid\ldots\mid r_{k-1}]$ is upper triangular. Let $[q_1 \mid \ldots \mid q_k ]$ denote the first $k$ columns of the Householderproduct $(H_0 \ldots H_{k-1})$.
Then $q_k^T q_l = \delta_{kl}$ (machine precision).
"""
# ╔═╡ 75d297f9-892e-44c4-80a4-1e1beb5d7d9c
md"**The following 4 cells are copied from notebook: 5.linear-least-square.jl**"
# ╔═╡ 191f0c0a-fb45-402e-9fdc-f3427c70bf25
struct HouseholderMatrix{T} <: AbstractArray{T, 2}
v::Vector{T}
β::T
end
# ╔═╡ af4d73c4-2f8e-4aaa-aa40-3be0ded47323
# the `mul!` interfaces can take two extra factors.
function left_mul!(B, A::HouseholderMatrix)
B .-= (A.β .* A.v) * (A.v' * B)
return B
end
# ╔═╡ 571936d4-3ce6-48e8-be5d-7dbc73e2d3d4
# the `mul!` interfaces can take two extra factors.
function right_mul!(A, B::HouseholderMatrix)
A .= A .- (A * (B.β .* B.v)) * B.v'
return A
end
# ╔═╡ 84408a62-5062-41fd-8f49-2ace02d7d685
function householder_matrix(v::AbstractVector{T}) where T
v = copy(v)
v[1] -= norm(v, 2)
return HouseholderMatrix(v, 2/norm(v, 2)^2)
end
# ╔═╡ 5e6a089b-e61b-4945-b8dc-1e069d8f0d92
md"The Lanczos algorithm with complete orthogonalization."
# ╔═╡ 91ab0a42-1a2f-4944-98f5-bda041fa9ff1
function lanczos_reorthogonalize(A, q1::AbstractVector{T}; abstol, maxiter) where T
n = length(q1)
# normalize the input vector
q1 = normalize(q1)
# the first iteration
q = [q1]
Aq1 = A * q1
α = [q1' * Aq1]
rk = Aq1 .- α[1] .* q1
β = [norm(rk)]
householders = [householder_matrix(q1)]
for k = 2:min(n, maxiter)
# reorthogonalize rk: 1. compute the k-th householder matrix
for j = 1:k-1
left_mul!(view(rk, j:n), householders[j])
end
push!(householders, householder_matrix(view(rk, k:n)))
# reorthogonalize rk: 2. compute the k-th orthonormal vector in Q
qk = zeros(T, n); qk[k] = 1 # qₖ = H₁H₂…Hₖeₖ
for j = k:-1:1
left_mul!(view(qk, j:n), householders[j])
end
push!(q, qk)
Aqk = A * q[k]
# compute the diagonal element as αₖ = qₖᵀ A qₖ
push!(α, q[k]' * Aqk)
rk = Aqk .- α[k] .* q[k] .- β[k-1] * q[k-1]
# compute the off-diagonal element as βₖ = |rₖ|
nrk = norm(rk)
# break if βₖ is smaller than abstol or the maximum number of iteration is reached
if abs(nrk) < abstol || k == n
break
end
push!(β, nrk)
end
return SymTridiagonal(α, β), hcat(q...)
end
# ╔═╡ 06d4453f-ea4e-4e5f-9aea-72a84cd56e99
let
n = 1000
graph = random_regular_graph(n, 3)
A = laplacian_matrix(graph)
q1 = randn(n)
tr, Q = lanczos_reorthogonalize(A, q1; abstol=1e-5, maxiter=100)
@info eigsolve(A, q1, 2, :SR)
eigen(tr)
end
# ╔═╡ bb65395e-4d11-439d-9dba-e25c7048df9f
md"""
## Notes on Lanczos
1. In practise, we do not store all $q$ vectors to save space.
2. Blocking technique is required if we want to compute multiple eigenvectors or a degenerate eigenvector.
2. Restarting technique can be used to improve the solution.
"""
# ╔═╡ d6487a34-1c62-4879-a92a-7fcfb7080d85
md"""
## The Arnoldi Process
"""
# ╔═╡ d0776be5-6a56-41c2-981c-e992ec8afdc5
md"""If $A$ is not symmetric, then the orthogonal tridiagonalization $Q^T A Q = T$ does not exist in general. The Arnoldi approach involves the column by column generation of an orthogonal $Q$ such that $Q^TAQ = H$ is a Hessenberg matrix.
```math
H = \left(\begin{matrix}
h_{11} & h_{12} & h_{13} & \ldots & h_{1k}\\
h_{21} & h_{22} & h_{23} & \ldots & h_{2k}\\
0 & h_{32} & h_{33} & \ldots & h_{3k}\\
\vdots & \vdots & \vdots & \ddots & \vdots\\
0 & 0 & 0 & \ldots & h_{kk}
\end{matrix}\right)
```
That is, $h_{ij} = 0$ for $i>j+1$.
"""
# ╔═╡ 71ebaf5b-5f20-4f73-b530-3ae1aea071f2
function arnoldi_iteration(A::AbstractMatrix{T}, x0::AbstractVector{T}; maxiter) where T
h = Vector{T}[]
q = [normalize(x0)]
n = length(x0)
@assert size(A) == (n, n)
for k = 1:min(maxiter, n)
u = A * q[k] # generate next vector
hk = zeros(T, k+1)
for j = 1:k # subtract from new vector its components in all preceding vectors
hk[j] = q[j]' * u
u = u - hk[j] * q[j]
end
hkk = norm(u)
hk[k+1] = hkk
push!(h, hk)
if abs(hkk) < 1e-8 || k >=n # stop if matrix is reducible
break
else
push!(q, u ./ hkk)
end
end
# construct `h`
kmax = length(h)
H = zeros(T, kmax, kmax)
for k = 1:length(h)
if k == kmax
H[1:k, k] .= h[k][1:k]
else
H[1:k+1, k] .= h[k]
end
end
return H, hcat(q...)
end
# ╔═╡ 68e61322-1f14-4098-ab3b-6349aaab669a
let
n = 10
A = randn(n, n)
q1 = randn(n)
h, q = arnoldi_iteration(A, q1; maxiter=100)
# using function `KrylovKit.eigsolve`
@info "KrylovKit.eigsolve: " eigsolve(A, q1, 2, :LR)
# diagonalize the triangular matrix obtained with our naive implementation
@info "Naive approach: " eigen(h).values
end;
# ╔═╡ b0b1e888-98d9-405c-8b01-a3b4f8cae1dd
md"# Assignment"
# ╔═╡ 1fd7a9d3-1f63-407b-b44c-1bd198351039
md"""
#### 1. Review
I forgot to copy the definitions of `rowindices`, `colindices` and `data` in the following code. Can you help me figure out what are their possible values?
```julia
julia> sp = sparse(rowindices, colindices, data);
julia> sp.colptr
6-element Vector{Int64}:
1
2
3
5
6
6
julia> sp.rowval
5-element Vector{Int64}:
3
1
1
4
5
julia> sp.nzval
5-element Vector{Float64}:
0.799
0.942
0.848
0.164
0.637
julia> sp.m
5
julia> sp.n
5
```
"""
# ╔═╡ dabdee6d-0c64-4dc7-9c09-15d388a32387
md"""
#### 2. Coding (Choose one of the following two problems):
1. (easy) Implement CSC format sparse matrix-vector multiplication as function `my_spv`. Please include the following test code into your project.
```julia
using SparseArrays, Test
@testset "sparse matrix - vector multiplication" begin
for k = 1:100
m, n = rand(1:100, 2)
density = rand()
sp = sprand(m, n, density)
v = randn(n)
@test Matrix(sp) * v ≈ my_spv(sp, v)
end
end
```
2. (hard) The restarting in Lanczos is a technique technique to reduce memory. Suppose we wish to calculate the largest eigenvalue of $A$. If $q_1 \in \mathbb{R}^{n}$ is a given normalized vector, then it can be refined as follows:
Step 1. Generate $q_2,\ldots,q_s \in \mathbb{R}^{n}$ via the block Lanczos algorithm.
Step 2. Form $T_s = [ q_1 \mid \ldots \mid q_s]^TA [ q_1 \mid \ldots \mid q_s]$, an s-by-s matrix.
Step 3. Compute an orthogonal matrix $U = [ u_1 \mid \ldots\mid u_s]$ such that $U^T T_s U = {\rm diag}(\theta_1, \ldots, \theta_s)$ with $\theta_1\geq \ldots \geq\theta_s$·
Step 4. Set $q_1^{({\rm new})} = [q_1 \mid \ldots \mid q_s]u_1$.
Please implement a Lanczos tridiagonalization process with restarting as a Julia function. You submission should include that function as well as a test.
"""
# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
[compat]
Graphs = "~1.8.0"
KrylovKit = "~0.6.0"
PlutoUI = "~0.7.50"
"""
# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised
julia_version = "1.8.5"
manifest_format = "2.0"
project_hash = "2810da36ff5c96af693608e9e35c2889d5a7d14b"
[[deps.AbstractPlutoDingetjes]]
deps = ["Pkg"]
git-tree-sha1 = "8eaf9f1b4921132a4cff3f36a1d9ba923b14a481"
uuid = "6e696c72-6542-2067-7265-42206c756150"
version = "1.1.4"
[[deps.Adapt]]
deps = ["LinearAlgebra", "Requires"]
git-tree-sha1 = "cc37d689f599e8df4f464b2fa3870ff7db7492ef"
uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
version = "3.6.1"
[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
version = "1.1.1"
[[deps.ArnoldiMethod]]
deps = ["LinearAlgebra", "Random", "StaticArrays"]
git-tree-sha1 = "62e51b39331de8911e4a7ff6f5aaf38a5f4cc0ae"
uuid = "ec485272-7323-5ecc-a04f-4719b315124d"
version = "0.2.0"
[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
[[deps.ChainRulesCore]]
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "c6d890a52d2c4d55d326439580c3b8d0875a77d9"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.15.7"
[[deps.ColorTypes]]
deps = ["FixedPointNumbers", "Random"]
git-tree-sha1 = "eb7f0f8307f71fac7c606984ea5fb2817275d6e4"
uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
version = "0.11.4"
[[deps.Compat]]
deps = ["Dates", "LinearAlgebra", "UUIDs"]
git-tree-sha1 = "61fdd77467a5c3ad071ef8277ac6bd6af7dd4c04"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.6.0"
[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.0.1+0"
[[deps.DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "d1fff3a548102f48987a52a2e0d114fa97d730f0"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.13"
[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
[[deps.Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
[[deps.Downloads]]
deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
version = "1.6.0"
[[deps.FileWatching]]
uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"
[[deps.FixedPointNumbers]]
deps = ["Statistics"]
git-tree-sha1 = "335bfdceacc84c5cdf16aadc768aa5ddfc5383cc"
uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
version = "0.8.4"
[[deps.GPUArraysCore]]
deps = ["Adapt"]
git-tree-sha1 = "1cd7f0af1aa58abc02ea1d872953a97359cb87fa"
uuid = "46192b85-c4d5-4398-a991-12ede77f4527"
version = "0.1.4"
[[deps.Graphs]]
deps = ["ArnoldiMethod", "Compat", "DataStructures", "Distributed", "Inflate", "LinearAlgebra", "Random", "SharedArrays", "SimpleTraits", "SparseArrays", "Statistics"]
git-tree-sha1 = "1cf1d7dcb4bc32d7b4a5add4232db3750c27ecb4"
uuid = "86223c79-3864-5bf0-83f7-82e725a168b6"
version = "1.8.0"
[[deps.Hyperscript]]
deps = ["Test"]
git-tree-sha1 = "8d511d5b81240fc8e6802386302675bdf47737b9"
uuid = "47d2ed2b-36de-50cf-bf87-49c2cf4b8b91"
version = "0.0.4"
[[deps.HypertextLiteral]]
deps = ["Tricks"]
git-tree-sha1 = "c47c5fa4c5308f27ccaac35504858d8914e102f9"
uuid = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
version = "0.9.4"
[[deps.IOCapture]]
deps = ["Logging", "Random"]
git-tree-sha1 = "f7be53659ab06ddc986428d3a9dcc95f6fa6705a"
uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
version = "0.2.2"
[[deps.Inflate]]
git-tree-sha1 = "5cd07aab533df5170988219191dfad0519391428"
uuid = "d25df0c9-e2be-5dd7-82c8-3ad0b3e990b9"
version = "0.1.3"
[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
[[deps.JSON]]
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
git-tree-sha1 = "3c837543ddb02250ef42f4738347454f95079d4e"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.3"
[[deps.KrylovKit]]
deps = ["ChainRulesCore", "GPUArraysCore", "LinearAlgebra", "Printf"]
git-tree-sha1 = "1a5e1d9941c783b0119897d29f2eb665d876ecf3"
uuid = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
version = "0.6.0"
[[deps.LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
version = "0.6.3"
[[deps.LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
version = "7.84.0+0"
[[deps.LibGit2]]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
[[deps.LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"
version = "1.10.2+0"
[[deps.Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
[[deps.LinearAlgebra]]
deps = ["Libdl", "libblastrampoline_jll"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
[[deps.MIMEs]]
git-tree-sha1 = "65f28ad4b594aebe22157d6fac869786a255b7eb"
uuid = "6c6e2e6c-3030-632d-7369-2d6c69616d65"
version = "0.1.4"
[[deps.MacroTools]]
deps = ["Markdown", "Random"]
git-tree-sha1 = "42324d08725e200c23d4dfb549e0d5d89dede2d2"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.5.10"
[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
[[deps.MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
version = "2.28.0+0"
[[deps.Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
[[deps.MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
version = "2022.2.1"
[[deps.NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
version = "1.2.0"