Sunday, March 28, 2021

Visualisation of Young diagrams in Clojure

This takes a Young diagram expressed as an additive partition and converts it into an image using the Java standard library. Then it creates a Swing frame for you to display the image. Young diagrams are important in a wide variety of mathematical contexts from number theory to abstract algebra.
(import java.awt.image.BufferedImage)
(import javax.swing.JFrame)
(import javax.swing.JLabel)
(import javax.swing.ImageIcon)

(defn determine-coordinates
  [coll]

  (let [sorted-seq (sort > (seq coll))]
    (apply
     concat
     (map-indexed
      (fn [x v]
        (map
         (fn [y]
           [x y])
         (range 0 v)))
      sorted-seq))))

(defn signature-image
  [sig block-size]

  (let [coords (determine-coordinates sig)
        sig-width (count sig)
        sig-height (if (empty? sig) 0 (apply max sig))
        padding-size (int (/ block-size 2))
        img-width (+ (* block-size (inc sig-width)))
        img-height (+ (* block-size (inc sig-height)))
        img (BufferedImage.
             img-width
             img-height
             BufferedImage/TYPE_INT_RGB)
        g (.createGraphics img)]
    (.setColor g java.awt.Color/WHITE)
    (.fillRect g 0 0 img-width img-height)
    (.setColor g java.awt.Color/BLACK)
    (doseq [coord coords]
      (let [[x y] coord]
        (.drawRect
         g
         (+ padding-size (* x block-size))
         (- img-height
            (+ padding-size
               (* (inc y) block-size)))
         block-size
         block-size)))
    img))

(defn display-image
  [img]

  (let [f (JFrame.)]
    (.add (.getContentPane f)
          (JLabel.
           (ImageIcon. img)))
    (.pack f)
    (.setVisible f true)))
The example that I have prepared is the Young diagram of the prime signature of the monster group. It is suprisingly imbalanced because the maximum multiplicity is much larger then the total number of primes.

No comments:

Post a Comment