Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Q-less tutorial #105

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4e5eb81
add qless tutorial
MaxenceGollier Jul 21, 2024
a98097c
Apply suggestions
MaxenceGollier Jul 22, 2024
70e8e0c
Add comments to tutorial
MaxenceGollier Jul 22, 2024
a67eb9e
add least norm solution to first example
MaxenceGollier Aug 1, 2024
abc8368
add suggestions
MaxenceGollier Oct 8, 2024
c3661bf
update qless1 for iterative refinement
MaxenceGollier Oct 31, 2024
51ba67f
Merge branch 'JuliaSmoothOptimizers:main' into Q-lessTutorials
MaxenceGollier Oct 31, 2024
69d7760
add iterative refinement on second example
MaxenceGollier Oct 31, 2024
a8051c5
Merge branch 'Q-lessTutorials' of https://github.com/MaxenceGollier/Q…
MaxenceGollier Oct 31, 2024
d22443b
Apply suggestions from code review
MaxenceGollier Nov 8, 2024
237e746
Update docs/src/tutorials/qless.md
MaxenceGollier Nov 8, 2024
bed0e6e
update first example, add citation to paige
MaxenceGollier Nov 11, 2024
717a0cc
update second example
MaxenceGollier Nov 11, 2024
a75766e
Apply suggestions from code review
MaxenceGollier Nov 12, 2024
92f7afc
make qless2 example non allocating
MaxenceGollier Nov 12, 2024
ed555a2
modify residual in qless2 example
MaxenceGollier Nov 12, 2024
27c26fa
change to 'normal equations residual norm' in qless2 example
MaxenceGollier Nov 12, 2024
807ef27
rename variable normal_residual_norm in qless2 example
MaxenceGollier Nov 12, 2024
9404cc8
Update docs/src/tutorials/qless.md
MaxenceGollier Nov 13, 2024
67565fe
rename variables in qless example 2
MaxenceGollier Nov 13, 2024
963ba87
make examples non allocating when computing errors in qless
MaxenceGollier Nov 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions docs/src/tutorials/qless.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
```@example qless1
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved
using QRMumps, LinearAlgebra, SparseArrays, Printf
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved

# Initialize data
m, n = 5, 7
irn = [1, 3, 5, 2, 3, 5, 1, 4, 4, 5, 2, 1, 3]
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved
jcn = [1, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 7, 7]
val = [1.0, 2.0, 3.0, 1.0, 1.0, 2.0, 4.0, 1.0, 5.0, 1.0, 3.0, 6.0, 1.0]

A = sparse(irn, jcn, val, m, n)
b = [40.0, 10.0, 44.0, 98.0, 87.0]
y_star = [16.0, 1.0, 10.0, 3.0, 19.0, 3.0, 2.0]
y₁ = zeros(n)
y₂ = zeros(m)
y₃ = zeros(n)

Δy₁ = zeros(n)
Δy₂ = zeros(m)
Δy₃ = zeros(n)
r = zeros(m)

# Initialize QRMumps
qrm_init()

# Initialize data structures
spmat = qrm_spmat_init(A)
dpo marked this conversation as resolved.
Show resolved Hide resolved
spfct = qrm_spfct_init(spmat)

# Specify not storing Q for the QR factorization
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved
qrm_set(spfct, "qrm_keeph", 0)

# Perform symbolic analysis of Aᵀ and factorize Aᵀ = QR
qrm_analyse!(spmat, spfct; transp='t')
qrm_factorize!(spmat, spfct, transp='t')

# Solve Rᵀy₁ = b
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved
qrm_solve!(spfct, b, y₁; transp='t')

# Solve Ry₂ = y₁
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved
qrm_solve!(spfct, y₁, y₂; transp='n')
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved

# Overall, RᵀRy₂ = b. Equivalently, RᵀQᵀQRy₂ = b or AAᵀy₂ = b
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved

# Compute least norm solution of min ‖b - Ay‖
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m confused. This step doesn’t belong in the solution of the least-norm problem.

Copy link
Contributor Author

@MaxenceGollier MaxenceGollier Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this comment. I am not sure if this answers your question @dpo ?

Thanks @MaxenceGollier ! I believe there is a mistake in the first example: y_star and y\_2 have to be of size n(=7) instead of m(=5). In fact you cannot compute norm(A*y\_2-b) because of incompatible dimensions. The actual least-norm solution is y\_3=A'*y\_2. Have I missed something?

y₃ .= A'*y₂

# Compute error norm and residual norm
error_norm = norm(y₃ - y_star)
residual_norm = norm(b - A*y₃)

@printf("Error norm ‖y* - y‖ = %10.5e\n", error_norm)
@printf("Residual norm ‖b - Ay‖ = %10.5e\n", residual_norm)

# We can improve this solution with iterative refinement: see Björck 1967 - Iterative refinement of linear least squares solutions I
# in BIT Numerical Mathematics.
# For this, we compute the least norm solution Δy₃ of min ‖r - AΔy‖, where r is the residual r = b - A*y₃.
# We then update y₃ := y₃ + Δy₃
r .= b - A*y₃

# Solve RᵀΔy₁ = r
qrm_solve!(spfct, r, Δy₁; transp='t')

# Solve RΔy₂ = Δy₁
qrm_solve!(spfct, Δy₁, Δy₂; transp='n')

# Overall, RᵀRΔy₂ = r. Equivalently, RᵀQᵀQRΔy₂ = r or AAᵀΔy₂ = r

# Compute least norm solution of min ‖r - Ay‖
Δy₃ .= A'*Δy₂

# Update the least norm solution
y₃ .= y₃ .+ Δy₃

error_norm = norm(y₃ - y_star)
residual_norm = norm(b - A*y₃)

@printf("Error norm - iterative refinement ‖y* - y‖ = %10.5e\n", error_norm)
@printf("Residual norm - iterative refinement ‖b - Ay‖ = %10.5e\n", residual_norm)
```

