Add sub2ind and test
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include "Function.h"
|
||||
#include "Function2D.h"
|
||||
#include "Function1D.h"
|
||||
//必须在Eigen之前
|
||||
#include "AuroraDefs.h"
|
||||
#include "Matrix.h"
|
||||
|
||||
#include <Eigen/Core>
|
||||
#include <Eigen/Eigen>
|
||||
@@ -813,3 +816,35 @@ Matrix Aurora::dot(const Matrix &aMatrix,const Matrix& aOther,FunctionDirection
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Matrix Aurora::sub2ind(const Matrix &aVMatrixSize, std::initializer_list<Matrix> aSliceIdxsList)
|
||||
{
|
||||
if (aSliceIdxsList.size() != aVMatrixSize.getDataSize()){
|
||||
std::cerr<<"sub2ind size not match"<<std::endl;
|
||||
return Matrix();
|
||||
}
|
||||
if (aSliceIdxsList.size() == 0){
|
||||
std::cerr<<"sub2ind no index need calc!"<<std::endl;
|
||||
return Matrix();
|
||||
}
|
||||
size_t returnVectorSize = aSliceIdxsList.begin()->getDataSize();
|
||||
double *strides =new double[aVMatrixSize.getDataSize()+1];
|
||||
strides[0] = 1;
|
||||
for (int i = 1; i<=aVMatrixSize.getDataSize(); ++i) {
|
||||
strides[i] = strides[i-1]*aVMatrixSize.getData()[i-1];
|
||||
}
|
||||
double* output = Aurora::malloc(returnVectorSize);
|
||||
for (size_t i = 0; i<returnVectorSize; ++i) {
|
||||
size_t j = 0;
|
||||
std::for_each(aSliceIdxsList.begin(), aSliceIdxsList.end(), [strides,output,i,&aVMatrixSize,&j](const Matrix& matrix){
|
||||
if(j == 0 ){
|
||||
output[i]= (matrix.getData()[i])*strides[j];
|
||||
}
|
||||
else{
|
||||
output[i]+= (matrix.getData()[i]-1)*strides[j];
|
||||
}
|
||||
++j;
|
||||
});
|
||||
}
|
||||
return Matrix::New(output,returnVectorSize,1,1);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#ifndef AURORA_FUNCTION2D_H
|
||||
#define AURORA_FUNCTION2D_H
|
||||
|
||||
#include <cstdio>
|
||||
#include <initializer_list>
|
||||
|
||||
#include "Matrix.h"
|
||||
#include "Function1D.h"
|
||||
|
||||
@@ -144,6 +147,9 @@ namespace Aurora
|
||||
Matrix prod(const Matrix &aMatrix);
|
||||
|
||||
Matrix dot(const Matrix &aMatrix, const Matrix &aOther, FunctionDirection direction = Column);
|
||||
|
||||
Matrix sub2ind(const Matrix &aVMatrixSize, std::initializer_list<Matrix> aSliceIdxs);
|
||||
|
||||
};
|
||||
|
||||
#endif // AURORA_FUNCTION2D_H
|
||||
|
||||
Reference in New Issue
Block a user