From b5a48822e0271fbff620aa54a888dcf3963b02bf Mon Sep 17 00:00:00 2001 From: Panadestein Date: Thu, 6 Feb 2025 14:58:45 +0000 Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20Panadest?= =?UTF-8?q?ein/blog@a07b19dc6ae1906048fb8ae3bb541ad82de45f08=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 | 206 ++++++++++++++++++++++++++-------------------------- hf.html | 32 ++++---- nn.html | 18 ++--- ns.html | 12 +-- qbqn.html | 40 +++++----- rollim.html | 85 ++++++++++++---------- si.html | 18 ++--- spodat.html | 30 ++++---- 8 files changed, 223 insertions(+), 218 deletions(-) diff --git a/aoc24.html b/aoc24.html index 58393bf..d55fcac 100644 --- a/aoc24.html +++ b/aoc24.html @@ -215,37 +215,37 @@

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: @@ -290,9 +290,9 @@

Foreword

-
-

I

-
+
+

I

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

I

-
-

II

-
+
+

II

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

II

-
-

III

-
+
+

III

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

III

-
-

IV

-
+
+

IV

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

IV

-
-

V

-
+
+

V

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

V

-
-

VI

-
+
+

VI

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

VI

-
-

VII

-
+
+

VII

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

VII

-
-

VIII

-
+
+

VIII

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

VIII

-
-

IX

-
+
+

IX

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

IX

-
-

X

-
+
+

X

