当前位置:网站首页>Numpy 的仿制 2
Numpy 的仿制 2
2022-07-04 16:08:00 【InfoQ】
- 第一步,Indicator 的设计
struct _ua_indicator {
// 作用的维度,例如是作用在第二维。
int __axis;
// 选择模式下有用。
int __picked;
// 截取模式下起始下标。
int __start;
// 截取模式下结束的下标 。
int __tail;
// 链接下一个 router
struct _ua_indicator* next;
};
- 第二步,估计截取后的体积
- 第三步,计算被选中的元素的地址
- 1 ua_data_chuck_t,这个对象非常重要,用于记录在计算过程中遇到符合 slice 条件的元素块的地址:
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,是保存 ua_data_chunk_t 的链表:
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;
// 计算总的维数
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) {
// 最后一个 route nod
size_t offset = 0;
// 计算下一个维度每一个块的大小。
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) {
// : 的情况
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 的情况
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)
{
// 分析计算 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) {
// 拷贝每一块选中的元素到新的 N 维数组中去。
memcpy(data_ptr, ptr->chunk_addr, ptr->chunk_size);
data_ptr += ptr->chunk_size;
ptr = ptr->next;
}
UArray_chunk_note_finalize(&chunk_note);
// 返回新的 N 维数组
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);

边栏推荐
- 【Hot100】32. 最长有效括号
- Implementation of super large-scale warehouse clusters in large commercial banks
- Win32 API access route encrypted web pages
- About the pit of firewall opening 8848 when Nacos is started
- Image retrieval
- celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
- Introduction of time related knowledge in kernel
- General environmental instructions for the project
- 2022年DCMM认证全国各地补贴政策汇总
- 上市公司改名,科学还是玄学?
猜你喜欢
Cocoscreator event dispatch use
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
居家打工年入800多万,一共五份全职工作,他还有时间打游戏
RecastNavigation 之 Recast
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
People in the workplace with a miserable expression
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
Wuzhicms code audit
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
Hidden corners of coder Edition: five things that developers hate most
随机推荐
VSCode修改缩进不成功,一保存就缩进四个空格
高中物理:力、物体和平衡
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
“在越南,钱就像躺在街上”
Russia arena data releases PostgreSQL based products
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
About the pit of firewall opening 8848 when Nacos is started
How to test MDM products
[proteus simulation] printf debugging output example based on VSM serial port
curl 命令妙用
你应该懂些CI/CD
Heartless sword Chinese translation of Elizabeth Bishop's a skill
Wuzhicms code audit
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
明星开店,退,退,退
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
Introduction of time related knowledge in kernel
俄罗斯 Arenadata 发布基于PostgreSQL的产品