Pivots (Gaussian reduction)
|
06-03-2015, 01:20 PM
(This post was last modified: 06-06-2015 01:36 PM by salvomic.)
Post: #1
|
|||
|
|||
Pivots (Gaussian reduction)
hi everybody,
please, help to debug and improve this program to calculate pivots of a matrix (Gauss-Jordan elimination). It is important to me to create also another program to calculate LDLt factorization (decomposition) [this is the thread], where for a symmetric matrix L is a lower triangular (Lt its transpose) and D diagonal with pivots... The program for pivots use the function pivot(), finding recursively the minors of the matrix and applying this function to them, saving pivots and rows in a matrix, and at end giving upper triangular matrix with pivots in diagonal and a list with pivots. The program can, if a pivot is 0, to swap two rows (this function could be improved) and retry for a new calculation... But my algorithm is not so good... I'd like to receive your approach, tips, advice... I wasn't able to find pivots directly with pivot() function, without using it more times... Or help to find pivots and reduced form with them in diagonal with ref(), RREF(), pivot(), lu(), diag()... Salvo The code: Code:
this seems to be good with matrices like [[2,1,1,5],[4,-6,0,-2],[-2,7,2,9]] (or [[2,1,1],[4,-6,0],[-2,7,2]], pivots 2,-8,1), [[2,-1,0],[-1,2,-1],[0,-1,1]] (pivots 2, 3/2 and 4/3) but has little wrong results with [[1,2,3],[2,4,3],[3,2,-1]]: the pivots should be (1,-4,-3) but I get (1,-4,3/4) and isn't good for echelon matrices (not RREF reduced), like [[1,3,3,2],[2,6,9,7],[-1,-3,3,4]] that gives [[1,3,3,2],[0,0,0,0],[0,0,0,0]] (pivots 1,0,0?) EDIT: please, help whit last rows of program: why difference in the last pivot? about what am I wrong? I need a more general solution... ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-05-2015, 05:06 PM
(This post was last modified: 06-05-2015 10:47 PM by salvomic.)
Post: #2
|
|||
|
|||
RE: Pivots (Gaussian elimination)
A version a bit more "advanced". Now it can handle swapping for rows if there is problem in finding solution (*if* the solution there is, the matrix isn't singular, and so on...).
But I've still not found a real *general* formula: help very appreciated! When it runs well, the program now returns pivots(), LDU() and LDLt() factorization, and a routine to calculate permutation matrix also. In this version there isn't still a menu... The code (sorry for some //comments for debugging): Code:
The examples (see at the end of the program are): M2: [[2,1,1], [4,-6,0], [-2,7,2]] with pivots: {2,-8,1} -> U [[2,1,1], [0,-8,2], [0,0,1]] M3: [[1,1,1], [1,2,-2], [2,1,-3]] with pivots: {1,1,8} -> [[1,1,1], [0,1,-3], [0,0,-8]] M4: [[0,3,-1], [1,1,2], [1,4,1]] with pivots: {1,3,0} -> [[1,1,2], [0,3,-1], [0,0,0]] (this one will swap two times; try also another version: [[1,1,2], [0,3,-1], [1,4,1]]: swap two times, with inverted pivots: without the function for swap in the program it'll give "error: invalid input"...): it's a singular matrix (determinant 0, as the product of pivots) M6: [[2,-1,0], [-1,2,-1], [0,-1,2]] with pivots: {2, 3/2, 4/3} -> U [[2, -1, 0], [0, 3/3, -1], [0,0, 4/3]] M7: [[1,2,3], [2,4,3], [3,2,-1]] with pivots: {1, -4, -3} -> [[1,2,3], [0,-4,-10], [0, 0, -3]] M8: [[1,1,1], [1,1,3], [2,5,8]] with pivots: {1,3,2} -> U [[1,1,1], [0,3,6], [0,0,2]] (with swapping row 2 and 3) And now the problem of the "non general" formula (please, let me understand where I'm wrong): Code:
But I'm searching for *one* formula, not more ;-) Thank you for your patience and effort! Salvo EDIT: thanks Dale (DrD) and Claudio L. for some tips! ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-05-2015, 07:03 PM
Post: #3
|
|||
|
|||
RE: Pivots (Gaussian elimination)
(06-05-2015 05:06 PM)salvomic Wrote: M1(1):= M1(1)./L1(j); // M3, M4, M6 In another post Han said to "put spaces around the ./ operator." |
|||
06-05-2015, 07:29 PM
(This post was last modified: 06-05-2015 10:03 PM by salvomic.)
Post: #4
|
|||
|
|||
RE: Pivots (Gaussian elimination)
(06-05-2015 07:03 PM)DrD Wrote: In another post Han said to "put spaces around the ./ operator." ok, I'm trying it, but doesn't change... The principal problem is: why M3, M4, M7 seem don't need division, M2 need recursive division, and M6 only for the first row that after will be treated by pivot()? If I execute manually pivot() -> finding a pivot -> finding minor (reducing first row and column) at the end I need to decide where divide or not also: but how to tell it to the calculator? I'm stuck only in this point... Salvo ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-06-2015, 09:41 PM
(This post was last modified: 06-07-2015 09:45 AM by salvomic.)
Post: #5
|
|||
|
|||
RE: Pivots (Gaussian reduction)
ok, this should works much better (with a trick, for now, maybe not the best of all).
Test it, if you like it. Maybe the program has issues with large matrix (col > 3) if c>r (and tell me if you find other bugs). For example still it fails (after the second row it gives all 0) with a matrix like: [[0,2,1,1,0], [1,-1,2,0,3], [1,1,3,1,2], [3,1,8,2,6], [4,2,-6,-8,0]] (require row swap, but manually it's solvable); the built in function pivot() supply "too many" 0... See here for the correct reduction. For that ref() (function echelon in my program) gives [[1,-1,2,0,3], [0,1,1/2,1/2,0], [0,0,1,11/17, 12/17], [0,0,0,0,1], [0,0,0,0,0]], that multiplied by {1,2,-17,-1,0} gives [[1,-1,2,0,3], [0,2,1,1,0], [0,0,-17,-11,-12], [0,0,0,0,1-], [0,0,0,0,0]] that's the correct result (but I knew the pivots *first*, so I could ...divide) Salvo Code:
∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-07-2015, 10:42 AM
Post: #6
|
|||
|
|||
RE: Pivots (Gaussian reduction)
Hi Salvo,
The program attached to your last post doesn't compile for me (permMatrix() isn't declared as a subroutine). After fixing that, I get errors with the Pivots sub program. Is this your latest edition? -Dale- |
|||
06-07-2015, 11:28 AM
(This post was last modified: 06-07-2015 12:55 PM by salvomic.)
Post: #7
|
|||
|
|||
RE: Pivots (Gaussian reduction)
(06-07-2015 10:42 AM)DrD Wrote: Hi Salvo, hi Dale, try this one (here it compile). The function are Pivots(), LDLt, LDU, permMatrix, echelon (gaussJordan is the about). permMatrix() is now exported to be available externally... I added the same in the program, as here it worked because I had another program with that routine... permMatrix({1,3,2}) or perMatrix([1,3,2]) give the permutation matrix 3x3 for P32, and so on... i.ex. if you use lu() you get (last firmware) as first element something to [1,3,2], pass it to permMatrix and you'll get [[1,0,0], [0,0,1], [0,1,0]], the permutation matrix. The program use it internally also... Use Pivots(matrix) to test, for now. LDLt require a symmetric matrix... (Lt is the traspose of L, D matrix with pivots). LDU (LDV) use lu() to get L*D*U, where D is the matrix that has diagonal with the pivots and 0 elsewhere... Code:
∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-07-2015, 12:13 PM
Post: #8
|
|||
|
|||
RE: Pivots (Gaussian reduction)
In your routine: calcPivot(c,r)
FOR j FROM 1 TO r DO M1:=pivot(M1,1,1); // Changed CAS.pivot(M1,1,1) to pivot(M1,1,1), <<clears syntax error>> |
|||
06-07-2015, 12:41 PM
(This post was last modified: 06-08-2015 10:07 AM by salvomic.)
Post: #9
|
|||
|
|||
RE: Pivots (Gaussian reduction)
(06-07-2015 12:13 PM)DrD Wrote: In your routine: calcPivot(c,r) ok, changed it! now I get right results with the matrix in some post above... Still wrong result with M9 = [[0,2,1,1,0], [1,-1,2,0,3], [1,1,3,1,2], [3,1,8,2,6], [4,2,-6,-8,0]]. For this I put "echelon()" as function to compare with ref() and RREF(). For now Pivots(M9) gives only the first two rows and the last three with zeros... Surely, after the row swapping pivot() doesn't offer the input that the program require... EDIT: with the last version I get for M9 the first 3 rows: why? More modular version, with a bit more clean code and some others //comments to help ...who want help me to debug Corrected a bug if the first row was 0... Code:
∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-08-2015, 04:02 PM
(This post was last modified: 06-08-2015 07:53 PM by salvomic.)
Post: #10
|
|||
|
|||
RE: Pivots (Gaussian reduction)
With this new version I get good results for almost 20 matrices (diverse types observed: 2x2, 3x3, 4x4, 5x5, singular, echelon...), among them the M2-M9 from above...
However I used two routines I called "tricks" (see line 55 and 106): I suspect they run as workaround and not as real formulas (if you want, please, control, advise...), but they work almost in the case examined by me. To have some global variables, for now I used M1, L1, A, B, D, E variables. Expect M1, L1, the other, perhaps, could be changed in local ones... I'm working for a more clean and better code, without "tricks" maybe EDIT: ok with about 20 types, but if fails (in part) with this "band, 3 diagonal" matrix: [[2,-1,0,0,0], [-1,2,-1,0,0], [0,-1,2,-1,0], [0,0,-1,2,-1], [0,0,0,-1,2]] with Pivots() I get {2, 3/2, ⅔, 5/12, 36/5} but the pivots should be {2/1, 3/2, 4/3, 5/4, 6/5}; LDLt gives right L and Lt (transpose) but not D (for the same reason): any help? Code:
∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-08-2015, 10:15 PM
(This post was last modified: 06-09-2015 10:02 AM by salvomic.)
Post: #11
|
|||
|
|||
RE: Pivots (Gaussian reduction)
a different approach is not to use pivot() command to calc Pivots(), but use lu()...
pivot() command calculates partial pivoting element by element, it isn't a complete gaussian elimination, so I had to use the ugly "tricks" to get the job... LU returns P (permutation, easily transformable into a matrix with permMatrix()), L (lower triangular that contains "multipliers" below the diagonal) and U (upper triangular that should have pivots, reduced, in diagonal). I need (of Pivots() ) to get a diagonal with pivots *not* reduced (and the product of which is equal to determinant, if the matrix is not singular), as then I get LDU (with LDU() ) and LDLt (with LDLt() ), that require a D matrix with pivots in its diagonal... I need an hint to extract multipliers from L returned by lu() and make with them the gaussian reduction (with or without permutation, given by the first list of lu())... I thought a cycle... i.e. if matrix is M2:= [[2,1,1], [4,-6,0], [-2,7,2]], lu(M2) returns [ [1 2 3] [[1,0,0], [-1,1,0], [2,-1,1]] [[2,1,1], [0,8,3], [0,0,1]] ] so the permutation matrix is P = [[1,0,0], [0,01], [0,1,0]] L is the second matrix in the list, and in it we have multipliers -1 (L(2,1)), 2 (L(3,1)), -1 (L(3,2)), so we need to subtract -1 times 1st row from 2nd, 2 times 1st row from 3rd, -1 times 2nd row from 3rd U should have pivots already in the diagonal, but in this case it has 2,8,1, but manually we get 2, -8, 1 (the actual form of my program returns in fact Pivots(M2) -> {2,-8,1} and the product of pivots must be equal to determinant that for M2 is -16 (2*1*(-8)), while lu() returns {2,8,1} because it consider the matrix with the rows swapped as (1,3,2) and in this case the determinant *is* 16... The cycle should extract multipliers, perform permutation (multiplying P*M2) and subtractions (following the multipliers) and return Gauss reduction and the required pivots. Any help? Salvo *** EDIT 2 ...but this routine (and the new cycle) only reproduces LU and returns ...simply U. I'm outside of the way... Code:
∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-09-2015, 10:55 AM
Post: #12
|
|||
|
|||
RE: Pivots (Gaussian reduction)
Recognizing that Gauss Jordan elimination can be accomplished in many different ways, Gauss Jordan, LU decomposition would be dependent on the algorithm used to perform the reduction. With this in mind, check your results with that of Wolfram Alpha once again: LU(M2) from Wolfram Alpha. Specifically, notice that the diagonal of their upper triangular matrix U is [2,-8, 1], yet your decomposition is different. To check the results of the decomposition, L * U / P, should return the original M2 matrix, but in your case it doesn't. The permutation matrix shown, is not correct.
Another decomposition for LU(M2) is: {[[1,0,0],[0.5,1,0],[−0.5,1,1]],[[4,−6,0],[0,4,1],[0,0,1]],[[0,1,0],[1,0,0],[0,0,1]]}, where the matrices are {L,U,P}. Checking: L*U/P ==M2. There may be another update released before long, and hopefully resolve some of the issues. |
|||
06-09-2015, 12:50 PM
(This post was last modified: 06-11-2015 10:45 PM by salvomic.)
Post: #13
|
|||
|
|||
RE: Pivots (Gaussian reduction)
(06-09-2015 10:55 AM)DrD Wrote: Recognizing that Gauss Jordan elimination can be accomplished in many different ways, ... yes, I know, in fact I could stop with the LU (and REF) that already Prime has, but I search also for others way, to control some results by books and for study purpose... My first attempt is no completely sure, as it uses two "tricks", the second attempt give no more nor less U as the LU() command, reproducing it in another way I made programs more difficult in less time than this one, but this is fascinating me, in some way... An yes, I control always with Wolfram and with the reverse formula... At the start I thought that the command pivot() gave the complete reduction, but I can see that applying it more time could get exactly LU or REF or ...RREF. Anyway, my purpose is to get LDU (LDV) and LDLt, and for now my first attempt is the nearest to them. Maybe. Salvo *** The last code with utilities: Pivots, LU_ext (LU also if the matrix is not square), LDLt, LDU, permMatrix, echelon (and a few bugs corrected) is here. It works much better than first and it is conform to some fonts, like the books of Gilbert Strang and a few others, obviously not with *every* matrices... I promise don't bore you more with this program... EDIT June 12 2015: every new version will be put in this thread of the Software Library of the Forum Code:
∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-13-2015, 02:56 PM
(This post was last modified: 06-13-2015 03:07 PM by salvomic.)
Post: #14
|
|||
|
|||
RE: Pivots (Gaussian reduction)
(06-09-2015 12:50 PM)salvomic Wrote: EDIT June 12 2015: every new version will be put in this thread of the Software Library of the Forum I need, however two hint (help, advice): 1. the program as is works (sure it could have other bugs, however...), but only if matrix is squared (rxr) or r<c, thank in most range of the cases, but not if r>c (I know that in this case there will be one or more rows dependent, too many equations than variables), but in some cases it is stil important to get elimination. A first hypothesis should be to add one or more columns with 0 than calculates Pivots() or LU_ext(), eliminate the columns with 0... but this is an arbitrary decision... 2. also elimination (reduction) for columns (instead of for rows) is used, sometimes: for now the program doesn't make it. I've only a confuse idea how to revert algorithm for this purpose, in simply way... Thank you Salvo ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 6 Guest(s)