Skip to content

Using matrices

Zuzu-Typ edited this page Feb 4, 2020 · 10 revisions
  1. Initialization
  2. Members
  3. Methods
  4. Operators

Initialization

Matrices can be constructed in quite a few different ways.

Initialization with no arguments

Initializing a matrix without any additional arguments will return an identity matrix, meaning that it's values are set to 1 where column and row indices are equal and set to 0 otherwise.
This results in a diagonal line of ones from top left to bottom right.
Example:

>>> print(glm.mat2())
[ 1 | 0 ]
[ 0 | 1 ]

>>> print(glm.mat4())
[ 1 | 0 | 0 | 0 ]
[ 0 | 1 | 0 | 0 ]
[ 0 | 0 | 1 | 0 ]
[ 0 | 0 | 0 | 1 ]

>>> print(glm.mat2x4())
[ 1 | 0 | 0 | 0 ]
[ 0 | 1 | 0 | 0 ]

Initialization with a single number

Initializing a matrix with a single number returns a matrix similar to the identity matrices shown above.
The only difference is that instead of setting equal column and row indices to 1, they're set to the provided number.
Example:

>>> print(glm.mat2(3))
[ 3 | 0 ]
[ 0 | 3 ]

>>> print(glm.mat4(0))
[ 0 | 0 | 0 | 0 ]
[ 0 | 0 | 0 | 0 ]
[ 0 | 0 | 0 | 0 ]
[ 0 | 0 | 0 | 0 ]

>>> print(glm.mat2x4(1.5))
[ 1.5 |   0 | 0 | 0 ]
[   0 | 1.5 | 0 | 0 ]

Initializing all components with numbers

A matrix matNxM can be initialized with N x M numbers, which will be copied (or may be converted) to their respective components.
Example:

>>> print(glm.mat2(1, 2, 3, 4))
[ 1 | 2 ]
[ 3 | 4 ]

>>> print(glm.mat4(*range(16)))
[  0 |  1 |  2 |  3 ]
[  4 |  5 |  6 |  7 ]
[  8 |  9 | 10 | 11 ]
[ 12 | 13 | 14 | 15 ]

Copying a matrix

A copy of a matrix can be obtained by initializing a matrix with an instance of a matrix.
i.e. glm.mat2(glm.mat2(5, 2, 4, 3)) returns matrix ((5, 2), (4, 3))
This is what's known as the copy constructor.

Initializing matrices with other matrices

You can initialize any matrix with another matrix (as long as they are from the same datatype).
Any values that don't fit into the new matrix are discarded and any values that aren't filled by the supplied matrix are padded up with values from the respective identity matrix.
Example:

>>> print(glm.mat4( glm.mat2(9, 8, 7, 6) ))
[ 9 | 8 | 0 | 0 ]
[ 7 | 6 | 0 | 0 ]
[ 0 | 0 | 1 | 0 ]
[ 0 | 0 | 0 | 1 ]

>>> m44 = glm.mat4(*range(16))
>>> print(m44)
[  0 |  1 |  2 |  3 ]
[  4 |  5 |  6 |  7 ]
[  8 |  9 | 10 | 11 ]
[ 12 | 13 | 14 | 15 ]

>>> print(glm.mat2(m44))
[ 0 | 1 ]
[ 4 | 5 ]

>>> m32 = glm.mat3x2(6, 5, 4, 3, 2, 1)
>>> print(m32)
[ 6 | 5 ]
[ 4 | 3 ]
[ 2 | 1 ]

>>> print(glm.mat2x3(m32))
[ 6 | 5 | 0 ]
[ 4 | 3 | 0 ]

>>> print(glm.mat3(m32))
[ 6 | 5 | 0 ]
[ 4 | 3 | 0 ]
[ 2 | 1 | 1 ]

>>> print(glm.imat2(glm.dmat2(7.9)))

TODO

Constructing vectors from other vectors and numbers

As long as you don't use any vec1s in your equation, you can construct any vector from a combination of vectors and / or numbers if their sum equals the length of the target vector.
i.e. glm.vec4(glm.vec2(1, 2), 3, 4) returns vector (1.0, 2.0, 3.0, 4.0)
likewise glm.vec3(5, glm.vec2(4, 3)) returns vector (5.0, 4.0, 3.0)

but glm.vec2(glm.vec1(1), 2) doesn't work.
glm.vec3(glm.vec2(1, 2), glm.vec2(3, 4)) also doesn't work.

Lists (and other iterables)

Instead of using vectors to initialize vectors, you can also use lists and other iterables.
e.g. glm.vec2([1, 2]) returns vector (1.0, 2.0)
or glm.vec3((3, 4), 5) returns vector (3.0, 4.0, 5.0)
(if you do not need this functionality, you might want to use PyGLM_FAST - see Building PyGLM)

Objects that support the buffer protocol (numpy, bytes)

A few objects in Python support a functionality called the buffer protocol.
One such example would be the Python bytes type or numpy.array.
PyGLM also supports this protocol and thus can be converted to or from any other object that supports it, granted it's in a fitting format.
e.g. bytes(glm.u8vec2(1,2)) returns b'\x01\x02'
and glm.u8vec2(b'\x01\x02') returns an 8-bit unsigned integer vector (1, 2)

or glm.vec3(numpy.array([4,5,6])) returns vector (4.0, 5.0, 6.0)
and numpy.array(glm.vec3(4, 5, 6)) returns array([4., 5., 6.], dtype=float32)