```@example qless2
using QRMumps, LinearAlgebra, SparseArrays, Printf

# Initialize data
m, n = 7, 5
irn = [1, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 7, 7]
jcn = [1, 3, 5, 2, 3, 5, 1, 4, 4, 5, 2, 1, 3]
val = [1.0, 2.0, 3.0, 1.0, 1.0, 2.0, 4.0, 1.0, 3.0, 1.0, 3.0, 2.0, 1.0]

A = sparse(irn, jcn, val, m, n)
b = [22.0, 2.0, 13.0, 8.0, 17.0, 6.0, 5.0]
x_star = [1.0, 2.0, 3.0, 4.0, 5.0]

# Initialize QRMumps
qrm_init()

# Initialize data structures
spmat = qrm_spmat_init(A)
spfct = qrm_spfct_init(spmat)

# Specify not storing Q for the QR factorization
qrm_set(spfct, "qrm_keeph", 0)

# Perform symbolic analysis of A and factorize A = QR
qrm_analyse!(spmat, spfct)
qrm_factorize!(spmat, spfct)

z = A'*b

# Solve Rᵀx₁ = z = Aᵀb
x₁ = qrm_solve(spfct, z; transp = 't')

# Solve Rx₂ = x₁
x₂ = qrm_solve(spfct, x₁; transp = 'n')

# Overall, RᵀRx₂ = Aᵀb. Equivalently, RᵀQᵀQRx₂ = Aᵀb or AᵀAx₂ = Aᵀb
error_norm = norm(x₂ - x_star)
residual_norm = norm(A*x₂ - b)

@printf("Error norm ‖x* - x‖ = %10.5e\n", error_norm)
@printf("Residual norm ‖Ax - b‖= %10.5e\n", residual_norm)

# We can improve this solution with iterative refinement: see Björck 1967 - Iterative refinement of linear least squares solutions I
# in BIT Numerical Mathematics.
# For this, we compute the least norm solution Δx₂ of min ‖r - AΔx‖, where r is the residual r = Aᵀb - AᵀA*x₂.
# We then update x₂ := x₂ + Δx₂
r = A'*(b - A*x₂)

# Solve RᵀΔx₁ = r
Δx₁ = qrm_solve(spfct, r; transp='t')

# Solve Rxy₂ = Δx₁
Δx₂ = qrm_solve(spfct, Δx₁; transp='n')

# Overall, RᵀRΔx₂ = r. Equivalently, RᵀQᵀQRx₂ = r or AᵀAx₂ = r

# Update the least squares solution
x₂ .= x₂ .+ Δx₂

error_norm = norm(x₂ - x_star)
residual_norm = norm(A*x₂ - b)

@printf("Error norm ‖x* - x‖ = %10.5e\n", error_norm)
@printf("Residual norm ‖Ax - b‖= %10.5e\n", residual_norm)
```
Loading