DSDP
onemat.c
Go to the documentation of this file.
1#include "dsdpdatamat_impl.h"
2#include "dsdpsys.h"
8typedef struct {
9 double cnst;
10 char UPLQ;
11 int n;
12} cmat;
13
14static int ConstMatDestroy(void*);
15static int ConstMatView(void*);
16static int ConstMatVecVec(void*, double[], int, double *);
17static int ConstMatDot(void*, double[],int,int,double *);
18static int ConstMatGetRank(void*, int*, int);
19static int ConstMatFactor(void*);
20static int ConstMatGetEig(void*, int, double*, double[], int,int[],int*);
21static int ConstMatRowNnz(void*, int, int[], int*, int);
22static int ConstMatAddRowMultiple(void*, int, double, double[], int);
23static int ConstMatAddMultiple(void*, double, double[], int,int);
24static int ConstMatTest(void*);
25
26static struct DSDPDataMat_Ops constantmatops;
27static int ConstMatOpsInitialize(struct DSDPDataMat_Ops*);
28
29#undef __FUNCT__
30#define __FUNCT__ "DSDPGetConstantMat"
31int DSDPGetConstantMat(int n, double value, char UPLQ, struct DSDPDataMat_Ops**mops, void**mmat){
32 int info;
33 cmat*AA;
34 DSDPFunctionBegin;
35 AA=(cmat*) malloc(1*sizeof(cmat));
36 if (AA==NULL) return 1;
37 AA->cnst=value;
38 AA->n=n;
39 AA->UPLQ=UPLQ;
40 info=ConstMatOpsInitialize(&constantmatops); if(info){return 1;}
41 if (mops){*mops=&constantmatops;}
42 if (mmat){*mmat=(void*)AA;}
43 DSDPFunctionReturn(0);
44}
45
46
47static int ConstMatDot(void* A, double x[], int nn, int n, double *v){
48
49 cmat* AA = (cmat*)A;
50 double dtmp=0.0;
51 int i,j;
52
53 for (i=0;i<n;i++){
54 for (j=0;j<=i;j++){
55 dtmp+= (x[j]);
56 }
57 if (AA->UPLQ=='U'){
58 x=x+n;
59 } else {
60 x=x+i+1;
61 }
62 }
63
64 *v=2*dtmp*AA->cnst;
65 return 0;
66}
67
68static int ConstMatVecVec(void* A, double x[], int n, double *v){
69
70 cmat* AA = (cmat*)A;
71 double dtmp=0.0;
72 int i;
73
74 for (i=0; i<n; i++){
75 dtmp+=x[i];
76 }
77 *v=dtmp*dtmp*AA->cnst;
78 return 0;
79}
80
81static int ConstMatAddMultiple(void*A, double dd, double vv[], int nn, int n){
82 cmat* AA = (cmat*)A;
83 int i,j;
84 double ddd=dd*AA->cnst;
85 for (i=0;i<n;i++){
86 for (j=0;j<i;j++){
87 (vv[j])+=ddd;
88 }
89 vv[i]+=ddd;
90 if (AA->UPLQ=='U'){
91 vv=vv+n;
92 } else {
93 vv=vv+i+1;
94 }
95 }
96 return 0;
97}
98
99static int ConstMatAddRowMultiple(void*A, int nrow, double dd, double row[], int n){
100 cmat* AA = (cmat*)A;
101 int i;
102 double ddd=dd*AA->cnst;
103 for (i=0;i<n;i++){
104 row[i] += ddd;
105 }
106 row[nrow] -= ddd;
107 return 0;
108}
109
110
111
112static int ConstMatFactor(void*A){
113 return 0;
114}
115
116static int ConstMatGetRank(void *A, int*rank, int n){
117 *rank=1;
118 return 0;
119}
120
121static int ConstMatGetEig(void*A, int neig, double *eig, double v[], int n, int indx[], int*nind){
122 cmat* AA = (cmat*)A;
123 int i;
124 if (neig!=0) return 1;
125 if (neig==0){
126 for (i=0;i<n;i++){ v[i]=1.0; indx[i]=i;}
127 *eig=AA->cnst; *nind=n;
128 } else { /* Or return an error */
129 for (i=0;i<n;i++){ v[i]=0.0; }
130 *eig=0; *nind=0;
131 }
132 return 0;
133}
134
135
136static int ConstMatRowNnz(void*A, int row, int nz[], int *nnz, int n){
137 int i;
138 for (i=0;i<n;i++){ nz[i]++; }
139 *nnz=n;
140 return 0;
141}
142
143static int ConstMatFNorm2(void*AA, int n, double *fnorm2){
144 cmat* A = (cmat*)AA;
145 *fnorm2=A->cnst*A->cnst*n*n;
146 return 0;
147}
148
149static int ConstMatCountNonzeros(void*A, int *nnz, int n){
150 *nnz=n*n;
151 *nnz=1;
152 *nnz=n;
153 return 0;
154}
155
156
157static int ConstMatView(void* AA){
158 cmat* A = (cmat*)AA;
159 printf("Every element of the matrix is the same: %10.8e\n",A->cnst);
160 return 0;
161}
162
163static int ConstMatTest(void* AA){
164 return 0;
165}
166
167
168static int ConstMatDestroy(void* A){
169 if (A) free(A);
170 return 0;
171}
172
173static const char *datamatname="ALL ELEMENTS THE SAME";
174static int ConstMatOpsInitialize(struct DSDPDataMat_Ops* cmatops){
175 int info;
176 if (cmatops==NULL) return 0;
177 info=DSDPDataMatOpsInitialize(cmatops); DSDPCHKERR(info);
178 cmatops->matfactor1=ConstMatFactor;
179 cmatops->matgetrank=ConstMatGetRank;
180 cmatops->matgeteig=ConstMatGetEig;
181 cmatops->matvecvec=ConstMatVecVec;
182 cmatops->matdot=ConstMatDot;
183 cmatops->mataddrowmultiple=ConstMatAddRowMultiple;
184 cmatops->mataddallmultiple=ConstMatAddMultiple;
185 cmatops->matdestroy=ConstMatDestroy;
186 cmatops->mattest=ConstMatTest;
187 cmatops->matview=ConstMatView;
188 cmatops->matrownz=ConstMatRowNnz;
189 cmatops->matfnorm2=ConstMatFNorm2;
190 cmatops->matnnz=ConstMatCountNonzeros;
191 cmatops->id=14;
192 cmatops->matname=datamatname;
193 return 0;
194}
195
int DSDPDataMatOpsInitialize(struct DSDPDataMat_Ops *dops)
Initialize the table of function pointers for SDP Data matrices.
Definition dsdpdatamat.c:47
Structure of function pointers that each SDP data matrix type (sparse, dense, constant,...
Error handling, printing, and profiling.
Table of function pointers that operate on the data matrix.