# Matrix multiplication convergence

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:

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



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

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.

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


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.

Hello Steve,

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

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?

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


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

Did you mean to type:

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]

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.

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

Discourse doesn’t provide Unicode tab completion.

What I usually do is to type it in the Julia REPL (which has Unicode tab completion) 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, for example.

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

while true

if A isapprox Π .* Π
break
else
A = Π .* Π
end
end



or

while true

if A isapprox Π .* Π
break

end
global A = Π .* Π
end



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

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


Error code:

syntax: unexpected "Π"

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


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.