BLAS中一些常用的转换算法

多维数组中offset和数组下表索引之间的转化:

#include <stdio.h>
#include <string.h>
 
static int offset_to_indices(int dims[], int dimensions, int offset, int indices[])
{
	int i;
 
	for(i = dimensions - 1; i >= 0; i--)	
	{
		indices[i] = offset % dims[i];
		offset /= dims[i];
	}
	return 0;
}

static int indices_to_offset(int dims[], int dimensions, int *offset, int indices[])
{
	int off = 0;
	int temp = 1; 
	int i;

	for(i = dimensions - 1; i >= 0; i--)
	{
		off = off + indices[i] * temp;
		temp = temp *dims[i];
	}

	*offset = off;
}
 
int main(void)
{
	int i;
	int dims[4] = {1, 1, 8, 8};
	int indices[4] = {0, 0, 0, 0};
 
	offset_to_indices(dims, 4, 57, indices);
 
	for(i = 0; i < 4; i ++)
	{
		printf("%d ", indices[i]);
	}

	printf("\n");

	int offset = -1;
	indices_to_offset(dims, 4, &offset, indices);

	printf("the offset is %d.\n", offset);
 
	return 0;
}

输出:


结束! 

おすすめ

転載: blog.csdn.net/tugouxp/article/details/119209773