当前位置:网站首页>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);
边栏推荐
- KS007基于JSP实现人个人博客系统
- Set the transparent hidden taskbar and full screen display of the form
- 项目通用环境使用说明
- “在越南,钱就像躺在街上”
- Master the use of auto analyze in data warehouse
- The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
- 补能的争议路线:快充会走向大一统吗?
- Vscode modification indentation failed, indent four spaces as soon as it is saved
- R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
- Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
猜你喜欢
Wuzhicms code audit
mysql5.7安装教程图文详解
VB cannot access database stocks
Electronic pet dog - what is the internal structure?
【Hot100】32. 最长有效括号
【Hot100】32. Longest valid bracket
ISO27001认证办理流程及2022年补贴政策汇总
Vscode modification indentation failed, indent four spaces as soon as it is saved
什么是低代码开发?
DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
随机推荐
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
To sort out messy header files, I use include what you use
Why are some online concerts always weird?
About the pit of firewall opening 8848 when Nacos is started
Firewall basic transparent mode deployment and dual machine hot standby
Win32 API access route encrypted web pages
华为云ModelArts的使用教程(附详细图解)
CocosCreator事件派发使用
无心剑中译伊丽莎白·毕肖普《一门技艺》
[daily question] 871 Minimum refueling times
曾经的“彩电大王”,退市前卖猪肉
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games
To sort out messy header files, I use include what you use
mysql5.7安装教程图文详解
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
内核中时间相关的知识介绍
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
【Hot100】32. Longest valid bracket