feat: 添加了eigenmath支持,但是目前存在问题

This commit is contained in:
feng-arch 2025-10-11 14:33:16 +08:00
parent dca783774d
commit 32ec5221d4
5 changed files with 101 additions and 56 deletions

View File

@ -1,15 +0,0 @@
# Create an INTERFACE library for our C module.
add_library(usermod_cexample INTERFACE)
# Add our source files to the lib
target_sources(usermod_cexample INTERFACE
${CMAKE_CURRENT_LIST_DIR}/examplemodule.c
)
# Add the current directory as an include directory.
target_include_directories(usermod_cexample INTERFACE
${CMAKE_CURRENT_LIST_DIR}
)
# Link our INTERFACE library to the usermod target.
target_link_libraries(usermod INTERFACE usermod_cexample)

View File

@ -3,6 +3,8 @@
// Used to get the time in the Timer class example.
#include "py/mphal.h"
#include "py/misc.h"
#include "shared/readline/readline.h"
/*
@ -44,12 +46,33 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <errno.h>
#include <time.h>
#define STACKSIZE 100000 // evaluation stack
#define BLOCKSIZE 10000
#define MAXBLOCKS 2000
#define BUCKETSIZE 100
#define STRBUFLEN 1000
#define MAXDIM 24
// #define STACKSIZE 100000 // evaluation stack
// #define BLOCKSIZE 10000
// #define MAXBLOCKS 2000
// #define BUCKETSIZE 100
// #define STRBUFLEN 1000
// #define MAXDIM 24
// #define STACKSIZE 4096 // 栈大小从100000减到4096
// #define BLOCKSIZE 512 // 块大小从10000减到512
// #define MAXBLOCKS 64 // 最大块数从2000减到64
// #define BUCKETSIZE 32 // 哈希桶大小相应减小
// #define STRBUFLEN 256 // 字符串缓冲区减小
// #define MAXDIM 16 // 维度限制适当降低
// #define STACKSIZE 128 // 栈大小128个元素每个元素按4字节算约512字节
// #define BLOCKSIZE 16 // 块大小每个块包含16个atom
// #define MAXBLOCKS 64 // 最大块数64块16*64=1024个atom
// #define BUCKETSIZE 4 // 哈希桶大小仅保留4个桶
// #define STRBUFLEN 32 // 字符串缓冲区每个字符串最大32字节
// #define MAXDIM 4 // 维度限制降低到4维
#define STACKSIZE 512 // 进一步减小栈大小
#define BLOCKSIZE 128 // 减小块大小
#define MAXBLOCKS 64 // 减少最大块数
#define BUCKETSIZE 20 // 减小哈希桶
#define STRBUFLEN 256 // 减小字符串缓冲区
#define MAXDIM 16 // 降低维度限制
// MAXBLOCKS * BLOCKSIZE = 20,000,000 atoms
@ -1019,7 +1042,7 @@ alloc_str(void)
void *
alloc_mem(int n)
{
void *p = malloc(n);
void *p = m_malloc(n);
if (p == NULL)
exit(1);
return p;
@ -1104,7 +1127,7 @@ mstr(uint32_t *u)
if (n > len) {
if (buf)
free(buf);
m_free(buf);
buf = alloc_mem(n);
len = n;
}
@ -1486,7 +1509,7 @@ mnew(int n)
void
mfree(uint32_t *u)
{
free(u - 1);
m_free(u - 1);
bignum_count--;
}
@ -5072,9 +5095,9 @@ eval_eigenvec(struct atom *p1)
stopf("eigenvec");
if (D)
free(D);
m_free(D);
if (Q)
free(Q);
m_free(Q);
D = alloc_mem(n * n * sizeof (double));
Q = alloc_mem(n * n * sizeof (double));
@ -9939,9 +9962,9 @@ nroots(void)
n = tos - h; // number of coeffs on stack
if (cr)
free(cr);
m_free(cr);
if (ci)
free(ci);
m_free(ci);
cr = alloc_mem(n * sizeof (double));
ci = alloc_mem(n * sizeof (double));
@ -12715,7 +12738,7 @@ read_file(char *filename)
}
if (read(fd, buf, n) != n) {
free(buf);
m_free(buf);
close(fd);
return NULL;
}
@ -15393,7 +15416,7 @@ fmt(void)
if (m > fmt_buf_len) {
if (fmt_buf)
free(fmt_buf);
m_free(fmt_buf);
fmt_buf = alloc_mem(m);
fmt_buf_len = m;
}
@ -16742,11 +16765,11 @@ gc(void)
switch (p->atomtype) {
case KSYM:
free(p->u.ksym.name);
m_free(p->u.ksym.name);
ksym_count--;
break;
case USYM:
free(p->u.usym.name);
m_free(p->u.usym.name);
usym_count--;
break;
case RATIONAL:
@ -16755,11 +16778,11 @@ gc(void)
break;
case STR:
if (p->u.str)
free(p->u.str);
m_free(p->u.str);
string_count--;
break;
case TENSOR:
free(p->u.tensor);
m_free(p->u.tensor);
tensor_count--;
break;
default:
@ -16864,21 +16887,31 @@ run_infile(char *infile)
exit(1);
}
run(buf);
free(buf);
m_free(buf);
}
void
run_stdin(void)
{
static char inbuf[1000];
for (;;) {
fputs("? ", stdout);
fflush(stdout);
fgets(inbuf, sizeof inbuf, stdin);
run(inbuf);
}
mp_printf(&mp_plat_print, "Eigenmath start %d\n", __LINE__);
// static char inbuf[1000];
vstr_t* vstr_inbuf = vstr_new(1);
// for (;;) {
// fputs("? ", stdout);
// fflush(stdout);
// fgets(inbuf, sizeof inbuf, stdin);
int res = readline(vstr_inbuf,"eigenmath> ");
mp_printf(&mp_plat_print, "Eigenmath run:\n");
mp_printf(&mp_plat_print, "res=%d\n", res);
mp_printf(&mp_plat_print, "%s\n", vstr_inbuf->buf);
run(vstr_inbuf->buf);
// }
}
void
display(void)
{
@ -16888,7 +16921,8 @@ display(void)
void
printbuf(char *s, int color)
{
fputs(s, stdout);
// fputs(s, stdout);
mp_printf(&mp_plat_print, "%s", s);
}
void
@ -17394,7 +17428,7 @@ run(char *buf)
shuntflag = 0;
errorflag = 0;
breakflag = 0;
mp_printf(&mp_plat_print, "run() has param: %s\n", buf);
if (zero == NULL) {
srand((unsigned) time(NULL));
init_symbol_table();
@ -17409,10 +17443,10 @@ run(char *buf)
push_rational(1, 2);
list(3);
imaginaryunit = pop();
run_init_script();
// run_init_script();
}
run_buf(buf);
mp_printf(&mp_plat_print, "start to run run_buf()\n");
// run_buf(buf);
}
void
@ -18015,7 +18049,7 @@ update_token_buf(char *a, char *b)
if (m > token_buf_len) {
if (token_buf)
free(token_buf);
m_free(token_buf);
token_buf = alloc_mem(m);
token_buf_len = m;
}
@ -18621,6 +18655,16 @@ static mp_obj_t example_add_ints(mp_obj_t a_obj, mp_obj_t b_obj) {
// Calculate the addition and convert to MicroPython object.
return mp_obj_new_int(a + b);
}
static mp_obj_t eigenmath_cmd() {
run_stdin();
// Calculate the addition and convert to MicroPython object.
return mp_obj_new_int(0);
}
static MP_DEFINE_CONST_FUN_OBJ_0(eigenmath_cmd_obj, eigenmath_cmd);
// Define a Python reference to the function above.
static MP_DEFINE_CONST_FUN_OBJ_2(example_add_ints_obj, example_add_ints);
@ -18762,19 +18806,20 @@ MP_DEFINE_CONST_OBJ_TYPE(
// and the MicroPython object reference.
// All identifiers and strings are written as MP_QSTR_xxx and will be
// optimized to word-sized integers by the build system (interned strings).
static const mp_rom_map_elem_t example_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cexample) },
static const mp_rom_map_elem_t eigenmath_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_eigenmath) },
{ MP_ROM_QSTR(MP_QSTR_add_ints), MP_ROM_PTR(&example_add_ints_obj) },
{ MP_ROM_QSTR(MP_QSTR_cmd), MP_ROM_PTR(&eigenmath_cmd_obj) },
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&example_type_Timer) },
{ MP_ROM_QSTR(MP_QSTR_AdvancedTimer), MP_ROM_PTR(&example_type_AdvancedTimer) },
};
static MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table);
static MP_DEFINE_CONST_DICT(eigenmath_module_globals, eigenmath_module_globals_table);
// Define module object.
const mp_obj_module_t example_user_cmodule = {
const mp_obj_module_t eigenmath_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&example_module_globals,
.globals = (mp_obj_dict_t *)&eigenmath_module_globals,
};
// Register the module to make it available in Python.
MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule);
MP_REGISTER_MODULE(MP_QSTR_eigenmath, eigenmath_user_cmodule);

View File

@ -0,0 +1,15 @@
# Create an INTERFACE library for our C module.
add_library(usermod_eigenmath INTERFACE)
# Add our source files to the lib
target_sources(usermod_eigenmath INTERFACE
${CMAKE_CURRENT_LIST_DIR}/eigenmath.c
)
# Add the current directory as an include directory.
target_include_directories(usermod_eigenmath INTERFACE
${CMAKE_CURRENT_LIST_DIR}
)
# Link our INTERFACE library to the usermod target.
target_link_libraries(usermod INTERFACE usermod_eigenmath)

View File

@ -1,7 +1,7 @@
CEXAMPLE_MOD_DIR := $(USERMOD_DIR)
# Add all C files to SRC_USERMOD.
SRC_USERMOD += $(CEXAMPLE_MOD_DIR)/examplemodule.c
SRC_USERMOD += $(CEXAMPLE_MOD_DIR)/eigenmath.c
# We can add our module folder to include paths if needed
# This is not actually needed in this example.

View File

@ -1,2 +1,2 @@
include(${CMAKE_CURRENT_LIST_DIR}/cexample/micropython.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/eigenmath/micropython.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/lv_binding_micropython/bindings.cmake)