"Fundamentals of Algorithm design and analysis" 16, Gauss elimination element method

Source: Internet
Author: User

Tag:better   array   unit   date    launch     str   for   highlight   turn   

Package Cn.xf.algorithm.ch06changerule;import Java.util.arraylist;import Java.util.list;import org.junit.Test; Import cn.xf.util.genericpair;/** * Function: Gaussian elimination method, that is, multivariate data matrix, and then each row of the other rows of data to eliminate to 0, and then find a solution of a meta, and then can be based on this data to hand out other meta solution * @author Xiaofeng * @date July 1, 2017 * @fileName Gaussianeliminationmethod.java * */public class Gaussianeliminationmethod {/** * This is defective if datas.get (i). get (i) is 0, then an error occurs * @param datas */public void Forwardelimination (list<list<double>> Datas) {if (datas.size () <= 0) {return;} Equation elimination data for (int i = 0; i < datas.size (); ++i) {//Traverse all lines, in this line, to remove the first item's parameter for (int j = i + 1; j < Datas.size (); ++j) {//Times The line after the calendar begins traversing for (int k = Datas.get (j). Size ()-1; k >= i;--k) {//When the last step forward, traverse forward and then remove the coefficients of the corresponding column I of each row by double value = Datas.get (j) . Get (k)-Datas.get (i). Get (k) * Datas.get (J). Get (i)/Datas.get (i). get (i);d Atas.get (j). Set (k, value);}}} /** * This idea is to start with the largest line of each column as the base row, and then do Gaussian elimination * @param datas */public void Betterforwardelimination (list<list<double >> datas) {if (datas.sizE () <= 0) {return;} First, traverse the diagonal data, because the data before the diagonal is merged into 0//traverse all the columns, the elimination of the element for (int column = 0; column < Datas.get (0). Size (); ++column) {//A variable is stored, The highest absolute value in this column is worth the line int maxvaluerow = column;//traverse all subsequent rows, comparing the absolute size for (int afterrow = Maxvaluerow; Afterrow < Datas.size (); + + Afterrow) {if (Math.Abs (Datas.get (Afterrow). Get (column)) > Math.Abs (Datas.get (Maxvaluerow). Get (column))) {// The absolute value of the line, assigned to him maxvaluerow = Afterrow;}} The current datum row and other rows are exchanged for (int swapcolumn = column; Swapcolumn < datas.get (0). Size (); ++swapcolumn) {if (column >= DATAS.S Ize ()) {break;} It is best to exchange Genericpair<integer with the current Datum line, integer> first = new Genericpair<integer, integer> (); First.setfirst (column); First.setsecond (Swapcolumn); Genericpair<integer, integer> second = new Genericpair<integer, integer> (); Second.setfirst (MaxValueRow); Second.setsecond (swapcolumn); Swap (datas, first, second);} Traverse each line, divided by the base after the interchange, starting at the following line, I, I------------------------------------------------------------------------------------------------------------------for (int afterrowr = column + 1; afterrowr < Datas.size (); ++afterrowr) {Double temp = datas.get (AFTERROWR). Get (column)/datas.get (column), or//with this divisor, exclude the data for this column of all rows after the interchange for (int rowchu = column; Rowchu < Datas.get ( AFTERROWR). Size (); ++rowchu) {//per row of data minus, base row and temp multiplier double resultvalue = Datas.get (AFTERROWR). Get (Rowchu)-datas.get (column). Get (Rowchu ) * Temp;datas.get (AFTERROWR). Set (Rowchu, Resultvalue);}}} public static void Swap (list<list<double>> datas, Genericpair<integer, integer> first, Genericpair <integer, integer> second) {if (datas.size () <= 0) {return;} Double temp = Datas.get (First.getfirst ()). Get (First.getsecond ());d Atas.get (First.getfirst ()). Set (First.getsecond ( ), Datas.get (Second.getfirst ()). Get (Second.getsecond ()));d Atas.get (Second.getfirst ()). Set (Second.getsecond (), temp);} @Testpublic void Test () {list<list<double>> datas = new arraylist<list<double>> (); list<double> data1 = new Arraylist<doubLe> ();d Ata1.add (2d);d Ata1.add ( -1d);d ata1.add (1d);d ata1.add (1d);d atas.add (data1); list<double> data2 = new arraylist<double> ();d ata2.add (4d);d ata2.add (1d);d Ata2.add ( -1d);d Ata2.add (5d); Datas.add (DATA2); list<double> data3 = new arraylist<double> ();d ata3.add (1d);d ata3.add (1d);d ata3.add (1d);d ata3.add (0d); Datas.add (DATA3);//Gaussian elimination Gaussianeliminationmethod gem = new Gaussianeliminationmethod (); Gem.forwardelimination ( datas);//gem.betterforwardelimination (datas); for (list<double> Data:datas) {System.out.println ( Data.tostring ());}}}

  

Result set:

"Fundamentals of Algorithm design and analysis" 16, Gauss elimination element method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.