From 61769a32f2bf929f89d95caf1134851487517049 Mon Sep 17 00:00:00 2001 From: Panadestein Date: Wed, 18 Dec 2024 09:13:10 +0000 Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20Panadest?= =?UTF-8?q?ein/blog@fce85bc99659df4376a040790c3ae8b6220674b7=20?= =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aoc24.html | 175 ++++++++++++++++++++++++++++------------------------ hf.html | 32 +++++----- nm.html | 10 +-- nn.html | 18 +++--- qbqn.html | 40 ++++++------ rollim.html | 60 +++++++++--------- si.html | 18 +++--- spodat.html | 30 ++++----- 8 files changed, 198 insertions(+), 185 deletions(-) diff --git a/aoc24.html b/aoc24.html index 13a075b..fb1806c 100644 --- a/aoc24.html +++ b/aoc24.html @@ -215,28 +215,28 @@

Coding in Advent

Table of Contents

-
-

Foreword

-
+
+

Foreword

+

Welcome to my Advent of Code solutions for the year 2024. Please read the puzzles' descriptions upstream to get more context. These definitions and imports are shared by the various puzzles: @@ -273,9 +273,9 @@

Foreword

-
-

I

-
+
+

I

+
I ← {
   inp ← >(•ParseFloat¨' '⊸Split)¨•FLines Input𝕩
@@ -291,9 +291,9 @@ 

I

-
-

II

-
+
+

II

+
II ← {
   inp ← (-´˘·2⊸↕·•ParseFloat¨' '⊸Split)¨•FLines Input𝕩
@@ -311,9 +311,9 @@ 

II

-
-

III

-
+
+

III

+
III ← {
   mem ← •FChars Input𝕩
@@ -334,9 +334,9 @@ 

III

-
-

IV

-
+
+

IV

+
IV ← {
   inp ← >•FLines Input 𝕩
@@ -353,9 +353,9 @@ 

IV

-
-

V

-
+
+

V

+
V ← {
   p‿r ← ",|" •ParseFloat⚇1∘(Split¨)¨ 1↓(∧`⊸+⟨⟩⊸≢¨)⊸⊔ •FLines Input𝕩
@@ -372,9 +372,9 @@ 

V

-
-

VI

-
+
+

VI

+
VI ← {
   hm ← (↕∘≢•HashMap○⥊⊢) inp ← >•Flines Input𝕩
@@ -395,9 +395,9 @@ 

VI

-
-

VII

-
+
+

VII

+
VII ← {
   inp ← (⊑⊸⋈○(•ParseFloat¨' '⊸Split)´':'⊸Split)¨•FLines Input𝕩
@@ -414,9 +414,9 @@ 

VII

-
-

VIII

-
+
+

VIII

+
VIII ← {
   n ← ≠⊑inp‿ant ← (⊢⋈1↓⍷∘⥊)>•FLines Input𝕩
@@ -433,9 +433,9 @@ 

VIII

-
-

IX

-
+
+

IX

+
IX ← {
    f‿s ← <˘⍉↑‿2⥊•ParseFloat∘⋈¨⊑•FLines Input𝕩
@@ -455,14 +455,9 @@ 

IX

-
-

X

-
-

-For this problem I have included additional functions to solve it in an array way using the -powers of the adjacency matrix. This method is unfortunately slower than simple dynamic programming. -

- +
+

X

+
X ← {
   inp ← >'0'-˜•FLines Input𝕩
@@ -492,11 +487,23 @@ 

X

 "Computed 776 and 1657 in 0.0073447794s"
 
+ + +
+Commentary +
+ +

+For this problem I have included additional functions to solve it in an array way using the +powers of the adjacency matrix. This method is unfortunately slower than simple dynamic programming. +

+ +
-
-

XI

-
+
+

XI

+
XI ← {
   inp ← •ParseFloat¨' 'Split⊑•Flines Input𝕩 ⋄ m ← •HashMap˜⟨⟩
@@ -519,9 +526,9 @@ 

XI

-
-

XII

-
+
+

XII

+
XII ← {
    n‿inp ← ≠⊸⋈>•FLines Input𝕩
@@ -545,9 +552,9 @@ 

XII

-
-

XIII

-
+
+

XIII

+
XIII ← {
  ∘‿3‿2⥊∾Ints¨⟨⟩⊸≢¨⊸/•FLines Input 𝕩
@@ -563,9 +570,9 @@ 

XIII

-
-

XIV

-
+
+

XIV

+
XIV ← {
   sm ← 0⥊˜s ← 101‿103 ⋄ inp ← 2‿2⊸⥊˘>'-'⊸Ints¨•FLines Input𝕩
@@ -582,9 +589,9 @@ 

XIV

-
-

XV

-
+
+

XV

+
XV ← {
   ri ← ⊑/○⥊⟜(↕≢)'@'=⊑w‿m ← >⊸⋈⟜∾´(⊢⊔˜·+`⟨⟩⊸≡¨)•FLines Input𝕩
@@ -613,20 +620,14 @@ 

XV

-
-

XVI

-
-

-I dislike implementing graph algorithms with their messy, imperative solutions, side effects, and fancy data structures. -Usually, a problem like this is solved using Dijkstra's algorithm, but after days of implementing “Fortranized” DFS and BFS, -I decided to borrow ideas from an expert array programmer's solutions and rework mine into a more elegant, array-oriented style: -

- +
+

XVI

+
XVI ← {
   r‿s‿e ← ⟨'#'⊸≠, 'S'⊸=, 'E'⊸=⟩{𝕎𝕩}¨<inp ← >•FLines Input𝕩
   _fp ← {𝔽∘⊢⍟≢⟜𝔽_𝕣∘⊢⍟≢⟜𝔽𝕩} ⋄ _m ← {𝕩{𝕏𝕨}¨𝔽⟨»,»˘,«,«˘⟩} ⋄ inf ← -2⋆31
-  dij ← ⌊´∘{⟨⊢_m, 1⊸⌽⌊1⊸⌽⁼, ⊢⟩{r<⊸×𝕎𝕩}¨⟨𝕩+1, 𝕩+1e3, 𝕩⟩}_fp 0‿inf‿0‿0×<s
+  dij ← ⌊´∘{⟨⊢_m, 1⊸⌽, 1⊸⌽⁼, ⊢⟩{r<⊸×𝕎𝕩}¨⟨𝕩+1, 𝕩+1e3, 𝕩+1e3, 𝕩⟩}_fp 0‿inf‿0‿0×<s
   mn‿mr‿mrc ← r⊸∧¨¨ {⟨2⊸⌽_m𝕩-1, 1⌽𝕩-1e3, 1⌽⁼𝕩-1e3⟩=<𝕩}dij
   eins ⇐ inf -˜ opt ← (⌊´dij) ⊑˜⊑/○⥊⟜(↕≢)e
   zwei ⇐ +´⥊∨´ ∨´∘{⟨mn∧2⊸⌽_m𝕩, mr∧1⌽𝕩, mrc∧1⌽⁼𝕩, 𝕩⟩}_fp e<⊸∧dij=opt
@@ -636,13 +637,23 @@ 

XVI

-"Computed 99448 and 498 in 0.0590324928s"
+"Computed 99448 and 498 in 0.0639844209s"
 
+
+Commentary +
+

+I dislike implementing graph algorithms with their messy, imperative solutions, side effects, and fancy data structures. +Usually, a problem like this is solved using Dijkstra's algorithm, but after days of implementing “Fortranized” DFS and BFS, +I decided to borrow ideas from an expert array programmer's solutions and rework mine into a more elegant, array-oriented style. As a note to myself and anyone interested in learning the craft, I will describe how the above code works. -We start by parsing the map and getting boolean masks for the walls, start and end positions (r‿s‿p). We then defined +

+ +

+We start by parsing the map and getting boolean masks for the walls, start and end positions (r‿s‿e). We then defined a fixed point modifier _fp, and a motion modifier _m. The latter performs nudge operations on the array, simulating cardinal coordinate steps up, left, down and right. By operating in the appropriate function, we can take these steps in any order. In addition, we need the initial position to be ¯∞, @@ -714,6 +725,8 @@

XVI

At the end, we have identified all tiles that are part of at least one best path.

+
+ diff --git a/hf.html b/hf.html index 6cb16a2..e94970d 100644 --- a/hf.html +++ b/hf.html @@ -234,9 +234,9 @@

Helonium's Hartree-Fock program

-
-

Exordium

-
+
+

Exordium

+

We will implement the Hartree-Fock1 program from the classic Szabo-Ostlund text, a staple in quantum chemistry. If you have any experience in the field, chances are you know it well. @@ -286,9 +286,9 @@

Exordium

-
-

Basis set

-
+
+

Basis set

+

Basis sets are used to transform the PDEs into linear algebra problems. Physical intuition suggests that Slater type orbitals4 are a good choice for our Hamiltonian. However, the computation of the integrals @@ -306,9 +306,9 @@

Basis set

-
-

Electronic integrals

-
+
+

Electronic integrals

+

Constructing the integrals' tensor is complicated6 and is the main reason for the poor scaling of electronic structure methods. The \(1s\) orbitals are the simplest case, and here two types of integrals @@ -387,7 +387,7 @@

Electronic integrals

-
+
 Got 38006 samples
 (self-hosted runtime1): 1067 samples
 (REPL): 36939 samples:
@@ -416,9 +416,9 @@ 

Electronic integrals

-
-

Fock matrix

-
+
+

Fock matrix

+

The following function constructs the Fock matrix, our approximation to the true Hamiltonian of the system:

@@ -472,9 +472,9 @@

Fock matrix

-
-

Self-consistent field

-
+
+

Self-consistent field

+

The final stage of the computation involves solving the pseudo-eigenvalue problem using a fixed-point iteration. This process is commonly known as the self-consistent field method, a term coined by D. R. Hartree. diff --git a/nm.html b/nm.html index ad85302..c3ae724 100644 --- a/nm.html +++ b/nm.html @@ -197,13 +197,13 @@

A numerical methods bonsai display

-
-

Foreword

+
+

Foreword

-
-

Bibliography

-
+
+

Bibliography

+

These are my favorite books on numerical analysis, in descending order of preference. I use the first two very often, and consult the others to deepen my understanding: diff --git a/nn.html b/nn.html index 4e80275..2f82c75 100644 --- a/nn.html +++ b/nn.html @@ -234,9 +234,9 @@

The miniaturist's neural network (WIP)

-
-

Preface

-
+
+

Preface

+

We will implement a fully-connected feed-forward neural network1, in other words, a

@@ -280,9 +280,9 @@

Preface

-
-

Tinn's diminution

-
+
+

Tinn's diminution

+

The original C implementation has 175 lines excluding the optimization loop. The BQN version has only

@@ -355,9 +355,9 @@

Tinn's diminution

-
-

Learning the logistic map

-
+
+

Learning the logistic map

+

_minn should handle digit recognition just fine3. However, I would like to switch clichés for the demonstration. Instead, we will use it to learn the logistic map4. This is a quintessential example of how chaos can emerge from simple systems. diff --git a/qbqn.html b/qbqn.html index cec83db..e982e2d 100644 --- a/qbqn.html +++ b/qbqn.html @@ -197,9 +197,9 @@

BQN's Quantum Noise

-
-

Preamble

-
+
+

Preamble

+

We will implement and test a Schrödinger-style1 quantum simulator in the BQN2 programming language. Initially, we import the necessary system functions and define a 1-modifier for handling @@ -207,7 +207,7 @@

Preamble

-
Sin‿Cos‿GCD ← •math
+
Sin‿Cos‿GCD ← •math
 U ← •rand.Range
 _cp ← {(-´𝔽¨)⋈(+´𝔽¨)⟜⌽}
 
@@ -221,16 +221,16 @@ 

Preamble

-
-

Interpreter

-
+
+

Interpreter

+

The (400 chars3) quantum interpreter is based on references arXiv:1711.02086 and arXiv:1608.03355. For simplicity, we always measure at the end of the execution:

-
Q ← {𝕊qb‿sc‿r:
+
Q ← {𝕊qb‿sc‿r:
   wf ← (1⌾⊑⋈⊢)⥊⟜0 2⋆qb
   M‿K ← ⟨+˝∘×⎉1‿∞ _cp, {1𝕊𝕩:𝕩; 𝕨𝕊1:𝕨; 𝕨∾∘×⟜<_cp𝕩}⟩
   E ← {0𝕊𝕩:1; K⍟(𝕨-1)˜𝕩}
@@ -246,9 +246,9 @@ 

Interpreter

-
-

Shor's algorithm

-
+
+

Shor's algorithm

+

As a test case, we employ the quantum circuit of Shor's algorithm for the number fifteen and base eleven, following references @@ -259,7 +259,7 @@

Shor's algorithm

-
n‿a‿qb‿r ← ⟨15, 11, 5, 0 U˜ 2⋆3⟩
+
n‿a‿qb‿r ← ⟨15, 11, 5, 0 U˜ 2⋆3⟩
 
 sc ← ⟨
   ⟨0⟩‿g.h ⋄ ⟨1⟩‿g.h ⋄ ⟨2⟩‿g.h
@@ -277,7 +277,7 @@ 

Shor's algorithm

-
C >+˝{Q qb‿sc‿𝕩}¨ r
+
C >+˝{Q qb‿sc‿𝕩}¨ r
 
@@ -291,9 +291,9 @@

Shor's algorithm

-
-

Epilogue

-
+
+

Epilogue

+

Why the hieroglyphs, you may ask? The tacit and functional style, coupled with numerous combinators, makes programming feel like solving a fun algebraic puzzle rather than drafting a manifesto. @@ -312,7 +312,7 @@

Epilogue

-
+
 ⟨ 44 64 ⟩
 
@@ -325,7 +325,7 @@

Epilogue

-
+
 ┌─                                                                                                                                                                                 
 ╵ '-' '´' '¨' '⋈' '+' '⟜' '⌽' '⊢' '≢' '⥊' '<' '=' '⌜' '˜' '↕' '∾' '○' '⌾' '⊸' '⊑' '÷' '√' '⊏' '⋆' '˝' '∘' '×' '⎉' '≡' '⊣' '⌊' '⁼' '≠' '⍟' '◶' '↓' '¬' '∊' '/' '»' '∨' '`' '>' '⍒'  
   8   8   10  5   8   3   6   7   1   5   9   6   3   12  6   5   2   5   7   9   5   1   1   5   4   8   5   1   3   3   1   1   5   1   2   1   1   1   1   1   1   2   3   1    
@@ -346,7 +346,7 @@ 

Epilogue

While the interpreter's performance is not particularly optimized, here is a comparison with the equivalent Common Lisp code:

-
+
 Benchmark 1: cbqn -f ./bqn/q.bqn
   Time (mean ± σ):      5.468 s ±  0.077 s    [User: 5.427 s, System: 0.005 s]
   Range (min … max):    5.358 s …  5.535 s    5 runs
@@ -369,7 +369,7 @@ 

Epilogue

-
+
 Got 25361 samples
 (REPL): 25361 samples:
      2│  Q ← {𝕊qb‿sc‿r:
diff --git a/rollim.html b/rollim.html
index 094c83c..27d69dc 100644
--- a/rollim.html
+++ b/rollim.html
@@ -241,13 +241,13 @@ 

A coding impromptu

Table of Contents

@@ -257,9 +257,9 @@

Table of Contents

would devise 35 to 40 distinct solutions to even the simplest problem in BQN. Therefore, I will frequently juxtapose my implementations with those of seasoned BQNators2.

-
-

Z algorithm

-
+
+

Z algorithm

+

This is a very efficient procedure that finds prefix strings in linear time. The imperative implementation reads: @@ -326,9 +326,9 @@

Z algorithm

-
-

Longest increasing sub-sequence

-
+
+

Longest increasing sub-sequence

+

This problem can be solved in \(O(n\log n)\) using dynamic programming. Here is an imperative implementation which is quadratic, but can be optimized: @@ -368,7 +368,7 @@

Longest increasing sub-sequence

Let's )explain this optimized version, so we can truly appreciate its beauty:

-
+
  +´∞≠∞¨{𝕨⌾((⊑𝕩⍋𝕨-1)⊸⊑)𝕩}´⌽ 
  │ │ │ ││    │ │ │  │ │  │ 
  │ │ │ {┼────┼─┼─┼──┼─┼─´│ 
@@ -398,9 +398,9 @@ 

Longest increasing sub-sequence

-
-

N-queens problem

-
+
+

N-queens problem

+

This problem is the archetypal example of backtracking. Initially, I tried to solve it using a function to place the queens in the full board, hoping that it would lead to a @@ -412,7 +412,7 @@

N-queens problem

-
+
 ┌─                 
 ╵ 0 1 0 1 0 1 0 0  
   0 0 1 1 1 0 0 0  
@@ -460,9 +460,9 @@ 

N-queens problem

-
-

Majority element

-
+
+

Majority element

+

The Boyer–Moore algorithm allows for finding the majority element (element that appears more than ⌊𝕩÷2 times in the array) in linear time. If such element exists, then it is @@ -497,9 +497,9 @@

Majority element

-
-

An identity on the naturals

-
+
+

An identity on the naturals

+

Some time ago, while working on performance optimization of linear algebra operations with Boolean arrays, I encountered an @@ -534,9 +534,9 @@

An identity on the naturals

-
-

Depth of nested lists

-
+
+

Depth of nested lists

+

Studying tree algorithms in APL, I learned about the depth vector representation. If the nested object in consideration is a string, the best approach is using boolean masks. @@ -554,9 +554,9 @@

Depth of nested lists

-
-

H-index

-
+
+

H-index

+

This metric is one of the reasons for the deplorable state of modern academia, and the headaches for outsiders trying to get in. Consider that Peter Higgs diff --git a/si.html b/si.html index 6feb9b4..2b98c0a 100644 --- a/si.html +++ b/si.html @@ -229,9 +229,9 @@

Scheming a mise-en-abîme in BQN

-
-

Prelude

-
+
+

Prelude

+

We will build an interpreter for a subset of the Scheme programming language, following an essay by Peter Norvig. An alternative reference would @@ -242,9 +242,9 @@

Prelude

-
-

A R5RS dialect

-
+
+

A R5RS dialect

+

Our goal is to adhere to the Revised\(^5\) Report on the Algorithmic Language Scheme (R5RS). However, seasoned schemers will quickly notice that our implementation still has @@ -337,9 +337,9 @@

A R5RS dialect

-
-

A Lisp quine

-
+
+

A Lisp quine

+

Given the title of this post, it's only fitting that we test our interpreter with a quine. In fact, building this interpreter was, for me, an exercise in bootstrapping the necessary machinery to produce this recursive effect: diff --git a/spodat.html b/spodat.html index 6699dc7..7c67023 100644 --- a/spodat.html +++ b/spodat.html @@ -201,9 +201,9 @@

Songs to pave the seasons

I have analyzed my Spotify data1 for the period 2016-2024. The results accurately represent my actual music taste2.

-
-

Technical details

-
+
+

Technical details

+

This is a suitable task for an array language, so I rely on BQN which is my favorite one: @@ -235,15 +235,15 @@

Technical details

-
-

Top songs

-
+
+

Top songs

+
s Q spd	
 
-
+
 ┌─                                                     
 ╵ 1  "Countless Skies"                                 
   2  "Divertimento I, K.136: Allegro"                  
@@ -269,15 +269,15 @@ 

Top songs

-
-

Top artists

-
+
+

Top artists

+
a Q spd
 
-
+
 ┌─                              
 ╵ 1  "Opeth"                    
   2  "Wolfgang Amadeus Mozart"  
@@ -303,9 +303,9 @@ 

Top artists

-
-

Bonus: Opeth anthology

-
+
+

Bonus: Opeth anthology

+

This is the Opeth album I would recommend to anyone. The query function needs to be modified a bit for generating it. But before that, let's look at the official discography: @@ -334,7 +334,7 @@

Bonus: Opeth anthology

-
+
 ┌─                             
 ╵ 1 "Ghost of Perdition"       
   2 "River"