Markov Cluster algorithm

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
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 !

Markov Cluster algorithm

Postby koteth on Sun Jun 10, 2018 12:15 pm

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
koteth
 
Posts: 3
Joined: Wed Jan 17, 2018 5:58 pm

Re: Markov Cluster algorithm

Postby Adam|Dyalog on Tue Jun 12, 2018 3:31 pm

Looks really cool. How did you generate the graphics? Would you be interested in automatically generating those from within APL?
User avatar
Adam|Dyalog
 
Posts: 135
Joined: Thu Jun 25, 2015 1:13 pm

Re: Markov Cluster algorithm

Postby Roger|Dyalog on Tue Jun 12, 2018 4:10 pm

Code from https://github.com/koteth/MCL_APL/blob/master/MCL.dyalog:

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                                                 
}
Roger|Dyalog
 
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Markov Cluster algorithm

Postby Roger|Dyalog on Wed Jun 13, 2018 12:41 am

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+⌿⍵}⍣≡ ⍵+∘.=⍨⍳≢⍵}
Roger|Dyalog
 
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Markov Cluster algorithm

Postby koteth on Sun Jun 17, 2018 8:51 am

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+⌿⍵}⍣≡ ⍵+∘.=⍨⍳≢⍵}
koteth
 
Posts: 3
Joined: Wed Jan 17, 2018 5:58 pm

Re: Markov Cluster algorithm

Postby koteth on Sun Jun 17, 2018 9:01 am

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?
koteth
 
Posts: 3
Joined: Wed Jan 17, 2018 5:58 pm

Re: Markov Cluster algorithm

Postby Roger|Dyalog on Mon Jun 18, 2018 6:39 pm

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.
Roger|Dyalog
 
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Markov Cluster algorithm

Postby Nicolas|Dyalog on Tue Jun 19, 2018 9:56 am

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.
Nicolas|Dyalog
 
Posts: 17
Joined: Wed Sep 10, 2008 9:39 am


Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest