Xmipp  v3.23.11-Nereus
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 
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 
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 
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 
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 */
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  }
134  R.multiply(1/R.euclidianNorm());
135 }
136 
138 {
140 }
141 
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 
154 {
155  // shallow copy
156  d=A.d;
157  (d->ref_count)++ ;
158 }
159 
161 {
162  // shallow copy
163  if (this != &A)
164  {
166  d=A.d;
167  (d->ref_count) ++ ;
168  }
169  return *this;
170 }
171 
173 {
174  // a deep copy
175  MatrixTriangle r(nLine());
176  r.copyFrom(*this);
177  return r;
178 }
179 
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 
MatrixTriangleData * d
void copyFrom(MatrixTriangle r)
Definition: Vector.h:37
static double * y
double euclidianNorm()
Definition: Vector.cpp:222
doublereal * w
MatrixTriangle & operator=(const MatrixTriangle &A)
MatrixTriangle clone()
doublereal * x
#define i
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
void setSize(int _n)
Definition: Vector.cpp:112
void solveTransposInPlace(Vector y)
unsigned sz()
Definition: Vector.h:79
doublereal * b
MatrixTriangle(int _n=0)
void solveInPlace(Vector b)
free((char *) ob)
void multiply(double a)
Definition: Vector.cpp:281
#define j
void LINPACK(Vector &u)
void destroyCurrentBuffer()
int * n
doublereal * a
void setSize(int _n)