我有一组KxK
平方矩阵,并且我想为其创建一个具有不同大小的新实例,即一个(K+1)x(K+1)
平方矩阵,其中第一个矩阵的每个K大小的块都复制到这个新的1将会有零的行和列,除了第K+1
个元素(它是常量C
)之外。最后的块也将全为零。这里有一个例子:
array([[ 0.09990096, -0.03676744, -0.04634088, -0.03676744, 0.01353258, 0.01704259, -0.04634088, 0.01704259, 0.02123895],
[-0.03676744, 0.07944992, -0.03132864, 0.01353258, -0.0292484 , 0.01152807, 0.01704259, -0.03700603, 0.01447335],
[-0.04634088, -0.03132864, 0.13579896, 0.01704259, 0.01152807, -0.04996734, 0.02123895, 0.01447335, -0.06275539],
[-0.03676744, 0.01353258, 0.01704259, 0.07944992, -0.0292484 , -0.03700603, -0.03132864, 0.01152807, 0.01447335],
[ 0.01353258, -0.0292484 , 0.01152807, -0.0292484 , 0.06309598, -0.02495025, 0.01152807, -0.02495025, 0.00981151],
[ 0.01704259, 0.01152807, -0.04996734, -0.03700603, -0.02495025, 0.1081392 , 0.01447335, 0.00981151, -0.04253148],
[-0.04634088, 0.01704259, 0.02123895, -0.03132864, 0.01152807, 0.01447335, 0.13579896, -0.04996734, -0.06275539],
[ 0.01704259, -0.03700603, 0.01447335, 0.01152807, -0.02495025, 0.00981151, -0.04996734, 0.1081392 , -0.04253148],
[ 0.02123895, 0.01447335, -0.06275539, 0.01447335, 0.00981151, -0.04253148, -0.06275539, -0.04253148, 0.18437719]])
新矩阵应如下所示:
array([[ 0.09990096, -0.03676744, -0.04634088, 0. , -0.03676744, 0.01353258, 0.01704259, 0. , -0.04634088, 0.01704259, 0.02123895, 0. , 0. , 0. , 0. , 0. ],
[-0.03676744, 0.07944992, -0.03132864, 0. , 0.01353258, -0.0292484 , 0.01152807, 0. , 0.01704259, -0.03700603, 0.01447335, 0. , 0. , 0. , 0. , 0. ],
[-0.04634088, -0.03132864, 0.13579896, 0. , 0.01704259, 0.01152807, -0.04996734, 0. , 0.02123895, 0.01447335, -0.06275539, 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 5. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[-0.03676744, 0.01353258, 0.01704259, 0. , 0.07944992, -0.0292484 , -0.03700603, 0. , -0.03132864, 0.01152807, 0.01447335, 0. , 0. , 0. , 0. , 0. ],
[ 0.01353258, -0.0292484 , 0.01152807, 0. , -0.0292484 , 0.06309598, -0.02495025, 0. , 0.01152807, -0.02495025, 0.00981151, 0. , 0. , 0. , 0. , 0. ],
[ 0.01704259, 0.01152807, -0.04996734, 0. , -0.03700603, -0.02495025, 0.1081392 , 0. , 0.01447335, 0.00981151, -0.04253148, 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 5. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[-0.04634088, 0.01704259, 0.02123895, 0. , -0.03132864, 0.01152807, 0.01447335, 0. , 0.13579896, -0.04996734, -0.06275539, 0. , 0. , 0. , 0. , 0. ],
[ 0.01704259, -0.03700603, 0.01447335, 0. , 0.01152807, -0.02495025, 0.00981151, 0. , -0.04996734, 0.1081392 , -0.04253148, 0. , 0. , 0. , 0. , 0. ],
[ 0.02123895, 0.01447335, -0.06275539, 0. , 0.01447335, 0.00981151, -0.04253148, 0. , -0.06275539, -0.04253148, 0.18437719, 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 5. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 5. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 5. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 5. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 5. ]])
我写的那段代码无法正常工作:
#include <stdio.h>
#include <iostream>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
int main () {
double Qx[]={ 0.09990096, -0.03676744, -0.04634088, -0.03676744, 0.01353258, 0.01704259, -0.04634088, 0.01704259, 0.02123895,
-0.03676744, 0.07944992, -0.03132864, 0.01353258, -0.0292484 , 0.01152807, 0.01704259, -0.03700603, 0.01447335,
-0.04634088, -0.03132864, 0.13579896, 0.01704259, 0.01152807, -0.04996734, 0.02123895, 0.01447335, -0.06275539,
-0.03676744, 0.01353258, 0.01704259, 0.07944992, -0.0292484 , -0.03700603, -0.03132864, 0.01152807, 0.01447335,
0.01353258, -0.0292484 , 0.01152807, -0.0292484 , 0.06309598, -0.02495025, 0.01152807, -0.02495025, 0.00981151,
0.01704259, 0.01152807, -0.04996734, -0.03700603, -0.02495025, 0.1081392 , 0.01447335, 0.00981151, -0.04253148,
-0.04634088, 0.01704259, 0.02123895, -0.03132864, 0.01152807, 0.01447335, 0.13579896, -0.04996734, -0.06275539,
0.01704259, -0.03700603, 0.01447335, 0.01152807, -0.02495025, 0.00981151, -0.04996734, 0.1081392 , -0.04253148,
0.02123895, 0.01447335, -0.06275539, 0.01447335, 0.00981151, -0.04253148, -0.06275539, -0.04253148, 0.18437719};
int nr=3;
gsl_matrix_view Qmatrix = gsl_matrix_view_array (Qx, nr*nr, nr*nr);
gsl_matrix *Qexp= gsl_matrix_calloc((nr+1)*(nr+1),(nr+1)*(nr+1));
gsl_matrix_view Qmatrix_view;
gsl_matrix_view Qexp_view;
double cons=5.;
for (int row=0; row<nr; ++row){
for (int col=0; col<nr; ++col){
Qmatrix_view = gsl_matrix_submatrix(&Qmatrix.matrix, row*nr, col*nr,nr,nr);
Qexp_view = gsl_matrix_submatrix(Qexp,row*(nr+1),col*(nr+1),nr,nr);
gsl_matrix_set(Qexp, row*nr,col*nr, cons);
gsl_matrix_memcpy(&Qexp_view.matrix, &Qmatrix_view.matrix);
}
}
printf("\n---\n---\n---\n");
for (int row=0; row<(nr+1)*(nr+1); ++row)
for (int col=0; col<(nr+1)*(nr+1); ++col)
printf(col==nr*nr?"%6.3f\n":"%6.3f ",gsl_matrix_get(Qexp,row,col));
printf("\n---\n---\n");
return 0;
}
如何使用block
或submatrix
以最快的方式使用GSL_matrix?