0

can i write multiplication of matrix directly as rx=u[1][4]*m[4][4]*gx[4][4]; and how i have to initialize rx? Is there any method to do direct product or any method which will not use loops?

user2828274
  • 31
  • 1
  • 5
  • 1
    If you know the dimensions in advance (and they're small), you can always unroll the loop, i.e. assign a direct value to each element. For example, result[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0], in the case of 2x2 matrices. –  Oct 31 '13 at 15:39
  • What's wrong with loops? Otherwise you will a specialized function for the specific array size. Any time the array size changes, you will have to add more statements. – Thomas Matthews Oct 31 '13 at 15:40
  • Indeed, but sometimes you do know the dimensions of the matrices you'll be working with. I'll go for loop-unrolling then... –  Oct 31 '13 at 15:42
  • A decent optimizing compiler may unroll loops for you, and _probably_ do a better job at it. – icabod Oct 31 '13 at 15:45
  • I think that your `[]` is to indicate the size of the matrices. I think it is unclear (as the real meaning is for the index...) – Jarod42 Oct 31 '13 at 15:46
  • @icabod I can always take a peek at the assembly it generated. If I'm happy with it, then fine; otherwise I'll do it by myself. That way I'm always sure. –  Oct 31 '13 at 15:50

1 Answers1

0

Assuming your matrices are just arrays, then yes. By calling matrix[x][y] you are just accessing the variable stored at that location.

So if:

u[1][4] == 2

m[4][4] == 8

gx[4][4] == 1

then

u[1][4] * m[4][4] * gx[4][4] = 16

In regards to rx, it must be assigned to the same variable type, or one that can be implicitly casted to from what your matrices are storing (implicitly since I see no explicit casts in your example). So if your matrices are float[][] then rx should be a float.

There are also various math libraries that may do the work for you. See Looking for an elegant and efficient C++ matrix library

Community
  • 1
  • 1
ssell
  • 6,429
  • 2
  • 34
  • 49