# Help with LU factorization

**URL:** <https://discourse.julialang.org/t/help-with-lu-factorization/39911>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 21, 2020, 10:03pm UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911 "2020-05-21T22:03:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 21, 2020, 10:03pm UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/1 "2020-05-21T22:03:56Z")

</div>

Is that possible to Implement 𝐿𝑈 factorization without row pivoting ? can someone give me an example?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 21, 2020, 10:09pm UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/2 "2020-05-21T22:09:57Z")

</div>

`lu(A,Val(false))`

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 21, 2020, 10:28pm UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/3 "2020-05-21T22:28:07Z")

</div>

I don’t want to use LinearAlgebra package , I want to implement the algorithm from scratch

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 22, 2020, 12:25am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/4 "2020-05-22T00:25:37Z")

</div>

Well, you can look at the [source code](https://github.com/JuliaLang/julia/blob/dd1e54d54dc4b6c342602c9169de268c5e37ac2a/stdlib/LinearAlgebra/src/lu.jl#L132-L180) for the `Pivot == false` case. But if this is a learning exercise (or a homework problem), you might want to try to implement it yourself from a textbook description.

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 22, 2020, 12:42am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/5 "2020-05-22T00:42:40Z")

</div>

Than you .Can you tell me the name of textbook that can describe how to implement this code ?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 22, 2020, 12:52am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/6 "2020-05-22T00:52:11Z")

</div>

If this really _is_ a homework assignment, you should do some of the work yourself. As for LA resources, check out e.g. [http://ulaff.net/](http://ulaff.net/).

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 22, 2020, 1:42am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/7 "2020-05-22T01:42:09Z")

</div>

I tried it like that :

```julia
using LinearAlgebra
using SparseArrays
function LU2(A::Union{Array{real},Array{Float64}})
n = size(A,1); # Assuming A is nxn.
U = copy(A);
L = eye(eltype(A),n);
for k=2:n
for i=k:n
L[i,k-1] = U[i,k-1]/U[k-1,k-1];
for j=k-1:n
U[i,j] = U[i,j] - L[i,k-1]*U[k-1,j];
end
end
end
return L,U
end

```

but the problem now is when I test it i got this error ( UndefVarError: eye not defined)

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 22, 2020, 1:44am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/8 "2020-05-22T01:44:39Z")

</div>

You can get the n \times n identity matrix using `Matrix{eltype(A)}(I,(n,n))`.

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 22, 2020, 1:49am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/9 "2020-05-22T01:49:32Z")

</div>

I got UndefVarError: eltpe not defined

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 22, 2020, 1:50am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/10 "2020-05-22T01:50:15Z")

</div>

Sorry, typo. Fixed now.

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 22, 2020, 1:51am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/11 "2020-05-22T01:51:39Z")

</div>

thanks it works

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 22, 2020, 1:59am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/12 "2020-05-22T01:59:20Z")

</div>

Deleted code

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 22, 2020, 2:10am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/13 "2020-05-22T02:10:48Z")

</div>

You probably want `abs.(array)`.

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 22, 2020, 2:27am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/14 "2020-05-22T02:27:19Z")

</div>

I did it like this

```julia
pivot = indmax(abs.(U[(k-1):n,k-1])) + k - 2;

```

but i got UndefVarError: indmax not defined

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 22, 2020, 5:10am UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/15 "2020-05-22T05:10:11Z")

</div>

The function that used to be called.`indmax` is now called `findmax`.

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 22, 2020, 1:21pm UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/16 "2020-05-22T13:21:57Z")

</div>

> [@dpsanders](#):
>
> findmax

Thanks.it works but i got different error  
MethodError: no method matching +(::Tuple{Float64,Int64}, ::Int64)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 22, 2020, 1:43pm UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/17 "2020-05-22T13:43:32Z")

</div>

Did you try running the `findmax` function to see what kind of object it returns? And you can read the documentation of the function using `?findmax` from the REPL.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 22, 2020, 1:45pm UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/18 "2020-05-22T13:45:48Z")

</div>

Ah there’s also `argmax`.

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [May 22, 2020, 1:46pm UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/19 "2020-05-22T13:46:43Z")

</div>

Yes i did. it works but when I test it on a matrix I got this strange error  
**( MethodError: no method matching +(::Tuple{Float64,Int64}, ::Int64) )**  
I tried to figure out the problem in the code again but i didn’t note what’s the problem

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 22, 2020, 1:48pm UTC](https://discourse.julialang.org/t/help-with-lu-factorization/39911/20 "2020-05-22T13:48:09Z")

</div>

`findmax` returns a tuple.  
It is telling you that you are trying to add that tuple to an integer.

[Next page](https://discourse.julialang.org/t/help-with-lu-factorization/39911.md?page=2)
