You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example solves a normally defined system of 3 equations of 3 unknowns.
10
26
11
27
```fortran
@@ -56,8 +72,8 @@ LU Solution: X =
56
72
0.0000
57
73
```
58
74
59
-
## Example 2
60
-
This example solves an overdefined system of 3 equations of 2 uknowns.
75
+
## Overdetermined System Example
76
+
This example solves an overdetermined system of 3 equations of 2 uknowns.
61
77
62
78
```fortran
63
79
program example
@@ -100,7 +116,7 @@ Least Squares Solution: X =
100
116
-0.57895
101
117
```
102
118
103
-
## Example 3
119
+
## Eigen Analysis Example
104
120
This example computes the eigenvalues and eigenvectors of a mechanical system consisting of several masses connected by springs.
105
121
106
122
```fortran
@@ -180,20 +196,64 @@ Mode 3: (923.5669 Hz)
180
196
-0.184
181
197
0.179
182
198
```
183
-
## Documentation
184
-
The documentation can be found [here](https://jchristopherson.github.io/linalg/).
185
199
186
-
## Building LINALG
187
-
[CMake](https://cmake.org/)This library can be built using CMake. For instructions see [Running CMake](https://cmake.org/runningcmake/).
200
+
## Sparse Matrix Example
201
+
The following example solves a sparse system of equations using a direct solver. The solution is compared to the solution of the same system of equations but in dense format for comparison.
202
+
```fortran
203
+
program example
204
+
use iso_fortran_env
205
+
use linalg
206
+
implicit none
188
207
189
-
[FPM](https://github.com/fortran-lang/fpm) can also be used to build this library using the provided fpm.toml.
190
-
```txt
191
-
fpm build
208
+
! Local Variables
209
+
integer(int32) :: ipiv(4)
210
+
real(real64) :: dense(4, 4), b(4), x(4), bc(4)
211
+
type(csr_matrix) :: sparse
212
+
213
+
! Build the matrices as dense matrices
214
+
dense = reshape([ &
215
+
5.0d0, 0.0d0, 0.0d0, 0.0d0, &
216
+
0.0d0, 8.0d0, 0.0d0, 6.0d0, &
217
+
0.0d0, 0.0d0, 3.0d0, 0.0d0, &
218
+
0.0d0, 0.0d0, 0.0d0, 5.0d0], [4, 4])
219
+
b = [2.0d0, -1.5d0, 8.0d0, 1.0d0]
220
+
221
+
! Convert to sparse (CSR format)
222
+
! Note, the assignment operator is overloaded to allow conversion.
223
+
sparse = dense
224
+
225
+
! Compute the solution to the sparse equations
226
+
call sparse_direct_solve(sparse, b, x) ! Results stored in x
227
+
228
+
! Print the solution
229
+
print "(A)", "Sparse Solution:"
230
+
print *, x
231
+
232
+
! Perform a sanity check on the solution
233
+
! Note, matmul is overloaded to allow multiplication with sparse matrices
234
+
bc = matmul(sparse, x)
235
+
print "(A)", "Computed RHS:"
236
+
print *, bc
237
+
print "(A)", "Original RHS:"
238
+
print *, b
239
+
240
+
! For comparison, solve the dense system via LU decomposition
241
+
call lu_factor(dense, ipiv)
242
+
call solve_lu(dense, ipiv, b) ! Results stored in b
243
+
print "(A)", "Dense Solution:"
244
+
print *, b
245
+
end program
192
246
```
193
-
The LINALG library can be used within your FPM project by adding the following to your fpm.toml file.
The dependencies do not necessarily have to be installed to be used. The build will initially look for installed items, but if not found, will then download and build the latest version as part of the build process.
0 commit comments