Euler problem 4.1

(describe #'elt)

(defun palindromep (seq)
  (let ((end (1- (length seq))))
    (or (< end 1)
        (and (eql (elt seq 0) (elt seq end))
             (palindromep (subseq seq 1 end))))))

(defun euler-4 ()
  (loop for a from 1 to 999
      maximize
      (loop for b from 1 to 999
            when (palindromep
                    (prin1-to-string (* a b)))
              maximize (* a b) into best
            finally (return (or best 0)))))

— skeeto/euler-cl

.

— Me@2022.10.03 03:03:30 PM

.

.

2022.10.03 Monday (c) All rights reserved by ACHK

SimCity 2013

Euler problem 3.4

.

.

primes = 2 : filter (null . tail . primeFactors) [3, 5 ..]

primeFactors n = factor n primes
  where
    factor n (p : ps)
      | p * p > n = [n]
      | n `mod` p == 0 = p : factor (n `div` p) (p : ps)
      | otherwise = factor n ps

f = last (primeFactors 600851475143)

— Haskell offical

.

— Me@2022.09.28 11:44:20 AM

.

.

2022.09.28 Wednesday (c) All rights reserved by ACHK

Ex 1.28 Prequel 1

Structure and Interpretation of Classical Mechanics

.

An analogous result holds when the f_\alpha‘s depend explicitly on time.

a. Show that in this case the kinetic energy contains terms that are linear in the generalized velocities.

~~~

Eq. (1.141):

\displaystyle{  \textbf{v}_\alpha = \partial_0 f_\alpha (t,q) + \partial_1 f_\alpha (t, q) v  }

Eq. (1.142):

\displaystyle{T(t, q, v) = \frac{1}{2} \sum_{\alpha} m_\alpha v^2_\alpha}

Eq. (1.133):

\displaystyle{  \begin{aligned}  \mathcal{P}_i &= (\partial_2 L)_i \\   \end{aligned}}

where \displaystyle{\mathcal{P}} is called the generalized momentum.

.

\displaystyle{  \begin{aligned}  \mathcal{P} \dot Q &= (\partial_2 L) \dot Q  \\   &= (\partial_2 (T-V)) \dot Q  \\   &= (\partial_2 T) \dot Q  \\   \end{aligned}}

where \displaystyle{\dot Q} is the velocity selector. And V has no velocity component?

.

“… T is a homogeneous function of the generalized velocities of degree 2.”

\displaystyle{  \begin{aligned}  n &= 2 \\   (\partial_2 T) \dot Q &= ? \\   \end{aligned}}

\displaystyle{  \begin{aligned}  x Df(x) &= nf(x)  \\   \end{aligned}}

.

What does the D actually mean?

\displaystyle{ \begin{aligned}    x Df(x) &= nf(x)  \\   x \frac{d}{dx} f(x) &= nf(x)  \\   \end{aligned}}

\displaystyle{ \begin{aligned}    \mathbf{v} D T (\mathbf{v}) &= 2 T (\mathbf{v}) ? \\     \end{aligned}}

.

\displaystyle{ \begin{aligned}     \partial_{\vec 2}   = \frac{\partial}{\partial \vec{\dot q}}   &= \begin{bmatrix} \frac{\partial}{\partial \dot q_1} & \frac{\partial}{\partial \dot q_2} & ... \end{bmatrix} \\ \\      \mathcal{P} \dot Q &= (\partial_2 T) \dot Q \\ \\    \begin{bmatrix} \mathcal{P}_1 & \mathcal{P}_2 & ... \end{bmatrix}     \begin{bmatrix} \dot Q_1 \\ \dot Q_2 \\ \vdots \end{bmatrix}     &=    \begin{bmatrix} (\partial_2 T)_1 & (\partial_2 T)_2 & ... \end{bmatrix}     \begin{bmatrix} \dot Q_1 \\ \dot Q_2 \\ \vdots \end{bmatrix} \\     \\    &=    \begin{bmatrix} \frac{\partial T}{\partial \dot q_1} &  \frac{\partial T}{\partial \dot q_2}  & ... \end{bmatrix}     \begin{bmatrix} \dot Q_1 \\ \dot Q_2 \\ \vdots \end{bmatrix} \\     \\     &=    \frac{\partial T}{\partial \dot q_1} \dot Q_1 + \frac{\partial T}{\partial \dot q_2} \dot Q_2  + ...     \\       \end{aligned}}

.

Euler’s Homogeneous Function Theorem:

If

\displaystyle{ \begin{aligned}      f(t x_1, t x_2, ...) &= t^n f( x_1, x_2 , ...),  \\     \end{aligned}}

then

\displaystyle{ \begin{aligned}      \sum_{i} x_i \frac{\partial f}{\partial x_i} &= n f(\mathbf{x}) \\    \end{aligned}}

.

\displaystyle{T(t, q, v) = \frac{1}{2} \sum_{\alpha} m_\alpha v^2_\alpha}

So

\displaystyle{\begin{aligned}     T(t, q, uv)     &= \frac{1}{2} \sum_{\alpha} m_\alpha (uv)^2_\alpha \\    &= u^2 T(t, q, v) \\    \end{aligned}    }

Therefore,

\displaystyle{ \begin{aligned}     \frac{\partial T}{\partial \dot q_1} \dot Q_1 + \frac{\partial T}{\partial \dot q_2} \dot Q_2  + ...     &= 2 T \\     \end{aligned}}

— Me@2022.09.27 12:26:09 PM

.

.

2022.09.27 Tuesday (c) All rights reserved by ACHK

Haskell mode, 2

Euler problem 3.3

.

The goal of this blog post is to install an advanced Haskell mode, called LSP mode, for Emacs.

.

1. Open the bash terminal, use the following commands to install the three packages:

sudo apt-get install elpa-haskell-mode

sudo apt-get install elpa-yasnippet

sudo apt-get install elpa-which-key

.

2. Read and follow the exact steps of my post titled “Haskell mode“.

.

.

— Me@2022.09.20 12:49:29 PM

.

.

2022.09.21 Wednesday (c) All rights reserved by ACHK

Way

There is always a WAY.

.

(defun sum-of-squares(x y &key (a 1) (b 1))
  (+ (* a x x) (* b y y)))

(deftype candidate()
  '(simple-array boolean (*)))

(defmacro get-sieve-function(a b mod-results)
  `(lambda(x y)
     (let* ((n (sum-of-squares x y :a ,a :b ,b))
        (r (mod n 12)))
       (when (and (<= n limit)
          (not (null
            (member r ,mod-results))))
     (setf (aref candidates n)
           (not (aref candidates n)))))))

(defun get-atkin-prime-candidates-map(limit)
  (let* ((lmt (isqrt limit))
         (candidates (make-array
              (1+ limit)
              :initial-element nil))
         (stage1 (get-sieve-function 4 1 '(1 5)))
         (stage2 (get-sieve-function 3 1 '(7)))
         (stage3 (get-sieve-function 3 -1 '(11))))
    (declare (type candidate candidates))
    (declare (optimize (speed 3)))
    (loop for x from 1 to lmt do
      (loop for y from 1 to lmt do
    (progn
      (funcall stage1 x y)
      (funcall stage2 x y)
      (when (> x y)
        (funcall stage3 x y)))))
    candidates))

(defun atkin-sieve-map(candidates)
  (let ((len (length candidates)))
    (loop for i from 1 to (1- len)
      when (aref candidates i)
        do (loop for j from 1
             for n = (* j i i)
             while (< n len)
             do (setf (aref candidates n) nil))))
  (setf (aref candidates 2) T)
  (setf (aref candidates 3) T)
  candidates)

(defun eratosthene-sieve-map(limit)
  (let ((candidates
      (make-array
       (1+ limit) :initial-element T)))
    (declare (type candidate candidates))
    (declare (optimize (speed 3)))
    (progn
      (setf (aref candidates 0) nil)
      (setf (aref candidates 1) nil))
    (loop for i from 2 to limit
      when (aref candidates i)
        do (loop for j from (+ i i) to limit by i
             when (aref candidates j)
               do (setf (aref candidates j) nil)))
    candidates))

;; This is free software released
;; into the public domain.
;;
;; ykm

(defun get-primes(limit
          &key
            (generator :eratosthene))
  (let ((candidates
      (case generator
        (:atkin (atkin-sieve-map
             (get-atkin-prime-candidates-map
              limit)))
        (:eratosthene (eratosthene-sieve-map
               limit))
        (otherwise
         (error "not valid type
(:atkin :eratosthene)")))))
    (declare (type candidate candidates))
    (loop for i from 0 to (1- (length candidates))
      when (aref candidates i)
        collect i)))

(format t "soe: ~d
" (time (length (get-primes 12345678))))

(format t "soa: ~d
" (time (length (get-primes 12345678
                :generator :atkin))))

(defmacro log10 (x)
  `(/ (log ,x) (log 10)))

.

— Me@2022.09.06 03:24:36 PM

.

.

2022.09.06 Tuesday (c) All rights reserved by ACHK

Homogenous function

A function f is homogenous of degree n if and only if f(ax) = a^n f(x).

— 1.8 Conserved Quantities

— Structure and Interpretation of Classical Mechanics

.

\displaystyle{\begin{aligned}     D_a f(ax)     &= \frac{d}{da} f(ax) \\     &= \frac{d}{da} a^n f(x) \\     &= f(x) \frac{d}{da} a^n  \\     &= n a^{n-1} f(x) \\     \end{aligned}}

.

\displaystyle{\begin{aligned}     D_a f(ax)     &= \frac{d}{da} f(ax) \\     &= \frac{d(ax)}{da} \frac{d}{d(ax)} f(ax) \\     &= x \frac{d}{d(ax)} f(ax) \\     &= x D_{ax} f(ax) \\     \end{aligned}}

.

\displaystyle{\begin{aligned}     x D_{ax} f(ax) &= n a^{n-1} f(x) \\    x D_{x} f(x) &= n f(x) \\    \end{aligned}}

— Me@2022.09.05 06:28:00 PM

.

.

2022.09.06 Tuesday (c) All rights reserved by ACHK

It doesn’t matter

Euler problem 3.1

.

The bad news is: You cannot make people like, love, understand, validate, accept, or be nice to you.

The good news is: It doesn’t matter.

(defmacro sq (x)
  `(* ,x ,x))

(defmacro list-head (lst)
  `(car ,lst))

(defmacro list-tail (lst)
  `(cdr ,lst))

(defmacro last-item (lst)
  `(car (last ,lst)))

(defun good-reverse (lst)
  (labels ((rev (lst acc)
         (if (null lst)
         acc
         (rev
          (cdr lst)
          (cons (car lst) acc)))))
    (rev lst nil)))

(defun prime-sieve-a-list (input-lst)
  (labels ((sieve-iter (go-lst acc-list)
         (if (not go-lst) 
         acc-list        
         (if (> (sq (list-head go-lst))
            (last-item go-lst))

             (append (good-reverse acc-list)
                 go-lst)
             
             (sieve-iter
              (remove-if #'(lambda (x)
                     (=
                      (mod x (list-head go-lst))
                      0))
                 (list-tail go-lst))
              (cons (list-head go-lst)
                acc-list))))))

    (sieve-iter input-lst '())))

(defun range (max &key (min 0) (step 1))
  (loop :for n :from min :below max :by step
    collect n))

(defmacro prime-sieve (n)
  `(prime-sieve-a-list (cons 2 (range (1+ ,n)
                      :min 3
                      :step 2))))

(time (length (prime-sieve 1234567)))

;; 0.764 seconds of real time
;; 95360

(time (length (prime-sieve 12345678)))

;; 20.128 seconds of real time
;; 809227

— Me@2022-08-27 07:59:30 PM

.

.

2022.08.28 Sunday (c) All rights reserved by ACHK

High level machine language

susam 16 days ago

The first article in this issue of BYTE has a very interesting characterization of Lisp that I have not come across before. I mean, famous quotes like “Lisp is a programmable programming language” by John Foderaro and “The greatest single programming language ever designed” by Alan Kay are often mentioned in articles about Lisp. But in this issue of BYTE, the article “An Overview of LISP” by John Allen at page 10 has something very interesting to say. Excerpt from the article:

“The best description of the LISP programming language is that it is a high level machine language. That is, it shares many of the facets of contemporary machine language –the necessity for attention to detail and the freedom to manipulate the machine’s data and programs without restriction– yet LISP is high level in that the language contains the expressive power and convenience of traditional high level languages. The contradiction is resolvable: a LISP machine is just a higher level machine whose data items are organized differently from the binary bit patterns of most machines, and the LISP programming language is the assembly language for this machine.”

Consider the Emacs Lisp (Elisp) interpreter for example. Elisp interpreter is the Lisp machine. It understands Elisp symbolic expressions, the language of this machine. With enough code written in this machine’s language, we get this fine editing and productivity software known as Emacs!

aap_ 16 days ago

This exactly matches my thoughts. It seems that machine language and LISP are the only two languages (that I know anyway) where code and data are fundamentally the same kind of thing.

— Byte Magazine: LISP (1979)

— Hacker News

.

.

2022.08.22 Monday ACHK

Haskell mode

The goal of this blog post is to install an advanced Haskell mode, called LSP mode, for Emacs.

0. In this tutorial, you will need to go to the official website of NixOS and that of MELPA (Milkypostman’s Emacs Lisp Package Archive). Make sure that both websites are the real official ones. Any instructions from an imposter website can get your machine infected with malware.

1. Assuming your computer OS is Ubuntu 20.04 or above, go to the NixOS official website. Follow the instructions to install the Nix package manager (not the NixOS) onto your OS. Choose the “single-user installation” method.

2. On the NixOS official website, click the magnifying glass at the top right corner to reach the package search engine.

3. Search “haskell language server” and then copy its installation command.

nix-env -iA nixpkgs.haskell-language-server

4. Run the command in the bash terminal to install the Haskell Language Server.

.

5. Search “stack” on the package search engine.

6. Run its installation command

nix-env -iA nixpkgs.stack

to install the Haskell Tool Stack.

7. Search “ghc” on the package search engine.

8. Run its installation command

nix-env -iA nixpkgs.ghc

to install the Glasgow Haskell Compiler.

.

9. Reboot your computer.

This step is needed for triggering the OS to recognize the Nix package manager setup.

.

10. Go to MELPA package manager’s official website. Follow the instructions to install “Melpa”, not “Melpa Stable”.

11. Open the Emacs editor. Click "Options" and then "Manage Emacs Packages".

Install the following packages. For each of them, make sure that you have chosen the source archive as “melpa“. Versions from other sources would not work.

company Modular text completion framework
flycheck On-the-fly syntax checking
lsp-haskell Haskell support for lsp-mode
lsp-mode LSP mode
lsp-ui UI modules for lsp-mode

12. Open Emacs’ initialization file, which has the filename

.emacs

Its location should be

~/.emacs

13. Add the following code to the file.

;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'company)

(require 'flycheck)

(require 'lsp-ui) 

;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'lsp)

(add-hook 'haskell-mode-hook #'lsp)

(add-hook 'haskell-literate-mode-hook #'lsp)

(save-place-mode 1)

;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun haskell-and-go-back ()

  (interactive)
  
  (haskell-process-load-file)
  
  (windmove-up))

(global-set-key 
            (kbd "C-n") 
            'haskell-and-go-back)

;;;;;;;;;;;;;;;;;;;;;;;;;;

14. Close the Emacs program.

.

15. Create a dummy Haskell source code file named “test.hs”.

16. Use Emacs to open it.

17. You should see this message:

18. Select one of the first 3 answers. Then you can start to do the Haskell source code editing.

19. To compile your code, hold the Ctrl key and press n.

Ctrl+n

— Me@2022-08-18 05:22:02 PM

.

.

2022.08.20 Saturday (c) All rights reserved by ACHK

Clojupyter

SICMUtils, 4

.

The goal of this post is to run SICMUtils in Clojure in Jupyter Notebook.

.

1. Read and follow the exact steps of my post titled “SICMUtils“.

2. Do the same for my another post “Jupyter Notebook“.

.

3. Use Emacs to open the file:

~/my-stuff/project.clj

4. Replace the existing :dependencies line with this one:

  :dependencies [[org.clojure/clojure "1.11.1"]
                 [sicmutils "0.22.0"]
                 [clojupyter "0.3.3"]]

And make sure that all version numbers are the most updated ones.

5. Save the file.

.

6. In bash terminal, go to your project folder such as “~/my-stuff“:

cd ~/my-stuff

7. Run this to download all the dependencies of the project.

lein deps 

.

8. Clojupyter is a Jupyter kernel for Clojure. According to its documentation, Clojupyter has provided some command line tools. However, I could not understand how to use those commands in the bash terminal.

Finally, I found out that to run a Clojupyter command, such as “list-commands“, we just need to add the following code before it:

lein run -m clojupyter.cmdline

.

That results the whole command as

lein run -m clojupyter.cmdline list-commands

However, that would be wordy. So we create a shortcut for it.

.

9. Use Emacs to open the file:

~/my-stuff/project.clj

10. Below the entry “:dependencies“, add another one called “:aliases“.

:dependencies [[org.clojure/clojure "1.11.1"]
               [sicmutils "0.22.0"]
               [clojupyter "0.3.3"]]
;
:aliases {"cjcmd"
; 
          ["run" "-m" "clojupyter.cmdline"]}

11. Then, the shortcut is made. To test, run:

lein cjcmd list-commands

.

12. Go to your project folder:

cd ~/my-stuff

13. And then run

lein uberjar

to create a binary file named my-stuff-0.1.0-SNAPSHOT-standalone.jar, which includes not only the program itself, but also all its dependencies.

We are going to use it to generate a Jupyter kernel that includes not only the Clojure language, but also the SICMUtils mechanics library.

.

14. For the following code,

lein cjcmd install 
    --ident cj-kernel 
    --jarfile 
    ~/my-stuff/target/uberjar/standalone.jar

replace the file name

standalone.jar

with

my-stuff-0.1.0-SNAPSHOT-standalone.jar

15. In the bash terminal, run the code in one line.

.

16. In your OS, try to open the SageMath program. It will open a Jupyter notebook page.

17. Click the “New” button at the top right corner and then select “cj-kernel“.

18. There might be some error messages, such as

“The kernel appears to have died. It will restart automatically.”

However, you can actually use the program, i.e. the Clojure language with the SICMUtils mechanics library.

.

19. Type

(clojure-version)

onto the input line.

20. Hit the keys shift-enter to get the output.

.

21. Input

(require '[sicmutils.env :as env])

and hit shift-enter.

22. Also

(env/bootstrap-repl!) 

There might be some WARNING messages. But you can just ignore them.

.

23. Code

(->TeX (simplify ((D cube) 'x)))

will result

3\\,{x}^{2}

.

24. Test some code examples provided by the official website of Clojupyter.

.

25. Go to the official website of SICMUtils. Go to its jupyter directory to read the example notebooks.

sicmutils/jupyter/

.

26. Go to the documentation of SICMUtils. Read the page “Comparison to scmutils“.

— Me@2022-07-30 12:18:50 PM

— Me@2022-08-18 09:07:36 AM

.

.

2022.08.18 Thursday (c) All rights reserved by ACHK

Before Mathematica

I figured if I couldn’t explain something clearly in documentation, nobody was ever going to understand it, and it probably wasn’t designed right. And once something was in the documentation, we knew both what to implement, and why we were doing it.

I think this a great practice to follow. I often find it very helpful to write documentation before writing code. I find I end up with a better designed system this way, and as an added bonus it has great documentation too.

— There Was a Time before Mathematica

— Stephen Wolfram

.

The main idea is to regard a program as a communication to human beings rather than as a set of instructions to a computer.

— Donald Knuth

.

Programs are meant to be read by humans and only incidentally for computers to execute.

— Structure and Interpretation of Computer Programs

.

.

2022.08.16 Tuesday ACHK

開心 vs 關心

Euler problem 2

.

The older I get, the less I care about what people think about me. Therefore the older I get, the happier I am.

.

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

(defun fib (n)
  (cond ((= n 0) 0)
        ((= n 1) 1)
        ((> n 1) (+ (fib (- n 2)) (fib (- n 1))))))

(time (fib 40))

;; Evaluation took:
;;   2.648 seconds of real time
;;   2.640080 seconds of total run time
;;   99.70% CPU
;;   9,002,590,370 processor cycles
;;   0 bytes consed
;; 

;; tail resursion

(defun fib-t (n)
  (labels ((fib-iter (m a b)
            (if (= m 0)
            a
            (fib-iter (- m 1) b (+ a b)))))
        (fib-iter n 0 1)))

(time (fib-t 40))

;; Evaluation took:
;;   0.000 seconds of real time
;;   0.000002 seconds of total run time
;;   100.00% CPU
;;   2,184 processor cycles
;;   0 bytes consed
;;
  
;; infinite list

(defmacro append-last (lst obj)
  `(append ,lst (list ,obj)))

(defun fib-i (n)
  (labels ((fib-iter (m fib-list a b)
            (if (> m (- n 2))
            fib-list
            (fib-iter (1+ m)
                (append-last fib-list (+ a b))
                b
                (+ a b)))))
    (fib-iter 0 '(0 1) 0 1)))

(time (fib-i 40))

;; Evaluation took:
;;   0.000 seconds of real time
;;   0.000008 seconds of total run time 
;;   100.00% CPU
;;   21,960 processor cycles
;;   32,768 bytes consed


(defun filter (fn lst)
  (let ((acc nil))
    (dolist (x lst)
      (let ((val (funcall fn x)))
    (if val (push val acc))))
    (nreverse acc)))

(defmacro filter-list (fn lst)
  `(filter #'(lambda (x)
                (if (funcall ,fn x) x))
            ,lst))

(defmacro filter-2 (fn1 fn2 lst)
  `(filter-list #'(lambda (x)
                    (if (and
                    (funcall ,fn1 x)
                    (funcall ,fn2 x))
                    x))
                ,lst))

(reduce #'+ (filter-2 #'evenp
              #'(lambda (x) (< x 4000000))
              (fib-i 100)))

; 4613732

— Me@2022-08-07 12:34:19 PM

.

.

2022.08.09 Tuesday (c) All rights reserved by ACHK

Mega Man Zero 3

Euler problem 1

.

(proclaim '(optimize speed))

(reduce #'+ '(1 2 3 4))

; 10

(loop :for n :below 10 :collect n)

; (0 1 2 3 4 5 6 7 8 9)

(describe :below)

(defun range (max &key (min 0) (step 1))
   (loop :for n :from min :below max :by step
      collect n))
      
(- (+ (* 3 (reduce #'+ (range 334 :min 1 :step 1)))
      (* 5 (reduce #'+ (range 200 :min 1 :step 1))))
   (* 15 (reduce #'+ (range 67 :min 1 :step 1))))
   
; 233168

(defun sum-1-n (n)
  (/ (* n (+ n 1)) 2))
  
(- (+ (* 3 (sum-1-n 333))
      (* 5 (sum-1-n 199)))
   (* 15 (sum-1-n 66)))
   
; 233168

— Me@2022-08-01 03:29:01 PM

.

.

2022.08.01 Monday (c) All rights reserved by ACHK

Jupyter Notebook

SICMUtils, 3

.

The goal of this post to setup Jupyter Notebook.

.

1. Read and follow the exact steps of my post titled “SICMUtils“.

.

2. To install the Jupyter Notebook software in Ubuntu, use this command:

sudo apt-get install sagemath-jupyter 

3. Try to open the SageMath program.

4. It will open a Jupyter notebook page.

5. Click the “New” button at the top right corner and then select “SageMath“.

.

6. Type

1 + 1

onto the input line.

7. Hit the keys shift-enter to get the output

2

.

8. Input

Integrate(x^3, x)

9. Hit shift-enter:

NameError: name 'Integrate' is not defined

.

10. Input

from sage.symbolic.integration.integral import *

indefinite_integral(x^3, x)

11. Hit shift-enter:

1/4*x^4

.

12. Input

%display latex

13. Hit shift-enter.

.

14. Input

indefinite_integral(x^3, x)

15. Hit shift-enter:

\displaystyle{\frac{1}{4} \, x^{4}}

— Me@2022-07-30 12:18:50 PM

.

.

2022.08.01 Monday (c) All rights reserved by ACHK

Org-babel-clojure

SICMUtils, 2

.

The goal of this post to setup the Emacs editor for Clojure programming.

.

1. Read and follow the exact steps of the last post.

.

2. Open the .emacs file. Go to the end of the file. Add the following code:

(require 'org)
(require 'ob-clojure)

(setq org-babel-clojure-backend 'cider)
(require 'cider)

(set-register ?c '(file . "~/my-stuff/my-stuff.org"))

(setq org-confirm-babel-evaluate nil)

(setq org-src-tab-acts-natively t)

3. Close Emacs.

.

4. Go to the directory “~/my-stuff/” and then create a file named “my-stuff.org“.

5. Use Emacs to open the file.

6. Within the file, add the following code:

#+BEGIN_SRC emacs-lisp

  (+ 1 1)

#+END_SRC

7. Place the text cursor in the code block (between the line #+BEGIN_SRC and the line #+END_SRC).

8. Hit the Emacs command

C-c C-c

9. You will get the evaluation result:

#+RESULTS:
: 2

.

10. Hit the Emacs command:

M-x cider-jack-in

11. Within the file “my-stuff.org“, add the code:

#+BEGIN_SRC clojure :results value		  

  (require '[sicmutils.env :as env])

#+END_SRC

12. Place the text cursor in the code block.

13. Hit the Emacs command

C-c C-c

.

14. Add the code:

#+BEGIN_SRC clojure :results value		  

  (env/bootstrap-repl!) 

#+END_SRC

15. Place the text cursor in the code block and then hit the Emacs command

C-c C-c

.

16. Add the code:


#+BEGIN_SRC clojure :results replace drawer
 
(->TeX (simplify ((D cube) 'x)))
 
#+END_SRC

17. Place the text cursor and then hit

C-c C-c

It will give you the \LaTeX code

#+RESULTS:
:RESULTS:
"3\\,{x}^{2}"
:END:

— Me@2022-07-27 11:34:28 PM

.

.

2022.07.28 Thursday (c) All rights reserved by ACHK

SICMUtils

A Clojure(script) implementation of the scmutils system for math and physics investigations in the Clojure and Clojurescript languages.

.

1. To install Clojure in Ubuntu, just this command is enough:

sudo apt-get install elpa-cider

Although the Clojure version you get is probably not the most updated one, that is not important, because you can specify which version you want in the config file of each project.

.

2. Then use this command to generate a new project named my-stuff:

lein new app my-stuff

.

3. Use Emacs to open the file:

~/my-stuff/project.clj

.

4. Replace the existing :dependencies line with this one

  :dependencies [[org.clojure/clojure "1.11.1"]
                 [sicmutils "0.22.0"]]

And make sure that both clojure and sicmutils have the most updated version numbers.

.

5. In Emacs, type the command

M-x cider-jack-in

.

6. In the clojure window (cider-repl), type

(clojure-version)

with enter at the end.

.

7. Type

(require '[sicmutils.env :as env])

.

8. Type

(env/bootstrap-repl!) 

.

9. Code

((D cube) 'x)

will result

(+ (* x x) (* x (+ x x)))

.

10. Type the Emacs command

M-p

to access the last input. Then modify it into

(simplify ((D cube) 'x))

.

It will result

(* 3 (expt x 2))

.

11. Code

(->TeX (simplify ((D cube) 'x)))

will give the LaTeX code

3\\,{x}^{2}

.

12. You can exit by the Emacs command

<C-c C-q> 

.

For the time being, SICMUtils is not suitable for the book SICM (Structure and Interpretation of Classical Mechanics). In other words, SICMUtils cannot replace the scmutils library yet, because:

a. You would have to do the translation manually, from the scmutils code in the book to SICMUtils.

b. Although it can generate LaTeX source code, it does NOT do the LaTeX rendering.

c. It cannot plot graphs.

However, SICMUtils has one advantage over scmutils. It can generate LaTeX source of an expression, but scmutils cannot. So I am planning to use both scmutils and SICMUtils.

Also, I will learn how to use SICMUtils with other Clojure libraries and the Jupyter Notebook. That would get LaTeX rendering and graph plotting running.

— Me@2022-07-26 11:03:51 AM

.

.

2022.07.26 Tuesday (c) All rights reserved by ACHK

Common Lisp Reloaded

; sudo apt-get install sbcl

; sudo apt-get install slime

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setq inferior-lisp-program "sbcl")

(defun load-slime-with-correct-buffer-position ()

  (save-excursion (slime))
     
 
  (delete-other-windows) 
) 

(defun prelude-start-slime ()
  (unless (slime-connected-p)
    (load-slime-with-correct-buffer-position)))

(add-hook 'slime-mode-hook 'prelude-start-slime)

(set-register ?f '(file . "/path_to/lisp_file.lisp"))

— Me@2022-07-23 05:20:32 PM

.

.

2022.07.23 Saturday (c) All rights reserved by ACHK

Cyclic coordinate

A generalized coordinate component that does not appear explicitly in the Lagrangian is called a cyclic coordinate. The generalized momentum component conjugate to any cyclic coordinate is a constant of the motion.

— 1.8 Conserved Quantities

— Structure and Interpretation of Classical Mechanics

.

This is a special case of Noether’s theorem. Such coordinates are called “cyclic” or “ignorable”.

— Wikipedia on Lagrangian mechanics

.

If only the cyclic coordinate \displaystyle{q(t)} varies with time (if it doesn’t, \displaystyle{q} is superfluous), the Lagrangian, or the essential physical situation, doesn’t vary. Hence the initial value of \displaystyle{q} doesn’t determine the path, which is only possible if the path is closed.

— edited Jul 28, 2014 at 15:55

— ACuriousMind

— answered Jul 28, 2014 at 15:50

— Pieter Kockx

— Why are they called “cyclic” coordinates?

— Physics Stack Exchange

.

.

2022.07.22 Friday ACHK

Functional programming jargon in plain English

mjburgess 11 days ago | next [–]

These definitions don’t really give you the idea, rather often just code examples..
“The ideas”, in my view:

Monoid = units that can be joined together
Functor = context for running a single-input function
Applicative = context for multi-input functions
Monad = context for sequence-dependent operations
Lifting = converting from one context to another
Sum type = something is either A or B or C…
Product type = a record
= something is both A and B and C
Partial application = defaulting an argument to a function
Currying = passing some arguments later
= rephrasing a function to return a functions of n-1 arguments when given 1, st. the final function will compute the desired result
EDIT: Context = compiler information that changes how the program will be interpreted (, executed, compiled,…)
Eg., context = run in the future, run across a list, redirect the i/o, …

— Functional programming jargon in plain English

— Hacker News

.

Currying and partial function application are often conflated. One of the significant differences between the two is that a call to a partially applied function returns the result right away, not another function down the currying chain; this distinction can be illustrated clearly for functions whose arity is greater than two.

.

Partial application can be seen as evaluating a curried function at a fixed point, e.g. given \displaystyle{f\colon (X\times Y\times Z)\to N} and \displaystyle{a\in X} then

\displaystyle{{\text{curry}}({\text{partial}}(f)_{a})(y)(z)={\text{curry}}(f)(a)(y)(z)}

or simply

\displaystyle{{\text{partial}}(f)_{a}={\text{curry}}_{1}(f)(a)}

where \displaystyle{{\text{curry}}_{1}} curries \displaystyle{f}‘s first parameter.

— Wikipedia on Currying

.

.

2022.07.16 Saturday ACHK

Matrix calculus

1.7 Evolution of Dynamical State, 2.3

Structure and Interpretation of Classical Mechanics

.

\displaystyle{ \begin{aligned}     \partial_1 L \circ \Gamma[q]     &= D ( \partial_2 L \circ \Gamma[q]) \\ \\    &= \partial_0 ( \partial_2 L \circ \Gamma[q]) Dt +  \partial_1 ( \partial_2 L \circ \Gamma[q]) Dq + \partial_2 ( \partial_2 L \circ \Gamma[q]) Dv \\ \\     &= \partial_0 \partial_2 L \circ \Gamma[q] +  ( \partial_1 \partial_2 L \circ \Gamma[q]) Dq + (\partial_2 \partial_2 L \circ \Gamma[q]) D^2 q \\ \\     \end{aligned}}

.

\displaystyle{ \begin{aligned}      (\partial_2 \partial_2 L \circ \Gamma[q]) D^2 q     &=     \partial_1 L \circ \Gamma[q]     - \partial_0 \partial_2 L \circ \Gamma[q]     - (\partial_1 \partial_2 L \circ \Gamma[q]) Dq  \\ \\         D^2 q     &=     \left[ \partial_2 \partial_2 L \circ \Gamma[q] \right]^{-1}    \left\{ \partial_1 L \circ \Gamma[q]     - \partial_0 \partial_2 L \circ \Gamma[q]     - (\partial_1 \partial_2 L \circ \Gamma[q]) Dq  \right\} \\ \\      \end{aligned}}

where \displaystyle{\left[ \partial_2 \partial_2 L \circ \Gamma \right]} is a structure that can be represented by a symmetric square matrix, so we can compute its inverse.

~~~

[guess]

\displaystyle{   \begin{aligned} D \left( \frac{\partial}{\partial \dot q_1} L \circ \Gamma[\begin{bmatrix} q_1 \\ q_2 \\ \vdots \end{bmatrix}] \right) - \left(\frac{\partial}{\partial q_1} L \circ \Gamma[\begin{bmatrix} q_1 \\ q_2 \\ \vdots \end{bmatrix}]\right)   &= 0 \\     D \left( \frac{\partial}{\partial \dot q_2} L \circ \Gamma[\begin{bmatrix} q_1 \\ q_2 \\ \vdots \end{bmatrix}] \right) - \left(\frac{\partial}{\partial q_2} L \circ \Gamma[\begin{bmatrix} q_1 \\ q_2 \\ \vdots \end{bmatrix}]\right) &= 0 \\     &\vdots \\   \end{aligned}}

.

\displaystyle{   \begin{aligned} D \left( \frac{\partial}{\partial \dot q_1} L \circ \Gamma[\vec q] \right)   - \left(\frac{\partial}{\partial q_1} L \circ \Gamma[\vec q]\right)   &= 0 \\     D \left( \frac{\partial}{\partial \dot q_2} L \circ \Gamma[\vec q] \right) - \left(\frac{\partial}{\partial q_2} L \circ \Gamma[\vec q]\right) &= 0 \\     &\vdots \\   \end{aligned}}

.

\displaystyle{   \begin{aligned} D \left( \vec \partial_2 L \circ \Gamma[\vec q] \right)   - \left(\vec \partial_1 L \circ \Gamma[\vec q]\right)   &= 0 \\   \end{aligned}}

\displaystyle{ \begin{aligned}  \partial_{\vec 1}   = \frac{\partial}{\partial \vec q}   &= \begin{bmatrix} \frac{\partial}{\partial q_1} & \frac{\partial}{\partial q_2} & ... \end{bmatrix} \\    \\  \partial_{\vec 2}   = \frac{\partial}{\partial \vec{\dot q}}   &= \begin{bmatrix} \frac{\partial}{\partial \dot q_1} & \frac{\partial}{\partial \dot q_2} & ... \end{bmatrix} \\    \end{aligned}}

\displaystyle{ \begin{aligned}  \vec \partial_{1}   = \left( \frac{\partial}{\partial \vec q} \right)^T  &= \begin{bmatrix} \frac{\partial}{\partial q_1} \\ \frac{\partial}{\partial q_2} \\ \vdots \end{bmatrix} \\    \\  \vec \partial_{2}   = \left( \frac{\partial}{\partial \vec{\dot q}} \right)^T  &= \begin{bmatrix} \frac{\partial}{\partial \dot q_1} \\ \frac{\partial}{\partial \dot q_2} \\ \vdots \end{bmatrix} \\    \end{aligned}}

.

\displaystyle{   \begin{aligned}   \vec \partial_1 L \circ \Gamma[\vec q]   &= D \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) \\ \\    &= \partial_0 \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) Dt    \\    &+ \partial_{q_1} \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) Dq_1    + \partial_{\dot q_1} \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) D \dot q_1 \\    &+ \partial_{q_2} \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) Dq_2    + \partial_{\dot q_2} \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) D \dot q_2 \\    &+ ... \\ \\      &= \partial_0 \left( \vec \partial_2 L \circ \Gamma[\vec q] \right)     \\    &+ \begin{bmatrix} \partial_{q_1} & \partial_{q_2} & ... \end{bmatrix}    \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) D \begin{bmatrix} q_1  \\ q_2 \\ \vdots \end{bmatrix} \\    &+ \begin{bmatrix} \partial_{\dot q_1} & \partial_{\dot q_2} & ... \end{bmatrix}    \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) D^2 \begin{bmatrix} q_1  \\ q_2 \\ \vdots \end{bmatrix} \\    \end{aligned}}

.

\displaystyle{\begin{aligned}    \vec \partial_1 L \circ \Gamma[\vec q]      &= \partial_0 \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) Dt    + \frac{\partial}{\partial \vec q}     \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) D \vec q     + \frac{\partial}{\partial \vec {\dot q}}     \left( \vec \partial_2 L \circ \Gamma[\vec q] \right) D^2   \vec q \\    \end{aligned}}

[guess]

— Me@2022-07-09 09:09:28 PM

.

.

2022.07.13 Wednesday (c) All rights reserved by ACHK