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

11X

~~~

... 中化口試
巴士:11X

~~~

10k trimmer pot: 7.31k

~~~

3150, 4260: Samuel

3170: Danny, 達
3260: Dennis

~~~

.

.

2022.08.29 Monday (c) All rights reserved by ACHK

Presentation 基本原理 1.2.2.3

反不相關推薦 3.3

這段改編自 2010 年 4 月 24 日的對話。

.

每一節課應該,只有一個重點。在該課中,你要用不同的句子、字眼、例子,來重複釐清和闡述,那個重點。講課時,一切的細節,都要圍繞著,那個重點來運行。

.

(安:但是,有時正正就是需要,解釋兩個要點的關係。那又如何做到,一節課只講一要點呢?)

.

「一課一要點」只是大概而言的企圖,毋須百分百執行。

另外,(甲、乙)兩個要點的關係,本身就已成一個要點(丙)。

.

(安:你的意思是,第一、二、三節課,分別講甲、乙、丙?)

.

漫畫化來說,正確。但是,你千萬不要,鑽牛角尖地問:「何謂『一個』要點?多大的要點,才為之『一個』呢?」

那是相對於,當刻的聽眾而言。一位聽者的「一個」要點,對於另外一位來說,可以是「兩個」。所以,你有責任,為當刻的聽眾,提供適合他們的「一個」要點。

.

另外,在那堆要點之中,你不單是選「一個」,而且還要選,對他們而言,最有用的那一個。那往往是在,他們知識邊緣的東西,即是介乎知與不知之間。

換句話說,究竟是眾多要點中的哪一個呢?

就是聽眾當刻,剛剛有能力理解的數個要點之中,最重要、最根本的那一個。

— Me@2022-08-28 07:56:28 PM

.

.

