当前位置:网站首页>Imitation of numpy 2
Imitation of numpy 2
2022-07-04 18:11:00 【InfoQ】
- First step ,Indicator The design of the
struct _ua_indicator {
// The dimension of action , For example, it acts on the second dimension .
int __axis;
// Useful in select mode .
int __picked;
// Start subscript in interception mode .
int __start;
// Subscript ending in intercept mode .
int __tail;
// Link to next router
struct _ua_indicator* next;
};
- The second step , Estimate the volume after interception
- The third step , Calculate the address of the selected element
- 1 ua_data_chuck_t, This object is very important , It is used to record the meeting of slice Address of element block of condition :
typedef struct _ua_data_chunk ua_data_chunk_t;
struct _ua_data_chunk {
char* chunk_addr;
size_t chunk_size;
struct _ua_data_chunk* next;
};
- 2 ua_chunk_note_t, Is save ua_data_chunk_t The linked list of :
struct _ua_chunk_note {
size_t* shape;
int axis_n;
ua_data_chunk_t* chunk_map;
};
int UArray_indicator_analysis(ua_indicator_t* indicator_list,
u_array_t* arr,
ua_chunk_note_t* chunk_note)
{
chunk_note->shape = NULL;
chunk_note->axis_n = 0;
chunk_note->chunk_map = NULL;
ua_indicator_t* ptr = indicator_list;
int last_axis = -1;
// Calculate the total dimension
while(ptr != NULL) {
if (ptr->__picked == -1) (chunk_note->axis_n)++;
last_axis = ptr->__axis;
ptr = ptr->next;
}
if (last_axis >= arr->axis_n) {
return -1;
}
chunk_note->axis_n = chunk_note->axis_n + UA_axisn(arr) - (last_axis+1);
if (chunk_note->axis_n > 0)
chunk_note->shape = malloc( chunk_note->axis_n * sizeof(size_t) );
else
return -1;
ptr = indicator_list;
int axis_index = 0;
while(ptr != NULL) {
if (ptr->__picked == -1) {
int tail = (ptr->__tail<=0?UA_shape_axis(arr, ptr->__axis) + ptr->__tail:ptr->__tail);
(chunk_note->shape)[axis_index++] = tail - ptr->__start;
}
ptr = ptr->next;
}
for (int i = (last_axis+1); i<arr->axis_n; ++i){
(chunk_note->shape)[axis_index++] = UA_shape_axis(arr, i);
}
// -------------------------
ptr = indicator_list;
if (ptr != NULL) {
UArray_survey_chuck_address(arr, UA_data_ptr(arr), ptr, chunk_note);
} else {
chunk_note->chunk_map = UArray_datachunk_create(UA_data_ptr(arr), UA_size(arr));
}
return 0;
}
int UArray_survey_chuck_address(u_array_t* arr,
char* chunk_start_from,
ua_indicator_t* indicator,
ua_chunk_note_t* chunk_note)
{
size_t sub_chunk_size = (indicator->__axis < UA_axisn(arr) -1 ? UArray_axis_mulitply(arr, indicator->__axis + 1) : 1) * sizeof(vfloat_t);
int sub_chunk_number = 1;
if (indicator->next == NULL) {
// the last one route nod
size_t offset = 0;
// Calculate the size of each block in the next dimension .
if (indicator->__picked == -1) {
sub_chunk_number = (indicator->__tail <= 0 ? UA_shape_axis(arr, indicator->__axis) + indicator->__tail : indicator->__tail) - indicator->__start;
offset = indicator->__start * sub_chunk_size;
} else {
offset = indicator->__picked * sub_chunk_size;
}
ua_data_chunk_t* new_chunk = UArray_datachunk_create(chunk_start_from + offset, sub_chunk_size * sub_chunk_number);
UArray_datachunk_addto(&(chunk_note->chunk_map), new_chunk);
} else {
if (indicator->__picked == -1) {
// : The situation of
int tail = (indicator->__tail <= 0 ? UA_shape_axis(arr, indicator->__axis) + indicator->__tail : indicator->__tail);
for (int i=indicator->__start; i<tail; ++i) {
char* sub_chunk_start_from = chunk_start_from + i * sub_chunk_size;
UArray_survey_chuck_address(arr, sub_chunk_start_from, indicator->next, chunk_note);
}
} else {
// picked The situation of
char* sub_chunk_start_from = chunk_start_from + indicator->__picked * sub_chunk_size;
UArray_survey_chuck_address(arr, sub_chunk_start_from, indicator->next, chunk_note);
}
}
return 0;
}
u_array_t UArray_fission_with_indicators(u_array_t* a, ua_indicator_t* indicators)
{
// Analysis and calculation indicator
ua_chunk_note_t chunk_note;
UArray_indicator_analysis(indicators, a, &chunk_note);
u_array_t fission = UArray_create_with_axes_array(chunk_note.axis_n, chunk_note.shape);
ua_data_chunk_t* ptr = chunk_note.chunk_map;
char* data_ptr = UA_data_ptr(&fission);
//
while( ptr != NULL) {
// Copy each selected element to a new N Go to dimension group .
memcpy(data_ptr, ptr->chunk_addr, ptr->chunk_size);
data_ptr += ptr->chunk_size;
ptr = ptr->next;
}
UArray_chunk_note_finalize(&chunk_note);
// Return to new N Dimension group
return fission;
}
import numpy as np
if __name__ == "__main__":
arr = np.arange(3*3*3).reshape((3,3,3))
print(arr)
print("arr[0:2,0:1]")
print(arr[0:2,0:1])
print(arr[0:2,0:1].shape)
u_array_t u1 = _UArray3d(3,3,3);
UA_arange(&u1, 3*3*3);
u_array_t u2 = UA_slice(&u1, "0:2,0:1");
printf("u1: \n");
UA_display(&u1);
printf("\n u2: \n");
UA_display(&u2);
UArray_(&u1);
UArray_(&u2);
边栏推荐
猜你喜欢
随机推荐
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
【HCIA持续更新】WLAN工作流程概述
无心剑中译伊丽莎白·毕肖普《一门技艺》
国产数据库TiDB初体验:简单易用,快速上手
[cloud native] what is the "grid" of service grid?
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
LD_LIBRARY_PATH 环境变量设置
五千字讲清楚团队自组织建设 | Liga 妙谈
大规模服务异常日志检索
Make a grenade with 3DMAX
估值900亿,超级芯片IPO来了
[HCIA continuous update] network management and operation and maintenance
大厂面试总结大全二
内核中时间相关的知识介绍
People in the workplace with a miserable expression
【HCIA持续更新】网络管理与运维
Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
你应该懂些CI/CD
What are cache penetration, cache breakdown, and cache avalanche