Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
miscmaths
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
miscmaths
Commits
624734e1
Commit
624734e1
authored
21 years ago
by
Mark Jenkinson
Browse files
Options
Downloads
Patches
Plain Diff
Added conjugate gradient matrix solver (conflicts should be resolved)
parent
c7be7897
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
miscmaths.cc
+29
-0
29 additions, 0 deletions
miscmaths.cc
miscmaths.h
+4
-0
4 additions, 0 deletions
miscmaths.h
with
33 additions
and
0 deletions
miscmaths.cc
+
29
−
0
View file @
624734e1
...
...
@@ -1647,6 +1647,35 @@ ReturnMatrix read_vest(string p_fname)
return
p_mat
;
}
int
conjgrad
(
ColumnVector
x
,
const
Matrix
&
A
,
const
ColumnVector
&
b
,
int
maxit
)
{
// solves: A * x = b (for x)
// implementation of algorithm in Golub and Van Loan (3rd ed, page 527)
ColumnVector
rk1
,
rk2
,
pk
,
apk
;
double
betak
,
alphak
,
rk1rk1
=
0
,
rk2rk2
;
int
k
=
0
;
rk1
=
b
-
A
*
x
;
// a *big* calculation
for
(
int
n
=
1
;
n
<=
maxit
;
n
++
)
{
//if (sufficiently accurate) break;
k
++
;
if
(
k
==
1
)
{
pk
=
rk1
;
rk1rk1
=
(
rk1
.
t
()
*
rk1
).
AsScalar
();
}
else
{
rk2rk2
=
rk1rk1
;
// from before
rk1rk1
=
(
rk1
.
t
()
*
rk1
).
AsScalar
();
betak
=
rk1rk1
/
rk2rk2
;
pk
=
rk1
+
betak
*
pk
;
// note RHS pk is p(k-1) in algorithm
}
apk
=
A
*
pk
;
// the *big* calculation in this algorithm
alphak
=
rk1rk1
/
(
pk
*
apk
).
AsScalar
();
x
=
x
+
alphak
*
pk
;
// note LHS is x(k) and RHS is x(k-1) in algorithm
rk2
=
rk1
;
// update prior to the next step
rk1
=
rk1
-
alphak
*
apk
;
// note LHS is r(k) in algorithm
}
return
0
;
}
vector
<
float
>
ColumnVector2vector
(
const
ColumnVector
&
col
)
{
vector
<
float
>
vec
(
col
.
Nrows
());
...
...
This diff is collapsed.
Click to expand it.
miscmaths.h
+
4
−
0
View file @
624734e1
...
...
@@ -193,6 +193,10 @@ namespace MISCMATHS {
ReturnMatrix
corrcoef
(
const
Matrix
&
mat
,
const
int
norm
=
0
);
void
symm_orth
(
Matrix
&
Mat
);
void
powerspectrum
(
const
Matrix
&
Mat1
,
Matrix
&
Result
,
bool
useLog
);
int
conjgrad
(
ColumnVector
x
,
const
Matrix
&
A
,
const
ColumnVector
&
b
,
int
maxit
=
3
);
// solves (for x) A * x = b
vector
<
float
>
ColumnVector2vector
(
const
ColumnVector
&
col
);
///////////////////////////////////////////////////////////////////////////
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment