forked from sqlp/sedumi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractA.c
190 lines (178 loc) · 7.14 KB
/
extractA.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* ************************************************************
% Apart = extractA(At,Ajc,blk0,blk1,blkstart[,blkstart2])
% EXTRACTA Fast alternative to
% Apart = At(blkstart(1):blkstart(2)-1,:).
% Instead of blkstart(2), it takes "blkstart2" (if supplied) or
% size(At,1)+1 (if neither blkstart(2) nor blkstart2) are available.
%
% Extract submatrix of
% A with subscripts "blkstart(1):blkstart(2)-1" and has its nonzeros per column
% bounded bij Ajc1:Ajc2. If Ajc1 = [] (Ajc2=[]) then start at column start
% (end at column end) of A.
%
% SEE ALSO partitA.
% ******************** INTERNAL FUNCTION OF SEDUMI ********************
function Apart = extractA(At,Ajc,blk0,blk1,blkstart[,blkstart2]) --
% This file is part of SeDuMi 1.1 by Imre Polik and Oleksandr Romanko
% Copyright (C) 2005 McMaster University, Hamilton, CANADA (since 1.1)
%
% Copyright (C) 2001 Jos F. Sturm (up to 1.05R5)
% Dept. Econometrics & O.R., Tilburg University, the Netherlands.
% Supported by the Netherlands Organization for Scientific Research (NWO).
%
% Affiliation SeDuMi 1.03 and 1.04Beta (2000):
% Dept. Quantitative Economics, Maastricht University, the Netherlands.
%
% Affiliations up to SeDuMi 1.02 (AUG1998):
% CRL, McMaster University, Canada.
% Supported by the Netherlands Organization for Scientific Research (NWO).
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
% 02110-1301, USA
************************************************************ */
#include <string.h>
#include "mex.h"
#include "blksdp.h"
#define APART_OUT plhs[0]
#define NPAROUT 1
#define AT_IN prhs[0]
#define AJC_IN prhs[1]
#define BLK0_IN prhs[2]
#define BLK1_IN prhs[3]
#define BLKSTART_IN prhs[4]
#define NPARINMIN 5
#define BLKSTART2_IN prhs[5]
#define NPARIN 6
/* ************************************************************
PROCEDURE extractA
INPUT
Ajc1, Ajc2, Air,Apr, m - sparse N x m matrix, we only consider nonzeros
in Air[Ajc1[k]:Ajc2[k]-1], k=0:m-1.
ifirst - subscript of 1st entry, thus to be subtracted from A-subscripts
OUTPUT
Y - sparse n x m matrix, with sum(Ajc2-Ajc1) nonzeros.
************************************************************ */
void extractA(jcir Y, const mwIndex *Ajc1,const mwIndex *Ajc2,
const mwIndex *Air, const double *Apr, const mwIndex ifirst,
const mwIndex m)
{
mwIndex i,j,inz,ajnnz;
inz = 0;
for(j = 0; j < m; j++){
Y.jc[j] = inz;
ajnnz = Ajc2[j]-Ajc1[j];
memcpy(Y.pr + inz, Apr+Ajc1[j], ajnnz * sizeof(double));
memcpy(Y.ir + inz, Air+Ajc1[j], ajnnz * sizeof(mwIndex));
inz += ajnnz;
}
/* ------------------------------------------------------------
Close last column of Y, and subtract "ifirst" from its subscripts
------------------------------------------------------------ */
Y.jc[m] = inz;
for(i = 0; i < inz; i++)
Y.ir[i] -= ifirst;
}
/* ************************************************************
PROCEDURE mexFunction - Entry for Matlab
************************************************************ */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
jcir At, Apart;
mwIndex i, m, njc, ifirst, n, ynnz, blk0,blk1;
mwIndex *Ajc;
const double *blkstartPr, *AjcPr;
bool isblk0negative;
/* ------------------------------------------------------------
Check for proper number of arguments
------------------------------------------------------------ */
mxAssert(nrhs >= NPARINMIN, "extractA requires more input arguments.");
mxAssert(nlhs <= NPAROUT, "extractA produces less output arguments.");
/* --------------------------------------------------
GET inputs At, blkstart, Ajc1, Ajc2
-------------------------------------------------- */
mxAssert(mxIsSparse(AT_IN), "At must be a sparse matrix.");
At.jc = mxGetJc(AT_IN);
At.ir = mxGetIr(AT_IN);
At.pr = mxGetPr(AT_IN);
m = mxGetN(AT_IN);
if(nrhs >= NPARIN){
n = (mwIndex) mxGetScalar(BLKSTART2_IN);
ifirst = (mwIndex) mxGetScalar(BLKSTART_IN);
}
else if(mxGetM(BLKSTART_IN) * mxGetN(BLKSTART_IN) ==2){
blkstartPr = mxGetPr(BLKSTART_IN);
ifirst = (mwIndex) blkstartPr[0];
n = (mwIndex) blkstartPr[1];
}
else
mxAssert(0==1, "blkstart size mismatch.");
n -= (ifirst--);
AjcPr = mxGetPr(AJC_IN);
mxAssert(m == mxGetM(AJC_IN), "Ablkjc size mismatch.");
njc = mxGetN(AJC_IN);
blk0 = (mwIndex) mxGetScalar(BLK0_IN); /* double to mwIndex */
if(blk0>0){
--blk0; /* Fortran to C */
isblk0negative=0;}
else
isblk0negative=1;
if(mxGetM(BLK1_IN) * mxGetN(BLK1_IN) != 1)
blk1 = njc; /*default to end */
else{
blk1 = (mwIndex) mxGetScalar(BLK1_IN); /* double to mwIndex (thus inf not allowed) */
mxAssert(blk1>0,"");
--blk1; /* Fortran to C */
}
/* ------------------------------------------------------------
Allocate mwIndex array blkstart Ajc(2*m)
------------------------------------------------------------ */
Ajc = (mwIndex *) mxCalloc(MAX(2*m,1), sizeof(mwIndex));
/* ------------------------------------------------------------
Convert Ajc from double to mwIndex:
------------------------------------------------------------ */
mxAssert(blk0 < njc, "Ablkjc size mismatches blk0.");
if(isblk0negative)
memcpy(Ajc,At.jc,m*sizeof(mwIndex)); /* default: start of column */
else
for(i = 0; i < m; i++){ /* to integers */
Ajc[i] = (mwIndex) AjcPr[m*blk0 + i];
}
mxAssert(blk1 >= 0, "blk1 must be positive.");
if(blk1 >= njc)
memcpy(Ajc+m,At.jc+1,m*sizeof(mwIndex)); /* default: end of column */
else
for(i = 0; i < m; i++){ /* to integers */
Ajc[m+i] = (mwIndex) AjcPr[blk1*m + i];
}
/* ------------------------------------------------------------
Apart = sparse(n,m,ynnz);
------------------------------------------------------------ */
ynnz = 0;
for(i = 0; i < m; i++)
ynnz += Ajc[m+i]-Ajc[i]; /* nnz(Apart(:,i)) */
APART_OUT = mxCreateSparse(n,m, ynnz,mxREAL);
Apart.jc = mxGetJc(APART_OUT);
Apart.ir = mxGetIr(APART_OUT);
Apart.pr = mxGetPr(APART_OUT);
/* ------------------------------------------------------------
The real job:
------------------------------------------------------------ */
extractA(Apart, Ajc,Ajc+m,At.ir,At.pr, ifirst, m);
/* ------------------------------------------------------------
Release working array
------------------------------------------------------------ */
mxFree(Ajc);
}