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
e63ee18d
Commit
e63ee18d
authored
3 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
STY: whitespace
parent
5f1495dc
No related branches found
No related tags found
1 merge request
!8
Mnt/conda
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
histogram.cc
+42
-76
42 additions, 76 deletions
histogram.cc
kernel.cc
+25
-26
25 additions, 26 deletions
kernel.cc
optimise.cc
+21
-29
21 additions, 29 deletions
optimise.cc
rungekutta.cc
+25
-25
25 additions, 25 deletions
rungekutta.cc
with
113 additions
and
156 deletions
histogram.cc
+
42
−
76
View file @
e63ee18d
...
@@ -14,13 +14,13 @@ using namespace NEWMAT;
...
@@ -14,13 +14,13 @@ using namespace NEWMAT;
namespace
MISCMATHS
{
namespace
MISCMATHS
{
float
Histogram
::
getPercentile
(
float
perc
)
float
Histogram
::
getPercentile
(
float
perc
)
{
{
if
(
histogram
.
Nrows
()
==
0
)
generate
();
if
(
histogram
.
Nrows
()
==
0
)
generate
();
generateCDF
();
generateCDF
();
float
percentile
=
getValue
(
1
);
float
percentile
=
getValue
(
1
);
for
(
int
i
=
2
;
i
<=
CDF
.
Nrows
();
i
++
){
for
(
int
i
=
2
;
i
<=
CDF
.
Nrows
();
i
++
){
if
(
CDF
(
i
)
>
perc
){
if
(
CDF
(
i
)
>
perc
){
double
diff
=
(
CDF
(
i
)
-
perc
)
/
(
CDF
(
i
)
-
CDF
(
i
-
1
));
double
diff
=
(
CDF
(
i
)
-
perc
)
/
(
CDF
(
i
)
-
CDF
(
i
-
1
));
...
@@ -35,9 +35,9 @@ namespace MISCMATHS {
...
@@ -35,9 +35,9 @@ namespace MISCMATHS {
void
Histogram
::
generate
()
void
Histogram
::
generate
()
{
{
Tracer
ts
(
"Histogram::generate"
);
Tracer
ts
(
"Histogram::generate"
);
int
size
=
sourceData
.
Nrows
();
int
size
=
sourceData
.
Nrows
();
if
(
calcRange
)
if
(
calcRange
)
{
{
// calculate range automatically
// calculate range automatically
...
@@ -50,21 +50,21 @@ namespace MISCMATHS {
...
@@ -50,21 +50,21 @@ namespace MISCMATHS {
histMin
=
sourceData
(
i
);
histMin
=
sourceData
(
i
);
}
}
}
}
// zero histogram
// zero histogram
histogram
.
ReSize
(
bins
);
histogram
.
ReSize
(
bins
);
histogram
=
0
;
histogram
=
0
;
// create histogram; the MIN is so that the maximum value falls in the
// create histogram; the MIN is so that the maximum value falls in the
// last valid bin, not the (last+1) bin
// last valid bin, not the (last+1) bin
for
(
int
i
=
1
;
i
<=
size
;
i
++
)
for
(
int
i
=
1
;
i
<=
size
;
i
++
)
{
{
histogram
(
getBin
(
sourceData
(
i
)))
++
;
histogram
(
getBin
(
sourceData
(
i
)))
++
;
}
}
datapoints
=
size
;
datapoints
=
size
;
}
}
void
Histogram
::
generate
(
ColumnVector
exclude
)
void
Histogram
::
generate
(
ColumnVector
exclude
)
{
{
Tracer
ts
(
"Histogram::generate"
);
Tracer
ts
(
"Histogram::generate"
);
...
@@ -81,13 +81,13 @@ namespace MISCMATHS {
...
@@ -81,13 +81,13 @@ namespace MISCMATHS {
histMin
=
sourceData
(
i
);
histMin
=
sourceData
(
i
);
}
}
}
}
histogram
.
ReSize
(
bins
);
histogram
.
ReSize
(
bins
);
histogram
=
0
;
histogram
=
0
;
datapoints
=
size
;
datapoints
=
size
;
for
(
int
i
=
1
;
i
<=
size
;
i
++
)
for
(
int
i
=
1
;
i
<=
size
;
i
++
)
{
{
if
(
exclude
(
i
)
>
0
){
if
(
exclude
(
i
)
>
0
){
...
@@ -95,7 +95,7 @@ namespace MISCMATHS {
...
@@ -95,7 +95,7 @@ namespace MISCMATHS {
}
else
datapoints
--
;
}
else
datapoints
--
;
}
}
}
}
...
@@ -108,19 +108,19 @@ namespace MISCMATHS {
...
@@ -108,19 +108,19 @@ namespace MISCMATHS {
// smooth in i direction
// smooth in i direction
newhist
=
0
;
newhist
=
0
;
ColumnVector
kernel
(
3
);
ColumnVector
kernel
(
3
);
// corresponds to Gaussian with sigma=0.8 voxels
// corresponds to Gaussian with sigma=0.8 voxels
// kernel(1)=0.5;
// kernel(1)=0.5;
// kernel(2)=0.2283;
// kernel(2)=0.2283;
// kernel(3)=0.0219;
// kernel(3)=0.0219;
// corresponds to Gaussian with sigma=0.6 voxels
// corresponds to Gaussian with sigma=0.6 voxels
// kernel(1)=0.6638;
// kernel(1)=0.6638;
// kernel(2)=0.1655;
// kernel(2)=0.1655;
// kernel(3)=0.0026;
// kernel(3)=0.0026;
//gauss(0.5,5,1)
//gauss(0.5,5,1)
kernel
(
1
)
=
0.7866
;
kernel
(
1
)
=
0.7866
;
kernel
(
2
)
=
0.1065
;
kernel
(
2
)
=
0.1065
;
kernel
(
3
)
=
0.0003
;
kernel
(
3
)
=
0.0003
;
for
(
int
i
=
1
;
i
<=
bins
;
i
++
)
for
(
int
i
=
1
;
i
<=
bins
;
i
++
)
...
@@ -136,7 +136,7 @@ namespace MISCMATHS {
...
@@ -136,7 +136,7 @@ namespace MISCMATHS {
if
(
i
>
2
)
if
(
i
>
2
)
{
{
val
+=
kernel
(
3
)
*
(
histogram
(
i
-
2
));
val
+=
kernel
(
3
)
*
(
histogram
(
i
-
2
));
norm
+=
kernel
(
3
);
norm
+=
kernel
(
3
);
}
}
if
(
i
<
bins
)
if
(
i
<
bins
)
{
{
...
@@ -146,7 +146,7 @@ namespace MISCMATHS {
...
@@ -146,7 +146,7 @@ namespace MISCMATHS {
if
(
i
<
bins
-
1
)
if
(
i
<
bins
-
1
)
{
{
val
+=
kernel
(
3
)
*
(
histogram
(
i
+
2
));
val
+=
kernel
(
3
)
*
(
histogram
(
i
+
2
));
norm
+=
kernel
(
3
);
norm
+=
kernel
(
3
);
}
}
val
/=
norm
;
val
/=
norm
;
...
@@ -188,7 +188,7 @@ namespace MISCMATHS {
...
@@ -188,7 +188,7 @@ namespace MISCMATHS {
void
Histogram
::
match
(
Histogram
&
target
){
void
Histogram
::
match
(
Histogram
&
target
){
int
bin
,
newbin
;
int
bin
,
newbin
;
double
cdfval
,
val
,
dist
;
double
cdfval
,
val
,
dist
;
ColumnVector
CDF_ref
;
ColumnVector
CDF_ref
;
...
@@ -200,52 +200,52 @@ namespace MISCMATHS {
...
@@ -200,52 +200,52 @@ namespace MISCMATHS {
olddata
=
sourceData
;
olddata
=
sourceData
;
histnew
=
0
;
dist
=
0
;
histnew
=
0
;
dist
=
0
;
if
(
exclusion
.
Nrows
()
==
0
){
cout
<<
" Histogram::match no excl "
<<
endl
;
exclusion
.
ReSize
(
sourceData
.
Nrows
());
exclusion
=
1
;}
if
(
exclusion
.
Nrows
()
==
0
){
cout
<<
" Histogram::match no excl "
<<
endl
;
exclusion
.
ReSize
(
sourceData
.
Nrows
());
exclusion
=
1
;}
float
binwidthtarget
=
(
target
.
getHistMax
()
-
target
.
getHistMin
())
/
target
.
getNumBins
();
float
binwidthtarget
=
(
target
.
getHistMax
()
-
target
.
getHistMin
())
/
target
.
getNumBins
();
for
(
int
i
=
1
;
i
<=
sourceData
.
Nrows
();
i
++
){
for
(
int
i
=
1
;
i
<=
sourceData
.
Nrows
();
i
++
){
if
(
exclusion
(
i
)
>
0
){
if
(
exclusion
(
i
)
>
0
){
bin
=
getBin
(
sourceData
(
i
));
bin
=
getBin
(
sourceData
(
i
));
cdfval
=
CDF
(
bin
);
cdfval
=
CDF
(
bin
);
newbin
=
1
;
newbin
=
1
;
val
=
sourceData
(
i
)
-
histMin
;
val
=
sourceData
(
i
)
-
histMin
;
if
(
bin
==
target
.
getNumBins
()){
if
(
bin
==
target
.
getNumBins
()){
newbin
=
bin
;
newbin
=
bin
;
}
else
if
(
(
cdfval
-
CDF_ref
(
bin
))
>
1e-20
){
}
else
if
(
(
cdfval
-
CDF_ref
(
bin
))
>
1e-20
){
for
(
int
j
=
bin
;
j
<
target
.
getNumBins
();
j
++
){
for
(
int
j
=
bin
;
j
<
target
.
getNumBins
();
j
++
){
if
((
cdfval
-
CDF_ref
(
j
))
>
1e-20
&&
(
cdfval
-
CDF_ref
(
j
+
1
))
<=
1e-20
){
if
((
cdfval
-
CDF_ref
(
j
))
>
1e-20
&&
(
cdfval
-
CDF_ref
(
j
+
1
))
<=
1e-20
){
newbin
=
j
+
1
;
newbin
=
j
+
1
;
dist
=
(
cdfval
-
CDF_ref
(
j
))
/
(
CDF_ref
(
j
+
1
)
-
CDF_ref
(
j
));
// find distance between current bins as proportion of bin width
dist
=
(
cdfval
-
CDF_ref
(
j
))
/
(
CDF_ref
(
j
+
1
)
-
CDF_ref
(
j
));
// find distance between current bins as proportion of bin width
break
;
break
;
}
}
}
}
}
}
else
{
else
{
//search backwards
//search backwards
int
j
=
bin
-
1
;
int
j
=
bin
-
1
;
while
(
j
>
0
){
while
(
j
>
0
){
// if(sourceData(i)<1.2 && sourceData(i)>1)
// if(sourceData(i)<1.2 && sourceData(i)>1)
if
((
cdfval
-
CDF_ref
(
1
))
<
-
1e-20
&&
fabs
(
CDF_ref
(
bin
)
-
CDF_ref
(
1
))
<
1e-20
){
newbin
=
j
+
1
;
break
;
}
if
((
cdfval
-
CDF_ref
(
1
))
<
-
1e-20
&&
fabs
(
CDF_ref
(
bin
)
-
CDF_ref
(
1
))
<
1e-20
){
newbin
=
j
+
1
;
break
;
}
else
if
((
cdfval
-
CDF_ref
(
1
))
>
-
1e-20
&&
fabs
(
CDF_ref
(
bin
)
-
CDF_ref
(
1
))
<
1e-20
){
newbin
=
j
+
1
;
break
;
}
else
if
((
cdfval
-
CDF_ref
(
1
))
>
-
1e-20
&&
fabs
(
CDF_ref
(
bin
)
-
CDF_ref
(
1
))
<
1e-20
){
newbin
=
j
+
1
;
break
;
}
else
if
((
cdfval
-
CDF_ref
(
j
))
>
1e-20
&&
(
cdfval
-
CDF_ref
(
j
+
1
))
<=
1e-20
){
else
if
((
cdfval
-
CDF_ref
(
j
))
>
1e-20
&&
(
cdfval
-
CDF_ref
(
j
+
1
))
<=
1e-20
){
newbin
=
j
+
1
;
newbin
=
j
+
1
;
dist
=
(
cdfval
-
CDF_ref
(
j
))
/
(
CDF_ref
(
j
+
1
)
-
CDF_ref
(
j
));
// find distance between current bins as proportion of bin width
dist
=
(
cdfval
-
CDF_ref
(
j
))
/
(
CDF_ref
(
j
+
1
)
-
CDF_ref
(
j
));
// find distance between current bins as proportion of bin width
break
;
break
;
}
}
...
@@ -253,51 +253,17 @@ namespace MISCMATHS {
...
@@ -253,51 +253,17 @@ namespace MISCMATHS {
}
}
//search forwards
//search forwards
}
}
sourceData
(
i
)
=
target
.
getHistMin
()
+
(
newbin
-
1
)
*
binwidthtarget
+
dist
*
binwidthtarget
;
sourceData
(
i
)
=
target
.
getHistMin
()
+
(
newbin
-
1
)
*
binwidthtarget
+
dist
*
binwidthtarget
;
histnew
(
newbin
)
++
;
histnew
(
newbin
)
++
;
if
(
sourceData
(
i
)
<
target
.
getHistMin
())
sourceData
(
i
)
=
target
.
getHistMin
();
if
(
sourceData
(
i
)
<
target
.
getHistMin
())
sourceData
(
i
)
=
target
.
getHistMin
();
if
(
sourceData
(
i
)
>
target
.
getHistMax
())
sourceData
(
i
)
=
target
.
getHistMax
();
if
(
sourceData
(
i
)
>
target
.
getHistMax
())
sourceData
(
i
)
=
target
.
getHistMax
();
}
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
kernel.cc
+
25
−
26
View file @
e63ee18d
...
@@ -13,7 +13,7 @@ using namespace NEWMAT;
...
@@ -13,7 +13,7 @@ using namespace NEWMAT;
namespace
MISCMATHS
{
namespace
MISCMATHS
{
set
<
kernelstorage
*
,
kernelstorage
::
comparer
>
kernel
::
existingkernels
;
set
<
kernelstorage
*
,
kernelstorage
::
comparer
>
kernel
::
existingkernels
;
//////// Support function /////////
//////// Support function /////////
...
@@ -28,42 +28,42 @@ namespace MISCMATHS {
...
@@ -28,42 +28,42 @@ namespace MISCMATHS {
dn
-=
n
;
dn
-=
n
;
if
(
n
>
(
kernel
.
Nrows
()
-
1
))
return
0.0
;
if
(
n
>
(
kernel
.
Nrows
()
-
1
))
return
0.0
;
if
(
n
<
1
)
return
0.0
;
if
(
n
<
1
)
return
0.0
;
return
kernel
(
n
)
*
(
1.0
-
dn
)
+
kernel
(
n
+
1
)
*
dn
;
return
kernel
(
n
)
*
(
1.0
-
dn
)
+
kernel
(
n
+
1
)
*
dn
;
}
}
inline
bool
in_bounds
(
const
ColumnVector
&
data
,
int
index
)
inline
bool
in_bounds
(
const
ColumnVector
&
data
,
int
index
)
{
return
(
(
index
>=
1
)
&&
(
index
<=
data
.
Nrows
()));
}
{
return
(
(
index
>=
1
)
&&
(
index
<=
data
.
Nrows
()));
}
inline
bool
in_bounds
(
const
ColumnVector
&
data
,
float
index
)
inline
bool
in_bounds
(
const
ColumnVector
&
data
,
float
index
)
{
return
(
((
int
)
floor
(
index
)
>=
1
)
&&
((
int
)
ceil
(
index
)
<=
data
.
Nrows
()));
}
{
return
(
((
int
)
floor
(
index
)
>=
1
)
&&
((
int
)
ceil
(
index
)
<=
data
.
Nrows
()));
}
float
sincfn
(
float
x
)
float
sincfn
(
float
x
)
{
{
if
(
fabs
(
x
)
<
1e-7
)
{
return
1.0
-
fabs
(
x
);
}
if
(
fabs
(
x
)
<
1e-7
)
{
return
1.0
-
fabs
(
x
);
}
float
y
=
M_PI
*
x
;
float
y
=
M_PI
*
x
;
return
sin
(
y
)
/
y
;
return
sin
(
y
)
/
y
;
}
}
float
hanning
(
float
x
,
int
w
)
float
hanning
(
float
x
,
int
w
)
{
// w is half-width
{
// w is half-width
if
(
fabs
(
x
)
>
w
)
if
(
fabs
(
x
)
>
w
)
return
0.0
;
return
0.0
;
else
else
return
(
0.5
+
0.5
*
cos
(
M_PI
*
x
/
w
));
return
(
0.5
+
0.5
*
cos
(
M_PI
*
x
/
w
));
}
}
float
blackman
(
float
x
,
int
w
)
float
blackman
(
float
x
,
int
w
)
{
// w is half-width
{
// w is half-width
if
(
fabs
(
x
)
>
w
)
if
(
fabs
(
x
)
>
w
)
return
0.0
;
return
0.0
;
else
else
return
(
0.42
+
0.5
*
cos
(
M_PI
*
x
/
w
)
+
0.08
*
cos
(
2.0
*
M_PI
*
x
/
w
));
return
(
0.42
+
0.5
*
cos
(
M_PI
*
x
/
w
)
+
0.08
*
cos
(
2.0
*
M_PI
*
x
/
w
));
}
}
float
rectangular
(
float
x
,
int
w
)
float
rectangular
(
float
x
,
int
w
)
{
// w is half-width
{
// w is half-width
if
(
fabs
(
x
)
>
w
)
if
(
fabs
(
x
)
>
w
)
return
0.0
;
return
0.0
;
else
else
return
1.0
;
return
1.0
;
...
@@ -94,8 +94,8 @@ namespace MISCMATHS {
...
@@ -94,8 +94,8 @@ namespace MISCMATHS {
return
ker
;
return
ker
;
}
}
kernel
sinckernel
(
const
string
&
sincwindowtype
,
int
w
,
int
nstore
)
kernel
sinckernel
(
const
string
&
sincwindowtype
,
int
w
,
int
nstore
)
{
{
kernel
sinck
;
kernel
sinck
;
sinck
=
sinckernel
(
sincwindowtype
,
w
,
w
,
w
,
nstore
);
sinck
=
sinckernel
(
sincwindowtype
,
w
,
w
,
w
,
nstore
);
...
@@ -119,7 +119,7 @@ namespace MISCMATHS {
...
@@ -119,7 +119,7 @@ namespace MISCMATHS {
kx
=
sinckernel1D
(
sincwindowtype
,
wx
,
nstore
);
kx
=
sinckernel1D
(
sincwindowtype
,
wx
,
nstore
);
ky
=
sinckernel1D
(
sincwindowtype
,
wy
,
nstore
);
ky
=
sinckernel1D
(
sincwindowtype
,
wy
,
nstore
);
kz
=
sinckernel1D
(
sincwindowtype
,
wz
,
nstore
);
kz
=
sinckernel1D
(
sincwindowtype
,
wz
,
nstore
);
sinckern
.
setkernel
(
kx
,
ky
,
kz
,
hwx
,
hwy
,
hwz
);
sinckern
.
setkernel
(
kx
,
ky
,
kz
,
hwx
,
hwy
,
hwz
);
return
sinckern
;
return
sinckern
;
}
}
...
@@ -140,13 +140,13 @@ namespace MISCMATHS {
...
@@ -140,13 +140,13 @@ namespace MISCMATHS {
return
extrapval
;
return
extrapval
;
}
}
// basic trilinear call
// basic trilinear call
float
interpolate_1d
(
const
ColumnVector
&
data
,
const
float
index
)
float
interpolate_1d
(
const
ColumnVector
&
data
,
const
float
index
)
{
{
float
interpval
;
float
interpval
;
int
low_bound
=
(
int
)
floor
(
index
);
int
low_bound
=
(
int
)
floor
(
index
);
int
high_bound
=
(
int
)
ceil
(
index
);
int
high_bound
=
(
int
)
ceil
(
index
);
if
(
in_bounds
(
data
,
index
))
if
(
in_bounds
(
data
,
index
))
interpval
=
data
(
low_bound
)
+
(
index
-
low_bound
)
*
(
data
(
high_bound
)
-
data
(
low_bound
));
interpval
=
data
(
low_bound
)
+
(
index
-
low_bound
)
*
(
data
(
high_bound
)
-
data
(
low_bound
));
...
@@ -156,7 +156,7 @@ namespace MISCMATHS {
...
@@ -156,7 +156,7 @@ namespace MISCMATHS {
return
interpval
;
return
interpval
;
}
}
//////// Spline Support /////////
//////// Spline Support /////////
float
hermiteinterpolation_1d
(
const
ColumnVector
&
data
,
int
p1
,
int
p4
,
float
t
)
float
hermiteinterpolation_1d
(
const
ColumnVector
&
data
,
int
p1
,
int
p4
,
float
t
)
...
@@ -165,7 +165,7 @@ namespace MISCMATHS {
...
@@ -165,7 +165,7 @@ namespace MISCMATHS {
// inputs: points P_1, P_4; tangents R_1, R_4; interpolation index t;
// inputs: points P_1, P_4; tangents R_1, R_4; interpolation index t;
float
retval
,
r1
=
0.0
,
r4
=
0.0
;
float
retval
,
r1
=
0.0
,
r4
=
0.0
;
if
(
!
in_bounds
(
data
,
p1
)
||
!
in_bounds
(
data
,
p4
))
{
if
(
!
in_bounds
(
data
,
p1
)
||
!
in_bounds
(
data
,
p4
))
{
cerr
<<
"Hermite Interpolation - ERROR: One or more indicies lie outside the data range. Returning ZERO"
<<
endl
;
cerr
<<
"Hermite Interpolation - ERROR: One or more indicies lie outside the data range. Returning ZERO"
<<
endl
;
retval
=
0.0
;
retval
=
0.0
;
...
@@ -176,17 +176,17 @@ namespace MISCMATHS {
...
@@ -176,17 +176,17 @@ namespace MISCMATHS {
retval = data(p1);
retval = data(p1);
} else if (t == 1.0) {
} else if (t == 1.0) {
retval = data(p4);
retval = data(p4);
*/
*/
}
else
{
}
else
{
r1
=
0.5
*
(
extrapolate_1d
(
data
,
p1
)
-
extrapolate_1d
(
data
,
p1
-
1
))
+
0.5
*
(
extrapolate_1d
(
data
,
p1
+
1
)
-
extrapolate_1d
(
data
,
p1
));
// tangent @ P_1
r1
=
0.5
*
(
extrapolate_1d
(
data
,
p1
)
-
extrapolate_1d
(
data
,
p1
-
1
))
+
0.5
*
(
extrapolate_1d
(
data
,
p1
+
1
)
-
extrapolate_1d
(
data
,
p1
));
// tangent @ P_1
r4
=
0.5
*
(
extrapolate_1d
(
data
,
p4
)
-
extrapolate_1d
(
data
,
p4
-
1
))
+
0.5
*
(
extrapolate_1d
(
data
,
p4
+
1
)
-
extrapolate_1d
(
data
,
p4
));
// tangent @ P_4
r4
=
0.5
*
(
extrapolate_1d
(
data
,
p4
)
-
extrapolate_1d
(
data
,
p4
-
1
))
+
0.5
*
(
extrapolate_1d
(
data
,
p4
+
1
)
-
extrapolate_1d
(
data
,
p4
));
// tangent @ P_4
float
t2
=
t
*
t
;
float
t3
=
t2
*
t
;
float
t2
=
t
*
t
;
float
t3
=
t2
*
t
;
retval
=
(
2
*
t3
-
3
*
t2
+
1
)
*
data
(
p1
)
+
(
-
2
*
t3
+
3
*
t2
)
*
data
(
p4
)
+
(
t3
-
2
*
t2
+
t
)
*
r1
+
(
t3
-
t2
)
*
r4
;
retval
=
(
2
*
t3
-
3
*
t2
+
1
)
*
data
(
p1
)
+
(
-
2
*
t3
+
3
*
t2
)
*
data
(
p4
)
+
(
t3
-
2
*
t2
+
t
)
*
r1
+
(
t3
-
t2
)
*
r4
;
}
}
// cerr << "p1, p4, t, r1, r4 = " << p1 << ", " << p4 << ", " << t << ", " << r1 << ", " << r4 << endl;
// cerr << "p1, p4, t, r1, r4 = " << p1 << ", " << p4 << ", " << t << ", " << r1 << ", " << r4 << endl;
return
retval
;
return
retval
;
}
}
...
@@ -199,14 +199,14 @@ namespace MISCMATHS {
...
@@ -199,14 +199,14 @@ namespace MISCMATHS {
// kernel half-width (i.e. range is +/- w)
// kernel half-width (i.e. range is +/- w)
int
ix0
;
int
ix0
;
ix0
=
(
int
)
floor
(
index
);
ix0
=
(
int
)
floor
(
index
);
int
wx
(
widthx
);
int
wx
(
widthx
);
vector
<
float
>
storex
(
2
*
wx
+
1
);
vector
<
float
>
storex
(
2
*
wx
+
1
);
for
(
int
d
=-
wx
;
d
<=
wx
;
d
++
)
for
(
int
d
=-
wx
;
d
<=
wx
;
d
++
)
storex
[
d
+
wx
]
=
kernelval
((
index
-
ix0
+
d
),
wx
,
userkernel
);
storex
[
d
+
wx
]
=
kernelval
((
index
-
ix0
+
d
),
wx
,
userkernel
);
float
convsum
=
0.0
,
interpval
=
0.0
,
kersum
=
0.0
;
float
convsum
=
0.0
,
interpval
=
0.0
,
kersum
=
0.0
;
int
xj
;
int
xj
;
for
(
int
x1
=
ix0
-
wx
;
x1
<=
ix0
+
wx
;
x1
++
)
{
for
(
int
x1
=
ix0
-
wx
;
x1
<=
ix0
+
wx
;
x1
++
)
{
if
(
in_bounds
(
data
,
x1
))
{
if
(
in_bounds
(
data
,
x1
))
{
...
@@ -224,7 +224,7 @@ namespace MISCMATHS {
...
@@ -224,7 +224,7 @@ namespace MISCMATHS {
}
}
return
interpval
;
return
interpval
;
}
}
////// Kernel wrappers //////
////// Kernel wrappers //////
float
kernelinterpolation_1d
(
const
ColumnVector
&
data
,
float
index
)
float
kernelinterpolation_1d
(
const
ColumnVector
&
data
,
float
index
)
...
@@ -239,4 +239,3 @@ namespace MISCMATHS {
...
@@ -239,4 +239,3 @@ namespace MISCMATHS {
return
kernelinterpolation_1d
(
data
.
t
(),
index
,
userkernel
,
7
);
return
kernelinterpolation_1d
(
data
.
t
(),
index
,
userkernel
,
7
);
}
}
}
}
This diff is collapsed.
Click to expand it.
optimise.cc
+
21
−
29
View file @
e63ee18d
...
@@ -28,7 +28,7 @@ namespace MISCMATHS {
...
@@ -28,7 +28,7 @@ namespace MISCMATHS {
using
std
::
exp
;
using
std
::
exp
;
using
std
::
log
;
using
std
::
log
;
bool
estquadmin
(
float
&
xnew
,
float
x1
,
float
xmid
,
float
x2
,
bool
estquadmin
(
float
&
xnew
,
float
x1
,
float
xmid
,
float
x2
,
float
y1
,
float
ymid
,
float
y2
)
float
y1
,
float
ymid
,
float
y2
)
{
{
// Finds the estimated quadratic minimum's position
// Finds the estimated quadratic minimum's position
...
@@ -64,7 +64,7 @@ namespace MISCMATHS {
...
@@ -64,7 +64,7 @@ namespace MISCMATHS {
}
}
return
xnew
;
return
xnew
;
}
}
float
nextpt
(
float
x1
,
float
xmid
,
float
x2
,
float
y1
,
float
ymid
,
float
y2
)
float
nextpt
(
float
x1
,
float
xmid
,
float
x2
,
float
y1
,
float
ymid
,
float
y2
)
...
@@ -82,10 +82,10 @@ namespace MISCMATHS {
...
@@ -82,10 +82,10 @@ namespace MISCMATHS {
return
xnew
;
return
xnew
;
}
}
void
findinitialbound
(
float
&
x1
,
float
&
xmid
,
float
&
x2
,
float
&
y1
,
float
&
ymid
,
float
&
y2
,
void
findinitialbound
(
float
&
x1
,
float
&
xmid
,
float
&
x2
,
float
&
y1
,
float
&
ymid
,
float
&
y2
,
float
(
*
func
)(
const
ColumnVector
&
),
float
(
*
func
)(
const
ColumnVector
&
),
const
ColumnVector
&
unitdir
,
const
ColumnVector
&
pt
)
const
ColumnVector
&
unitdir
,
const
ColumnVector
&
pt
)
{
{
...
@@ -109,15 +109,15 @@ namespace MISCMATHS {
...
@@ -109,15 +109,15 @@ namespace MISCMATHS {
y2
=
(
*
func
)(
x2
*
unitdir
+
pt
);
y2
=
(
*
func
)(
x2
*
unitdir
+
pt
);
while
(
ymid
>
y2
)
{
// note: must maintain y1 >= ymid
while
(
ymid
>
y2
)
{
// note: must maintain y1 >= ymid
// cout << " <" << Min(x1,x2) << "," << xmid
// cout << " <" << Min(x1,x2) << "," << xmid
// << "," << Max(x1,x2) << ">" << endl;
// << "," << Max(x1,x2) << ">" << endl;
maxx2
=
xmid
+
maxextrap
*
(
x2
-
xmid
);
maxx2
=
xmid
+
maxextrap
*
(
x2
-
xmid
);
quadok
=
estquadmin
(
newx2
,
x1
,
xmid
,
x2
,
y1
,
ymid
,
y2
);
quadok
=
estquadmin
(
newx2
,
x1
,
xmid
,
x2
,
y1
,
ymid
,
y2
);
if
((
!
quadok
)
||
((
newx2
-
x1
)
*
dir
<
0
)
||
((
newx2
-
maxx2
)
*
dir
>
0
))
{
if
((
!
quadok
)
||
((
newx2
-
x1
)
*
dir
<
0
)
||
((
newx2
-
maxx2
)
*
dir
>
0
))
{
newx2
=
xmid
+
extrapolationfactor
*
(
x2
-
x1
);
newx2
=
xmid
+
extrapolationfactor
*
(
x2
-
x1
);
}
}
newy2
=
(
*
func
)(
newx2
*
unitdir
+
pt
);
newy2
=
(
*
func
)(
newx2
*
unitdir
+
pt
);
if
((
newx2
-
xmid
)
*
(
newx2
-
x1
)
<
0
)
{
// newx2 is between x1 and xmid
if
((
newx2
-
xmid
)
*
(
newx2
-
x1
)
<
0
)
{
// newx2 is between x1 and xmid
...
@@ -141,19 +141,19 @@ namespace MISCMATHS {
...
@@ -141,19 +141,19 @@ namespace MISCMATHS {
x2
=
newx2
;
y2
=
newy2
;
x2
=
newx2
;
y2
=
newy2
;
}
}
}
}
}
}
if
(
(
y2
<
ymid
)
||
(
y1
<
ymid
)
)
{
if
(
(
y2
<
ymid
)
||
(
y1
<
ymid
)
)
{
cerr
<<
"findinitialbound failed to bracket: current triplet is"
<<
endl
;
cerr
<<
"findinitialbound failed to bracket: current triplet is"
<<
endl
;
}
}
}
}
float
optimise1d
(
ColumnVector
&
pt
,
const
ColumnVector
dir
,
const
ColumnVector
tol
,
int
&
iterations_done
,
float
optimise1d
(
ColumnVector
&
pt
,
const
ColumnVector
dir
,
const
ColumnVector
tol
,
int
&
iterations_done
,
float
(
*
func
)(
const
ColumnVector
&
),
int
max_iter
,
float
(
*
func
)(
const
ColumnVector
&
),
int
max_iter
,
float
&
init_value
,
float
boundguess
)
float
&
init_value
,
float
boundguess
)
{
{
// Golden Search Routine
// Golden Search Routine
// Must pass in the direction vector in N-space (dir), the initial
// Must pass in the direction vector in N-space (dir), the initial
...
@@ -247,10 +247,10 @@ namespace MISCMATHS {
...
@@ -247,10 +247,10 @@ namespace MISCMATHS {
float
optimise
(
ColumnVector
&
pt
,
int
numopt
,
const
ColumnVector
&
tol
,
float
optimise
(
ColumnVector
&
pt
,
int
numopt
,
const
ColumnVector
&
tol
,
float
(
*
func
)(
const
ColumnVector
&
),
int
&
iterations_done
,
float
(
*
func
)(
const
ColumnVector
&
),
int
&
iterations_done
,
int
max_iter
,
const
ColumnVector
&
boundguess
,
int
max_iter
,
const
ColumnVector
&
boundguess
,
const
string
type
)
const
string
type
)
{
{
// Note that numopt can be less than pt.Nrows() - e.g. 6 dof optimisation
// Note that numopt can be less than pt.Nrows() - e.g. 6 dof optimisation
...
@@ -291,12 +291,12 @@ namespace MISCMATHS {
...
@@ -291,12 +291,12 @@ namespace MISCMATHS {
if
(
avtol
<
1.0
)
break
;
if
(
avtol
<
1.0
)
break
;
// if continuing then change the directions if using Powell's method
// if continuing then change the directions if using Powell's method
if
(
type
==
"powell"
)
if
(
type
==
"powell"
)
{
{
// find direction of maximal change
// find direction of maximal change
int
bestm
=
1
;
int
bestm
=
1
;
for
(
int
m
=
1
;
m
<=
numopt
;
m
++
)
{
for
(
int
m
=
1
;
m
<=
numopt
;
m
++
)
{
if
(
deltaf
(
m
)
<
deltaf
(
bestm
))
bestm
=
m
;
if
(
deltaf
(
m
)
<
deltaf
(
bestm
))
bestm
=
m
;
}
}
fend
=
fval
;
fend
=
fval
;
fextrap
=
(
*
func
)(
initpt
+
2
*
(
pt
-
initpt
));
fextrap
=
(
*
func
)(
initpt
+
2
*
(
pt
-
initpt
));
...
@@ -317,7 +317,7 @@ namespace MISCMATHS {
...
@@ -317,7 +317,7 @@ namespace MISCMATHS {
}
}
}
}
}
}
}
}
// cout << endl << "Major iterations = " << it << endl;
// cout << endl << "Major iterations = " << it << endl;
iterations_done
=
littot
;
iterations_done
=
littot
;
...
@@ -326,11 +326,3 @@ namespace MISCMATHS {
...
@@ -326,11 +326,3 @@ namespace MISCMATHS {
}
}
This diff is collapsed.
Click to expand it.
rungekutta.cc
+
25
−
25
View file @
e63ee18d
...
@@ -14,34 +14,34 @@ using namespace NEWMAT;
...
@@ -14,34 +14,34 @@ using namespace NEWMAT;
namespace
MISCMATHS
{
namespace
MISCMATHS
{
void
rk
(
ColumnVector
&
ret
,
const
ColumnVector
&
y
,
const
ColumnVector
&
dy
,
float
x
,
float
h
,
const
Derivative
&
deriv
,
const
ColumnVector
&
paramvalues
)
void
rk
(
ColumnVector
&
ret
,
const
ColumnVector
&
y
,
const
ColumnVector
&
dy
,
float
x
,
float
h
,
const
Derivative
&
deriv
,
const
ColumnVector
&
paramvalues
)
{
{
Tracer
tr
(
"rk"
);
Tracer
tr
(
"rk"
);
float
hh
=
h
*
0.5
;
float
hh
=
h
*
0.5
;
float
xh
=
x
+
hh
;
float
xh
=
x
+
hh
;
//first step
//first step
ColumnVector
yt
=
y
+
hh
*
dy
;
ColumnVector
yt
=
y
+
hh
*
dy
;
//second step
//second step
ColumnVector
dyt
=
deriv
.
evaluate
(
xh
,
yt
,
paramvalues
);
ColumnVector
dyt
=
deriv
.
evaluate
(
xh
,
yt
,
paramvalues
);
yt
=
y
+
hh
*
dyt
;
yt
=
y
+
hh
*
dyt
;
//third step
//third step
ColumnVector
dym
=
deriv
.
evaluate
(
xh
,
yt
,
paramvalues
);
ColumnVector
dym
=
deriv
.
evaluate
(
xh
,
yt
,
paramvalues
);
yt
=
y
+
h
*
dym
;
yt
=
y
+
h
*
dym
;
dym
=
dym
+
dyt
;
dym
=
dym
+
dyt
;
//fourth step
//fourth step
dyt
=
deriv
.
evaluate
(
x
+
h
,
yt
,
paramvalues
);
dyt
=
deriv
.
evaluate
(
x
+
h
,
yt
,
paramvalues
);
//addup
//addup
ret
=
y
+
h
*
(
dy
+
dyt
+
2
*
dym
)
/
6
;
ret
=
y
+
h
*
(
dy
+
dyt
+
2
*
dym
)
/
6
;
}
}
void
rkqc
(
ColumnVector
&
y
,
float
&
x
,
float
&
hnext
,
ColumnVector
&
dy
,
float
htry
,
float
eps
,
const
Derivative
&
deriv
,
const
ColumnVector
&
paramvalues
)
void
rkqc
(
ColumnVector
&
y
,
float
&
x
,
float
&
hnext
,
ColumnVector
&
dy
,
float
htry
,
float
eps
,
const
Derivative
&
deriv
,
const
ColumnVector
&
paramvalues
)
{
{
Tracer
tr
(
"rkqc"
);
Tracer
tr
(
"rkqc"
);
float
xsav
=
x
;
float
xsav
=
x
;
ColumnVector
dysav
=
dy
;
ColumnVector
dysav
=
dy
;
...
@@ -53,12 +53,12 @@ void rkqc(ColumnVector& y, float& x, float& hnext, ColumnVector& dy, float htry,
...
@@ -53,12 +53,12 @@ void rkqc(ColumnVector& y, float& x, float& hnext, ColumnVector& dy, float htry,
while
(
true
)
while
(
true
)
{
{
// take 2 1/2 step sizes
// take 2 1/2 step sizes
// first 1/2 step
// first 1/2 step
float
hh
=
h
*
0.5
;
float
hh
=
h
*
0.5
;
rk
(
ytemp
,
ysav
,
dysav
,
xsav
,
hh
,
deriv
,
paramvalues
);
rk
(
ytemp
,
ysav
,
dysav
,
xsav
,
hh
,
deriv
,
paramvalues
);
// second 1/2 step
// second 1/2 step
x
=
xsav
+
hh
;
x
=
xsav
+
hh
;
dy
=
deriv
.
evaluate
(
x
,
ytemp
,
paramvalues
);
dy
=
deriv
.
evaluate
(
x
,
ytemp
,
paramvalues
);
...
@@ -68,32 +68,32 @@ void rkqc(ColumnVector& y, float& x, float& hnext, ColumnVector& dy, float htry,
...
@@ -68,32 +68,32 @@ void rkqc(ColumnVector& y, float& x, float& hnext, ColumnVector& dy, float htry,
// take large step size
// take large step size
rk
(
ytemp
,
ysav
,
dysav
,
xsav
,
h
,
deriv
,
paramvalues
);
rk
(
ytemp
,
ysav
,
dysav
,
xsav
,
h
,
deriv
,
paramvalues
);
// eval accuracy
// eval accuracy
float
errmax
=
0.0
;
float
errmax
=
0.0
;
for
(
int
i
=
1
;
i
<=
y
.
Nrows
();
i
++
)
for
(
int
i
=
1
;
i
<=
y
.
Nrows
();
i
++
)
{
{
//errmax=max(abs((y-ytemp)./y));
//errmax=max(abs((y-ytemp)./y));
float
tmp
=
fabs
((
y
(
i
)
-
ytemp
(
i
))
/
y
(
i
));
float
tmp
=
fabs
((
y
(
i
)
-
ytemp
(
i
))
/
y
(
i
));
if
(
tmp
>
errmax
)
errmax
=
tmp
;
if
(
tmp
>
errmax
)
errmax
=
tmp
;
}
}
errmax
=
errmax
/
eps
;
errmax
=
errmax
/
eps
;
if
(
errmax
<=
1.0
)
if
(
errmax
<=
1.0
)
{
{
// step OK, compute step size for next step
// step OK, compute step size for next step
hdid
=
h
;
hdid
=
h
;
if
(
errmax
>
6e-4
)
if
(
errmax
>
6e-4
)
hnext
=
h
*
std
::
exp
(
-
0.2
*
std
::
log
(
errmax
));
hnext
=
h
*
std
::
exp
(
-
0.2
*
std
::
log
(
errmax
));
else
else
hnext
=
4
*
h
;
hnext
=
4
*
h
;
break
;
break
;
}
}
else
else
{
{
// step too large,
// step too large,
h
=
h
*
std
::
exp
(
-
0.25
*
std
::
log
(
errmax
));
h
=
h
*
std
::
exp
(
-
0.25
*
std
::
log
(
errmax
));
...
@@ -105,7 +105,7 @@ void rkqc(ColumnVector& y, float& x, float& hnext, ColumnVector& dy, float htry,
...
@@ -105,7 +105,7 @@ void rkqc(ColumnVector& y, float& x, float& hnext, ColumnVector& dy, float htry,
void
runge_kutta
(
Matrix
&
yp
,
ColumnVector
&
xp
,
ColumnVector
&
hp
,
const
ColumnVector
&
ystart
,
float
x1
,
float
x2
,
float
eps
,
float
hmin
,
const
Derivative
&
deriv
,
const
ColumnVector
&
paramvalues
)
void
runge_kutta
(
Matrix
&
yp
,
ColumnVector
&
xp
,
ColumnVector
&
hp
,
const
ColumnVector
&
ystart
,
float
x1
,
float
x2
,
float
eps
,
float
hmin
,
const
Derivative
&
deriv
,
const
ColumnVector
&
paramvalues
)
{
{
Tracer
tr
(
"runge_kutta"
);
Tracer
tr
(
"runge_kutta"
);
int
MAXSTEP
=
1000
;
int
MAXSTEP
=
1000
;
...
@@ -124,20 +124,20 @@ void runge_kutta(Matrix& yp, ColumnVector& xp, ColumnVector& hp, const ColumnVec
...
@@ -124,20 +124,20 @@ void runge_kutta(Matrix& yp, ColumnVector& xp, ColumnVector& hp, const ColumnVec
yp
=
0
;
yp
=
0
;
int
kout
=
1
;
int
kout
=
1
;
ColumnVector
dy
;
ColumnVector
dy
;
for
(
int
k
=
1
;
k
<=
MAXSTEP
;
k
++
)
for
(
int
k
=
1
;
k
<=
MAXSTEP
;
k
++
)
{
{
dy
=
deriv
.
evaluate
(
x
,
y
,
paramvalues
);
dy
=
deriv
.
evaluate
(
x
,
y
,
paramvalues
);
// store results:
// store results:
xp
(
kout
)
=
x
;
xp
(
kout
)
=
x
;
yp
.
Row
(
kout
)
=
y
;
yp
.
Row
(
kout
)
=
y
;
hp
(
kout
)
=
h
;
hp
(
kout
)
=
h
;
kout
=
kout
+
1
;
kout
=
kout
+
1
;
// stop overshoot of step past x2:
// stop overshoot of step past x2:
if
((
x
+
h
-
x2
)
*
(
x
+
h
-
x1
)
>
0
)
h
=
x2
-
x
;
if
((
x
+
h
-
x2
)
*
(
x
+
h
-
x1
)
>
0
)
h
=
x2
-
x
;
...
@@ -150,7 +150,7 @@ void runge_kutta(Matrix& yp, ColumnVector& xp, ColumnVector& hp, const ColumnVec
...
@@ -150,7 +150,7 @@ void runge_kutta(Matrix& yp, ColumnVector& xp, ColumnVector& hp, const ColumnVec
yp
.
Row
(
kout
)
=
y
;
yp
.
Row
(
kout
)
=
y
;
hp
(
kout
)
=
h
;
hp
(
kout
)
=
h
;
//kout=kout+1;
//kout=kout+1;
xp
=
xp
.
Rows
(
1
,
kout
);
xp
=
xp
.
Rows
(
1
,
kout
);
yp
=
yp
.
Rows
(
1
,
kout
);
yp
=
yp
.
Rows
(
1
,
kout
);
...
@@ -160,8 +160,8 @@ void runge_kutta(Matrix& yp, ColumnVector& xp, ColumnVector& hp, const ColumnVec
...
@@ -160,8 +160,8 @@ void runge_kutta(Matrix& yp, ColumnVector& xp, ColumnVector& hp, const ColumnVec
{
{
if
(
hnext
<=
hmin
)
cerr
<<
"step size too small"
<<
endl
;
if
(
hnext
<=
hmin
)
cerr
<<
"step size too small"
<<
endl
;
h
=
hnext
;
h
=
hnext
;
}
}
}
}
cerr
<<
"too many steps"
<<
endl
;
cerr
<<
"too many steps"
<<
endl
;
}
}
...
...
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