## LU decomposition

## LU decomposition

(OP)

Hi all,

I wonder why LU for this matrix retrieves such LU decomposition result.

M=

-0.5 -1 0

-1 -1 -1

0 -1 -0.5

L=

1 0 0

0 1 0

0.5 0.5 1

U=

-1 -1 -1

0 -1 -0.5

0 0 0.75

While it should be:

L=

1 0 0

2 1 0

0 -1 1

I wonder why LU for this matrix retrieves such LU decomposition result.

M=

-0.5 -1 0

-1 -1 -1

0 -1 -0.5

L=

1 0 0

0 1 0

0.5 0.5 1

U=

-1 -1 -1

0 -1 -0.5

0 0 0.75

While it should be:

L=

1 0 0

2 1 0

0 -1 1

## RE: LU decomposition

Here's some Octave

>> M=[.5,-1,0;-1,-1,-1;0,-1,0.5]

M =

0.5000 -1.0000 0

-1.0000 -1.0000 -1.0000

0 -1.0000 0.5000

>> [L,U]=lu(M)

L =

-0.5000 1.0000 0

1.0000 0 0

0 0.6667 1.0000

U =

-1.0000 -1.0000 -1.0000

0 -1.5000 -0.5000

0 0 0.8333

>> M=[-.5,-1,0;-1,-1,-1;0,-1,0.5]

M =

-0.5000 -1.0000 0

-1.0000 -1.0000 -1.0000

0 -1.0000 0.5000

>> [L,U]=lu(M)

L =

0.5000 0.5000 1.0000

1.0000 0 0

0 1.0000 0

U =

-1.0000 -1.0000 -1.0000

0 -1.0000 0.5000

0 0 0.2500

>> M=[1.5,-1,0;-1,-1,-1;0,-1,0.5]

M =

1.5000 -1.0000 0

-1.0000 -1.0000 -1.0000

0 -1.0000 0.5000

>> [L,U]=lu(M)

L =

1.0000 0 0

-0.6667 1.0000 0

0 0.6000 1.0000

U =

1.5000 -1.0000 0

0 -1.6667 -1.0000

0 0 1.1000

>>

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

## RE: LU decomposition

thanks for your response. None of M matrix in your examples correspond to M in my example. Please try the M matrix in my example.

## RE: LU decomposition

M =

-0.5000 -1.0000 0

-1.0000 -1.0000 -1.0000

0 -1.0000 -0.5000

>> [L,U]=lu(M)

L =

0.5000 0.5000 1.0000

1.0000 0 0

0 1.0000 0

U =

-1.0000 -1.0000 -1.0000

0 -1.0000 -0.5000

0 0 0.7500

Which is a very strange result, L is not triangular. Sorry I don't know enough about lu to explain what that means.

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

## RE: LU decomposition

If I set permute_I to false I get the same as hoshang:

Lower =

1 0 0

-0 1 0

0.5 0.5 1

Upper =

-1 -1 -1

0 -1 -0.5

0 0 0.75

If I set permute_I to true I get the same as greg:

Permuted Lower =

0.5 0.5 1

1 0 0

-0 1 0

Upper =

-1 -1 -1

0 -1 -0.5

0 0 0.75

So greg's results give the permuted lower matrix. I'd have to remind myself of how these things work to know what you do with that.

Where did the values:

L=

1 0 0

2 1 0

0 -1 1

come from?

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

L=

1 0 0

2 1 0

0 -1 1

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

## RE: LU decomposition

TTFN (ta ta for now)

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg

FAQ731-376: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers Entire Forum list http://www.eng-tips.com/forumlist.cfm

## RE: LU decomposition

L=

1 0 0

2 1 0

0 -1 1

U=

-0.5 -1 0

0 1 -1

0 0 -1.5

## RE: LU decomposition

The function assumes that a permutation matrix P is required to make the decomposition work; this is why the lu() function returns 3 matrices.

TTFN (ta ta for now)

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg

FAQ731-376: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers Entire Forum list http://www.eng-tips.com/forumlist.cfm

