Skip navigation.

Phicar's world

turkish blog

Sunday, 8. November 2009

Cramer Law

, ,

Hi, i was working in some geometric shit with circles, and one point was like "deduce the equation of the circle with 3 points" so i had a 3x3 type of equation, was to late and i am not in my wave in math so i decide to code it :wink:...

there is the kramer law for a 3x3 equation :wink:

first a little explanation if we have a system like this Ax=B we can obtain x doing this x = (B/A) and we have a matrix with the constants of the equation so is basically xn = Det[xn]/Det[General]

where Det is the substracion of the addition of the multiplication of the cross values in matrix :wink:

here is the code :wink: enjoy it...

import java.io.*;
import java.util.*;
public class deter{
public static double coe[][]=new double[3][3];
public static double sol[] = new double[3];
public static void main(String args[]) throws IOException{
if(args.length<1){
System.err.println("Ingresa el archivo");
return;
}
BufferedReader lector = new BufferedReader(new FileReader(args[0]));
String tmp = "";
int p = 0;
while((tmp=lector.readLine())!=null){
if(p == 3){
StringTokenizer sr = new StringTokenizer(tmp,",");
for(int n = 0;n<3;n++)
sol[n]=Double.parseDouble(sr.nextToken());
break;
}
StringTokenizer st = new StringTokenizer(tmp);
for(int n = 0;n<3;n++)
coe[p][n]=Double.parseDouble(st.nextToken());
p++;
}
resolv();
}
public static void resolv(){
double d = Det(coe);
for(int n = 0;n<3;n++){
double tmp[][] = new double[3][3];
for(int w = 0;w<3;w++){
for(int e = 0;e<3;e++)
tmp[w][e]=coe[w][e];
}
for(int x = 0;x<3;x++)
tmp[x][n]=sol[x];
System.out.println(Det(tmp)/d);
}
}
public static double Det(double a[][]){
double c = 0,b = 0;
for(int n = 0;n<3;n++){
c+=(a[n][0]*a[(n+1)%3][1]*a[(n+2)%3][2]);
b+=(a[(n+0)%3][2]*a[(n+1)%3][1]*a[(n+2)%3][0]);
}
return c-b;
}
}



Pd: the file need to be in this way

x1 y1 z1
x2 y2 z2
x3y3 z3
res1,res2,res3
December 2009
M T W T F S S
November 2009January 2010
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31