---
jupytext:
  formats: ipynb,md:myst
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
    jupytext_version: 1.13.6
kernelspec:
  display_name: Python 3 (phys-581)
  language: python
  name: phys-581
---

Class Log
=========

<!--
pi = 11.00100100001111110110
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[3, 5, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11]
[11, 101, 101, 110, 111, 1000, 1001, 1001, 1010, 1010, 1011]

10^(2*digits-2)
2^(2*digits-4)

10^0 = 1 digit
10^2 = 2 digits
10^4 = 3 digits


     0b1 = 1                = 1
1    0b11 = 3          2^0  = 2
4    0b110 = 6         2^2  = 3
16   0b1100 = 12       2^4  = 4 digits
64   0b11001 = 25      2^6  = 5 digits
256  0b110010 = 50     2^8  = 6 digits
1024 0b1100100 = 100   2^10 = 7 digits
0b11001001 = 201
0b110010010 = 402
0b1100100100 = 804
0b11001001000 = 1608
0b110010010000 = 3216
-->

Wed 3 Dec 2025
--------------
Project Checklist:
* Hosted on GitLab (please confirm with me the project you want me to review.)
* Serious attempt to write some code.
* Tests with > 85% code coverage.
* Documentation hosted on GitLab Pages (i.e. using the `.gitlab-ci.yml` file I
  provided).
* `README.md` file that explains how to get started (i.e. how to install your code, run your
  tests etc.)
* Automated CI running of your tests which you push with `CI` in your commit message.

  This is new: please try to add a target to `.gitlab-ci.yml` that will automatically
  run your tests.
  
Please have a complete project for me to review by **Wed 10 December**.  I will give
comments and then do my final review on **Sunday 14 December**.


Mon 1 Dec 2025
--------------
* Discussed projects, especially how to get started with LaTeX for APS PRL publications.
* Discuss {ref}`sec:Graphs`.  I created a new sampled project for this on GitLab:
  * <https://gitlab.com/wsu-courses/physics-581-2025/graphs/>
  * <https://wsu-courses.gitlab.io/physics-581-2025/graphs/>

