Xmipp
v3.23.11-Nereus
xmipp
external
condor
MatrixTriangle.cpp
Go to the documentation of this file.
1
/*
2
3
CONDOR 1.06 - COnstrained, Non-linear, Direct, parallel Optimization
4
using trust Region method for high-computing load,
5
noisy functions
6
Copyright (C) 2004 Frank Vanden Berghen
7
8
This program is free software; you can redistribute it and/or
9
modify it under the terms of the GNU General Public License
10
as published by the Free Software Foundation version 2
11
of the License.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22
If you want to include this tools in any commercial product,
23
you can contact the author at fvandenb@iridia.ulb.ac.be
24
25
*/
26
27
#include <stdio.h>
28
#include <memory.h>
29
#include "
MatrixTriangle.h
"
30
31
MatrixTriangle::MatrixTriangle
(
int
_n)
32
{
33
d
=(
MatrixTriangleData
*)malloc(
sizeof
(
MatrixTriangleData
));
34
d
->
n
=_n;
d
->
ext
=_n;
35
d
->
ref_count
=1;
36
if
(_n>0)
37
{
38
double
**t,*t2;
39
int
i
=1;
40
t=
d
->
p
=(
double
**)malloc(_n*
sizeof
(
double
*));
41
t2=(
double
*)malloc((_n+1)*_n/2*
sizeof
(double));
42
while
(_n--)
43
{
44
*(t++)=t2; t2+=
i
; i++;
45
}
46
}
else
d
->
p
=NULL;
47
}
48
49
void
MatrixTriangle::setSize
(
int
_n)
50
{
51
d
->
n
=_n;
52
if
(_n>
d
->
ext
)
53
{
54
d
->
ext
=_n;
55
double
**t,*t2;
56
if
(!
d
->
p
)
57
{
58
t2=(
double
*)malloc((_n+1)*_n/2*
sizeof
(double));
59
t=
d
->
p
=(
double
**)malloc(_n*
sizeof
(
double
));
60
}
else
61
{
62
t2=(
double
*)realloc(*
d
->
p
,(_n+1)*_n/2*
sizeof
(double));
63
t=
d
->
p
=(
double
**)realloc(
d
->
p
,_n*
sizeof
(
double
));
64
}
65
int
i
=1;
66
while
(_n--)
67
{
68
*(t++)=t2; t2+=
i
; i++;
69
}
70
}
71
}
72
73
void
MatrixTriangle::solveInPlace
(
Vector
b
)
74
{
75
int
i
,
k
,
n
=
nLine
();
76
double
**
a
=(*this), *
x
=
b
, sum;
77
78
if
((
int
)b.
sz
()!=
n
)
79
{
80
printf(
"error in matrixtriangle solve.\n"
); getchar(); exit(254);
81
}
82
for
(i=0; i<
n
; i++)
83
{
84
sum=x[
i
]; k=
i
;
85
while
(k--) sum-=a[
i
][
k
]*x[
k
];
86
x[
i
]=sum/a[
i
][
i
];
87
}
88
}
89
90
void
MatrixTriangle::solveTransposInPlace
(
Vector
y
)
91
{
92
int
n
=
nLine
(),
i
=
n
,
k
;
93
double
**
a
=(*this), *
x
=
y
, sum;
94
95
while
(
i
--)
96
{
97
sum=x[
i
];
98
for
(k=
i
+1; k<
n
; k++) sum-=a[k][
i
]*x[k];
99
x[
i
]=sum/a[
i
][
i
];
100
}
101
}
102
/*
103
void MatrixTriangle::invert()
104
{
105
int i,j,k,n=nLine();
106
double **a=(*this), sum;
107
for (i=0; i<n; i++)
108
{
109
a[i][i]=1/a[i][i];
110
for (j=i+1; j<n; j++)
111
{
112
sum=0;
113
for (k=i; k<j; k++) sum-=a[j][k]*a[k][i];
114
a[j][i]=sum/a[j][j];
115
}
116
}
117
}
118
*/
119
void
MatrixTriangle::LINPACK
(
Vector
&R)
120
{
121
int
i
,
j
,
n
=
nLine
();
122
R.
setSize
(n);
123
double
**L=(*this), *
w
=R, sum;
124
125
for
(i=0; i<
n
; i++)
126
{
127
if
(L[i][i]==0) w[
i
]=1.0;
128
129
sum=0; j=i-1;
130
if
(i)
while
(j--) sum+=L[
i
][
j
]*w[
j
];
131
if
(((1.0-sum)/L[i][i])>((-1.0-sum)/L[i][i])) w[
i
]=1.0;
else
w[
i
]=-1.0;
132
}
133
solveTransposInPlace
(R);
134
R.
multiply
(1/R.
euclidianNorm
());
135
}
136
137
MatrixTriangle::~MatrixTriangle
()
138
{
139
destroyCurrentBuffer
();
140
}
141
142
void
MatrixTriangle::destroyCurrentBuffer
()
143
{
144
if
(!
d
)
return
;
145
(
d
->
ref_count
) --;
146
if
(
d
->
ref_count
==0)
147
{
148
if
(
d
->
p
) {
free
(*
d
->
p
);
free
(
d
->
p
); }
149
free
(
d
);
150
};
151
}
152
153
MatrixTriangle::MatrixTriangle
(
const
MatrixTriangle
&A)
154
{
155
// shallow copy
156
d
=A.
d
;
157
(
d
->
ref_count
)++ ;
158
}
159
160
MatrixTriangle
&
MatrixTriangle::operator=
(
const
MatrixTriangle
& A )
161
{
162
// shallow copy
163
if
(
this
!= &A)
164
{
165
destroyCurrentBuffer
();
166
d
=A.
d
;
167
(
d
->
ref_count
) ++ ;
168
}
169
return
*
this
;
170
}
171
172
MatrixTriangle
MatrixTriangle::clone
()
173
{
174
// a deep copy
175
MatrixTriangle
r(
nLine
());
176
r.
copyFrom
(*
this
);
177
return
r;
178
}
179
180
void
MatrixTriangle::copyFrom
(
MatrixTriangle
r)
181
{
182
int
n
=r.
nLine
();
183
setSize
(n);
184
if
(n==0)
return
;
185
memcpy(*
d
->
p
,*(r.
d
->
p
),(n+1)*n/2*
sizeof
(
double
));
186
}
187
MatrixTriangle::nLine
int nLine()
Definition:
MatrixTriangle.h:63
MatrixTriangle::d
MatrixTriangleData * d
Definition:
MatrixTriangle.h:46
MatrixTriangle::copyFrom
void copyFrom(MatrixTriangle r)
Definition:
MatrixTriangle.cpp:180
MatrixTriangle::MatrixTriangleDataTag::p
double ** p
Definition:
MatrixTriangle.h:44
MatrixTriangle.h
Vector
Definition:
Vector.h:37
y
static double * y
Definition:
numerical_recipes.cpp:8487
Vector::euclidianNorm
double euclidianNorm()
Definition:
Vector.cpp:222
w
doublereal * w
Definition:
numerical_recipes.cpp:2477
MatrixTriangle::operator=
MatrixTriangle & operator=(const MatrixTriangle &A)
Definition:
MatrixTriangle.cpp:160
MatrixTriangle::clone
MatrixTriangle clone()
Definition:
MatrixTriangle.cpp:172
MatrixTriangle::MatrixTriangleDataTag
Definition:
MatrixTriangle.h:39
x
doublereal * x
Definition:
numerical_recipes.cpp:2230
i
#define i
Definition:
numerical_recipes.cpp:2493
k
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
Vector::setSize
void setSize(int _n)
Definition:
Vector.cpp:112
MatrixTriangle::solveTransposInPlace
void solveTransposInPlace(Vector y)
Definition:
MatrixTriangle.cpp:90
Vector::sz
unsigned sz()
Definition:
Vector.h:79
b
doublereal * b
Definition:
numerical_recipes.cpp:2230
MatrixTriangle
Definition:
MatrixTriangle.h:34
MatrixTriangle::MatrixTriangle
MatrixTriangle(int _n=0)
Definition:
MatrixTriangle.cpp:31
MatrixTriangle::solveInPlace
void solveInPlace(Vector b)
Definition:
MatrixTriangle.cpp:73
MatrixTriangle::MatrixTriangleDataTag::ref_count
int ref_count
Definition:
MatrixTriangle.h:43
free
free((char *) ob)
Vector::multiply
void multiply(double a)
Definition:
Vector.cpp:281
MatrixTriangle::~MatrixTriangle
~MatrixTriangle()
Definition:
MatrixTriangle.cpp:137
j
#define j
Definition:
numerical_recipes.cpp:2493
MatrixTriangle::MatrixTriangleDataTag::ext
int ext
Definition:
MatrixTriangle.h:42
MatrixTriangle::LINPACK
void LINPACK(Vector &u)
Definition:
MatrixTriangle.cpp:119
MatrixTriangle::MatrixTriangleDataTag::n
int n
Definition:
MatrixTriangle.h:41
MatrixTriangle::destroyCurrentBuffer
void destroyCurrentBuffer()
Definition:
MatrixTriangle.cpp:142
n
int * n
Definition:
numerical_recipes.cpp:2229
a
doublereal * a
Definition:
numerical_recipes.cpp:2230
MatrixTriangle::setSize
void setSize(int _n)
Definition:
MatrixTriangle.cpp:49
Generated by
1.8.13