Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Oct 18th, 2007, 3:15 PM   #1
juggernot
Newbie
 
Join Date: Oct 2007
Posts: 6
Rep Power: 0 juggernot is on a distinguished road
Guass Jordan Program- Inverse

Hi, I'm new to these forums. I'm a first year student in University, and we are learning Java code. I'm working on a project(not for school work), which I need some help with. This requires some knowledge of matrices and their mathematical application. I've created a program that lets the user create a matrix. The program then uses Guass Jordan Elimination to put the matrix into its Reduced Row Echelon form. I've recently tried editing the program so that it would also find the inverse of the given matrix. I thought this would be pretty simple. I simply perform the same operations on the given matrix and the identity matrix. I should end up with the given matrix becoming the identity(which I do), and the identity becoming the inverse(not quite). What I get resembles the inverse, some of the entries are the same. I'll post my code below. There is a lot more output then necessary, as I'm in the debugging process.I output the inverse matrix after each step to see where I go wrong. I hope you find my code understandable. If anyone needs me to I will try to explain the Gauss Jordan Algorithm.
java Syntax (Toggle Plain Text)
  1. //Construct the Identity Matrix
  2. identity = new double[rows][coloumns];
  3. for (int x=0;x<rows;x+=1){
  4. for (int y=0;y<coloumns;y+=1){
  5. if ( x==y){
  6. identity[x][y] = 1;
  7. }else{
  8. identity[x][y] = 0;
  9. }
  10. }
  11. }
  12. inverse = new double[rows][coloumns];
  13. for (int x = 0;x<rows;x++){
  14. for (int y = 0;y<coloumns;y++){
  15. inverse[x][y] = identity[x][y];
  16. }
  17. }
  18.  
  19.  
  20. //Outputs initial matrix
  21. System.out.println("Initial:");
  22. for (int x=0;x<rows;x+=1){
  23. for (int y=0;y<coloumns;y+=1){
  24. output = output +"|"+matrix[x][y];
  25. }
  26. System.out.println(output);
  27. output="";
  28. }
  29. //outputs identity matrix
  30. System.out.println();
  31. System.out.println("Identity");
  32. for (int x=0;x<rows;x+=1){
  33. for (int y=0;y<coloumns;y+=1){
  34. output = output +"|"+inverse[x][y];
  35. }
  36. System.out.println(output);
  37. output="";
  38. }
  39. System.out.println();
  40.  
  41.  
  42.  
  43. //Begin Guassian Elimination
  44. //for each coloumn, check each row for leading integer. If that integer is not found in the current working row, move it there. If no integer is found in the coloumn, move to next one.
  45. int workingRow=0;
  46. int workingColoumn=0;
  47. boolean leadingI = false;
  48. for (entry=0;entry<coloumns;entry+=1){
  49. for(i=workingRow;i<rows;i+=1){
  50. if (matrix[i][entry]!=0.0){
  51. if (i!=workingRow){
  52. //Bring Row with leading integer to the working row. Do ERO for Identity as well.
  53. for(int j = 0;j<coloumns;j+=1){
  54. tempdouble=inverse[workingRow][j];
  55. inverse[workingRow][j] = inverse[i][j];
  56. inverse[i][j] = tempdouble;
  57. tempdouble = matrix[workingRow][j];
  58. matrix[workingRow][j] = matrix[i][j];
  59. matrix[i][j] = tempdouble;
  60.  
  61. }
  62. }
  63. //Outputs Inverse Matrix
  64. System.out.println();
  65. System.out.println("Inverse");
  66. for (int x=0;x<rows;x+=1){
  67. for (int y=0;y<coloumns;y+=1){
  68. output = output +"|"+inverse[x][y];
  69. }
  70. System.out.println(output);
  71. output="";
  72. }
  73. System.out.println();
  74.  
  75.  
  76. //Make Working Row a leading 1. Multiply Identity by same scaler.
  77. scaler = 1/matrix[workingRow][workingColoumn];
  78. for (int j = 0;j<coloumns;j+=1){
  79. System.out.println(matrix[workingRow][j]+"*"+scaler);
  80. inverse[workingRow][j] = inverse[workingRow][j]*scaler;
  81. matrix[workingRow][j] = matrix[workingRow][j]*scaler;
  82.  
  83.  
  84. }
  85.  
  86. //Outputs Inverse Matrix
  87. System.out.println();
  88. System.out.println("Inverse");
  89.  
  90. for (int x=0;x<rows;x+=1){
  91. for (int y=0;y<coloumns;y+=1){
  92. output = output +"|"+inverse[x][y];
  93. }
  94. System.out.println(output);
  95. output="";
  96. }
  97. System.out.println();
  98.  
  99.  
  100. //Subtract mulitples of the working row from rows beneath, only if the row beneath doesn't have an entry of zero. Do same to Identity.
  101.  
  102. for (int k = workingRow+1; k<rows;k+=1){
  103. scaler = matrix[k][workingColoumn];
  104. if (matrix[k][workingColoumn] !=0.0){
  105. for (int j=workingColoumn;j<coloumns;j+=1){
  106. System.out.println(matrix[k][j]+"-"+(matrix[workingRow][j]*scaler)+ "=" + (matrix[k][j]- (matrix[workingRow][j]*scaler)) );
  107. inverse[k][j]=inverse[k][j]-(inverse[workingRow][j]*scaler);
  108. matrix[k][j] = matrix[k][j]- (matrix[workingRow][j]*scaler);
  109.  
  110. }
  111. }
  112. }
  113. leadingI=true;
  114. workingRow+=1;
  115. }
  116. }
  117.  
  118. workingColoumn+=1;
  119. }
  120.  
  121. //Outputs Inverse Matrix
  122. System.out.println();
  123. System.out.println("Inverse");
  124. for (int x=0;x<rows;x+=1){
  125. for (int y=0;y<coloumns;y+=1){
  126. output = output +"|"+inverse[x][y];
  127. }
  128. System.out.println(output);
  129. output="";
  130. }
  131. System.out.println();
  132.  
  133. //Done Guassian Elimination
  134.  
  135. for (i=0;i<rows;i+=1){
  136. for (entry=0;entry<coloumns;entry+=1){
  137. output = output +"|"+matrix[i][entry];
  138. }
  139. System.out.println(output);
  140. output="";
  141. }
  142.  
  143.  
  144.  
  145. //Begin Jordan Elimination
  146.  
  147. for (i=rows-1;i>=0;i-=1){
  148. leadingI=false;
  149. //For each row, starting at bottom going to top
  150. for (entry=0;entry<coloumns;entry+=1){
  151. //For each coloumn going from left to right
  152. if (matrix[i][entry]==1.0 & leadingI==false){
  153. //Find an entry of 1
  154. for (int k=i-1;k>=0;k-=1){
  155. scaler = (matrix[k][entry]);
  156. if (matrix[k][entry]!=0.0){
  157. for (int j=entry;j<coloumns;j+=1){
  158. System.out.println(matrix[k][j]+"-"+(matrix[i][j]*scaler));
  159. inverse[k][j]=inverse[k][j]-(inverse[i][j]*scaler);
  160. matrix[k][j]=matrix[k][j]- (matrix[i][j]*scaler);
  161.  
  162. }
  163. }
  164. }
  165. leadingI=true;
  166. }
  167. }
  168. }
  169.  
  170. System.out.println("Finished");
  171. for (i=0;i<rows;i+=1){
  172. for (entry=0;entry<coloumns;entry+=1){
  173. output = output +"|"+matrix[i][entry];
  174. }
  175. System.out.println(output);
  176. output="";
  177. }
  178. System.out.println();
  179. System.out.println("Inverse:");
  180. for (i=0;i<rows;i+=1){
  181. for (entry=0;entry<coloumns;entry+=1){
  182. output = output +"|"+inverse[i][entry];
  183. }
  184. System.out.println(output);
  185. output="";
  186. }
  187.  
  188.  
  189.  
  190.  
  191. }
  192. }
juggernot is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Language display in program Prm753 C++ 3 May 30th, 2006 6:45 PM
Help me make a program. read ---> DBZ Visual Basic 8 Mar 10th, 2006 9:53 AM
Creating a program to test a program sixstringartist C 8 Jan 21st, 2006 2:15 PM
Program gets aborted on large input shinni C++ 3 Mar 12th, 2005 9:26 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC