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 23 commits into
base: main
Choose a base branch
from
Open
Changes from 18 commits
Commits
Show all changes
23 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
f5cc129
Merge branch 'JuliaSmoothOptimizers:main' into Q-lessTutorials
MaxenceGollier Jan 22, 2025
f837c9b
update make.jl for qless tutorial
MaxenceGollier Jan 22, 2025
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
156 changes: 156 additions & 0 deletions docs/src/tutorials/qless.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
```@example qless1
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved
# The Q-less QR factorization may be used to solve the least-norm problem
#
# minimize ‖x‖ subject to Ax=b
#
# while saving storage because Q is not formed.
# Thus it is appropriate for large problems where storage is at a premium.
# The normal equations of the second kind AAᵀy = b are the optimality conditions of the least-norm problems, where x = Aᵀy.
# If Aᵀ = QR, they can be equivalently written RᵀRy = b.
#
# The stability of this procedure is comparable to the method that uses Q---see
#
# C. C. Paige, An error analysis of a method for solving matrix equations,
# Mathematics of Computations, 27, pp. 355-359, 1973, DOI 10.2307/2005623.

using LinearAlgebra, Printf, SparseArrays
using QRMumps

# 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]
x_star = [16.0, 1.0, 10.0, 3.0, 19.0, 3.0, 2.0]
y₁ = zeros(n)
y = zeros(m)
x = zeros(n)

# 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 that we want the Q-less QR factorization
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ᵀR y = b in two steps:
# 1. Solve Rᵀy₁ = b
qrm_solve!(spfct, b, y₁; transp='t')

# 2. Solve Ry = y₁
qrm_solve!(spfct, y₁, y; transp='n')


# Compute the least norm solution of Ax = b
x .= A'*y

# Compute error norm and residual norm
error_norm = norm(x - x_star)
residual_norm = norm(b - A*x)

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

```@example qless2
# The Q-less QR factorization may be used to solve the least-square problem
#
# minimize ‖Ax - b‖
#
# while saving storage because Q is not formed.
# Thus it is appropriate for large problems where storage is at a premium.
# The normal equations AᵀAx = Aᵀb are the optimality conditions of the least squares problems.
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved
# If A = QR, they can be equivalently written RᵀRx = Aᵀb.
#
# This procedure is backward stable if we perform one step of iterative refinement---see
#
# Å. Björck, Stability analysis of the method of seminormal equations for linear least squares problems,
# Linear Algebra and its Applications, 88–89, pp. 31-48, 1987, DOI 10.1016/0024-3795(87)90101-7.

using LinearAlgebra, Printf, SparseArrays
using QRMumps

# 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]

z = zeros(n)
x₁ = zeros(m)
x = zeros(n)

y = zeros(m)
r = zeros(n)
Δx₁ = zeros(m)
Δx = zeros(n)

# Initialize QRMumps
qrm_init()

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

# Specify that we want the Q-less 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)

# Compute the RHS of the semi-normal equations
mul!(z, A', b)

# Solve RᵀR x = z = Aᵀb in two steps:
# 1. Solve Rᵀx₁ = z
qrm_solve!(spfct, z, x₁; transp = 't')

# 2. Solve Rx = x₁
qrm_solve!(spfct, x₁, x; transp = 'n')

MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved
error_norm = norm(x - x_star)
Aresidual_norm = norm(A'*(A*x - b))

@printf("Error norm ‖x* - x‖ = %10.5e\n", error_norm)
@printf("Normal equations residual norm ‖Aᵀ(Ax - b)‖= %10.5e\n", Aresidual_norm)

# As such, this method is not backward stable and we need to add an iterative refinement step:
# For this, we compute the least-squares solution Δx of min ‖r - AΔx‖, where r is the residual r = Aᵀb - AᵀA*x.
# We then update x := x + Δx

# Compute the residual in two steps to prevent allocating memory:
# 1. Compute y = b - Ax
mul!(y, A, x)
@. y = b - y
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved

# 2. Compute r = Aᵀy = Aᵀ(b - A*x)
mul!(r, A', y)
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved

# Solve the semi-normal equations as before
qrm_solve!(spfct, r, Δx₁; transp='t')
qrm_solve!(spfct, Δx₁, Δx; transp='n')

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

error_norm = norm(x - x_star)
Aresidual_norm = norm(A'*(A*x - b))
MaxenceGollier marked this conversation as resolved.
Show resolved Hide resolved

@printf("Error norm (iterative refinement step) ‖x* - x‖ = %10.5e\n", error_norm)
@printf("Normal equations residual norm (iterative refinement step) ‖Aᵀ(Ax - b)‖= %10.5e\n", Aresidual_norm)
```