Page 1 of 1
Storing Matrices for Multiplying
Posted: Wed Aug 06, 2025 5:57 am
by matalog
Is there a most suitable way of storing multiple values related to one entry?
For example if I have a Matrix of 1 row by 3 columns, and I want to multiply that by a Matrix of 3 rows by 3 columns, I will want to access the values individually of each Matrix.
Initially I thought of storing the values in an array, but then I need to specify multiple values to get a single value back, whereas I want to specify a single value and get multiple values back.
Maybe Vectors is another way of looking at it, is there existing vector math in Purebasic?
Re: Storing Matrices for Multiplying
Posted: Wed Aug 06, 2025 6:52 am
by jacdelad
Maybe an array with more dimensions? Or an array of a custom structure?
Re: Storing Matrices for Multiplying
Posted: Wed Aug 06, 2025 9:41 am
by SMaag
Matrix of 1 row by 3 columns => thats a Vector
here is my definiton for 3D coordinates! A 4D Vector and a 4x4 Matrix.
If you want to do to 3D calculations it is better to use 4D defintions.
That's the standard for 3D operations.
Code: Select all
Structure TPoint2D Align 4 ; to be sure it will be 4Byte Aligned at x64
X.f
Y.f
EndStructure
#VEC_X = 0
#VEC_Y = 1
#VEC_Z = 2
#VEC_W = 3
Structure TVector Align 4 ; Single precicion Vector [16 Bytes / 128 Bit]
StructureUnion
v.f[0] ; virutal Array v[0]=x, v[1]=y, v[2]=z, v[3]=w
Pt2D.TPoint2D[0]
EndStructureUnion
x.f
y.f
z.f
w.f
EndStructure
Debug "SyzeOf(VECf::TVector) = " + SizeOf(TVector)
; we need this construction of a Matrix because in common C and Pascal Code
; it's in this way
; ------------------
; m11 m12 m13 m14
; m21 m22 m23 m24
; m31 m32 m33 m34
; m41 m42 m43 m44
; ------------------
; This is the C-Definition of a TMatrix3D
; struct
; float m11;
; float m12;
; float m13;
; float m21;
; float m22;
; float m23;
; float m31;
; float m32;
; float m33;
; Single precicion Matrix
Structure TMatrix Align 4 ; to be sure it will be 4Byte Aligned at x64
StructureUnion
v.TVector[0] ; Vector interpretation of the Matrix Structure
Pt2D.TPoint2D[0] ; Point interpretation of the Matrix Structure
EndStructureUnion
m11.f : m12.f : m13.f : m14.f
m21.f : m22.f : m23.f : m24.f
m31.f : m32.f : m33.f : m34.f
m41.f : m42.f : m43.f : m44.f
EndStructure
here you can find the full code
https://github.com/Maagic7/PureBasicFra ... VECTORf.pb