operator+ and operator- for Matrix classes: do they make sense?
When and why would you ever want to add, subtract, or even divide a matrix for a game engine? I'm rewriting my engine and I've encountered these operators in the old code, but I've never used them. Does it make sense? Maybe they should be private and unimplemented to make a point?
Thoughts?
division isn't even a proper matrix opperator. unless you interprit it as a multiplication by an inverse. addition is a common opperation mathematically. but usually when it's used, the person will do the addition by hand and present the final matrix in singular form for the computer.
Ax=cx, Ax-cx=0, Ax-cIx=0, (A-cI)x=0 where A is a matrix, I is the identity matrix, c is a scalar constant, x is an unknown vector and 0 is the 0 vector. Solve that for x and you get the eigenvectors of A. Extracting the axis and angle from a rotation matrix is another place you use the differance of two matrices in the calculation.
Addition and subtraction of a scalar from a matrix is a bit more unusual. It is really of no use in linear algebra and isn't even defined in linear algebra. Packages like MatLab do define it though because not all two dimensional arrays are matrices in a linear algebra sense. An example is scaling and translating a height field. You use perlin noise or some function generating numbers in the interval [0,1] or [-1,1] and you want numbers in the interval [100,200] or something. So you have to multiply and add each element of the array. Adding or subtracting a scalar to/from an array is a compact way to specify that rather than requiring the user to code a loop.
Addition and subtraction of a scalar from a matrix is a bit more unusual. It is really of no use in linear algebra and isn't even defined in linear algebra. Packages like MatLab do define it though because not all two dimensional arrays are matrices in a linear algebra sense. An example is scaling and translating a height field. You use perlin noise or some function generating numbers in the interval [0,1] or [-1,1] and you want numbers in the interval [100,200] or something. So you have to multiply and add each element of the array. Adding or subtracting a scalar to/from an array is a compact way to specify that rather than requiring the user to code a loop.
A couple more examples where these operators can be useful: lerping of matrices (addition, and division by a scalar), integration of a rotation matrix (addition, and multiplication by a scalar), and matrix inversion (division by the determinant). There may be some compelling argument to the contrary that I'm not aware of, but I would go ahead and implement all the (basic) operators that are defined mathematically for matrices, as it's likely you'll end up using them all at some time or another.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement