-
Notifications
You must be signed in to change notification settings - Fork 2
Docs: V0.2-tutorials/consistent linear system example #129
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
base: main
Are you sure you want to change the base?
Changes from all commits
53c4e55
f82346b
95b0cd8
64760f1
1633503
c6de884
f7a284f
0d10ada
1d5ac70
ffba348
2c54d6a
ff88338
6708ae9
63a9479
c557f17
25f4099
7c3fb87
7b0534d
6226f6b
e83a636
e5b94a9
99fa32a
bba815a
b6760bd
0827c79
ff201f0
43bec29
3198fad
c6caee7
45f413f
2b5de08
80129e4
34c0dcc
1909a3e
80aeb6b
c79ffec
c359f9d
20fea52
8c51331
543f3e5
8863bcb
569da4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,143 @@ | ||||||||
| # Solving a Consistent Linear System | ||||||||
|
|
||||||||
| This guide demonstrates how to use `RLinearAlgebra.jl` package to solve a | ||||||||
| **consistent linear system**—a system where at least one solution | ||||||||
| exists—expressed in the form: | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
|
|
||||||||
| $$Ax = b$$ | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs a punctuation mark at the end of the equation. |
||||||||
|
|
||||||||
| We'll walk through setting up the problem, using a solver, and verifying the result. | ||||||||
|
|
||||||||
| --- | ||||||||
| ## Problem setup and solve the system | ||||||||
|
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this |
||||||||
|
|
||||||||
| First, let's define our linear system $Ax = b$. | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| To easily verify the accuracy of our solver, we'll construct a problem where the true | ||||||||
| solution, $x_{\text{true}}$, is known beforehand. We'll start by creating a random | ||||||||
| matrix $A$ and a known solution vector $x_{\text{true}}$. Then, we can generate the | ||||||||
| right-hand side vector $b$ by computing $b = Ax_{\text{true}}$. | ||||||||
|
|
||||||||
| The following Julia code imports the necessary libraries, sets up the dimensions, and | ||||||||
| creates $A$, $x_{\text{true}}$, and $b$. We also initialize a starting guess, `x_init`, | ||||||||
| for our iterative solver. | ||||||||
|
|
||||||||
|
Comment on lines
+15
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove this. |
||||||||
| ```julia | ||||||||
| using LinearAlgebra | ||||||||
| num_rows, num_cols = 1000, 20; | ||||||||
| A = randn(Float64, num_rows, num_cols); | ||||||||
| x_true = randn(Float64, num_cols); | ||||||||
| x_init = zeros(Float64, num_cols); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove this line as it is not relevant to problem setup. |
||||||||
| b = A * x_true; | ||||||||
| ``` | ||||||||
|
|
||||||||
| ```@setup ConsistentExample | ||||||||
| using LinearAlgebra | ||||||||
| num_rows, num_cols = 1000, 20; | ||||||||
| A = randn(Float64, num_rows, num_cols); | ||||||||
| x_true = randn(Float64, num_cols); | ||||||||
| x_init = zeros(Float64, num_cols); | ||||||||
| b = A * x_true; | ||||||||
| ``` | ||||||||
|
|
||||||||
| As simple as you can imagine, `RLinearAlgebra.jl` can solve this system in just a | ||||||||
| few lines of codes and high efficiency: | ||||||||
|
Comment on lines
+43
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| ```@example ConsistentExample | ||||||||
| using RLinearAlgebra | ||||||||
| logger = BasicLogger(max_it = 300) | ||||||||
| kaczmarz_solver = Kaczmarz(log = logger) | ||||||||
|
Comment on lines
+48
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| solver_recipe = complete_solver(kaczmarz_solver, x_init, A, b) | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| rsolve!(solver_recipe, x_init, A, b) | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| solution = x_init; | ||||||||
| println("Solution to the system: \n", solution) | ||||||||
|
Comment on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove these |
||||||||
| ``` | ||||||||
| **Done! How simple it is!** | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. " |
||||||||
|
|
||||||||
|
|
||||||||
| Let's check how close our calculated `solution` is to the known `x_true`. | ||||||||
| We can measure the accuracy by calculating the Euclidean norm of the difference | ||||||||
| between the two vectors. A small norm indicates that our solver found a good approximation. | ||||||||
|
|
||||||||
| ```@example ConsistentExample | ||||||||
| # Calculate the norm of the error | ||||||||
| error_norm = norm(solution - x_true) | ||||||||
| println(" - Norm of the error between the solution and x_true: ", error_norm) | ||||||||
| ``` | ||||||||
| As you can see, by using the modular Kaczmarz solver, we were able to configure a | ||||||||
| randomized block-based method and find a solution vector that is very close to | ||||||||
| the true solution. | ||||||||
|
|
||||||||
|
|
||||||||
| Let's break down the solver code line by line to understand what each part does. | ||||||||
|
|
||||||||
| --- | ||||||||
| ## Codes breakdown | ||||||||
|
|
||||||||
| As shown in the code, we used the [`Kaczmarz` solver](@ref Kaczmarz). A key feature of | ||||||||
| **RLinearAlgebra.jl** is its modularity; you can customize the solver's behavior by passing | ||||||||
| in different "component" objects for tasks, such as system compression, progress logging, | ||||||||
| and termination checks. | ||||||||
|
|
||||||||
| For this example, we kept it simple by only customizing the maximum iteration located | ||||||||
| in [`Logger`](@ref Logger) component. Let's break down each step. | ||||||||
|
|
||||||||
|
|
||||||||
| ### Configure the logger | ||||||||
|
|
||||||||
| We start with the simplest component: the [`Logger`](@ref Logger). The | ||||||||
| [`Logger`](@ref Logger) is | ||||||||
| responsible for tracking metrics (such as the error history) and telling the solver | ||||||||
| when to stop. For this guide, we use the default [`BasicLogger`](@ref BasicLogger) | ||||||||
| and configure | ||||||||
| it with a single stopping criterion: a maximum number of iterations. | ||||||||
|
|
||||||||
| ```julia | ||||||||
| # Configure the maximum iteration to be 300 | ||||||||
| logger = BasicLogger(max_it = 300) | ||||||||
| ``` | ||||||||
|
|
||||||||
| ### Create the solver | ||||||||
|
|
||||||||
| Before running the solver on our specific problem (`A, b, x_init`), we must prepare it | ||||||||
| using the [`complete_solver`](@ref complete_solver) function. This function creates | ||||||||
| a [`KaczmarzRecipe`](@ref KaczmarzRecipe), which combines the solver | ||||||||
| configuration with the problem data. | ||||||||
|
|
||||||||
| Crucially, this "recipe" pre-allocates all necessary memory buffers, which is a | ||||||||
| key step for ensuring efficient and high-performance computation. | ||||||||
|
|
||||||||
| ```julia | ||||||||
| # Create the Kaczmarz solver object by passing in the ingredients | ||||||||
| kaczmarz_solver = Kaczmarz(log = logger) | ||||||||
| # Create the solver recipe by combining the solver and the problem data | ||||||||
| solver_recipe = complete_solver(kaczmarz_solver, x_init, A, b) | ||||||||
| ``` | ||||||||
|
|
||||||||
| ### Solve the system using the solver | ||||||||
|
|
||||||||
| Finally, we call [`rsolve!`](@ref rsolve!) to execute the algorithm. The `!` at the end | ||||||||
| of the function name is a Julia convention indicating that the function will inplace | ||||||||
| update part of its arguments. In this case, `rsolve!` modifies `x_init` in-place, | ||||||||
| filling it with the final solution vector. The solver will iterate until a stopping | ||||||||
| criterion in the `logger` is met, i.e. iteration goes up to $300$. | ||||||||
|
|
||||||||
| ```julia | ||||||||
| # Run the inplace solver! | ||||||||
| rsolve!(solver_recipe, x_init, A, b) | ||||||||
|
|
||||||||
| # The solution is now stored in the updated x_init vector | ||||||||
| solution = x_init; | ||||||||
| ``` | ||||||||
|
Comment on lines
+59
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can go somewhere else. |
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--->---