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
3b2fb660
Commit
3b2fb660
authored
21 years ago
by
Stephen Smith
Browse files
Options
Downloads
Patches
Plain Diff
Added ols and SD
parent
e2490dec
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
+52
-0
52 additions, 0 deletions
miscmaths.cc
miscmaths.h
+11
-1
11 additions, 1 deletion
miscmaths.h
with
63 additions
and
1 deletion
miscmaths.cc
+
52
−
0
View file @
3b2fb660
...
@@ -1407,6 +1407,27 @@ ReturnMatrix neq(const Matrix& mat1,const Matrix& mat2)
...
@@ -1407,6 +1407,27 @@ ReturnMatrix neq(const Matrix& mat1,const Matrix& mat2)
return
res
;
return
res
;
}
}
ReturnMatrix
SD
(
const
Matrix
&
mat1
,
const
Matrix
&
mat2
)
{
if
((
mat1
.
Nrows
()
!=
mat2
.
Nrows
())
||
(
mat1
.
Ncols
()
!=
mat2
.
Ncols
())
){
cerr
<<
"MISCMATHS::SD - matrices are of different dimensions"
<<
endl
;
exit
(
-
1
);
}
Matrix
ret
(
mat1
.
Nrows
(),
mat1
.
Ncols
());
for
(
int
r
=
1
;
r
<=
mat1
.
Nrows
();
r
++
)
{
for
(
int
c
=
1
;
c
<=
mat1
.
Ncols
();
c
++
)
{
if
(
mat2
(
r
,
c
)
==
0
)
ret
(
r
,
c
)
=
0
;
else
ret
(
r
,
c
)
=
mat1
(
r
,
c
)
/
mat2
(
r
,
c
);
}
}
ret
.
Release
();
return
ret
;
}
...
@@ -1648,6 +1669,37 @@ ReturnMatrix read_vest(string p_fname)
...
@@ -1648,6 +1669,37 @@ ReturnMatrix read_vest(string p_fname)
return
p_mat
;
return
p_mat
;
}
}
void
ols
(
const
Matrix
&
data
,
const
Matrix
&
des
,
const
Matrix
&
tc
,
Matrix
&
cope
,
Matrix
&
varcope
){
// ols
// data is t x v
// des is t x ev (design matrix)
// tc is cons x ev (contrast matrix)
// cope and varcope will be cons x v
// but will be resized if they are wrong
// hence may be passed in uninitialised
// TB 2004
if
(
data
.
Nrows
()
!=
des
.
Nrows
()){
cerr
<<
"MISCMATHS::ols - data and design have different number of time points"
<<
endl
;
exit
(
-
1
);
}
if
(
des
.
Ncols
()
!=
tc
.
Ncols
()){
cerr
<<
"MISCMATHS::ols - design and contrast matrix have different number of EVs"
<<
endl
;
exit
(
-
1
);
}
Matrix
pdes
=
pinv
(
des
);
Matrix
prevar
=
diag
(
tc
*
pdes
*
pdes
.
t
()
*
tc
.
t
());
Matrix
R
=
Identity
(
des
.
Nrows
())
-
des
*
pdes
;
float
tR
=
R
.
Trace
();
Matrix
pe
=
pdes
*
data
;
cope
=
tc
*
pe
;
Matrix
res
=
data
-
des
*
pe
;
Matrix
sigsq
=
sum
(
SP
(
res
,
res
))
/
tR
;
varcope
=
prevar
*
sigsq
;
}
int
conjgrad
(
ColumnVector
&
x
,
const
Matrix
&
A
,
const
ColumnVector
&
b
,
int
maxit
,
int
conjgrad
(
ColumnVector
&
x
,
const
Matrix
&
A
,
const
ColumnVector
&
b
,
int
maxit
,
float
reltol
)
float
reltol
)
{
{
...
...
This diff is collapsed.
Click to expand it.
miscmaths.h
+
11
−
1
View file @
3b2fb660
...
@@ -186,7 +186,8 @@ namespace MISCMATHS {
...
@@ -186,7 +186,8 @@ namespace MISCMATHS {
ReturnMatrix
leqt
(
const
Matrix
&
mat1
,
const
Matrix
&
mat2
);
ReturnMatrix
leqt
(
const
Matrix
&
mat1
,
const
Matrix
&
mat2
);
ReturnMatrix
eq
(
const
Matrix
&
mat1
,
const
Matrix
&
mat2
);
ReturnMatrix
eq
(
const
Matrix
&
mat1
,
const
Matrix
&
mat2
);
ReturnMatrix
neq
(
const
Matrix
&
mat1
,
const
Matrix
&
mat2
);
ReturnMatrix
neq
(
const
Matrix
&
mat1
,
const
Matrix
&
mat2
);
ReturnMatrix
SD
(
const
Matrix
&
mat1
,
const
Matrix
&
mat2
);
// Schur (element-wise) divide
void
remmean
(
const
Matrix
&
mat
,
Matrix
&
demeanedmat
,
Matrix
&
Mean
,
const
int
dim
=
1
);
void
remmean
(
const
Matrix
&
mat
,
Matrix
&
demeanedmat
,
Matrix
&
Mean
,
const
int
dim
=
1
);
ReturnMatrix
remmean
(
const
Matrix
&
mat
,
const
int
dim
=
1
);
ReturnMatrix
remmean
(
const
Matrix
&
mat
,
const
int
dim
=
1
);
ReturnMatrix
stdev
(
const
Matrix
&
mat
,
const
int
dim
=
1
);
ReturnMatrix
stdev
(
const
Matrix
&
mat
,
const
int
dim
=
1
);
...
@@ -194,6 +195,15 @@ namespace MISCMATHS {
...
@@ -194,6 +195,15 @@ namespace MISCMATHS {
ReturnMatrix
corrcoef
(
const
Matrix
&
mat
,
const
int
norm
=
0
);
ReturnMatrix
corrcoef
(
const
Matrix
&
mat
,
const
int
norm
=
0
);
void
symm_orth
(
Matrix
&
Mat
);
void
symm_orth
(
Matrix
&
Mat
);
void
powerspectrum
(
const
Matrix
&
Mat1
,
Matrix
&
Result
,
bool
useLog
);
void
powerspectrum
(
const
Matrix
&
Mat1
,
Matrix
&
Result
,
bool
useLog
);
// ols
// data is t x v
// des is t x ev (design matrix)
// tc is cons x ev (contrast matrix)
// cope and varcope will be cons x v
// but will be resized if they are wrong
void
ols
(
const
Matrix
&
data
,
const
Matrix
&
des
,
const
Matrix
&
tc
,
Matrix
&
cope
,
Matrix
&
varcope
);
// Conjugate Gradient methods to solve for x in: A * x = b
// Conjugate Gradient methods to solve for x in: A * x = b
// A must be symmetric and positive definite
// A must be symmetric and positive definite
...
...
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