• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Mantear

Matrix inversion

7 posts in this topic

I'm a linear algebra weakling and am looking for help anywhere I can find it.

 

I'm attempting to calculate the inverse of a 14x14 square matrix using Gauss-Jordan elimination.  This seems to work for some matrix input, but not all.  I end up with an almost zero value on the diagonal, which really blows up the scaling of the values in the matrix.

 

I'm comparing my results to those calculated by Matlab.  For my initial set of matricies, the results match (within rounding differences).  But for the following matrix, they do not.  (sorry for the large data set: if there's a better way to show this please let me know).

 

 

    -10.685512    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    63.545570    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    -213.193800    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    493.092433    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    -831.846712    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    1055.702065    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    -1024.015861    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    762.935063    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    -434.501577    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000
    186.205620    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000
    -58.228421    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000
    12.560150    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000
    -1.672796    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000
    0.103779    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000
 
Element (14,14) becomes nearly zero [~4.354*10^-12] when eliminating element (14,13) by scaling and subtracting row 13 from row 14.  So when I go to scale row 14 to get the diagonal value to equal 1.0, the scaling just gets huge.  [Note: I'm using long double data types to operate on.]
The Matlab result is also a matrix with very large values, but they are quite different than mine.  I'm guess the very small/very large numbers are throwing things off, but I can't tell where.  Is there a different technique I need to employ to get the same answer as Matlab?
0

Share this post


Link to post
Share on other sites

Your matrix is very badly conditioned and will be extremely sensitive to numerical errors. If you want the same results as Matlab, then you're simply going to have to do the same calculations as Matlab.

 

As far as I remember, the Gauss-Jordan method is one of the worst general matrix inversion algorithms when it comes to numerical precision. You really need to feed it well-conditioned matrices for it to work.

1

Share this post


Link to post
Share on other sites

@ Brother Bob;  I'm sure that's good advice.  The problem is I don't understand enough for it to help me.  I wouldn't know how to properly condition the matrix.  I also do not know how Matlab is coming up with its answer.

 

@ unbird: I'm looking at your linked article.  My current solution claims to use row-pivoting; I'm going to attempt to better understand pivoting to see if: A) i'm already pivoting and B) if I'm not, if doing so will help.

 

Thanks.

0

Share this post


Link to post
Share on other sites

@ Brother Bob;  I'm sure that's good advice.  The problem is I don't understand enough for it to help me.  I wouldn't know how to properly condition the matrix.  I also do not know how Matlab is coming up with its answer.

The condition of the matrix is a property of the matrix itself and not something you can just adjust to improve it. Your specific matrix has a specific condition, and the specific matrix you posted is not well conditioned. In this context, you could see the condition of the matrix as how numerically sensitive it is to perform calculations on it.

 

If the exact matrix itself is not important but its general structure is, then you can improve the condition by a technique called regularization. It basically means adding a constant value to the diagonal of your matrix, effectively turning your matrix M into the new matrix M'=M+r*I where r is called a regularization factor and I is the identity matrix.

Edited by Brother Bob
0

Share this post


Link to post
Share on other sites

Matlab has a lot of algorithms it uses to solve problems where matrix inversion might be used. Which one it uses depends on its own analysis of the input matrix to determine what answer is most correct. In fact, Matlab tells you not to use the result of inv(A) to solve an Ax=b problem because of numerical error, probably because inv(A) is a quick naive Gauss elimination algorithm.

 

Looking at your matrix, it could be using a sparse matrix algorithm because a lot of the entries are zero, but the size of the matrix makes me think it's not. If you're not explicitly using a specific algorithm in your code, I think there is a command for it to tell you which algorithms it's using to solve your problem. By the way, which command are you using to get the inverse in Matlab?

 

For your information, naive Gaussian elimination is not numerically stable because of the mathematical roundoff errors that arise from the precision in the computer. Even though you might be calculating with double precision, tiny errors due to roundoff can compound to throw off the entire result if not checked. Partial pivoting checks these small errors so they don't become big. Complete pivoting does this even better, but takes more computations to do this. However, if you're trying to solve an Ax=B problem, don't use matrix inversion. There are better techniques for this, such as LU decomposition.