## RE: LU decomposition

I wonder why LU for this matrix retrieves such LU decomposition result.

is that there is more than one way to decompose a matrix.

It seems that Scipy and Octave are using a pivotted lower matrix, so that for an original matrix A:

PLU = A

whereas for hoshang's matrices:

LU = A

Excel screen shot, calling Scipy fumctions:

Apparently Mathcad is doing something different, but I don't have Mathcad to see what is going on there.

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

TTFN (ta ta for now)

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg

FAQ731-376: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers Entire Forum list http://www.eng-tips.com/forumlist.cfm

## RE: LU decomposition

I have updated my spreadsheet to include the Mathcad way.

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

TTFN (ta ta for now)FAQ731-376: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers Entire Forum list http://www.eng-tips.com/forumlist.cfm

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg

## RE: LU decomposition

So, how one can get hoshang results using Mathcad?

## RE: LU decomposition

I can't help with Mathcad, but why do you want to?

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

https://courses.physics.illinois.edu/cs357/sp2020/...#

It has python code for each stage of the process.

At the moment I'm having trouble getting their lu_decomp function to work. If anyone get's it working, please let me know.

.

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

I have now got their code working. In the lu_decomp function:

Replace the two lines using np.block as below:

## CODE --> python

Also in the forward_sub function, replace:

for j in range(i-1):

with

for j in range(i):

With those changes the functions I tried seem to work correctly:

forward_sub

back_sub

lu_solve

lu_decomp

I haven't yet tried the functions using pivoting (but I'm working on it).

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

What about my concerns?

## RE: LU decomposition

If you have remaining concerns perhaps you might tell us what they are.

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg

## RE: LU decomposition

Is there any workaround in Mathcad so that I can get hoshang results?

## RE: LU decomposition

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg

## RE: LU decomposition

to which the answer is:

The link previously posted:

https://courses.physics.illinois.edu/cs357/sp2020/...

has a good clear explanation of how to do LU decomposition, with and without pivoting, and why it is a good idea to use pivoting for general use, to avoid problems if there are any zeros on the leading diagonal. It also has Python code which will generate the unpivoted matrices (after applying corrections as posted above).

You haven't told us why you have to use the unpivoted matrices, or why it has to be done in Mathcad, but if both are essential for some reason, and if Mathcad doesn't have that as an option, it shouldn't be too hard to convert the Python code to Mathcad, or you could look at using:

https://pypi.org/project/MathcadPy/

or something similar.

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

https://newtonexcelbach.com/2023/05/26/lu-decompos...

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

Thanks, do you know when you'll be posting the actual code?

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg

## RE: LU decomposition

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg

## RE: LU decomposition

https://newtonexcelbach.com/2023/05/30/scipy-linea...

Note that calling the Python code from Excel requires the pyxll add-in.

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg

## RE: LU decomposition

https://stackoverflow.com/questions/69241952/to-fi...

It will probably run much faster than code you write yourself because scipy is really compiled C or Fortran code with a python wrapper.

Peter Nachtwey

Delta Computer Systems

http://www.deltamotion.com

http://forum.deltamotion.com/

IFPS Hall of Fame Member

## RE: LU decomposition

The great majority of the code in my link is for calling the Scipy linear algebra functions from Excel, including the LU decomposition functions.

The Python routines were written purely so I (and others) can see how the process works, in response to the question in the first post of this thread.

Also note that the download file includes links to the pyPardiso library which is very much faster than the Scipy LU functions.

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

Any workaround using Mathcad.

## RE: LU decomposition

You haven't told us why you have to use the unpivoted matrices, or why it has to be done in Mathcad, but if both are essential for some reason, and if Mathcad doesn't have that as an option, it shouldn't be too hard to convert the Python code to Mathcad, or you could look at using:

https://pypi.org/project/MathcadPy/

or something similar.

Doug Jenkins

Interactive Design Services

http://newtonexcelbach.wordpress.com/

## RE: LU decomposition

You CAN do this in Mathcad, albeit, a bit, or more, painfully

I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg