onemat.c

Go to the documentation of this file.
00001 #include "dsdpdatamat_impl.h"
00002 #include "dsdpsys.h"
00008 typedef struct {
00009   double cnst;
00010   char UPLQ;
00011   int n;
00012 } cmat;
00013 
00014 static int ConstMatDestroy(void*);
00015 static int ConstMatView(void*);
00016 static int ConstMatVecVec(void*, double[], int, double *);
00017 static int ConstMatDot(void*, double[],int,int,double *);
00018 static int ConstMatGetRank(void*, int*, int);
00019 static int ConstMatFactor(void*);
00020 static int ConstMatGetEig(void*, int, double*, double[], int,int[],int*);
00021 static int ConstMatRowNnz(void*, int, int[], int*, int);
00022 static int ConstMatAddRowMultiple(void*, int, double, double[], int);
00023 static int ConstMatAddMultiple(void*, double, double[], int,int);
00024 static int ConstMatTest(void*);
00025 
00026 static struct  DSDPDataMat_Ops constantmatops;
00027 static int ConstMatOpsInitialize(struct  DSDPDataMat_Ops*);
00028 
00029 #undef __FUNCT__  
00030 #define __FUNCT__ "DSDPGetConstantMat"
00031 int DSDPGetConstantMat(int n, double value, char UPLQ, struct  DSDPDataMat_Ops**mops, void**mmat){ 
00032   int info;
00033   cmat*AA;
00034   DSDPFunctionBegin;
00035   AA=(cmat*) malloc(1*sizeof(cmat));
00036   if (AA==NULL) return 1;
00037   AA->cnst=value;
00038   AA->n=n;
00039   AA->UPLQ=UPLQ;
00040   info=ConstMatOpsInitialize(&constantmatops); if(info){return 1;}
00041   if (mops){*mops=&constantmatops;}
00042   if (mmat){*mmat=(void*)AA;}
00043   DSDPFunctionReturn(0);
00044 }
00045 
00046 
00047 static int ConstMatDot(void* A, double x[], int nn, int n, double *v){
00048 
00049   cmat* AA = (cmat*)A;
00050   double dtmp=0.0;
00051   int i,j;
00052 
00053   for (i=0;i<n;i++){
00054     for (j=0;j<=i;j++){
00055       dtmp+= (x[j]);
00056     }
00057     if (AA->UPLQ=='U'){
00058       x=x+n;
00059     } else {
00060       x=x+i+1;
00061     }
00062   }
00063 
00064   *v=2*dtmp*AA->cnst;
00065   return 0;
00066 }
00067 
00068 static int ConstMatVecVec(void* A, double x[], int n, double *v){
00069 
00070   cmat* AA = (cmat*)A;
00071   double dtmp=0.0;
00072   int i;
00073 
00074   for (i=0; i<n; i++){
00075     dtmp+=x[i];
00076   }
00077   *v=dtmp*dtmp*AA->cnst;
00078   return 0;
00079 }
00080 
00081 static int ConstMatAddMultiple(void*A, double dd, double vv[], int nn, int n){
00082   cmat* AA = (cmat*)A;
00083   int i,j;
00084   double ddd=dd*AA->cnst;
00085   for (i=0;i<n;i++){
00086     for (j=0;j<i;j++){
00087       (vv[j])+=ddd;
00088     }
00089     vv[i]+=ddd;
00090     if (AA->UPLQ=='U'){
00091       vv=vv+n;
00092     } else {
00093       vv=vv+i+1;
00094     }
00095   }
00096   return 0;
00097 }
00098 
00099 static int ConstMatAddRowMultiple(void*A, int nrow, double dd, double row[], int n){
00100   cmat* AA = (cmat*)A;
00101   int i;
00102   double ddd=dd*AA->cnst;
00103   for (i=0;i<n;i++){
00104     row[i] += ddd;
00105   }
00106   row[nrow] -= ddd;
00107  return 0;
00108 }
00109 
00110 
00111 
00112 static int ConstMatFactor(void*A){
00113   return 0;
00114 }
00115 
00116 static int ConstMatGetRank(void *A, int*rank, int n){
00117   *rank=1;
00118   return 0;
00119 }
00120 
00121 static int ConstMatGetEig(void*A, int neig, double *eig, double v[], int n, int  indx[], int*nind){
00122   cmat* AA = (cmat*)A;
00123   int i;
00124   if (neig!=0) return 1;
00125   if (neig==0){
00126     for (i=0;i<n;i++){ v[i]=1.0; indx[i]=i;}
00127     *eig=AA->cnst; *nind=n;
00128   } else {  /* Or return an error */
00129     for (i=0;i<n;i++){ v[i]=0.0; }
00130     *eig=0; *nind=0;
00131   }
00132   return 0;
00133 }
00134 
00135 
00136 static int ConstMatRowNnz(void*A, int row, int nz[], int *nnz, int n){
00137   int i;
00138   for (i=0;i<n;i++){ nz[i]++; }
00139   *nnz=n;
00140   return 0;
00141 }
00142 
00143 static int ConstMatFNorm2(void*AA, int n, double *fnorm2){
00144   cmat* A = (cmat*)AA;
00145   *fnorm2=A->cnst*A->cnst*n*n;
00146   return 0;
00147 }
00148 
00149 static int ConstMatCountNonzeros(void*A, int *nnz, int n){
00150   *nnz=n*n;
00151   *nnz=1;
00152   *nnz=n;
00153   return 0;
00154 }
00155 
00156 
00157 static int ConstMatView(void* AA){
00158   cmat* A = (cmat*)AA;
00159   printf("Every element of the matrix is the same: %10.8e\n",A->cnst);
00160   return 0;
00161 }
00162 
00163 static int ConstMatTest(void* AA){
00164   return 0;
00165 }
00166 
00167 
00168 static int ConstMatDestroy(void* A){
00169   if (A) free(A);
00170   return 0;
00171 }
00172 
00173 static const char *datamatname="ALL ELEMENTS THE SAME";
00174 static int ConstMatOpsInitialize(struct  DSDPDataMat_Ops* cmatops){
00175   int info;
00176   if (cmatops==NULL) return 0;
00177   info=DSDPDataMatOpsInitialize(cmatops); DSDPCHKERR(info);
00178   cmatops->matfactor1=ConstMatFactor;
00179   cmatops->matgetrank=ConstMatGetRank;
00180   cmatops->matgeteig=ConstMatGetEig;
00181   cmatops->matvecvec=ConstMatVecVec;
00182   cmatops->matdot=ConstMatDot;
00183   cmatops->mataddrowmultiple=ConstMatAddRowMultiple;
00184   cmatops->mataddallmultiple=ConstMatAddMultiple;
00185   cmatops->matdestroy=ConstMatDestroy;
00186   cmatops->mattest=ConstMatTest;
00187   cmatops->matview=ConstMatView;
00188   cmatops->matrownz=ConstMatRowNnz;
00189   cmatops->matfnorm2=ConstMatFNorm2;
00190   cmatops->matnnz=ConstMatCountNonzeros;
00191   cmatops->id=14;
00192   cmatops->matname=datamatname;
00193   return 0;
00194 }
00195 

Generated on Sun Mar 23 07:30:49 2008 for DSDP by  doxygen 1.5.5