当前位置:网站首页>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】31. Next spread
- Recast of recastnavigation
- 设置窗体透明 隐藏任务栏 与全屏显示
- 【华为HCIA持续更新】SDN与FVC
- 2022年全国CMMI认证补贴政策|昌旭咨询
- DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
- 2022 national CMMI certification subsidy policy | Changxu consulting
- Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
- curl 命令妙用
- 【HCIA持续更新】网络管理与运维
猜你喜欢
celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
2022年全国CMMI认证补贴政策|昌旭咨询
如何进行MDM的产品测试
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
Is it science or metaphysics to rename a listed company?
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
表情包坑惨职场人
[HCIA continuous update] WLAN overview and basic concepts
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
Why are some online concerts always weird?
随机推荐
General environmental instructions for the project
动态规划股票问题对比
Vscode modification indentation failed, indent four spaces as soon as it is saved
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
Pytorch深度学习之环境搭建
CocosCreator事件派發使用
regular expression
78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
Developers, MySQL column finish, help you easily from installation to entry
雨量预警广播自动化数据平台BWII 型广播预警监测仪
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
高中物理:力、物体和平衡
“在越南,钱就像躺在街上”
【HCIA持续更新】WLAN概述与基本概念
2022年DCMM认证全国各地补贴政策汇总
Large scale service exception log retrieval
7 RSA Cryptosystem
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
New technology releases a small program UNIPRO to meet customers' mobile office scenarios
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!