48 if ((_extLine>0)&&(_extColumn>0))
51 t=
d->
p=(
double**)malloc(_extLine*
sizeof(
double*));
52 t2=(
double*)malloc(_extColumn*_extLine*
sizeof(
double));
55 *(t++)=t2; t2+=_extColumn;
62 init(_nLine,_nColumn,_nLine, _nColumn);
70 while (n--) { *p=dd; p+=
i; }
78 memcpy(*
d->
p,a,a.
sz()*
sizeof(double));
83 int nl=a.
sz(), nc=b.
sz(),
i,
j;
94 init(_nLine,_nColumn,_extLine,_extColumn);
102 FILE *
f=fopen(filename,
"rb");
105 printf(
"file not found.\n"); exit(255);
110 char *r=fgets(line,30000,f);
111 if (r==NULL) {
init(0,0,0,0);
return; }
114 printf(
"not a ASCII matrix.\n"); exit(255);
116 if (fgets(line,30000,f) != line) {
117 std::cerr <<
"error while reading " << filename << std::endl; exit(255);
120 if (fgets(line,30000,f) != line) {
121 std::cerr <<
"error while reading " << filename << std::endl; exit(255);
124 init(_nLine,_nColumn,_nLine,_nColumn);
125 if (fgets(line,30000,f) != line) {
126 std::cerr <<
"error while reading " << filename << std::endl; exit(255);
131 for (i=0; i<_nLine; i++)
133 if (fgets(line,30000,f) != line) {
134 std::cerr <<
"error while reading " << filename << std::endl; exit(255);
141 if (fread(c, 13,
sizeof(
char), f)==0) {
init(0,0,0,0);
return; }
144 printf(
"not a binary matrix.\n"); exit(255);
146 if (fread(&_nLine,
sizeof(
unsigned), 1, f) !=
sizeof(
unsigned)) {
147 std::cerr <<
"error while reading " << filename << std::endl; exit(255);
149 if (fread(&_nColumn,
sizeof(
unsigned), 1, f) !=
sizeof(
unsigned)) {
150 std::cerr <<
"error while reading " << filename << std::endl; exit(255);
152 init(_nLine,_nColumn,_nLine,_nColumn);
154 if (fread(&i,
sizeof(
int), 1, f) !=
sizeof(
int)) {
155 std::cerr <<
"error while reading " << filename << std::endl; exit(255);
159 char **names=(
char**)malloc(_nColumn*
sizeof(
char**)),
161 if (fread(
n,i,1,f) != i) {
162 std::cerr <<
"error while reading " << filename << std::endl; exit(255);
164 for (j=0;j<_nColumn-1;j++)
175 if (fread(*
d->
p,
sizeof(
double)*noOfLines,1,f) !=
sizeof(
double)*noOfLines){
176 std::cerr <<
"error while reading " << filename << std::endl; exit(255);
223 double *tmp,*tmp2,**tmp3=
d->
p,*oldBuffer=*tmp3;
226 tmp3=
d->
p=(
double**)realloc(tmp3,_extLine*
sizeof(
double*));
229 tmp2=tmp=(
double *)malloc(_extLine*_extColumn*
sizeof(
double));
232 printf(
"memory allocation error");
233 getchar(); exit(255);
250 memmove(tmp,tmp2,nc);
264 tmp=(
double *)realloc(*
d->
p,_extLine*ec*
sizeof(
double));
267 printf(
"memory allocation error");
268 getchar(); exit(255);
271 tmp3=
d->
p=(
double **)malloc(_extLine*
sizeof(
double*));
287 f=fopen(filename,
"w");
290 printf(
"cannot save ascii Matrix into file '%s'.\n",filename);
295 f=fopen(filename,
"wb");
298 printf(
"cannot save binary Matrix into file '%s'.\n",filename);
308 char cc[32]=
"CONDORMBv1.0";
323 for (i=0; i<
d->
nLine; i++)
331 fwrite(cc,
sizeof(
char), 13, f);
332 fwrite(&
d->
nLine,
sizeof(
unsigned), 1, f);
333 fwrite(&
d->
nColumn,
sizeof(
unsigned), 1, f);
337 fwrite(&j,
sizeof(
int), 1, f);
342 j=0; fwrite(&j,
sizeof(
int), 1, f);
344 for (i=0; i<
d->
nLine; i++)
345 fwrite(p[i],
sizeof(
double)*
d->
nColumn,1,f);
352 FILE *
f=fopen(saveFileName,
"rb+");
355 save(saveFileName,0);
362 save(saveFileName,0);
366 fseek(f,13,SEEK_SET);
367 if (fread(&nlfile,
sizeof(
int),1,f) !=
sizeof(
int)) {
368 std::cerr <<
"error while updating file " << saveFileName << std::endl; exit(255);
370 fseek(f,13,SEEK_SET);
371 fwrite(&
d->
nLine,
sizeof(
unsigned), 1, f);
374 for (
i=nlfile;
i<nl;
i++)
375 fwrite(p[
i],
sizeof(
double)*nc,1,f);
382 double *tmp,*tmp2,**tmp3;
384 if ((nc==ec)&&(nl==el))
return;
392 memmove(tmp,tmp2,nc*
sizeof(
double));
398 tmp=(
double *)realloc(*
d->
p,nl*nc*
sizeof(
double));
401 printf(
"memory allocation error");
402 getchar(); exit(255);
406 tmp3=
d->
p=(
double **)realloc(
d->
p,nl*
sizeof(
double*));
413 }
else d->
p=(
double **)realloc(
d->
p,nl*
sizeof(
double*));
424 for (i=0; i<
d->
nLine; i++)
427 if (p[i][j]>=0.0) printf(
" %2.3f ",p[i][j]);
428 else printf(
"%2.3f ",p[i][j]);
429 if (i==
d->
nLine-1) printf(
"]\n");
else printf(
";\n");
483 printf(
"Matrix: copyFrom: size do not agree");
484 getchar(); exit(254);
488 memcpy(*
d->
p,*m.
d->
p,nc*nl*
sizeof(
double));
491 double *pD=*
d->
p,*pS=*m.
d->
p;
494 memcpy(pD,pS,nc*
sizeof(
double));
505 double **p=(*this),t;
517 double **sp=temp, **dp=(*this);
522 while (j--) dp[
j][
i]=sp[
i][
j];
530 double **sp=temp, **dp=(*this);
535 while (j--) sp[
j][
i]=dp[
i][
j];
563 printf(
"(matrix * matrix_diagonal) error");
564 getchar(); exit(249);
566 double **p1=(*this),*p2=v;
577 printf(
"(matrix * matrix) error");
578 getchar(); exit(249);
582 double sum,**p1=(*this),**p2=Bplus,**pr=R;
588 for (k=0; k<
n; k++) sum+=p1[i][k]*p2[k][j];
597 printf(
"(matrix^t * matrix) error");
598 getchar(); exit(249);
602 double sum,**p1=(*this),**p2=Bplus,**pr=R;
608 for (k=0; k<
n; k++) sum+=p1[k][i]*p2[k][j];
617 printf(
"(matrix * matrix^t) error");
618 getchar(); exit(249);
622 double sum,**p1=(*this),**p2=Bplus,**pr=R;
628 for (k=0; k<
n; k++) sum+=p1[i][k]*p2[j][k];
656 printf(
"matrix multiply error");
657 getchar(); exit(250);
659 double **p=(*this), *
x=v, *r=rv, sum;
664 while (j--) sum+=p[
i][
j]*x[
j];
675 printf(
"matrix multiply error");
676 getchar(); exit(250);
678 double **p=(*this), *
x=v, *r=rv, sum;
683 while (j--) sum+=p[
j][
i]*x[
j];
697 double *x1=v, *x2=
d->
p[nl], sum=0;
699 while (n--) { sum+=*(x1++) * *(x2++); };
708 printf(
"matrix addition error");
709 getchar(); exit(250);
713 double **p1=(*this),**p2=B;
725 printf(
"matrix addition error");
726 getchar(); exit(250);
730 double **p1=(*this),**p2=B;
734 p1[i][j]+=d*p2[i][j];
739 #ifndef NOMATRIXTRIANGLE 746 double **pD=(*this), **pS=A;
752 if (j>=
i) pD[
i][
j]=pS[
j][
i];
758 if (j<=
i) pD[
i][
j]=pS[
i][
j];
770 double **A=(*this), **L_=matL;
771 if (lambdaCorrection) *lambdaCorrection=0;
776 while ( k-- ) s2-=
sqr(L_[i][k]);
779 if (lambdaCorrection)
789 for (k=i+1; k<
n; k++) sum-=L_[k][i]*x[k];
798 for (j=i+1; j<
n; j++)
801 while (k--) s-=L_[
j][
k]*L_[
i][
k];
898 int i,
j,
k,kmax,minmn;
899 double ajnorm,sum,temp;
902 const double epsmch = 1e-20;
907 printf(
"QR factorisation of A^t is currently not possible when number of lines is greater than number of columns.\n");
908 getchar(); exit(255);
913 double *wa=vWA, *rdiag, **
a=*
this;
929 if (pivot) ipvt[
j]=
j;
935 for (j=0; j<minmn; j++)
943 for (k=j+1; k<nl; k++)
944 if (rdiag[k]>rdiag[kmax]) kmax=
k;
951 a[
j][
i] = a[kmax][
i];
954 rdiag[kmax] = rdiag[
j];
957 ipvt[
j] = ipvt[kmax];
969 if (ajnorm==0.0) { rdiag[
j]=0.0;
continue; }
970 if (a[j][j]<0.0) ajnorm = -ajnorm;
971 for (i=j; i<nc; i++) a[j][i]=a[j][i]/ajnorm;
978 if (j>=nc) { rdiag[
j] = -ajnorm;
continue; }
980 for (k = j+1; k<nl; k++)
983 for (i=j; i<nc; i++) sum=sum+a[j][i]*a[k][i];
986 for (i=j; i<nc; i++) a[k][i]=a[k][i]-temp*a[j][i];
988 if ((!pivot)||(rdiag[k]==0.0))
continue;
990 temp = a[
k][
j]/rdiag[
k];
991 rdiag[
k] *=
sqrt(
mmax(0.0,1.0-temp*temp));
993 if (0.05*
sqr(rdiag[k]/wa[k])> epsmch)
continue;
1004 if (!(Rt==MatrixTriangle::emptyMatrixTriangle))
1008 for (i=0; i<minmn; i++)
1011 for (j=i+1; j<minmn; j++)
1016 if (!(Q==Matrix::emptyMatrix))
1021 for (j=nl-1; j>=0; j--)
1023 if (a[j][j]==0.0)
continue;
1024 for (k=j; k<nc; k++)
1027 for (i=j; i<nc; i++) sum=sum+a[j][i]*q[i][k];
1030 for (i=j; i<nc; i++) q[i][k]=q[i][k]-temp*a[j][i];
1042 while (
i--) { (*a)+=dd; a+=
nn; };
1063 double **
a=(*this), *xp;
1066 sum=0; j=nc; xp=*(a++);
1075 double **
a=(*this), sum, maxSum=0;
1080 while(
j--) sum+=
sqr(a[
j][i]);
1090 while (
j--) r[
j]=a[
j][
k];
1105 memcpy((
double*)r,
d->
p[i]+startc, n*
sizeof(
double));
1110 if (n==0) n=
nLine();
1112 double **
d=*
this, *rr=r;
1113 while (n--) rr[
n]=d[
n][
i];
1119 if (n==0) n=
nLine();
1121 double **
d=*
this, *rr=r;
1122 while (n--) rr[
n]=d[
n][
i];
1128 memcpy(
d->
p[i], (
double*)v, n*
sizeof(
double));
1133 if (!Source.
nLine())
return;
1134 double **dest=(*this), **sour=Source;
1136 if (number==0) number=Source.
nLine()-indexSource;
1137 while (number--) memcpy(dest[indexDest+number], sour[indexSource+number], snl);
1150 double **sd=(*this), **dd=R;
1152 memcpy(dd[nl], sd[nl+startL]+startC, nc*
sizeof(
double));
1159 double *p1=
d->
p[
i], *p2=
d->
p[
j], t;
1170 if (nn==0) nn=
mmin((
int)
nColumn(),(
int)r.
sz())*
sizeof(
double);
1171 else nn*=
sizeof(double);
1174 double **dp=
d->
p, *dp2=r;
1176 if (memcmp(dp[i],dp2,nn)==0)
break;
1193 for (
i=0;
i<nc;
i++) l+=(
int)strlen(c[
i])+1;
1194 char *t1=(
char*)malloc(l);
1195 for (i=0; i<nc; i++)
1199 t1+=(int)strlen(t1)+1;
1209 printf(
"Merging: Size do not agree.\n");
1215 for (
i=0;
i<nlm;
i++)
1217 pdi=((
double**)m)[
i];
1218 for (j=0; j<nl; j++)
1219 if (memcmp(
d->
p[j],pdi,nc)==0)
break;
1220 if (j!=nl)
continue;
1222 memcpy(
d->
p[nl],pdi,nc);
double scalarProduct(int nl, Vector v)
void setColNames(char **c, int nc=0)
void merge(Matrix m, int eliminateDoubles=1)
void sqrt(Image< double > &op)
void QR(Matrix Q=Matrix::emptyMatrix, MatrixTriangle R=MatrixTriangle::emptyMatrixTriangle, VectorInt permutation=VectorInt::emptyVectorInt)
Matrix multiply(Matrix B)
void swapLines(int i, int j)
void addInPlace(Matrix B)
Matrix & operator=(const Matrix &A)
void setSize(int _nLine, int _nColumn)
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
char * getFromLine(char *line)
void solveTransposInPlace(Vector y)
int lineIndex(Vector r, int nn=0)
void setNColumn(int _nColumn)
void getSubMatrix(Matrix R, int startL, int StartC, int nl=0, int nc=0)
bool cholesky(MatrixTriangle matL, double lambda=0, double *lambdaCorrection=NULL)
void solveInPlace(Vector b)
void multiplyInPlace(double d)
double euclidianNorm(int i)
static MatrixTriangle emptyMatrixTriangle
Vector getColumn(int i, int n=0)
Matrix(int _ligne=0, int _nColumn=0)
void setExtSize(int _extLine, int _extColumn)
void setLines(int indexDest, Matrix Source, int indexSource=0, int number=0)
void init(int _nLine, int _nColumn, int _extLine, int _extColumn, MatrixData *d=NULL)
void save(char *filename, char ascii)
Vector getLine(int i, int n=0, int startCol=0)
void addUnityInPlace(double d)
void setLine(int i, Vector v, int n=0)
static VectorInt emptyVectorInt
fprintf(glob_prnt.io, "\)
void multiplyByTranspose(Matrix R, Matrix a)
void transposeAndMultiply(Vector R, Vector a)
void setNLine(int _nLine)
static Matrix emptyMatrix
void addMultiplyInPlace(double d, Matrix B)
void choleskySolveInPlace(Vector b)
void destroyCurrentBuffer()
void updateSave(char *saveFileName)
void multiplyByDiagonalMatrix(Vector v)