PDA

View Full Version : [Java] Algoritmo di gauss


fix87
31-03-2009, 22:44
Salve ragazzi,

sto provando ad implementare l'algoritmo di gauss tramite java!!! Però c'è un piccolo problemino, sto semplicemente uscendo pazzo!!! Qualcuno di voi conosce qualche sito dove c'è l'intero codice per l'implementazione???

Grazie anticipatamente!!!

P.s: Non lo devo portare ad un esame quindi non mi serve per fini scolastici ma soltanto per curiosità personale!!!

wingman87
31-03-2009, 22:53
Fixa il titolo: http://www.hwupgrade.it/forum/showthread.php?t=1649196
E poi posta il codice e il problema.
Se invece cerchi il codice già fatto basta spendere un po' di tempo su google e lo trovi, dubito che qualcuno ce l'abbia da parte solo per tirarlo fuori nel caso qualcuno sul forum lo chieda.

fix87
31-03-2009, 23:11
Se invece cerchi il codice già fatto basta spendere un po' di tempo su google e lo trovi.

Ho girato per parecchio tempo e non avendo trovato niente, mi sono rivolto a voi. Ne dubito anche io che se qualcuno ce l'abbia da parte lo tira fuori appena un fesso come me lo chiede. Infatti ho chiesto se qualcuno conosce un sito dove c'è il codice e non che uno mi deve dare il codice se ce l'ha.:)

wingman87
31-03-2009, 23:49
Ho cercato Gauss Java:
http://www.math.tu-berlin.de/~moehring/Coma/Java-Programme/Gauss/

fix87
01-04-2009, 00:22
Ho cercato Gauss Java:
http://www.math.tu-berlin.de/~moehring/Coma/Java-Programme/Gauss/

Ho provato a scaricare il file gauss.java ma non si capisce niente è tutto in disordine. Cmq grazie per la disponibilità!!

banryu79
01-04-2009, 09:12
Ho provato a scaricare il file gauss.java ma non si capisce niente è tutto in disordine. Cmq grazie per la disponibilità!!
tutto in disordine? :)


/**
* Gauss.java
*
* demonstrates the use of Gauss' algorithms for solving linear equations
*
* Assumptions:
* Input is per row, row i contains coefficients a_ij and
* the righthandside b_j as last number,
* missing a_ij are interpreted as 0
* numbers in a row are separated by white space
*/
import java.awt.*;
import java.applet.Applet;
import java.util.*; // for class StringTokenizer
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Gauss extends Applet {

private TextArea input, output;
private Button startBtn, helpBtn;
private Choice choiceBtn;
private Panel p1, p2, p3;
private String helpStr =
"Bitte oben Koeffizienten a_ij und rechte Seite b_i \n"
+ "zeilenweise eingeben.\n"
+ "Die letze Zahl der Zeile i ist die rechte Seite b_i,\n"
+ "die Zahlen davor als a_i1, a_i2, ...\n"
+ "Fehlende a_ij in Zeile i werden als 0 interpretiert.\n"
+ "Das eingegebene Gleichungssystem wird mit \n"
+ "der gewaehlten Pivotsuche geloest. \nDabei wird die Zeit "
+ "gemessen und ausgegeben.\n";

public LinearEquation linEq = new LinearEquation();

// setup the graphical user interface components
public void init() {
setLayout(new FlowLayout(FlowLayout.LEFT));
setFont(new Font("Times", Font.PLAIN, 18));
Font courier = new Font("Courier", Font.PLAIN, 18);

p1 = new Panel();
p2 = new Panel();
p3 = new Panel();

p1.setLayout(new FlowLayout(FlowLayout.LEFT));

input = new TextArea(" 1 0 1 0 4 \n"
+ " 1 1 0 1 7 \n"
+ " 1 1 2 0 6 \n"
+ " 1 0 0 1 5 ", 10, 60);
input.setFont(courier);
p1.add(input); // put input on panel

p2.setLayout(new FlowLayout(FlowLayout.LEFT));

helpBtn = new Button(" Hilfe ");
helpBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
showHelp();
}
});
p2.add(helpBtn);

