An attempt to implement the MCL (Markov Cluster) algorithm in APL.
I'm still a beginner in APL, any advice is welcome.
https://github.com/koteth/MCL_APL
Markov Cluster algorithm
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
- Adam|Dyalog
- Posts: 143
- Joined: Thu Jun 25, 2015 1:13 pm
Re: Markov Cluster algorithm
Looks really cool. How did you generate the graphics? Would you be interested in automatically generating those from within APL?
-
- Posts: 238
- Joined: Thu Jul 28, 2011 10:53 am
Re: Markov Cluster algorithm
Code from https://github.com/koteth/MCL_APL/blob/master/MCL.dyalog:
"Improved" version of as follows.
• separate code to create the matrix and to do the Markov iteration
• trivial functions included in-line and left unnamed
Code: Select all
matrix ← ⎕csv 'PATH TO MATRIX/matrix.csv'
InflateCoef ← 2
M ← ⍎¨ matrix
id←{⍵ ⍵⍴1,⍵⍴0}
Nor←{+⌿⍵}
Nmat ← {( ( ⍴ ⍵) ⍴ (Nor ⍵)) }
NormColumn ← {( ⍴ ⍵ ) ⍴ ((×/⍴⍵) ⍴⍵)÷((×/⍴⍵) ⍴ Nmat ⍵ )}
Expand ← {⍵ +.× ⍵}
Inflate ← NormColumn { (⍴⍺) ⍴ ( (×/⍴⍺) ⍴ ⍺) * ⍵ }
M2 ← NormColumn (M + id (≢ M))
Markov ← { ( Expand (NormColumn ⍵) ) Inflate InflateCoef }
FP ← (Markov ⍣ ≡ ⊢ M2)
R ← ((+/FP) >0) ⌿ FP
ClustMap ← ⌽↑⍸R
"Improved" version of as follows.
• separate code to create the matrix and to do the Markov iteration
• trivial functions included in-line and left unnamed
Code: Select all
MCL3←{
⍺←2 ⍝ inflation coefficient default
id←{(⍵,⍵)⍴1,⍵⍴0} ⍝ identity matrix of order ⍵
NormColumn←{⍵÷⍤1+⌿⍵}
Markov←{⍺ *⍨ +.×⍨ NormColumn ⍵} ⍝ one Markov iteration
FP←⍺ Markov⍣≡NormColumn ⍵+id≢⍵
⌽↑⍸(0<+/FP)⌿FP
}
-
- Posts: 238
- Joined: Thu Jul 28, 2011 10:53 am
Re: Markov Cluster algorithm
Since NormColumn is idempotent and is the first function to be applied in Markov, it is not necessary to apply it before launching the iteration. Whence NormColumn is used just once and can be inserted in-line. Therefore:
MCL4 ← {⍺←2 ⋄ ⌽↑⍸(0<+/FP)⌿FP← ⍺ {⍺ *⍨ +.×⍨ ⍵÷⍤1+⌿⍵}⍣≡ ⍵+∘.=⍨⍳≢⍵}
Re: Markov Cluster algorithm
Interesting rewriting. Now is more clear to me how to use the ⍨ operator , thanks.
Roger|Dyalog wrote:Since NormColumn is idempotent and is the first function to be applied in Markov, it is not necessary to apply it before launching the iteration. Whence NormColumn is used just once and can be inserted in-line. Therefore:
MCL4 ← {⍺←2 ⋄ ⌽↑⍸(0<+/FP)⌿FP← ⍺ {⍺ *⍨ +.×⍨ ⍵÷⍤1+⌿⍵}⍣≡ ⍵+∘.=⍨⍳≢⍵}
Re: Markov Cluster algorithm
Adam|Dyalog wrote:Looks really cool. How did you generate the graphics? Would you be interested in automatically generating those from within APL?
For the image I've used python with networkx. It would be nice to use APL, i've never tried to generate graphics. I'm currently using Dyalog 16 for linux, would you be able to suggest some starting point?
-
- Posts: 238
- Joined: Thu Jul 28, 2011 10:53 am
Re: Markov Cluster algorithm
koteth wrote:Interesting rewriting. Now is more clear to me how to use the ⍨ operator , thanks.
There are illuminating parallels between dyadic f⍨ and the passive case and monadic f⍨ and the reflexive case for verbs in natural languages.
-
- Posts: 17
- Joined: Wed Sep 10, 2008 9:39 am
Re: Markov Cluster algorithm
koteth wrote:For the image I've used python with networkx. It would be nice to use APL, i've never tried to generate graphics. I'm currently using Dyalog 16 for linux, would you be able to suggest some starting point?
Network graphs are documented in https://sharpplot.com/NetworkMap.htm
If you've never used SharpPlot, see https://sharpplot.com/Languages.htm which should help you translate the C# samples of the rest of the documentation.