@@ -89,7 +89,7 @@ Thus if your matrix is symmetric, specifically building with `Symmetric(A)` will
8989and will generally lead to better automatic linear solver choices. Note that you can also choose the type for ` b ` ,
9090but generally a dense vector will be the fastest here and many solvers will not support a sparse ` b ` .
9191
92- ## Using Matrix-Free Operators
92+ ## Using Matrix-Free Operators via SciMLOperators.jl
9393
9494In many cases where a sparse matrix gets really large, even the sparse representation
9595cannot be stored in memory. However, in many such cases, such as with PDE discretizations,
@@ -98,4 +98,67 @@ operators allow the user to define the `Ax=b` problem to be solved giving only t
9898of ` A*x ` and allowing specific solvers (Krylov methods) to act without ever constructing
9999the full matrix.
100100
101- ** This will be documented in more detail in the near future**
101+ The Matrix-Free operators are provided by the [ SciMLOperators.jl interface] ( https://docs.sciml.ai/SciMLOperators/stable/ ) .
102+ For example, for the matrix ` A ` defined via:
103+
104+ ``` @example linsys1
105+ A = [-2.0 1 0 0 0
106+ 1 -2 1 0 0
107+ 0 1 -2 1 0
108+ 0 0 1 -2 1
109+ 0 0 0 1 -2]
110+ ```
111+
112+ We can define the ` FunctionOperator ` that does the ` A*v ` operations, without using the matrix ` A ` . This is done by defining
113+ a function ` func(w,v,u,p,t) ` which calculates ` w = A(u,p,t)*v ` (for the purposes of this tutorial, ` A ` is just a constant
114+ operator. See the [ SciMLOperators.jl documentation] ( https://docs.sciml.ai/SciMLOperators/stable/ ) for more details on defining
115+ non-constant operators, operator algebras, and many more features). This is done by:
116+
117+ ``` @example linsys1
118+ function Afunc!(w,v,u,p,t)
119+ w[1] = -2v[1] + v[2]
120+ for i in 2:4
121+ w[i] = v[i-1] - 2v[i] + v[i+1]
122+ end
123+ w[5] = v[4] - 2v[5]
124+ nothing
125+ end
126+
127+ function Afunc!(v,u,p,t)
128+ w = zeros(5)
129+ Afunc!(w,v,u,p,t)
130+ w
131+ end
132+
133+ using SciMLOperators
134+ mfopA = FunctionOperator(Afunc!, zeros(5), zeros(5))
135+ ```
136+
137+ Let's check these are the same:
138+
139+ ``` @example linsys1
140+ v = rand(5)
141+ mfopA*v - A*v
142+ ```
143+
144+ Notice ` mfopA ` does this without having to have ` A ` because it just uses the equivalent ` Afunc! ` instead. Now, even though
145+ we don't have a matrix, we can still solve linear systems defined by this operator. For example:
146+
147+ ``` @example linsys1
148+ b = rand(5)
149+ prob = LinearProblem(mfopA, b)
150+ sol = solve(prob)
151+ sol.u
152+ ```
153+
154+ And we can check this is successful:
155+
156+ ``` @example linsys1
157+ mfopA * sol.u - b
158+ ```
159+
160+ !!! note
161+
162+ Note that not all methods can use a matrix-free operator. For example, ` LUFactorization() ` requires a matrix. If you use an
163+ invalid method, you will get an error. The methods particularly from KrylovJL are the ones preferred for these cases
164+ (and are defaulted to).
0 commit comments