Add Matrix method setBlockValue

This commit is contained in:
kradchen
2023-05-29 11:09:09 +08:00
parent 320d6c81cb
commit ce91b7a868
3 changed files with 94 additions and 3 deletions

View File

@@ -616,6 +616,69 @@ namespace Aurora {
}
}
bool Matrix::setBlockValue(int aDim,int aBeginIndex, int aEndIndex, double value) {
if(aDim>2 ){
std::cerr<<"block only support 1D-3D data!"<<std::endl;
return false;
}
//横向vector切面为0为1都强制设置aDim为1来处理
if (isVector() && aDim == 0 && getDimSize(1)>1){
aDim = 1;
}
if (aBeginIndex>=getDimSize(aDim) || aBeginIndex<0){
std::cerr<<"block BeginIndx error!BeginIndx:"<<aBeginIndex<<std::endl;
return false;
}
if (aEndIndex>=getDimSize(aDim) || aEndIndex<0){
std::cerr<<"block EndIndex error!EndIndex:"<<aEndIndex<<std::endl;
return false;
}
int dimLength = std::abs(aEndIndex-aBeginIndex)+1;
int dataSize = getDataSize()/getDimSize(aDim)*dimLength;
double * dataOutput = malloc(dataSize);
int colStride = getDimSize(0);
int sliceStride = getDimSize(0)*getDimSize(1);
switch (aDim) {
case 0:{
int colStride2 = dimLength;
int sliceStride2 = dimLength*getDimSize(1);
for (size_t i = 0; i < getDimSize(2); i++)
{
for (size_t j = 0; j < getDimSize(1); j++)
{
cblas_dcopy(
dimLength,
&value,0,
getData() + aBeginIndex + j * colStride +
i * sliceStride,
1);
}
}
return true;
}
case 1:{
int colStride2 = getDimSize(0);
int sliceStride2 = dimLength*getDimSize(0);
int copySize = sliceStride2;
for (size_t i = 0; i < getDimSize(2); i++)
{
cblas_dcopy(copySize,
&value, 0 ,
getData() + aBeginIndex * colStride +
i * sliceStride, 1);
}
return true;
}
case 2:{
int copySize = dimLength*sliceStride;
cblas_dcopy(copySize, &value, 0 ,getData() + aBeginIndex * sliceStride ,1);
return true;
}
}
}
void Matrix::printf() {

View File

@@ -183,6 +183,8 @@ namespace Aurora {
*/
Matrix block(int aDim,int aBeginIndx, int aEndIndex) const;
bool setBlockValue(int aDim,int aBeginIndx, int aEndIndex,double value);
/**
* 矩阵乘法
* @attention 目前只支持矩阵乘向量