Mon 17 Nov 2025
---------------
Work through [Fitting: Model
Selection](https://wsu-courses.gitlab.io/physics-581-2025/data-fitting/fitting-2/)

* <https://gitlab.com/wsu-courses/physics-581-2025/data-fitting>

Mon 27 Oct 2025
---------------
* GitLab Pages.  CI.
* Optimization - Gradient Descent.
  * 

Mon 20 Oct 2025
---------------
* Optimization: Root finding vs Extremization.
* Ground state of the SEQ.
* A good read (from Dan Dolan): [The Great Software Quality Collapse: How We Normalized
  Catastrophe][].


[The Great Software Quality Collapse: How We Normalized Catastrophe]: <https://techtrenches.substack.com/p/the-great-software-quality-collapse>

Wed 15 Oct 2025
---------------

Mon 13 Oct 2025
---------------

Wed 8 Oct 2025
--------------
Work through {ref}`sec:SEQ` following the notes {ref}`sec:SEQNotes`.
* By the end of class, everyone should have their own version of this project, as well
  as a fork of the official "class" version.

Mon 6 Oct 2025
--------------
* Discussed how one can simply truncated finite difference schemes
  {ref}`sec:Derivatives` to implement Dirichlet boundary conditions on complicated
  domains.  See [Physics 571: Drums][] for more details.
* Discussed {ref}`sec:FourierTechniques`.

[Physics 571: Drums]: <https://physics-571-math-methods.readthedocs.io/en/latest/Notes/Drums.html>

Wed 1 Oct 2025
--------------
* Classes in Python.

  ```
  class A:
      p = 2
      def pow(self, x):
          return x**self.p
  a = A()
  a.pow(x=3) == A.pow(self=a, x=3)
  ```

  Can you explain this?
  ```
  class A:
      p = 2
      def pow(self, x):
          return x**self.p
  a = A()
  a.pow(x=3) == 9
  A.p = 3
  a.pow(x=3) == 27
  
  a = A()
  a.p = 3
  a.pow(x=3) == 27

  a = A()
  a.p = 1
  A.p = 3
  a.pow(x=3) == 3
  ```

* Introduction to the FFT as a way to compute derivatives.  Please read
  {ref}`sec:FourierTechniques`.

Mon 29 Sept 2025
----------------
* Test-driven development for solving the Schrödinger equation.  See {ref}`sec:TDSEQ`
  for details.  The 

Wed 24 Sept 2025
----------------
We continued discussion about the Schrödinger equation, and I showed how to
make interactive plots with `ipywidgets` and animations using `display`
{class}`phys_581.contexts.FPS`.

Mon 22 Sept 2025
----------------
We started by integrating $y' = \mu y$ with $y(0) = 1$ for $\mu = -20.0$ using Euler's
method from $x=0$ to $x=1$.  Following the example from §8.2 of {cite}`Gezerlis:2023`,
we demonstrated that for $N\leq 9$ points, the method is unstable leading to exponential
growth.  We then showed that this could be remedied by backwards Euler.

For more details see {ref}`sec:ODEs`.

We then went through how to compute $\op{H}\ket{\psi}$ for a 1D harmonic oscillator
using finite differences.  For more details, see {ref}`sec:TDSEQ` and
{ref}`sec:Derivatives`. 

Wed 17 Sept 2025
----------------

Mon 15 Sept 2025
----------------
Walk-through using Git, GitLab, etc. with Richardson Extrapolation example.

* Make sure you have `conda`, `mamba`, or `micromamba`

```bash
mkdir Physics-581-tools
git init
pixi init
pixi add python==3.12 pytest numpy pytest-cov
git add .
git commit -m "Initial commit with pixi files"
mkdir -p src/phys_581_tools
mkdir tests
pixi shell
```




Wed 10 Sept 2025
----------------
* Globally Convergent Newton's Method for LambertW.
* Overview of Git, the Shell, etc.
* Create GitLab project.
  * Start with Git, move to Jujutsu.
* Create project
  *

Mon 8 Sept 2025
---------------
* Review sections from Pragmatic Programmer.
* Review code progress.
  * Showed how one might accelerate series acceleration.
  * Discussed Newton's method.

Wed 3 Sept 2025
---------------
* Finite difference for derivatives and round-off error.  Working on
  {ref}`sec:Assignment1`.

Wed 26 Aug 2025
---------------
* Insertion sort: $S(N) = N + 2S(N/2)$, $S(1) = 1$.  Show that $S(N) = N\log_{2}(2N)$.
  General asymptotic properties of recursive algorithms can be found using the [Master
  theorem][].
* Fibonacci: $F_{n} = ...$ (perhaps using generating functions?).
* Mostly let students program {ref}`sec:Assignment0`.  Some issues came up.
  * Some students had difficulty with `compose(f, n)`.  If they have not been exposed to
    higher-order functions (and they are not common in some languages), then this is
    quite new.
  * Some students were over-complicating things, such as trying to write different code
    for different types.  Mentioned duck typing in python.  Should mention generics in C++.

[Master theorem]: <https://en.wikipedia.org/wiki/Master_theorem_(analysis_of_algorithms)>

Mon 25 Aug 2025
---------------
* PP2: The Cat Ate My Source Code
* PP2: Software Entropy
* PP5: Good-Enough Software
* Recursion (Fibonacci, Insertion Sort, Invariants).  See
  {ref}`sec:RecursionAndInvariants`.  As a data-fitting project, fit the model $t(n)
  \propto b^n$ for the execution time of the simplest recursive implementation of
  `fib(n)` and provide uncertainties on the parameter $b$.  Where does it come from?
* Heapsort.  Implement this using only small tuples: pairs `(a, b)`, triples `(a, b, c)`
  (which could be implemented in terms of pairs `(a, (b, c))`) etc.  This is a good
  problem for an in-person programming interview.  (Don't use AI!  These are well
  studied so there are lots of generic solutions.)
* CoCalc demo of Fib.

Wed 20 Aug 2025
---------------
* PP2: The Cat Ate My Source Code
* Create the [2025 Physics 581 Class Project][].  Record this and show students how to
  do this (for later).
* Discuss [CoCalc][]
* [Visualizing sorting](https://www.youtube.com/watch?v=kPRA0W1kECg&ab_channel=TimoBingmann)

[2025 Physics 581 Class Project]:  <https://gitlab.com/wsu-courses/2025-physics-581-class-project>

Mon 18 Aug 2025
---------------
* Went over aspects of the Syllabus, including use of LLMs, Textbooks, and the first 2 SLOs.
* Read [PP22: Engineering Daybooks][]: discussed uses of a daily "experimental" logbook for
  the purposes of reproducible research, and the reason for writing by hand vs typing.
  Example of reproducible computing: recording the date and details of the machine on
  which you ran a simulation might help you reproduce the run in the future if hardware
  changes -- maybe through an emulator.
* Discussed random number generators and the importance of seeds for reproducible results.
* Discussed the following abstract data types ([ADT][]s):
  * **Sets**: A collection of data `s` with operations `s.add(x)`, `s.pop()`, and
    `s.contains(x)` (the latter is written `x in s` in Python).  We discussed other
    operations like `union(s1, s2)`, `intersection(s1, s2)` but argued that these more
    complex operations can be implemented in terms of the others.
    
    :::{doit} Describe how to implement these.
    :class: dropdown
    For example, using the python-like pseudocode from class:
    ```
    def union(s1, s2):
        s = Set()
        
        # Backups for sets s1 and s2.
        s1a = Set()
        s2a = Set()
        while (x = s1.pop()) != ∅:
            s.add(x)
            s1a.add(x)
        while (x = s2.pop()) != ∅:
            s.add(x)
            s2a.add(x)
            
        # Restore s1 and s2
        while (x = s1a.pop()) != ∅:
            s1.add(x)
        while (x = s2a.pop()) != ∅:
            s2.add(x)
        return s
    ```
    More simply, we could first defined a `s.copy()` operation (do it!), then write:
    ```
    def union(s1, s2):
        s = Set()
        
        # Work with copies
        s1 = s1.copy()
        s2 = s2.copy()
        
        while (x = s1.pop()) != ∅:
            s.add(x)
        while (x = s2.pop()) != ∅:
            s.add(x)
        return s
    ```
    
    :::
  * **Stack**: A collection of data `s` with operations `s.put(x)`, and `s.pop()` where
    `s.pop()` removes and returns the **last** item pushed onto the stack:
    last-in-first-out (LIFO) (sometimes called first-in-last-out or FILO).
  * **Queue**: A collection of data `s` with operations `s.put(x)`, and `s.pop()` where
    `s.pop()` removes and returns the **first** item pushed into the queue: first in
    first out (FIFO).

We discussed how consistency of an [ADT][] can be most easily ensured by basing it on
physical reality:
* A [stack][] represents a pile of items where you can put and take (pop) from only the top.
* A [queue][] represents a pipe where you can push items in one and take them from the
  other.  We discussed how this can be used for communicating between multiple processes
  in a thread0safe manner.
  * A [deque][] (double-ended queue) is a related representation of a pipe, where you
    can add and remove elements from either end (preserving the physical order in the
    pipe).

## Problem

In class, students were asked to implement a [Priority Queue][] in terms of a [Queue][]
with operations `q.put(x)` and `q.pop()` where `q.pop()` returns the largest value in
the queue, or a [sentinel value][] `∅` if the queue is empty.  We assume that the
elements can be compared `x < y` etc. 

We discussed that it might be useful to first implement methods like `q.empty()` which
returns `True` if the queue is empty, and `False` otherwise, and `q.copy()` which
returns a copy.

:::{doit} Implement the `q.empty()` and `q.copy()`.
:class: dropdown

```
def empty(q):
    q = q.copy()  # If this is not done, q will be changed.
    return q.pop() == ∅

def copy(q):
    q1 = Queue()
    q2 = Queue()
    while (x = q.pop()) != ∅:
        q1.put(x)
        q2.put(x)
        
    # Restore q
    while (x = q1.pop()) != ∅:
        q.put(x)
        
    return q1
```
:::

:::{doit} Implement the [Priority Queue][].
:class: dropdown

```
# Your solution goes here:-)
```
:::
[PP22: Engineering Daybooks]: <https://research.ebsco.com/plink/02aa1c94-8474-3ad2-9ce0-cf5bcd81b7d7>
[Queue]: <https://en.wikipedia.org/wiki/Queue_(abstract_data_type)>
[Stack]: <https://en.wikipedia.org/wiki/Stack_(abstract_data_type)>
[Priority Queue]: <https://en.wikipedia.org/wiki/Priority_queue>
[Deque]: <https://en.wikipedia.org/wiki/Double-ended_queue>
[sentinel value]: <https://en.wikipedia.org/wiki/Sentinel_value>
[ADT]: <https://en.wikipedia.org/wiki/Abstract_data_type>
  

[CoCalc]: <https://cocalc.com> "CoCalc: Collaborative Calculation and Data Science"
[GitLab]: <https://gitlab.com> "GitLab"
[Official Course Repository]: <https://gitlab.com/wsu-courses/physics-581-physics-inspired-computation/> "Official Physics 581 Repository hosted on GitLab"
[`solve_ivp`]: <https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html>