2022.08.29 Monday (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

3.1 Lorentz covariance for motion in electromagnetic fields, 2

.

Is \displaystyle{\frac{d p_\mu}{ds} = \frac{q}{c} F_{\mu \nu} \frac{d x^\nu}{ds}} gauge invariant?

.

~~~

… the defining property of a Lorentz transformation, \Lambda^\mu_{\;\;\nu}:

\eta_{\mu\nu} \Lambda^{\mu}_{\;\;\alpha}  \Lambda^\nu_{\;\;\beta} = \eta_{\alpha\beta}

.

… 4-vectors and (Lorentz)-tensors are transformed like this:

U'^\mu = \Lambda^\mu_{\;\;\nu}U^\nu

and

F'_{\mu\nu} = \Lambda_\mu^{\;\;\alpha} \Lambda_\nu^{\;\;\beta}F_{\alpha\beta}= \Lambda_\mu^{\;\;\alpha} F_{\alpha\beta} (\Lambda^{-1})^{\beta}_{\;\;\nu}

where we have used the conventional notation

\displaystyle{\Lambda_\nu^{\;\;\mu} = (\Lambda^{-1})^\mu_{\;\;\nu}}

.

Let us then take your equation and apply \displaystyle{\Lambda_\sigma^{\;\;\mu}} on both sides (recall this Lorentz transformation does not depend on \displaystyle{\tau}), and try rewriting everything in terms of prime quantities:

\displaystyle{  \begin{aligned}  m \Lambda_\sigma^{\;\;\mu}\frac{{\rm d}U_\mu}{{\rm d}\tau}     &= e \Lambda_\sigma^{\;\;\mu} F_{\mu\nu} U^\nu \\     &= e \Lambda_\sigma^{\;\;\mu} F_{\mu\nu}\eta^{\nu\alpha} U_\alpha\\    m\frac{{\rm d}U'_\sigma}{{\rm d}\tau} &= e \Lambda_\sigma^{\;\;\mu} F_{\mu\alpha}\eta^{\nu\alpha} ((\Lambda^{-1})^{\beta}_{\;\;\nu} \Lambda^\alpha_{\;\;\beta}) U_\alpha\\    &= e \Big(\Lambda_\sigma^{\;\;\mu} F_{\mu\alpha}\eta^{\nu\alpha}(\Lambda^{-1})^{\beta}_{\;\;\nu}\Big) \Big(\Lambda^\alpha_{\;\;\beta} U_\alpha\Big)\\    &= e \Big(\Lambda_\sigma^{\;\;\mu} F_{\mu\alpha}\eta^{\nu\alpha}\Lambda^{\;\;\beta}_\nu\Big) U'_\beta\\    &= e \Big(\Lambda_\sigma^{\;\;\mu} F_{\mu\alpha}\eta^{\nu\beta}\Lambda^{\;\;\alpha}_\nu\Big) U'_\beta\\    &= e F'_{\sigma\nu}\eta^{\nu\beta} U'_\beta \\    \\    m\frac{{\rm d}U'_\sigma}{{\rm d}\tau} &= e F'_{\sigma\nu} U'^\nu \\  \end{aligned}}

This “game” can always be done with contracted indices, …

— answered Jul 7, 2020 at 15:06

— ohneVal

— Lorentz invariance of the Lorentz force law

— Physics StackExchange

.

How come \displaystyle{ \begin{aligned}     \eta^{\nu\alpha}\Lambda^{\;\;\beta}_\nu     &= \eta^{\nu\beta}\Lambda^{\;\;\alpha}_\nu \\     \end{aligned}}?

.

Note that

\displaystyle{  \begin{aligned}    \sum ... \sum \Lambda_\sigma^{\;\;\mu} F_{\mu\alpha}\eta^{\nu\alpha}\Lambda^{\;\;\beta}_\nu    &= \sum ... \sum \Lambda_\sigma^{\;\;\mu} F_{\mu\alpha}\eta^{\nu\beta}\Lambda^{\;\;\alpha}_\nu  \\    \end{aligned}}

does not mean that

\displaystyle{ \begin{aligned}     \eta^{\nu\alpha}\Lambda^{\;\;\beta}_\nu     &= \eta^{\nu\beta}\Lambda^{\;\;\alpha}_\nu \\     \end{aligned}}

.

Also

\displaystyle{ \begin{aligned}     \sum \eta^{\nu\alpha}\Lambda^{\;\;\beta}_\nu     &= \sum \eta^{\nu\beta}\Lambda^{\;\;\alpha}_\nu \\     \end{aligned}}

does not mean that

\displaystyle{ \begin{aligned}     \eta^{\nu\alpha}\Lambda^{\;\;\beta}_\nu     &= \eta^{\nu\beta}\Lambda^{\;\;\alpha}_\nu \\     \end{aligned}}

.

\displaystyle{  \begin{aligned}    A  = \sum_{\alpha, \beta, \nu} F_{\mu\alpha}\eta^{\nu\alpha}\Lambda^{\;\;\beta}_\nu U_\beta'    &=     \sum_{\alpha, \beta} F_{\mu\alpha}\eta^{\alpha\alpha}\Lambda^{\;\;\beta}_{\nu=\alpha} U_\beta' \\    &=     \sum_{\beta}     \left(     - F_{\mu 0} \Lambda^{\;\;\beta}_{0}    + F_{\mu 1} \Lambda^{\;\;\beta}_{1}    + F_{\mu 2} \Lambda^{\;\;\beta}_{2}    + F_{\mu 3} \Lambda^{\;\;\beta}_{3}      \right)     U_\beta' \\        \\    \end{aligned}}

\displaystyle{ \begin{aligned}     B = \sum_{\alpha, \beta, \nu} F_{\mu\alpha}\eta^{\nu\beta}\Lambda^{\;\;\alpha}_\nu U_\beta'    &=     \sum_{\alpha, \beta}     F_{\mu\alpha}     \eta^{\beta \beta}\Lambda^{\;\;\alpha}_{\nu=\beta} U_\beta      \\     &=     \sum_{\alpha}     F_{\mu\alpha}     \left(    - \Lambda^{\;\;\alpha}_{0} U_0'    + \Lambda^{\;\;\alpha}_{1} U_1'     + \Lambda^{\;\;\alpha}_{2} U_2'     + \Lambda^{\;\;\alpha}_{3} U_3'     \right)     \\       \end{aligned}}

At the first glance, it seems to be unlikely that

\displaystyle{  \begin{aligned}    \sum_{\alpha, \beta, \nu} F_{\mu\alpha}\eta^{\nu\alpha}\Lambda^{\;\;\beta}_\nu U_\beta'    &= \sum_{\alpha, \beta, \nu}  F_{\mu\alpha}\eta^{\nu\beta}\Lambda^{\;\;\alpha}_\nu U_\beta'    \\    \end{aligned}},

because while in A, for any \beta, U_\beta'‘s have visible negative terms; in B, only U_0'‘s do.

Without additional mathematical properties among those physical quantities, the identity is impossible to prove.

.

Just for future reference:

From the invariance of the spacetime interval it follows

\displaystyle{\eta =\Lambda ^{\mathrm {T} }\eta \Lambda}

— Wikipedia on Lorentz transformation

.

\displaystyle{  \begin{aligned}    \eta_{\mu \nu} = \eta'_{\mu \nu} &= \Lambda^{\alpha}{}_{\mu} \Lambda^{\beta}{}_{\nu} \eta_{\alpha \beta} \\     &= (\Lambda^T)_{\mu}{}^{\alpha} \eta_{\alpha \beta} \Lambda^{\beta}{}_{\nu} \\ \\     \eta_{\alpha\beta} &= \Lambda^{\mu}_{\;\;\alpha} \Lambda^{\nu}_{\;\;\beta}\eta_{\mu\nu} \\    \eta^{\alpha\beta} &= \Lambda^{\alpha}_{\;\;\mu} \Lambda^{\beta}_{\;\;\nu}\eta^{\mu\nu} \\    \end{aligned}}

.

\displaystyle{  \begin{aligned}    m \Lambda_\sigma^{\;\;\mu}\frac{{\rm d}U_\mu}{{\rm d}\tau} &= e \Lambda_\sigma^{\;\;\mu} F_{\mu\nu} U^\nu \\     & = e \Lambda_\nu^{\;\;\mu} F_{\mu\nu}\eta^{\nu\alpha} U_\alpha\\    \end{aligned}}

Inserting the identity \sum_\beta (\Lambda^{-1})^{\beta}_{\;\;\nu} \Lambda^\alpha_{\;\;\beta} into the expression:

\displaystyle{  \begin{aligned}    m\frac{{\rm d}U'_\sigma}{{\rm d}\tau} &= e \Big(\Lambda_\sigma^{\;\;\mu} F_{\mu\alpha}\eta^{\nu\alpha}(\Lambda^{-1})^{\beta}_{\;\;\nu}\Big) \Big(\Lambda^\alpha_{\;\;\beta} U_\alpha\Big) \\      &= e \Big(\Lambda_\sigma^{\;\;\mu} F_{\mu\alpha}\eta^{\nu\alpha}\Lambda^{\;\;\beta}_\nu\Big) U'_\beta\\    \end{aligned}}

This path does not work. Also, the formula \displaystyle{ \Lambda^\alpha_{\;\;\beta} U_\alpha = U'_\beta } is plain wrong!

.

Inserting the identity \sum_\beta (\Lambda^{-1})^{\beta}_{\;\;\nu} \Lambda^\alpha_{\;\;\beta} into the expression before (actually without) lowering the index:

\displaystyle{  \begin{aligned}    m \Lambda_\sigma^{\;\;\mu}\frac{{\rm d}U_\mu}{{\rm d}\tau}     &= e \Lambda_\sigma^{\;\;\mu} F_{\mu\nu} U^\nu \\    &= e \Lambda_\sigma^{\;\;\mu} F_{\mu\alpha} ((\Lambda^{-1})^{\alpha}_{\;\;\beta} \Lambda^\beta_{\;\;\nu})  U^\nu \\    m \frac{{\rm d}U_\sigma'}{{\rm d}\tau}      &= e (\Lambda_\sigma^{\;\;\mu} F_{\mu\alpha} (\Lambda^{-1})^{\alpha}_{\;\;\beta}) (\Lambda^\beta_{\;\;\nu}  U^\nu) \\    &= e {F'}_{\sigma \beta} {U'}^\beta \\    \end{aligned}}

— Me@2022-08-23 12:03:54 PM

.

.

2022.08.26 Friday (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

Daily life is boring

地獄篇 3.2 | 開山祖師牛 6.4

.

YOU are the missing MAGIC in your daily life.

— Me@2016-08-24 12:32 AM

.

.

2022.08.22 Monday (c) All rights reserved by ACHK

反情感勒索

這段改編自 2021 年 12 月 15 日的對話。

.

在那愚善的年代,我受到誤導,以為「任何情況下,不可令人不開心」。

那是十分荒謬的。

.

合理的說法應該是:「不要在沒有必要的情況下,令到他人或自己不開心」。

亦即是話,不要主動,不要刻意,令人不開心。

如果他人的不開心,不是我引起的話,那樣,我就沒有責任要,做些什麼事情,令到他開心。

.

要做到,不作攻擊,但堅決防守。

換句話說,眾害取其輕。

.

例如,有推銷員推介產品甲。如果甲對我有用的話,我就買,沒有用就不買。我並不應該考慮:「如果我不買的話,他會不開心。」

又例如,我對女士 A 示好。她對我都有意思的話,就回應;沒有意思的話,就拒絕。她並不應該考慮:「如果我不回應的話,他會不開心。」

她沒有責任,亦沒有方法,可以令我開心。

— Me@2022-08-20 11:27:16 PM

.

.

2022.08.21 Sunday (c) All rights reserved by 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

Presentation 基本原理 1.2.2.2

反不相關推薦 3.2

這段改編自 2010 年 4 月 24 日的對話。

.

每一節課應該,只有一個重點。在該課中,你要用不同的句子、字眼、例子,來重複釐清和闡述,那個重點。講課時,一切的細節,都要圍繞著,那個重點來運行。

.

(安:但是,有時正正就是需要,解釋兩個要點的關係。那又如何做到,一節課只講一要點呢?)

.

「一課一要點」只是大概而言的企圖,毋須百分百執行。

另外,(甲、乙)兩個要點的關係,本身就已成一個要點(丙)。第一、二、三節課,分別講甲、乙、丙。

— Me@2022-08-15 11:16:21 AM

.

.

2022.08.15 Monday (c) All rights reserved by 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

3.1 Lorentz covariance for motion in electromagnetic fields, 1

A First Course in String Theory

.

The Lorentz force equation (3.5) can be written relativistically as

\displaystyle{\frac{d p_\mu}{ds} = \frac{q}{c} F_{\mu \nu} \frac{d x^\nu}{ds}},

where \displaystyle{p_{\mu}} is the four-momentum.

(a) Check explicitly that this equation reproduces (3.5) when \displaystyle{\mu} is a spatial index.

(b) What does (1) gives when \displaystyle{\mu = 0}? Does it make sense?

~~~

Eq. (3.5):

\displaystyle{\frac{d \vec p}{dt} = q \left( \vec E + \frac{\vec v}{c} \times \vec B \right)}

Eq. (2.20):

\displaystyle{ds \equiv \sqrt{ds^2}}    if    \displaystyle{ds^2 > 0}

Eq. (2.21):

\displaystyle{-ds^2 = \eta_{\mu \nu} dx^\mu dx^\nu}

The spacetime interval \displaystyle{ds^2} is Lorentz invariant. If \displaystyle{ds^2 > 0}, we have Eq. (2.27) and (2.28):

\displaystyle{\begin{aligned}     ds &= c dt_p \\     ds &= c dt \sqrt{1 - \beta^2}     \end{aligned}}

.

\displaystyle{\frac{d p_\mu}{ds} \left( \frac{ds}{dt} \right) = \frac{q}{c} F_{\mu \nu} \frac{d x^\nu}{ds}} \left( \frac{ds}{dt} \right)

\displaystyle{    \begin{aligned}     \frac{d p_1}{dt}     &= \frac{q}{c} F_{1 \nu} \frac{d x^\nu}{dt} \\    &= \frac{q}{c} \left( F_{1 0} \frac{d x^0}{dt} + F_{1 1} \frac{d x^1}{dt} + F_{1 2} \frac{d x^2}{dt} + F_{1 3} \frac{d x^3}{dt} \right) \\    \frac{d p_x}{dt} &= \frac{q}{c} \left( E_x c \frac{d t}{dt} + (0) \frac{d x^1}{dt} + B_z \frac{d x^2}{dt} - B_y \frac{d x^3}{dt} \right) \\    &= q \left( E_x + \frac{1}{c} \left( \vec v \times \vec B \right)_x \right) \\    \end{aligned}}

\displaystyle{    \begin{aligned}     \frac{d p_0}{dt}     &= \frac{q}{c} \left( F_{0 0} \frac{d x^0}{dt} + F_{0 1} \frac{d x^1}{dt} + F_{0 2} \frac{d x^2}{dt} + F_{0 3} \frac{d x^3}{dt} \right) \\    &= \frac{q}{c} \left( 0 \frac{d x^0}{dt} - E_x \frac{d x^1}{dt} - E_y \frac{d x^2}{dt} - E_z \frac{d x^3}{dt} \right) \\    \frac{d }{dt} \left( \frac{-E}{c} \right)&= - \frac{q}{c} \left( \vec v \cdot \vec   E \right) \\    \frac{d E}{dt}  &= \vec v \cdot \vec F_E \\      \end{aligned}}

— Me@2022-08-04 04:17:59 PM

.

.

2022.08.06 Saturday (c) All rights reserved by ACHK

Bravado

Anyone who expects to create, be it as scientist or artist, scholar or writer, needs self-confidence, even bravado. How else can one dare to imagine understanding what no one else has understood, discovering what no one else has discovered? Where does this confidence come from? Fortunately, every young person is blessed with some of it. It is part of human character. What of the girl or boy who reads about Newton and Maxwell and Bohr and Einstein and says, “I want to build on what they have built; I want to add to the sum of knowledge about the most basic laws of nature”?

– Geons, Black Holes & Quantum Foam, p. 84

– 2000
.

.

2022.08.03 Wednesday (c) All rights reserved by ACHK

CSCI3420


~~~

砌摩打

~~~

Design:
1: make car moving
2: make car stop itself
3: follow the track
4: change direction
5: tuning

~~~

CSCI3420 Project Phase 2

Deadline: 23:59

~~~

.

.

2022.08.03 Wednesday (c) All rights reserved by ACHK

排列組合 1.1

nCr, 0

這段改編自 2010 年 7 月 27 日的對話。

.

\displaystyle{n!}

.

1.1  意思:

\displaystyle{n} 個人 \displaystyle{n} 個座位的話,有多少種坐法?

1.2.1  算式:

\displaystyle{n! = (n)(n-1)(n-2) \cdots (3)(2)(1)}

1.2.2  由來:

第一個位,有 \displaystyle{n} 個可能的人選;第一個位被坐後,第二個位只有 \displaystyle{(n-1)} 個,可能的人選;如此類推,直到最後一個位,被餘下的唯一個人,選了為此。

.

1.3 

留意,\displaystyle{n} 是多少,就有多少項。

\displaystyle{n! = (n)(n-1)(n-2) \cdots (3)(2)(1)}

例如,\displaystyle{5!},就有 5 項相乘;

\displaystyle{\begin{aligned}     5! &= (5)(4)(3)(2)(1) \\ \\     \end{aligned}}

\displaystyle{3!},就有 3 項;等等。

\displaystyle{\begin{aligned}     3! &= (3)(2)(1) \\    \end{aligned}}

.

1.4.0  零的階乘,\displaystyle{0!},還未有定義,因為,算式 \displaystyle{n! = (n)(n-1) \cdots (2)(1)} 中的 \displaystyle{n},只可以是正整體,不可以零。

階乘零,\displaystyle{0!},需要額外定義,因為會常用到。那樣,\displaystyle{0!} 應該定義為,什麼數值呢?

1.4.1  既然 \displaystyle{n!}意思是「\displaystyle{n} 個人 \displaystyle{n} 個座位,有多少種坐法」,那樣,你就可以視,\displaystyle{0!} 的意思是「\displaystyle{0} 個人 \displaystyle{0} 個座位,有多少種坐法」;那明顯是一,因為,那個情況之下,只有一個「坐法」,就是「沒有人又沒有位」這個唯一的可能性。

\displaystyle{0! = 1}

.

1.4.2  另外一種設計定義的想法是,由 \displaystyle{n!}算式取靈感。

既然 \displaystyle{n} 是多少,\displaystyle{n!} 就有多少項相乘,那樣,零的階乘,\displaystyle{0!},理應只有零項相乘,\displaystyle{~a^0~}

\displaystyle{0! = a^0}

但是,「零項相乘」的數值,又應該是什麼呢?

「零項相乘」即是「乘了等如沒有乘」,所以是一,因為任何數乘了一,效果都等於沒有乘。

\displaystyle{a^0 = 1}

(「零項相乘」的正式學名是,「空積」或「零項積」。)

\displaystyle{0! = 1}

— Me@2022-08-02 02:41:43 PM

.

.

2022.08.02 Tuesday (c) All rights reserved by ACHK