外部インタフェース/API | ![]() ![]() |
スパース行列の操作
MATLAB APIは、MEX-ファイル内部からスパース行列を作成し操作できる関数を提供しています。スパース行列に関連する特殊なパラメータir
, jc
, nzmax
があります。これらのパラメータの使用と一般的なMATLABのスパース行列の格納方法に関する情報は、「MATLAB配列」を参照してください。
C $ Revision: 1.6 $ C=============================================================== C C fulltosparse.f C Example for illustrating how to populate a sparse matrix. C For the purpose of this example, you must pass in a C non-sparse 2-dimensional argument of type real double. C C NOTE: The subroutine loadsparse() is in the file called C loadsparse.f. C This is a MEX-file for MATLAB. C Copyright (c) 1984-2000 The MathWorks, Inc. C C=============================================================== C The gateway routine. subroutine mexFunction(nlhs, plhs, nrhs, prhs) integer nlhs, nrhs C--------------------------------------------------------------- C (pointer) Replace integer by integer*8 on the DEC Alpha C 64-bit platform C integer plhs(*), prhs(*) integer mxGetPr, mxCreateSparse, mxGetIr, mxGetJc integer pr, sr, irs, jcs C--------------------------------------------------------------- C integer m, n, nzmax integer mxGetM, mxGetN, mxIsComplex, mxIsDouble integer loadsparse C Check for proper number of arguments. if (nrhs .ne. 1) then call mexErrMsgTxt('One input argument required.') endif if (nlhs .gt. 1) then call mexErrMsgTxt('Too many output arguments.') endif C Check data type of input argument. if (mxIsDouble(prhs(1)) .eq. 0) then call mexErrMsgTxt('Input argument must be of type double.') endif if (mxIsComplex(prhs(1)) .eq. 1) then call mexErrMsgTxt('Input argument must be real only') endif C Get the size and pointers to input data. m = mxGetM(prhs(1)) n = mxGetN(prhs(1)) pr = mxGetPr(prhs(1)) C Allocate space. C NOTE: Assume at most 20% of the data is sparse. nzmax = dble(m*n) *.20 + .5 C NOTE: The maximum number of non-zero elements cannot be less C than the number of columns in the matrix. if (n .gt. nzmax) then nzmax = n endif plhs(1) = mxCreateSparse(m,n,nzmax,0) sr = mxGetPr(plhs(1)) irs = mxGetIr(plhs(1)) jcs = mxGetJc(plhs(1)) C Load the sparse data. if (loadsparse(%val(pr),%val(sr),%val(irs),%val(jcs),m,n,nzmax) + .eq. 1) then call mexPrintf('Truncating output, input is > 20%% sparse') endif return end
以下は、スパースデータをもつmxArray
の要素を満たすためにfulltosparse
が呼び出すサブルーチンです。
C $ Revision: 1.4 $ C=============================================================== C C loadsparse.f C This is the subfunction called by fulltosparse that fills the C mxArray with the sparse data. Your version of C loadsparse can operate however you would like it to on the C data. C C This is a MEX-file for MATLAB. C Copyright (c) 1984-2000 The MathWorks, Inc. C C=============================================================== C Load sparse data subroutine. function loadsparse(a,b,ir,jc,m,n,nzmax) integer nzmax, m, n integer ir(*), jc(*) real*8 a(*), b(*) integer i, j, k C Copy nonzeros. k = 1 do 100 j=1,n C NOTE: Sparse indexing is zero based. jc(j) = k-1 do 200 i=1,m if (a((j-1)*m+i).ne. 0.0) then if (k .gt. nzmax) then jc(n+1) = nzmax loadsparse = 1 goto 300 endif b(k) = a((j-1)*m+i) C NOTE: Sparse indexing is zero based. ir(k) = i-1 k = k+1 endif 200 continue 100 continue C NOTE: Sparse indexing is zero based. jc(n+1) = k-1 loadsparse = 0 300 return end
full = eye(5) full = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
と入力すると、5行5列のフル単位行列を作成します。フル行列に対してfulltosparse
を使うと、対応するスパース行列を生成します。
spar = fulltosparse(full) spar = (1,1) 1 (2,2) 1 (3,3) 1 (4,4) 1 (5,5) 1
![]() | メモリのダイナミックな割り当て | Fortran MEX-ファイルからの関数の呼び出し | ![]() |