2

Share this post


Link to post
Share on other sites

This is a rank-1 update to a very nice matrix, so you could use the Sherman-Morrison formula:
http://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula

 

bbe580913be6ab3b0f74fc48e47b2440.png

Here

A = 
    [1    -1     0     0     0     0     0     0     0     0     0     0     0     0
     0     1    -1     0     0     0     0     0     0     0     0     0     0     0
     0     0     1    -1     0     0     0     0     0     0     0     0     0     0
     0     0     0     1    -1     0     0     0     0     0     0     0     0     0
     0     0     0     0     1    -1     0     0     0     0     0     0     0     0
     0     0     0     0     0     1    -1     0     0     0     0     0     0     0
     0     0     0     0     0     0     1    -1     0     0     0     0     0     0
     0     0     0     0     0     0     0     1    -1     0     0     0     0     0
     0     0     0     0     0     0     0     0     1    -1     0     0     0     0
     0     0     0     0     0     0     0     0     0     1    -1     0     0     0
     0     0     0     0     0     0     0     0     0     0     1    -1     0     0
     0     0     0     0     0     0     0     0     0     0     0     1    -1     0
     0     0     0     0     0     0     0     0     0     0     0     0     1    -1
     0     0     0     0     0     0     0     0     0     0     0     0     0     1]
u= [-11.7, 63.5,  -213.2,  493.1,  -831.8, 1055.7,  -1024.0,  762.9,  -434.5,  186.2,   -58.2,  12.6,  -1.7,  0.1]'
v=[1,0,0,0,0,0,0,0,0,0,0,0,0,0]'

Interestingly, some matlab calculations reveal this:

 

inv(A)=     
    [1     1     1     1     1     1     1     1     1     1     1     1     1     1
     0     1     1     1     1     1     1     1     1     1     1     1     1     1
     0     0     1     1     1     1     1     1     1     1     1     1     1     1
     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     0     0     0     0     1     1     1     1     1     1     1     1     1     1
     0     0     0     0     0     1     1     1     1     1     1     1     1     1
     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     0     0     0     0     0     0     0     1     1     1     1     1     1     1
     0     0     0     0     0     0     0     0     1     1     1     1     1     1
     0     0     0     0     0     0     0     0     0     1     1     1     1     1
     0     0     0     0     0     0     0     0     0     0     1     1     1     1
     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     1]
 

 

Also, 

1+v'*invA*u = 1.0000e-06


Now at this point you could use just perform the above sequence of calculations to solve the linear system
So... putting this together

invM = invA-(invA*u*v'*invA)/(1+v'*invA*u)


   1.0e+08 *


  Columns 1 through 11


    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100
   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069
    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286
   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033
    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276
   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909
    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661
   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740
    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553
   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897
    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724
   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099
    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157
   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010


  Columns 12 through 14


    0.0100    0.0100    0.0100
   -0.1069   -0.1069   -0.1069
    0.5286    0.5286    0.5286
   -1.6033   -1.6033   -1.6033
    3.3276    3.3276    3.3276
   -4.9909   -4.9909   -4.9909
    5.5661    5.5661    5.5661
   -4.6740   -4.6740   -4.6740
    2.9553    2.9553    2.9553
   -1.3897   -1.3897   -1.3897
    0.4724    0.4724    0.4724
   -0.1099   -0.1099   -0.1099
    0.0157    0.0157    0.0157
   -0.0010   -0.0010   -0.0010
 

And it seems to be decently accurate:

norm(invM*M-eye(14))


ans =


   9.5220e-05

This is pretty good, but if you want to go further, in practice you might instead apply the inverse to solve a particular system rather than computing the full inverse. In that case you can just do the matrix multiplications in the sherman-morrison formula to your right hand side vector one at a time.

Edited by Nick Alger
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0