Note: objects that use the buffer protocol may request a reference instead of a copy of the object, meaning that if you change the 'copy', you'll also change the original.

(if you do not need this functionality, you might want to use PyGLM_FAST - see Building PyGLM)

Members

A vector has a member for each of it's values.
vec1 has members: (x)
vec2 has members: (x, y)
vec3 has members: (x, y, z)
vec4 has members: (x, y, z, w)

Using swizzling, you can also construct vectors from up to four members:

v  = vec4(1, 2, 3, 4)
v2 = v.xy             # returns vec2(1, 2)
v3 = v.zw             # returns vec2(3, 4)
v4 = v.xxxw           # returns vec4(1, 1, 1, 4)

Methods

Any vector type implements the following methods:

The copy protocol

Vectors support the copy protocol (see here).
You can use copy.copy(<vector>) or copy.deepcopy(<vector>) to get a copy of a vector.

To list / tuple

Any vector type has a to_list() and a to_tuple() function, which return's the vector's data represented as a list or tuple respectively.

Operators

Vector types support a lot of operators.

add (+ operator)

Vectors support addition with other vectors and numbers.

sum1 = vec2(1, 2) + vec2(4, 0) # returns vec2(5, 2)
sum2 = vec2(1, 2) + 4          # returns vec2(5, 6)

sub (- operator)

Vectors support subtraction with other vectors and numbers.

diff1 = vec2(1, 2) - vec2(4, 0) # returns vec2(-3,  2)
diff2 = vec2(1, 2) - 4          # returns vec2(-3, -2)

mul (* operator)

Vectors support multiplication with other vectors and numbers.

prod1 = vec2(1, 2) * vec2(4, 0) # returns vec2(4, 0)
prod2 = vec2(1, 2) * 4          # returns vec2(4, 8)

div (/ operator)

Vectors support division with other vectors and numbers.

quot1 = vec2(1, 2) / vec2(4, 0.5) # returns vec2(0.25, 4  )
quot2 = vec2(1, 2) / 4            # returns vec2(0.25, 0.5)

mod (% operator)

Vectors support modulo operations with other vectors and numbers.

mod1 = vec2(1, 2) % vec2(4, 2) # returns vec2(1, 0)
mod2 = vec2(1, 2) % 4            # returns vec2(1, 2)

floordiv (// operator)

Vectors support floored division with other vectors and numbers.

fquot1 = vec2(1, 2) // vec2(4, 0.5) # returns vec2(0, 4)
fquot2 = vec2(1, 2) // 4            # returns vec2(0, 0)

divmod

Vectors support combined floor division and modulo operations with other vectors and numbers.

divmod1 = divmod(vec2(1, 2), vec2(4, 2)) # returns (vec2(0, 1), vec2(1, 0))
divmod2 = divmod(vec2(1, 2), 4)          # returns (vec2(0, 0), vec2(1, 2))

pow (** operator)

Vectors support pow operations with other vectors and numbers.

pow1 = vec2(1, 2) ** vec2(4, 2) # returns vec2(1,  4)
pow2 = vec2(1, 2) ** 4          # returns vec2(1, 16)

len

The length of a vector can be queried using len().

vec_length = len(vec2()) # returns 2

getitem and setitem ([] operator)

You can get the values of a vector using indices.

v = vec2(1, 2)
print(v[0]) # prints 1.0
print(v[1]) # prints 2.0

Likewise you can set the values.

v    = vec2(1, 2)
v[0] = 9
print(v.x) # prints 9.0

contains (in operator)

You can query wether or not a value is contained by a vector using the in operator.

v     = vec2(1, 2)
true  = 2    in v
false = 2.01 in v

richcompare (e.g. == operator)

You can compare vectors using the richcompare operators:

vec2(1, 2) == vec2(1, 2)    # true
vec2(1, 2) == vec2(2, 2)    # false
vec2(1, 2) == vec3(1, 2, 3) # false
vec2(1, 2) == dvec2(1, 2)   # true

vec2(1, 2) != vec2(1, 2)    # false
vec2(1, 2) != vec2(2, 2)    # true
vec2(1, 2) != vec3(1, 2, 3) # true
vec2(1, 2) != dvec2(1, 2)   # false

vec2(1, 2) < vec2(5, 5)     # vec2(1, 1)
vec2(1, 2) < vec2(2, 2)     # vec2(1, 0)
vec2(1, 2) < vec2(0, 0)     # vec2(0, 0)

vec2(1, 2) <= vec2(5, 5)    # vec2(1, 1)
vec2(1, 2) <= vec2(2, 2)    # vec2(1, 1)
vec2(1, 2) <= vec2(0, 0)    # vec2(0, 0)

vec2(1, 2) > vec2(5, 5)     # vec2(0, 0)
vec2(1, 2) > vec2(2, 2)     # vec2(0, 0)
vec2(1, 2) > vec2(0, 0)     # vec2(1, 1)

vec2(1, 2) >= vec2(5, 5)     # vec2(0, 0)
vec2(1, 2) >= vec2(2, 2)     # vec2(0, 1)
vec2(1, 2) >= vec2(0, 0)     # vec2(1, 1)

iter

You can generate an iterable from vectors using iter().

v  = vec2(1, 2)
it = iter(v)
print(next(it)) # prints 1.0
print(next(it)) # prints 2.0
Clone this wiki locally