当前位置:网站首页>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);

边栏推荐
- shell脚本的替换功能实现
- Great Wall Securities security does not open a securities account
- 高中物理:力、物体和平衡
- 【每日一题】871. 最低加油次数
- 通过事件绑定实现动画效果
- 股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
- Implementation of super large-scale warehouse clusters in large commercial banks
- The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
- 为啥有些线上演唱会总是怪怪的?
- [system analyst's road] Chapter 7 double disk system design (structured development method)
猜你喜欢
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding

Is it science or metaphysics to rename a listed company?

【华为HCIA持续更新】SDN与FVC

Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see

Superscalar processor design yaoyongbin Chapter 7 register rename excerpt

Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection

明星开店,退,退,退

“在越南,钱就像躺在街上”

表情包坑惨职场人

Master the use of auto analyze in data warehouse
随机推荐
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
Redis主从复制
R语言plotly可视化:plotly可视化多分类变量小提琴图(multiple variable violin plot in R with plotly)
Win32 API 访问路由的加密网页
Once the "king of color TV", he sold pork before delisting
You should know something about ci/cd
超标量处理器设计 姚永斌 第6章 指令解码 摘录
2022年全国CMMI认证补贴政策|昌旭咨询
Face_ Attendance statistics of recognition face recognition
shell脚本的替换功能实现
【Hot100】32. Longest valid bracket
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
【HCIA持续更新】广域网技术
你应该懂些CI/CD
gatling 之性能测试
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
我写了一份初学者的学习实践教程!
[daily question] 871 Minimum refueling times
曾经的“彩电大王”,退市前卖猪肉