# Matrix multiplication convergence

**URL:** <https://discourse.julialang.org/t/matrix-multiplication-convergence/94042>\
**Category:** Finance and Economics\
**Tags:** question, matrix\
**Created:** [February 4, 2023, 11:54am UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042 "2023-02-04T11:54:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![WhY](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/why/32/46541_2.png) [@WhY](https://discourse.julialang.org/u/WhY)\
**Post date:** [February 4, 2023, 11:54am UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/1 "2023-02-04T11:54:27Z")

</div>

I am trying to coding a transition matrix I generated using Rouwenhorst converge to a idempotent by multiply itself multiple times in Julia using if loop, but I am new to Julia and don’t know how.

Basic enviroment:

```julia
\Pi #transition matrix generated
if A != \Pi * \Pi
     then B = A * A
          then C = B * B
          else end
     else end 
 else end

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 4, 2023, 11:57am UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/2 "2023-02-04T11:57:33Z")

</div>

Hello and welcome to the community!

I recommend you to study the documentation of Julia, you’ll find this section relevant to your problem [Control Flow · The Julia Language](https://docs.julialang.org/en/v1/manual/control-flow/)

---

<div class="post-metadata">

**Author:** ![WhY](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/why/32/46541_2.png) [@WhY](https://discourse.julialang.org/u/WhY)\
**Post date:** [February 4, 2023, 1:28pm UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/3 "2023-02-04T13:28:40Z")

</div>

I actually read this but and tried to write some code couple times but I know it is wrong because I feel somewhere not right, here is what I tried before.

```julia
while ture
    A=\Pi
    if A != \Pi *\Pi
         A = \Pi *\Pi
    else 
         break
    end
end

```

---

<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:** [February 4, 2023, 1:45pm UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/4 "2023-02-04T13:45:02Z")

</div>

> [@WhY](#):
>
> ```julia
> while true
> A=Π
> 
> ```

You re-initialize `A` to `Π` on every loop iteration, so it will never converge — you square it only once and then re-initialize it.

(I assume by `\Pi` you mean `Π`, i.e. you meant to tab-complete the unicode symbol.)

That is, your problem is not (just) Julia syntax, but rather you are struggling with the basic logic of the loop. There are a variety of programming tutorials that you might find helpful, e.g. the book [Think Julia](https://benlauwens.github.io/ThinkJulia.jl/latest/book.html).

---

<div class="post-metadata">

**Author:** ![WhY](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/why/32/46541_2.png) [@WhY](https://discourse.julialang.org/u/WhY)\
**Post date:** [February 4, 2023, 2:13pm UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/5 "2023-02-04T14:13:39Z")

</div>

Hello Steve,

Thanks for the reply, I know I need to update my matrix A after every logic check which is

```julia
if A != Π

```

(By the way may I ask how to type Π in this community? I search for this and did not find any document explain how)

So do you think I should just remove that condition?

```julia
while true
    if A != Π .* Π
         A = Π .* Π
    else 
         break
    end
end

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 4, 2023, 2:29pm UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/6 "2023-02-04T14:29:45Z")

</div>

> [@WhY](#):
>
> `if A != Π .* Π`

If the matrices are Float64, then comparing using `!=` isn’t useful because of numerical errors. Use `isapprox` and `!isapprox`.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [February 4, 2023, 3:08pm UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/7 "2023-02-04T15:08:18Z")

</div>

Did you mean to type:

```julia
while true
    Π=A
    if A ≉ Π * Π
        A = Π * Π
    else 
        break
    end
end

```

You can copy-paste this, and it’s good to know, that you always can, in (whole or in part), e.g. if you do not remember the correct TAB-completion.  
[/quote]

> [@Dan](#):
>
> Use `isapprox` and `!isapprox`.

That’s good advise, you can use this in function form or even better, type \napprox then press . The prefix \n for not (usually but not there `!`) does not work as \not (but allowing either way, a PR for, would be welcome, and LaTeX completions DO sometimes allow for two forms already). You can type \approx for ≈ but in that case, as for the functional form (and some ASCII operators), you can’t prefix with ! for !≈. That’s likely by design, to not allow more than one Unicode for (after TAB-completion), plus the ASCII form. Julia is in general case-sensitive, also e.g. for \Pi and \pi.

---

<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:** [February 4, 2023, 3:14pm UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/8 "2023-02-04T15:14:59Z")

</div>

> [@WhY](#):
>
> `A = Π .* Π`

Note that this is not a matrix multiplication: `.*` is _elementwise_ multiplication. If you want an ordinary matrix multiplication, use `Π * Π`.

> [@WhY](#):
>
> (By the way may I ask how to type Π in this community? I search for this and did not find any document explain how)

Discourse doesn’t provide Unicode tab completion.

What I usually do is to type it in the Julia REPL (which has [Unicode tab completion](https://docs.julialang.org/en/v1/manual/unicode-input/)) or similar Julia-editing environments (Jupyter, vsCode, etcetera), and copy and paste into a Discourse post.

I also set up my keyboard to allow typing Greek letters easily. There is a nice way to configure a Mac so that you [switch back and forth to Greek mode with command-space](https://apple.stackexchange.com/questions/181328/how-to-access-greek-symbols-on-macos-from-a-basic-keyboard-without-copy-paste), for example.

---

<div class="post-metadata">

**Author:** ![WhY](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/why/32/46541_2.png) [@WhY](https://discourse.julialang.org/u/WhY)\
**Post date:** [February 6, 2023, 6:00am UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/9 "2023-02-06T06:00:42Z")

</div>

Thanks for all the advice, I have try the following but I don’t know how to add the update condition into the loop:

```julia
while true
   
    if A isapprox Π .* Π
        break
    else 
       A = Π .* Π
    end
end

```

or

```julia
while true
   
    if A isapprox Π .* Π
        break

    end
   global A = Π .* Π
end

```

---

<div class="post-metadata">

**Author:** ![WhY](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/why/32/46541_2.png) [@WhY](https://discourse.julialang.org/u/WhY)\
**Post date:** [February 8, 2023, 7:00am UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/10 "2023-02-08T07:00:10Z")

</div>

I rewrite my code as following but don’t know why I have this error code:

```julia
i=1
A= Π*Π
while true 
    if A isapprox Π ^ i
        break
    end 
    global i +=1
    global A = Π ^ i
end

```

Error code:

```julia
syntax: unexpected "Π"

Stacktrace:
 [1] top-level scope
   @ In[15]:4

```

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [February 8, 2023, 7:30am UTC](https://discourse.julialang.org/t/matrix-multiplication-convergence/94042/11 "2023-02-08T07:30:37Z")

</div>

> [@WhY](#):
>
> `A isapprox Π ^ i`

`isapprox` (in contrast to `isa` or `in` or `+`…) is not an infix operator. It should read `isapprox(A, Π^i)`, or as was suggested above by Palli `A ≉ Π^i`.

Furthermore, you should put your code in a function and avoid globals. Using global variables, particularly in performance critical code, will usually lead to very poor performance.

I suggest to consult the manual or one of the many excellent introductory texts on Julia to familiarize yourself better with the language and its best practices.
