Forum Archive

Go Back   3D Realms Forums > General Topics > Programming Forum
Blogs FAQ Community Calendar

Notices

 
 
Thread Tools
Old 03-04-2007, 08:16 PM   #1
Lain

Lain's Avatar
Java inverse matrix?
Hello i need to get the inverse matrix for a 3x3 3x3 4x4 and 5x5 matrix's can you guys help me? (i need in java)
__________________
Frag=Superczar kissing my shotgun..

Wii code: 1999-5973-3442-4435
Brawl: 3050-7344-2657
Lain is offline  
Old 03-04-2007, 09:12 PM   #2
phreak
Re: Java inverse matrix?
Matrices are hard to invert, generally.
I would suggest implementing something like Gaussian Elimination, which is illustarted for finding inverses here.

There is also the LU decomposition method, which is supposedly the most used method for computers, but I don't understand it. I haven't studied matrices other than random glimpses in books from time to time, so don't trust me too much.
__________________
"A fool sees not the same tree that a wise man sees." - William Blake
phreak is offline  
Old 03-05-2007, 05:00 AM   #3
theHunted
Re: Java inverse matrix?
Inverting 3x3 matrices can still be done by hand (see http://mathworld.wolfram.com/MatrixInverse.html).
Everything higher than that has to be done as mentioned by phreak.
__________________
M:I - New Dawn
a Max Payne 2 modification
theHunted is offline  
Old 10-14-2009, 11:20 PM   #4
joze_grilj
Ted Re: Java inverse matrix?
Hay, here is my java method for calculating inverse matrix with Gauss-Jordan Method:
Code:
	public double[][] inverz_matrike(double[][]in){
		int st_vrs=in.length, st_stolp=in[0].length;
		double[][]out=new double[st_vrs][st_stolp];
		double[][]old=new double[st_vrs][st_stolp*2];
		double[][]new=new double[st_vrs][st_stolp*2];

		
		for (int v=0;v<st_vrs;v++){//ones vector
			for (int s=0;s<st_stolp*2;s++){
				if (s-v==st_vrs) 
					old[v][s]=1;
				if(s<st_stolp)
					old[v][s]=in[v][s];
			}
		}
		//zeros below the diagonal
		for (int v=0;v<st_vrs;v++){
			for (int v1=0;v1<st_vrs;v1++){
				for (int s=0;s<st_stolp*2;s++){
					if (v==v1)
						new[v][s]=old[v][s]/old[v][v];
					else
						new[v1][s]=old[v1][s];
				}
			}
			old=prepisi(new);		
			for (int v1=v+1;v1<st_vrs;v1++){
				for (int s=0;s<st_stolp*2;s++){
					new[v1][s]=old[v1][s]-old[v][s]*old[v1][v];
				}
			}
			old=prepisi(new);
		}
		//zeros above the diagonal
		for (int s=st_stolp-1;s>0;s--){
			for (int v=s-1;v>=0;v--){
				for (int s1=0;s1<st_stolp*2;s1++){
					new[v][s1]=old[v][s1]-old[s][s1]*old[v][s];
				}
			}
			old=prepisi(new);
		}
		for (int v=0;v<st_vrs;v++){//rigt part of matrix is invers
			for (int s=st_stolp;s<st_stolp*2;s++){
				out[v][s-st_stolp]=new[v][s];
			}
		}
		return out;
	}

	public double[][] prepisi(double[][]in){
		double[][]out=new double[in.length][in[0].length];
		for(int v=0;v<in.length;v++){
			for (int s=0;s<in[0].length;s++){
				out[v][s]=in[v][s];
			}
		}
		return out;
	}
http://nptel.iitm.ac.in/courses/Webc...-2/node27.html
joze_grilj is offline  
Old 10-15-2009, 03:31 PM   #5
Feared
 
Re: Java inverse matrix?
Quote:
Originally Posted by joze_grilj View Post
Hay, here is my java method for calculating inverse matrix with Gauss-Jordan Method:
Code:
	public double[][] inverz_matrike(double[][]in){
		int st_vrs=in.length, st_stolp=in[0].length;
		double[][]out=new double[st_vrs][st_stolp];
		double[][]old=new double[st_vrs][st_stolp*2];
		double[][]new=new double[st_vrs][st_stolp*2];

		
		for (int v=0;v<st_vrs;v++){//ones vector
			for (int s=0;s<st_stolp*2;s++){
				if (s-v==st_vrs) 
					old[v][s]=1;
				if(s<st_stolp)
					old[v][s]=in[v][s];
			}
		}
		//zeros below the diagonal
		for (int v=0;v<st_vrs;v++){
			for (int v1=0;v1<st_vrs;v1++){
				for (int s=0;s<st_stolp*2;s++){
					if (v==v1)
						new[v][s]=old[v][s]/old[v][v];
					else
						new[v1][s]=old[v1][s];
				}
			}
			old=prepisi(new);		
			for (int v1=v+1;v1<st_vrs;v1++){
				for (int s=0;s<st_stolp*2;s++){
					new[v1][s]=old[v1][s]-old[v][s]*old[v1][v];
				}
			}
			old=prepisi(new);
		}
		//zeros above the diagonal
		for (int s=st_stolp-1;s>0;s--){
			for (int v=s-1;v>=0;v--){
				for (int s1=0;s1<st_stolp*2;s1++){
					new[v][s1]=old[v][s1]-old[s][s1]*old[v][s];
				}
			}
			old=prepisi(new);
		}
		for (int v=0;v<st_vrs;v++){//rigt part of matrix is invers
			for (int s=st_stolp;s<st_stolp*2;s++){
				out[v][s-st_stolp]=new[v][s];
			}
		}
		return out;
	}

	public double[][] prepisi(double[][]in){
		double[][]out=new double[in.length][in[0].length];
		for(int v=0;v<in.length;v++){
			for (int s=0;s<in[0].length;s++){
				out[v][s]=in[v][s];
			}
		}
		return out;
	}
http://nptel.iitm.ac.in/courses/Webc...-2/node27.html
You do realize that this thread is from 2007?
If so then cool, I'm not a fan of Java though.

Feared is offline  
 

Bookmarks


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


All times are GMT -6. The time now is 06:42 AM.

Page generated in 0.10813999 seconds (100.00% PHP - 0% MySQL) with 15 queries

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.

Website is ©1987-2014 Apogee Software, Ltd.
Ideas and messages posted here become property of Apogee Software Ltd.