|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Jan 2008
Città: Firenze
Messaggi: 150
|
[Java] Algoritmo di gauss
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!!!
__________________
ACER ASPIRE 5920G - INTEL CORE2 DUO T7700 - NVIDIA GEFORCE 8600GT - 2GB RAM DDR2 - 250GB HDD - HD DVD Ultima modifica di fix87 : 31-03-2009 alle 22:59. |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2776
|
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. |
![]() |
![]() |
![]() |
#3 | |
Member
Iscritto dal: Jan 2008
Città: Firenze
Messaggi: 150
|
Quote:
![]()
__________________
ACER ASPIRE 5920G - INTEL CORE2 DUO T7700 - NVIDIA GEFORCE 8600GT - 2GB RAM DDR2 - 250GB HDD - HD DVD |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2776
|
Ho cercato Gauss Java:
http://www.math.tu-berlin.de/~moehri...ogramme/Gauss/ |
![]() |
![]() |
![]() |
#5 | |
Member
Iscritto dal: Jan 2008
Città: Firenze
Messaggi: 150
|
Quote:
__________________
ACER ASPIRE 5920G - INTEL CORE2 DUO T7700 - NVIDIA GEFORCE 8600GT - 2GB RAM DDR2 - 250GB HDD - HD DVD |
|
![]() |
![]() |
![]() |
#6 | |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
![]() Codice:
/** * 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()); } } }
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 03:11.