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
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.
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.
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.