0

I know how to find these in a 1x1 2x2 and 3x3 but I want to make a program that lets me enter the dimensions of my matrix and then enter the numbers that go in the matrix. After entering the numbers I want it to give the determinate (if able to get one) and the inverse of the matrix, but i don't know how to go around the dimensions part. What is a good way to start out on this? What headers should I use? I am running on Linux so i don't have some headers like windows does if that helps any. :)

Amro
  • 123,847
  • 25
  • 243
  • 454
Matthew
  • 1
  • 1
  • First, do you understand the (paper) math behind taking the determinant of an NxN matrix? And finding the inverse of an NxN matrix? – Cornstalks Feb 19 '13 at 23:11
  • You should learn about how you do this on paper (i.e. learn the maths behind it), then it will become clear to you. Unless of course you just want to invert matrices, but not write it yourself, then you can use a library like Eigen. – us2012 Feb 19 '13 at 23:12
  • 1
    Supposing the matrix is square, what you're probably looking for is an in-place matrix inversion algorithm. You should check out http://math.stackexchange.com/questions/16940/in-place-inversion-of-large-matrices – user2051349 Feb 19 '13 at 23:12
  • To clarify: are you asking on how to input and store matrices of arbitrary size or is this about finding the determinant/inverse? – user634618 Feb 19 '13 at 23:15
  • inputting a custom size and all the numbers in the matrix, then finding the inverse and determinant. And thanks! :) An algorithm would help a lot! I heard that arrays are good with this kind of stuff but i don't know how to use arrays... (I've been pretty simple sticking with iostream. lol. imma newb.) plus i have to start somewhere and once i figure this thing out, i'm sure it will get somewhat easier! :D – Matthew Feb 19 '13 at 23:36
  • and yes I do know how to find the determinante of any size matrix. :) – Matthew Feb 19 '13 at 23:50

2 Answers2

1

Start by picking a Linear Algebra book. The one by Gilbert Strang. It reads like a nice story book. Go through LU decomoposition and further into Eigen Values and Eigen Vectors. Somewhere along the way, you would have found the answer to both your questions.

shashydhar
  • 801
  • 3
  • 8
  • 26
1

An easy way to do it would be to use a library like Eigen.

If you really want to do it yourself, you have to

  1. decide how to represent your matrix in memory,
  2. write code to read and store the matrix according to your definition, and
  3. write code to calculate the inverse/determinant of the matrix according to your definition.

Obviously, 2. and 3. depend on your choice for 1., and finding a good representation is not trivial and depends on your application, e.g. how large your matrix is going to be or whether it is sparse or dense.

If you do not care about efficiency or just assume your matrix to be small, then you might want to represent your matrix as a one-dimensional vector. For example, of you have a 4x4 matrix, you could allocate a vector of length 16.

If I understood your question correctly, you were actually asking about how to do that dynamically (at runtime). Using arrays, it looks something like this:

double* yourMatrix
yourMatrix = new double[columnCount * rowCount];

This dynamically allocates space for columnCount * rowCount doubles, and you can now access that memory like a static array. When you are done, you have to delete[] yourMatrix to prevent memory leaks.

If you know how to calculate the determinant / inverse on paper, you should be able to go from here.

user634618
  • 3,573
  • 1
  • 24
  • 17