Storing Matrices for Multiplying

Just starting out? Need help? Post your questions and find answers here.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 304
Joined: Tue Sep 05, 2017 10:07 am

Storing Matrices for Multiplying

Post 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?
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Storing Matrices for Multiplying

Post by jacdelad »

Maybe an array with more dimensions? Or an array of a custom structure?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
SMaag
Enthusiast
Enthusiast
Posts: 324
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Storing Matrices for Multiplying

Post 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
Post Reply