Java - 522 434 228 213 chars
Solves by systematically checking all possible integer n-tuples by direct multiplication until one is found that works.
Function takes augmented matrix, A, trial solution vector, x, and dimension, n, as input - outputs solution vector, x. Note that the vector x is actually one larger than the dimension to help step through possible solutions. (If I declared variables A,x,n,j,k,s as instance variables, the function would be 31 chars shorter - for a total of 182, but that feels like bending the rules too far.)
int[]Z(int[][]A,int[]x,int n){int j,k,s;for(;;){for(j=0;j<n;j++){for(k=s=0;k<n;s+=A[j][k]*x[k++]);if(s!=A[j][n])j+=n;}if(j==n)return x;for(j=0;j<=n;j++)if(x[j]!=x[n]||j==n){x[j]++;for(k=0;k<j;x[k++]=-x[n]);j=n;}}}
Program for testing (somewhat ungolfed):
import java.util.*;
class MatrixSolver{
public MatrixSolver() {
Scanner p=new Scanner(System.in); //initialize everything from stdin
int j,k,n=p.nextInt(),A[][]=new int[n][n+1],x[]=new int[n+1];
for(j=0;j<n;j++)for(k=0;k<=n;A[j][k++]=p.nextInt());
x=Z(A,x,n); //call the magic function
for(j=0;j<n;j++) System.out.print(x[j]+" "); //print the output
}
public static void main(String[]args){
new MatrixSolver();
}
int[]Z(int[][]A,int[]x,int n){
int j,k,s;
for(;;){
for(j=0;j<n;j++){ //multiply each row of matrix by trial solution and check to see if it is correct
for(k=s=0;k<n;s+=A[j][k]*x[k++]);
if(s!=A[j][n])j+=n;
}
if(j==n)return x; //if it is correct return the trial solution
for(j=0;j<=n;j++)if(x[j]!=x[n]||j==n){//calculate the next trial solution
x[j]++;
for(k=0;k<j;x[k++]=-x[n]);
j=n;
}
}
}
}
Program takes input from stdin as space separated integers as follows: first, the dimension of the problem, second, the entries of the augmented matrix by row.
Sample run:
$java -jar MatrixSolver.jar
3 2 1 -1 8 -3 -1 2 -11 -2 1 2 -3
2 3 -1
I shaved several characters by following Victor's advice about loops and "public", storing the RHS in the augmented matrix instead of separately, and adding an extra entry to my trial solution to simplify the generation of each new trial solution. The OP also said that a function is enough - no need to count the entire program.
2Can the coefficients of the variables and the constant terms be separated into two arrays in the input? – user12205 – 2014-03-01T21:58:57.307
@ace yes, that's fine – qwr – 2014-03-01T22:08:42.693
1What exactly are you saying by degenerated cases? I guess that you are refering to all of those cases: 1) Malformed input; 2) Things like
0x=0
or0x=5
; 4) Cases where the number of equations is different than the number of variables; 5) Contradictory cases likex+5y=7, x+5y=8
; 6) Cases without linear independency, likex+3y=6, 2x+6y=12
. Am I right? – Victor Stafusa – 2014-03-01T22:40:00.413@Victor Yes, any input that has any ambiguity at all or isn't solvable. – qwr – 2014-03-01T22:53:26.370
What about cases which aren't degenerate but are ill-conditioned? (Or, in other words, what kind of pivoting is required?) – Peter Taylor – 2014-03-01T23:43:45.247
@PeterTaylor I'm not exactly sure what's meant by ill-conditioned, but only (relatively) small integer solutions need to work, and any pivoting method should be fine. – qwr – 2014-03-01T23:53:16.780
The problem specification requires a program. Do you need a whole program or just a function (like the Javascript answer)? If so, I can shorten my Java answer considerable. – Wally – 2014-03-02T06:23:54.630
@Wally A function is okay – qwr – 2014-03-02T07:04:47.857
Cool - I'll golf all of the program boilerplate out of my answer! – Wally – 2014-03-02T07:39:36.893