1
0
Эх сурвалжийг харах

rename `linalg` to `vmath`

blueloveTH 10 сар өмнө
parent
commit
94ce95c74d

+ 1 - 1
amalgamate.py

@@ -8,7 +8,7 @@ from typing import List, Dict
 assert os.system("python prebuild.py") == 0
 
 ROOT = 'include/pocketpy'
-PUBLIC_HEADERS = ['config.h', 'export.h', 'linalg.h', 'pocketpy.h']
+PUBLIC_HEADERS = ['config.h', 'export.h', 'vmath.h', 'pocketpy.h']
 
 COPYRIGHT = '''/*
  *  Copyright (c) 2025 blueloveTH

+ 3 - 5
docs/modules/linalg.md

@@ -1,12 +1,10 @@
 ---
 icon: package
-label: linalg
+label: vmath
 ---
 
-Provide `mat3x3`, `vec2`, `vec3`, `vec2i` and `vec3i` types.
-
-This classes adopt `torch`'s naming convention. Methods with `_` suffix will modify the instance itself.
+Provide vector math operations.
 
 #### Source code
 
-:::code source="../../include/typings/linalg.pyi" :::
+:::code source="../../include/typings/vmath.pyi" :::

+ 1 - 0
include/pocketpy/common/_generated.h

@@ -11,5 +11,6 @@ extern const char kPythonLibs_dataclasses[];
 extern const char kPythonLibs_datetime[];
 extern const char kPythonLibs_functools[];
 extern const char kPythonLibs_heapq[];
+extern const char kPythonLibs_linalg[];
 extern const char kPythonLibs_operator[];
 extern const char kPythonLibs_typing[];

+ 1 - 1
include/pocketpy/interpreter/modules.h

@@ -18,7 +18,7 @@ void pk__add_module_pickle();
 void pk__add_module_base64();
 void pk__add_module_importlib();
 
-void pk__add_module_linalg();
+void pk__add_module_vmath();
 void pk__add_module_array2d();
 void pk__add_module_colorcvt();
 

+ 3 - 3
include/pocketpy/pocketpy.h

@@ -6,7 +6,7 @@
 
 #include "pocketpy/config.h"
 #include "pocketpy/export.h"
-#include "pocketpy/linalg.h"
+#include "pocketpy/vmath.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -667,7 +667,7 @@ PK_API bool
 /// noexcept
 PK_API int py_dict_len(py_Ref self);
 
-/************* linalg module *************/
+/************* vmath module *************/
 void py_newvec2(py_OutRef out, c11_vec2);
 void py_newvec3(py_OutRef out, c11_vec3);
 void py_newvec2i(py_OutRef out, c11_vec2i);
@@ -761,7 +761,7 @@ enum py_PredefinedType {
     tp_ImportError,
     tp_AssertionError,
     tp_KeyError,
-    /* linalg */
+    /* vmath */
     tp_vec2,
     tp_vec3,
     tp_vec2i,

+ 0 - 0
include/pocketpy/linalg.h → include/pocketpy/vmath.h


+ 1 - 1
include/typings/array2d.pyi

@@ -1,5 +1,5 @@
 from typing import Callable, Literal, overload, Iterator, Self
-from linalg import vec2i
+from vmath import vec2i
 
 Neighborhood = Literal['Moore', 'von Neumann']
 

+ 1 - 1
include/typings/colorcvt.pyi

@@ -1,4 +1,4 @@
-from linalg import vec3
+from vmath import vec3
 
 def linear_srgb_to_srgb(rgb: vec3) -> vec3: ...
 def srgb_to_linear_srgb(rgb: vec3) -> vec3: ...

+ 1 - 1
include/typings/pkpy.pyi

@@ -1,5 +1,5 @@
 from typing import Self, Literal
-from linalg import vec2, vec2i
+from vmath import vec2, vec2i
 
 class TValue[T]:
     def __new__(cls, value: T) -> Self: ...

+ 0 - 0
include/typings/linalg.pyi → include/typings/vmath.pyi


+ 1 - 0
python/linalg.py

@@ -0,0 +1 @@
+from vmath import *

+ 1 - 1
scripts/c_bind/c_bind/__init__.py

@@ -1,5 +1,5 @@
 from .function import gen_function
-from .converters import get_converter, set_linalg_converter, set_enum_converters
+from .converters import get_converter, set_vmath_converter, set_enum_converters
 from .writer import Writer
 from .struct import gen_struct
 from .enum import gen_enum

+ 1 - 1
scripts/c_bind/c_bind/converters.py

@@ -149,7 +149,7 @@ for t in LINALG_TYPES:
 _CONVERTERS['void'] = VoidConverter('void')
 _CONVERTERS['c11_array2d'] = StructConverter('c11_array2d', 'tp_array2d')
 
-def set_linalg_converter(T: str, py_T: str):
+def set_vmath_converter(T: str, py_T: str):
     assert py_T in LINALG_TYPES
     _CONVERTERS[T] = BuiltinVectorConverter(T, py_T)
 

+ 1 - 1
scripts/c_bind/c_bind/library.py

@@ -26,7 +26,7 @@ class Library:
 
         w, pyi_w = Writer(), Writer()
 
-        pyi_w.write('from linalg import vec2, vec3, vec2i, vec3i, mat3x3')
+        pyi_w.write('from vmath import vec2, vec3, vec2i, vec3i, mat3x3')
         pyi_w.write('from typing import overload')
         pyi_w.write('intptr = int')
         pyi_w.write('')

+ 3 - 3
scripts/c_bind/gen_box2d.py

@@ -1,6 +1,6 @@
 import pcpp
 import pycparser
-from c_bind import Library, set_linalg_converter, set_enum_converters
+from c_bind import Library, set_vmath_converter, set_enum_converters
 from c_bind.meta import Header
 import os
 
@@ -18,8 +18,8 @@ header.remove_functions({'b2CreateTimer', 'b2Hash', 'b2DefaultDebugDraw'})
 
 lib = Library.from_header('box2d', header)
 
-set_linalg_converter('b2Vec2', 'vec2')
-set_linalg_converter('b2Vec3', 'vec3')
+set_vmath_converter('b2Vec2', 'vec2')
+set_vmath_converter('b2Vec3', 'vec3')
 
 set_enum_converters([enum.name for enum in lib.enums])
 

+ 3 - 3
scripts/c_bind/gen_raylib.py

@@ -1,12 +1,12 @@
 import json
-from c_bind import Library, set_linalg_converter
+from c_bind import Library, set_vmath_converter
 
 with open('../3rd/raylib/parser/output/raylib_api.json') as f:
     data = json.load(f)
 
 lib = Library.from_raylib(data)
-set_linalg_converter('Vector2', 'vec2')
-set_linalg_converter('Vector3', 'vec3')
+set_vmath_converter('Vector2', 'vec2')
+set_vmath_converter('Vector3', 'vec3')
 
 lib.build(
     includes=['raylib.h'],

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
src/common/_generated.c


+ 1 - 1
src/interpreter/vm.c

@@ -213,7 +213,7 @@ void VM__ctor(VM* self) {
 
     py_newnotimplemented(py_emplacedict(&self->builtins, py_name("NotImplemented")));
 
-    pk__add_module_linalg();
+    pk__add_module_vmath();
     pk__add_module_array2d();
     pk__add_module_colorcvt();
 

+ 1 - 1
src/modules/array2d.c

@@ -774,7 +774,7 @@ static void register_array2d_like(py_Ref mod) {
     py_bindmethod(type, "convolve", array2d_like_convolve);
 
     const char* scc =
-        "\ndef get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:\n    from collections import deque\n    from linalg import vec2i\n\n    DIRS = [vec2i.LEFT, vec2i.RIGHT, vec2i.UP, vec2i.DOWN]\n    assert neighborhood in ['Moore', 'von Neumann']\n\n    if neighborhood == 'Moore':\n        DIRS.extend([\n            vec2i.LEFT+vec2i.UP,\n            vec2i.RIGHT+vec2i.UP,\n            vec2i.LEFT+vec2i.DOWN,\n            vec2i.RIGHT+vec2i.DOWN\n            ])\n\n    visited = array2d[int](self.width, self.height, default=0)\n    queue = deque()\n    count = 0\n    for y in range(self.height):\n        for x in range(self.width):\n            if visited[x, y] or self[x, y] != value:\n                continue\n            count += 1\n            queue.append((x, y))\n            visited[x, y] = count\n            while queue:\n                cx, cy = queue.popleft()\n                for dx, dy in DIRS:\n                    nx, ny = cx+dx, cy+dy\n                    if self.is_valid(nx, ny) and not visited[nx, ny] and self[nx, ny] == value:\n                        queue.append((nx, ny))\n                        visited[nx, ny] = count\n    return visited, count\n\narray2d_like.get_connected_components = get_connected_components\ndel get_connected_components\n";
+        "\ndef get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:\n    from collections import deque\n    from vmath import vec2i\n\n    DIRS = [vec2i.LEFT, vec2i.RIGHT, vec2i.UP, vec2i.DOWN]\n    assert neighborhood in ['Moore', 'von Neumann']\n\n    if neighborhood == 'Moore':\n        DIRS.extend([\n            vec2i.LEFT+vec2i.UP,\n            vec2i.RIGHT+vec2i.UP,\n            vec2i.LEFT+vec2i.DOWN,\n            vec2i.RIGHT+vec2i.DOWN\n            ])\n\n    visited = array2d[int](self.width, self.height, default=0)\n    queue = deque()\n    count = 0\n    for y in range(self.height):\n        for x in range(self.width):\n            if visited[x, y] or self[x, y] != value:\n                continue\n            count += 1\n            queue.append((x, y))\n            visited[x, y] = count\n            while queue:\n                cx, cy = queue.popleft()\n                for dx, dy in DIRS:\n                    nx, ny = cx+dx, cy+dy\n                    if self.is_valid(nx, ny) and not visited[nx, ny] and self[nx, ny] == value:\n                        queue.append((nx, ny))\n                        visited[nx, ny] = count\n    return visited, count\n\narray2d_like.get_connected_components = get_connected_components\ndel get_connected_components\n";
     if(!py_exec(scc, "array2d.py", EXEC_MODE, mod)) {
         py_printexc();
         c11__abort("failed to execute array2d.py");

+ 7 - 7
src/modules/linalg.c → src/modules/vmath.c

@@ -1,4 +1,4 @@
-#include "pocketpy/linalg.h"
+#include "pocketpy/vmath.h"
 #include "pocketpy/pocketpy.h"
 
 #include "pocketpy/common/sstream.h"
@@ -1034,7 +1034,7 @@ static bool color32_ansi_bg(int argc, py_Ref argv) {
     return true;
 }
 
-static bool linalg_rgb(int argc, py_Ref argv) {
+static bool vmath_rgb(int argc, py_Ref argv) {
     PY_CHECK_ARGC(3);
     PY_CHECK_ARG_TYPE(0, tp_int);
     PY_CHECK_ARG_TYPE(1, tp_int);
@@ -1048,7 +1048,7 @@ static bool linalg_rgb(int argc, py_Ref argv) {
     return true;
 }
 
-static bool linalg_rgba(int argc, py_Ref argv) {
+static bool vmath_rgba(int argc, py_Ref argv) {
     PY_CHECK_ARGC(4);
     PY_CHECK_ARG_TYPE(0, tp_int);
     PY_CHECK_ARG_TYPE(1, tp_int);
@@ -1077,8 +1077,8 @@ static bool color32_alpha_blend_STATIC(int argc, py_Ref argv) {
     return true;
 }
 
-void pk__add_module_linalg() {
-    py_Ref mod = py_newmodule("linalg");
+void pk__add_module_vmath() {
+    py_Ref mod = py_newmodule("vmath");
 
     py_Type vec2 = pk_newtype("vec2", tp_object, mod, NULL, false, true);
     py_Type vec3 = pk_newtype("vec3", tp_object, mod, NULL, false, true);
@@ -1263,8 +1263,8 @@ void pk__add_module_linalg() {
     py_bindmethod(color32, "to_vec3i", color32_to_vec3i);
     py_bindmethod(color32, "ansi_fg", color32_ansi_fg);
     py_bindmethod(color32, "ansi_bg", color32_ansi_bg);
-    py_bindfunc(mod, "rgb", linalg_rgb);
-    py_bindfunc(mod, "rgba", linalg_rgba);
+    py_bindfunc(mod, "rgb", vmath_rgb);
+    py_bindfunc(mod, "rgba", vmath_rgba);
     py_bindstaticmethod(color32, "alpha_blend", color32_alpha_blend_STATIC);
 }
 

+ 4 - 1
tests/72_json.py

@@ -87,4 +87,7 @@ class A:
         self.b = b
 
 a = A(1, ['2', False, None])
-assert json.dumps(a.__dict__) == '{"a": 1, "b": ["2", false, null]}'
+assert json.dumps(a.__dict__) in [
+    '{"a": 1, "b": ["2", false, null]}',
+    '{"b": ["2", false, null], "a": 1}',
+]

+ 1 - 1
tests/80_color32.py

@@ -1,4 +1,4 @@
-from linalg import color32, rgb, rgba
+from vmath import color32, rgb, rgba
 
 a = color32(100, 200, 255, 120)
 assert a.r == 100

+ 1 - 1
tests/80_linalg.py → tests/80_vmath.py

@@ -1,4 +1,4 @@
-from linalg import mat3x3, vec2, vec3, vec2i, vec3i
+from vmath import mat3x3, vec2, vec3, vec2i, vec3i
 import random
 import math
 

+ 1 - 1
tests/90_array2d.py

@@ -1,5 +1,5 @@
 from array2d import array2d
-from linalg import vec2i
+from vmath import vec2i
 
 def exit_on_error():
     raise KeyboardInterrupt

+ 1 - 1
tests/90_chunked_array2d.py

@@ -1,5 +1,5 @@
 import array2d
-from linalg import vec2i
+from vmath import vec2i
 
 
 def on_builder(a:vec2i):

+ 1 - 1
tests/90_colorcvt.py

@@ -1,5 +1,5 @@
 import colorcvt
-from linalg import vec3
+from vmath import vec3
 
 def oklch(expr: str) -> vec3:
     # oklch(82.33% 0.37 153)

+ 2 - 2
tests/90_pickle.py

@@ -22,7 +22,7 @@ test(False)                     # PKL_FALSE
 test("hello")                   # PKL_STRING
 test(b"hello")                  # PKL_BYTES
 
-from linalg import vec2, vec3, vec2i, vec3i
+from vmath import vec2, vec3, vec2i, vec3i
 
 test(vec2(2/3, 1.0))            # PKL_VEC2
 test(vec3(2/3, 1.0, 3.0))       # PKL_VEC3
@@ -186,7 +186,7 @@ class Foo:
 test(Foo(1, 2))
 test(Foo([1, True], 'c'))
 
-from linalg import vec2
+from vmath import vec2
 
 test(vec2(1, 2))
 

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно