@@ -18,79 +18,137 @@ This is a hobby project for my own needs (games/simulations). If it helps you, f
1818
1919## Installation
2020
21- Requirements: Lua >= 5.2
21+ Requirements: Lua >= 5.4
2222
2323### Using LuaRocks (local install from this repo)
2424
2525``` bash
26- luarocks make rockspecs/vmsl-1.0-1 .rockspec
26+ luarocks make rockspecs/vmsl-1.0-4 .rockspec
2727```
2828
2929Then, in your code:
3030
3131``` lua
32- local V = require (" vectors" )
33- local M = require (" matrix" )
34- -- ou
35- local VMSL = require (" VMSL" )
36- local V = VMSL .vectors
37- local M = VMSL .matrix
32+ local V = require (" vectors" ) -- exports from vectors/core.lua
33+ local M = require (" matrix" ) -- exports from matrix/core.lua
3834```
3935
36+ Breaking change (since 1.0-4): the root ` init.lua ` aggregator module was removed; require modules directly.
37+
4038### Without LuaRocks (adjusting package.path)
4139
4240Point your ` package.path ` to this repo and require the modules:
4341
4442``` lua
45- -- Adapt the path below to where you cloned this repository
46- package.path = package.path .. " ;/path/to/VectorAndMatrixSystemLua/?/init .lua"
43+ -- Adapt the path; we append both module roots
44+ package.path = package.path .. " ;/path/to/VectorAndMatrixSystemLua/?.lua;/path/to/VectorAndMatrixSystemLua/?/core .lua"
4745
48- local V = require (" vectors" )
49- local M = require (" matrix" )
46+ local V = require (" vectors" ) -- vectors/core.lua via rockspec mapping or custom path
47+ local M = require (" matrix" ) -- matrix/core.lua
5048```
5149
52- Note: there is a root ` init.lua ` that exposes both modules through a single table:
53-
54- ``` lua
55- local VMSL = require (" VMSL" )
56- local V = VMSL .vectors
57- local M = VMSL .matrix
58- ```
59-
60- Requiring ` vectors ` and ` matrix ` directly is often the clearest approach at this stage.
50+ Root aggregator no longer exists; prefer direct requires.
6151
6252## Quick start
6353
6454``` lua
65- -- Vectors
55+ -- Vectors basics
6656local V = require (" vectors" )
6757local v1 = V .CreateVector (1 , 2 , 3 )
6858local v2 = V .CreateVector (4 , 5 , 6 )
6959
7060print (v1 ) -- Vector3:{ 1, 2, 3 }
7161print (# v1 ) -- 3 (dimension)
72- print (" dot:" , v1 :dot (v2 )) -- 32
62+ print (" dot:" , v1 :Dot (v2 )) -- 32 (capitalized internal name; colon allows method)
7363
74- local v3 = v1 + v2 -- component-wise sum
64+ local v3 = v1 + v2 -- component-wise sum (uses __add)
7565print (v3 ) -- Vector3:{ 5, 7, 9 }
7666
77- -- Matrices
67+ -- Matrix creation
7868local M = require (" matrix" )
79- local A = M .transformInMatrix ({
80- { 1 , 2 },
81- { 3 , 4 }
82- })
69+ local A = M .CreateMatrix ( 2 , 2 ). data ( 1 , 2 , 3 , 4 ) -- row-major fill
70+ local B = M . CreateRowMatrix ( 1 , 2 , 3 ) -- 3x1
71+ local C = M . CreateColumnMatrix ( 4 , 5 , 6 ) -- 1x3
72+ local Z = M . CreateNullMatrix ( 2 , 3 ) -- 2x3 all zeros
8373
8474print (A )
8575-- | 1 2 |
8676-- | 3 4 |
8777
8878print (" det(A):" , A :determinant ()) -- -2
79+ print (" A transpose:\n " , A :T ()) -- __tostring prints nicely
80+
81+ -- Cross product (2D -> perpendicular vector)
82+ local v2d = V .CreateVector (3 , 4 )
83+ print (" cross 2D:" , v2d :Cross ()) -- Vector2:{ -4, 3 }
84+
85+ -- Cross product (3D)
86+ local v3a = V .CreateVector (1 , 2 , 3 )
87+ local v3b = V .CreateVector (2 , 3 , 1 )
88+ print (" cross 3D:" , v3a :Cross (v3b )) -- Vector3:{ -7, 5, -1 }
89+
90+ -- Generalized cross product (n>3): need n-1 equipollent vectors
91+ -- Example for 5D: result is a vector orthogonal to all given vectors
92+ local v5a = V .CreateVector (1 ,2 ,0 ,1 ,3 )
93+ local v5b = V .CreateVector (0 ,1 ,1 ,2 ,1 )
94+ local v5c = V .CreateVector (3 ,1 ,0 ,0 ,2 )
95+ local v5d = V .CreateVector (1 ,0 ,2 ,1 ,1 )
96+ local orth5 = v5a :Cross (v5b , v5c , v5d )
97+ print (" cross 5D:" , orth5 )
8998```
9099
91100> API stability: experimental. Names, signatures and behavior may change as the project evolves.
92101
93- ## API style
102+ ## API style & surface
103+
104+ Vector factory (selected):
105+ | Function | Description |
106+ | ----------| -------------|
107+ | ` CreateVector(...) ` | Build a vector from numeric coordinates. Errors if any argument is non-number or empty list. |
108+ | ` CreateConstVector(dim,value) ` | Fill vector with ` value ` (defaults: dim=1,value=0). |
109+ | ` CreateVectorZero(n) ` | Shorthand for all zeros. |
110+ | ` CreateVectorOne(n) ` | Shorthand for all ones. |
111+
112+ Vector operations (methods via colon or metamethods):
113+ | Operation | Form | Notes |
114+ | -----------| ------| -------|
115+ | Addition | ` v1 + v2 ` | Requires equipollence. |
116+ | Subtraction | ` v1 - v2 ` | Requires equipollence. |
117+ | Unary minus | ` -v ` | Component negation. |
118+ | Dot product | ` v1:Dot(v2) ` or ` v1 * v2 ` | Scalar when both vectors; scalar-times-vector also supported (right side). |
119+ | Map | ` v:map(function(i,val) ... end) ` | Returns new transformed vector. |
120+ | Projection | ` v1:projection(v2) ` | Projection of v2 onto v1 (returns vector). |
121+ | Cross (2D) | ` v:Cross() ` | Perpendicular ` (-y,x) ` . |
122+ | Cross (3D) | ` v1:Cross(v2) ` | Standard cross product. |
123+ | Cross (n>3) | ` v1:Cross(v2, ..., v_{n-1}) ` | Generalized cofactor-based orthogonal vector. |
124+ | Dimension | ` #v ` | Returns number of components. |
125+ | Equipollence check | ` v1:checkEquipollence(v2) ` | Returns bool, dimension. |
126+
127+ Matrix factory:
128+ | Function | Description |
129+ | ----------| -------------|
130+ | ` CreateMatrix(rows, cols) ` | Creates empty then fill via ` .data(...) ` . |
131+ | ` CreateRowMatrix(...) ` | 1×N row. |
132+ | ` CreateColumnMatrix(...) ` | N×1 column. |
133+ | ` CreateNullMatrix(r,c) ` | All zero entries. |
134+
135+ Matrix operations:
136+ | Operation | Form | Notes |
137+ | -----------| ------| -------|
138+ | Addition | ` A + B ` | Same shape required. |
139+ | Subtraction | ` A - B ` | Same shape required. |
140+ | Scalar mult | ` k * A ` or ` A * k ` | Returns scaled matrix. |
141+ | Matrix mult | ` A * B ` | Requires ` A.ncols == B.nrows ` . Produces ` (A.nrows x B.ncols) ` . |
142+ | Map | ` A:map(function(pos,val) ... end) ` | Returns new matrix. ` pos.i ` , ` pos.j ` . |
143+ | Transpose | ` A:T() ` | Returns ` A^T ` . |
144+ | Determinant | ` A:determinant() ` | Recursive (slow for large n). Square matrices only. |
145+ | Extended determinant | ` A:ExtendDeterminant() ` | Internal symbolic/extended form (used previously). |
146+ | Submatrix | ` A:submatrix(i,j) ` | Minor without row i, col j. |
147+
148+ Version / dependency changes:
149+ - Lua minimum version raised from 5.2 to 5.4 in rockspec 1.0-3.
150+ - Removed root aggregator module (` init.lua ` ).
151+ - Modules exported directly as ` vectors ` and ` matrix ` .
94152
95153- Style: Lua module API accessed via ` require ` .
96154- Surface: modules ` vectors ` and ` matrix ` are exposed directly, and also via root module ` VMSL ` (` VMSL.vectors ` , ` VMSL.matrix ` ).
@@ -107,8 +165,61 @@ print("det(A):", A:determinant()) -- -2
107165
108166- Better docs (more examples and topic-based guides).
109167- LuaRocks packaging improvements.
110- - Automated tests for base and edge cases.
111- - Additional operations for matrices and vectors (matrix multiplication, transpose, norms, etc.).
168+ - Automated tests (coverage: vector/matrix creation, arithmetic, error paths).
169+ - Norms, distances, angle utilities for vectors.
170+ - LU / Gaussian determinant + matrix inversion.
171+ - Null-space based generalized cross product (faster for n>7).
172+ - Left scalar multiplication support for vectors (` number * vector ` ).
173+ - Performance benchmarks + optional optimization layer.
174+
175+ ## Cross product behavior
176+
177+ | Dimension | Call pattern | Result |
178+ | -----------| ---------------------------------------| --------|
179+ | 2D | ` v:Cross() ` | Perpendicular vector ` (-y, x) ` |
180+ | 3D | ` v1:Cross(v2) ` | Classical cross product |
181+ | n>3 | ` v1:Cross(v2, ..., v_{n-1}) ` | Generalized (cofactor-based) cross product, orthogonal to all input vectors |
182+
183+ Rules:
184+ - All vectors must be equipollent (same dimensions) or an error is raised.
185+ - For n>3 you must supply exactly n-1 vectors total (the receiver ` v1 ` plus n-2 extra).
186+ - Implementation for n>3 uses cofactors (Laplace expansion) and is VERY expensive for large n (factorial growth) and can overflow numeric ranges quickly.
187+
188+ Performance notes:
189+ - 2D/3D versions are fast (O(1)).
190+ - n>3 version is exponential; avoid for n>7 unless necessary. Future optimization will switch to a more efficient method (e.g. forming an (n-1)×n matrix and computing a null-space via Gaussian elimination).
191+
192+ ## Recent changes (since <=1.0-1)
193+
194+ - Rockspec now maps directly to ` matrix/core.lua ` and ` vectors/core.lua ` (removed stale ` init.lua ` ).
195+ - Added ` checkEquipollence ` to vector ops; fixed nil method errors in dot/cross.
196+ - Vector creation helpers: ` CreateConstVector ` , ` CreateVectorZero ` , ` CreateVectorOne ` documented.
197+ - Cross product fully refactored (2D / 3D direct formulas; n>3 numeric cofactors).
198+ - Fixed 2D cross "dimensionless vector" bug.
199+ - Added scalar-matrix multiplication both sides.
200+ - Added matrix transpose ` T ` .
201+ - Matrix multiplication with shape checks.
202+
203+ ## Known limitations / caveats
204+
205+ - Scalar multiplication left-associative (` number * vector ` ) still unsupported; use ` vector * number ` .
206+ - Generalized cross product: factorial cost & large magnitudes; normalize if needed.
207+ - Determinant: recursive Laplace expansion (slow, no numeric optimization yet).
208+ - No matrix inversion / norms yet.
209+ - Extended determinant symbolic path retained only for legacy; may be removed.
210+
211+ ## Normalizing large cross product results
212+
213+ Example:
214+ ``` lua
215+ local big = v5a :Cross (v5b , v5c , v5d )
216+ -- Normalize (L2)
217+ local lenSq = 0
218+ big :map (function (_ , val ) lenSq = lenSq + val * val end )
219+ local len = math.sqrt (lenSq )
220+ local unit = big :map (function (_ , val ) return val / len end )
221+ print (" unit orthogonal:" , unit )
222+ ```
112223
113224## Contributing
114225
0 commit comments