choiceBtn = new Choice();
choiceBtn.addItem("ohne Pivotsuche");
choiceBtn.addItem("mit totaler Pivotsuche");
p2.add(choiceBtn);

startBtn = new Button(" Start ");
startBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
runGauss();
}
});
p2.add(startBtn);

p3.setLayout(new FlowLayout(FlowLayout.LEFT));
output = new TextArea(helpStr, 15, 60);
output.setFont(courier);
p3.add(output);

add(p1);
add(p2);
add(p3);
}

/**
* reads numbers from input to arrays a and b and to precision
*/
private void initialize() throws NumberFormatException,
NoSuchElementException, DimensionException,
NullPointerException {

String inputStr = input.getText();

// divide input into lines
StringTokenizer inputLines = new StringTokenizer(inputStr,
"\n\r");

// get the number of rows
if (! inputLines.hasMoreTokens()) {
throw new NoSuchElementException();
}
int m = inputLines.countTokens();

StringBuffer strBuf = new StringBuffer();
strBuf.append(m + " Gleichungen");

// define array of m lines of tokens
StringTokenizer[] line = new StringTokenizer[m];
for (int i = 0; i < m; i++) {
line[i] = new StringTokenizer(inputLines.nextToken());
}

// get the number of columns including b
int n = 0;
for (int i = 0; i < m; i++) {
if (line[i].countTokens() < 2) {
throw new NoSuchElementException();
}
if (line[i].countTokens() - 1 > n) {
n = line[i].countTokens() - 1;
}
}

strBuf.append(" und " + n + " Variable\n");

// initialize linEq
double[][] a = new double[m][n];
double[] b = new double[m];
for (int i = 0; i < m; i++) {
int j;
int k = line[i].countTokens() - 1;
// that many coeffs on line i
for (j = 0; j < k; j++) {
a[i][j] = Double.parseDouble(
line[i].nextToken());
}
for (j = k; j < n; j++) {
a[i][j] = 0;
}
b[i] = Double.parseDouble(line[i].nextToken());
}

linEq = new LinearEquation(a, b);

// write info in output field
output.setText(strBuf.toString());
}

/**
* displays help text
*/
public void showHelp() {
output.setText(helpStr);
}

/**
* solves system of linear equations by Gaussian elimination
*
* @see <code>LinearEquationSolver</code>
*/
public void runGauss() {
try {
output.setText("Rechne ...\n");
initialize();

int choice = choiceBtn.getSelectedIndex();
String choiceStr = choiceBtn.getSelectedItem();

long startTime = System.currentTimeMillis();
switch (choice) {
case 0 :
linEq.solveWithoutPivotSearch();
break;
case 1 :
linEq.solveByTotalPivotSearch();
break;
}
long endTime = System.currentTimeMillis();
long runTime = endTime - startTime;

// Construct the output string in a
// StringBuffer object
StringBuffer outputBuf = new StringBuffer();
outputBuf.append("Benoetigte Zeit "
+ choiceStr + " in Millisekunden: "
+ Long.toString(runTime) + "\n");

outputBuf.append("solvable = " + linEq.isSolvable()
+ "\n");
outputBuf.append(linEq.equationsToString() + "\n");
if (linEq.isSolvable()) {
outputBuf.append(linEq.solutionToString());
}
output.append("\n" + outputBuf.toString());

} catch (NoSuchElementException exp) {
output.append("\nJede Zeile muss mindestens "
+ "2 Zahlen enthalten.");
} catch (NumberFormatException exp) {
output.append("\nNur double Zahlen eingeben.");
} catch (DimensionException exp) {
output.append(exp.toString());
} catch (Exception exp) { // other exceptions
output.append(exp.toString());
}
}
}