+
X ← {
   inp ← >'0'-˜•FLines Input𝕩
@@ -517,9 +517,9 @@ 

X

-
-

XI

-
+
+

XI

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

XI

-
-

XII

-
+
+

XII

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

XII

-
-

XIII

-
+
+

XIII

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

XIII

-
-

XIV

-
+
+

XIV

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

XIV

-
-

XV

-
+
+

XV

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

XV

-
-

XVI

-
+
+

XVI

+
XVI ← {
   inf ← -2⋆31 ⋄ r‿s‿e ← ⟨'#'⊸≠, 'S'⊸=, 'E'⊸=⟩{𝕎𝕩}¨<inp ← >•FLines Input𝕩
@@ -742,9 +742,9 @@ 

XVI

-
-

XVII

-
+
+

XVII

+
XVII ← {
   inp ← 3(↑⋈∘‿2⥊↓)∾⟨⟩⊸Ints¨•FLines Input𝕩
@@ -778,9 +778,9 @@ 

XVII

-
-

XVIII

-
+
+

XVIII

+
XVIII ← {
   n ← 70 ⋄ inp ← >⟨⟩⊸Ints¨•FLines Input𝕩
@@ -801,9 +801,9 @@ 

XVIII

-
-

XIX

-
+
+

XIX

+
XIX ← {
   p‿d ← (1⊸↑⋈2⊸↓) •FLines Input𝕩
@@ -823,9 +823,9 @@ 

XIX

-
-

XX

-
+
+

XX

+
XX ← {
   r‿s ← ⟨'#'⊸≠, 'S'⊸=⟩{𝕎𝕩}¨<inp ← >•FLines Input𝕩
@@ -844,9 +844,9 @@ 

XX

-
-

XXI

-
+
+

XXI

+
XXI ← {
   inp ← •FLines Input𝕩
@@ -870,9 +870,9 @@ 

XXI

-
-

XXII

-
+
+

XXII

+
XXII ← {
   dp ← •HashMap˜⟨⟩ ⋄ inp ← •ParseFloat¨ •FLines Input𝕩
@@ -891,9 +891,9 @@ 

XXII

-
-

XXIII

-
+
+

XXIII

+
XXIII ← {
   p‿c ← (⊢⋈·⍷∾)'-'⊸Split¨•FLines Input𝕩
@@ -910,9 +910,9 @@ 

XXIII

-
-

XXIV

-
+
+

XXIV

+
XXIV ← {
   gs‿os ← (⊢⊔˜·+`⟨⟩⊸≡¨)•FLines Input𝕩
@@ -934,9 +934,9 @@ 

XXIV

-
-

XXV

-
+
+

XXV

+
XXV ← {
   inp ← >¨⟨⟩SplitF•FLines Input𝕩
diff --git a/hf.html b/hf.html
index d6a1349..36d81c8 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/nn.html b/nn.html index 125cb4b..23d7eff 100644 --- a/nn.html +++ b/nn.html @@ -234,9 +234,9 @@

The miniaturist's neural network

-
-

Preface

-
+
+

Preface

+

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

@@ -281,9 +281,9 @@

Preface

-
-

Tinn's diminution

-
+
+

Tinn's diminution

+

The original C implementation has 175 lines, excluding the optimisation loop. The BQN version has only 10, and is more general as it supports an arbitrary number of hidden layers. The biases are left unoptimised @@ -359,9 +359,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/ns.html b/ns.html index fdf620e..d08c2ef 100644 --- a/ns.html +++ b/ns.html @@ -234,9 +234,9 @@

Navier-Stokes simulator (WIP)

-
-

Foreword

-
+
+

Foreword

+

Under classical, non-relativistic continuum mechanics, effectively incompressible Newtonian fluids are governed with remarkable accuracy by the following system of partial differential equations: @@ -258,9 +258,9 @@

Foreword

-
-

Simulator

-
+
+

Simulator

+

The simulator is based on the material from this book, but aiming for a zero dependency implementation.

diff --git a/qbqn.html b/qbqn.html index e7a1e12..44e8601 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 0a1aeb6..3ab1479 100644
--- a/rollim.html
+++ b/rollim.html
@@ -241,14 +241,14 @@ 

A coding impromptu

Table of Contents

@@ -258,9 +258,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: @@ -327,9 +327,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: @@ -369,7 +369,7 @@

Longest increasing sub-sequence

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

-
+
  +´∞≠∞¨{𝕨⌾((⊑𝕩⍋𝕨-1)⊸⊑)𝕩}´⌽ 
  │ │ │ ││    │ │ │  │ │  │ 
  │ │ │ {┼────┼─┼─┼──┼─┼─´│ 
@@ -399,9 +399,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 @@ -413,7 +413,7 @@

N-queens problem

-
+
 ┌─                 
 ╵ 0 1 0 1 0 1 0 0  
   0 0 1 1 1 0 0 0  
@@ -461,9 +461,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 @@ -498,9 +498,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 @@ -535,9 +535,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. @@ -555,9 +555,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 @@ -599,11 +599,11 @@

H-index

-
-

Trapping rain water

-
+
+

Trapping rain water

+

-This is a classical interview problem that can be solved in linear time. Interestingly, +This is a classical interview problem that can be solved in linear time. Interestingly, it admits a very elegant array solution:

@@ -618,18 +618,23 @@

Trapping rain water

-That is, we take the minimum of max scans from the left and from the right, and subtract the corresponding height. -Folding sum over the result gives the answer. A closely related problem is container with most water, here is a -slow but short quadratic solution: +That is, we take the minimum of max-scans from the left and from the right, and subtract the corresponding height. +Reducing the resulting array gives the amount of trapped water. +

+ +

+A closely related problem is container with most water, which unfortunately is not so easy to solve in linear +time using an array approach (one can easily implement the imperative two pointers solution in BQN, but it +will probably be slow). Here are two solutions, one \(O(n^2)\) and the other \(O(n\log n)\), both tacit:

-
(⌈´∘⥊⌊⌜˜×·-⌜˜⊒˜) [1,8,6,2,5,4,8,3,7]
+
⟨⌈´∘⥊⌊⌜˜×·-⌜˜⊒˜, ⌈´∨×(⌈`⊸-⌈⊢-⌊`)∘⍒⟩ {10 𝕎•_timed𝕩}¨< •rand.Range˜1e4
 
-49
+⟨ 0.080050875 4.14558e¯5 ⟩
 
diff --git a/si.html b/si.html index 2ae415e..9d88e72 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 dd0628f..928a93e 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"