Computing Note, 3


Summer 2002

1. Perhaps the Encyclopedia Britannica 
                    Research Assistant 
                        is written by[in] Java.

2. Work Hard Play Hard 
           Live your life with[in] all [its] fullness.

3. H(Learning): HyperLearning 真切學習:
[]
Be a professional professional: 比專業還專業

.

To edit Lazarus code in Emacs:

1. Open Emacs’ initialization file, whose location should be

~/.emacs

2. Add the follwing code:

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

(defmacro compile-with (command-string)

  `(progn

    (save-buffer)

    (unless visual-line-mode
      (visual-line-mode 1))

    (universal-argument)

    (compile ,command-string)))

(defmacro defcomp (fn command-string kbd-string)

  `(progn

     (defun ,fn ()

       (interactive)

       (compile-with ,command-string))

     (global-set-key (kbd ,kbd-string) ',fn)))

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

(defcomp g-la-com

    (concat
     "lazbuild "
     (replace-regexp-in-string
      "pas" "lpi" buffer-file-name))

    "C-x C-p")

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

(defcomp g-la-run

    (replace-regexp-in-string
     ".pas" "" buffer-file-name)

    "C-x C-[")

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

3. Close Emacs.

4. Use the Lazarus IDE to create a console project named p11.

5. Close Lazarus.

6. Use Emacs to open the file p11.pas.

— Me@2022-12-07 08:35:48 PM

.

.

2022.12.08 Thursday (c) All rights reserved by ACHK

Pier, 2.2

Euler problem 9.1

.

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

(defun e9c ()
  (loop :for a :from 1 :to 1000 :do
    (loop :for b :from 1 :to a :do
      (let ((c (- 1000 (+ a b))))
        (if (= (+ (* a a) (* b b)) (* c c))
            (return-from e9c
                (list a b c (* a b c))))))))

— colorized by palette fm

— Me@2022-12-05 05:59:49 PM

.

.

2022.12.05 Monday (c) All rights reserved by ACHK

Fortran Package Manager

Haskell mode, 3

.

The goal of this blog post is to set up an integrated programming environment for Fortran.

.

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

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

3. Install the package manager Anaconda.

4. Use Anaconda to install the Fortran Package Manager (fpm), by following the fpm installation guide.

4.1. Add the additional channel mentioned in the fpm installation guide.

4.2. Install the fpm itself:

conda create -n fpm_env fpm

conda activate fpm_env

5. Install the Fortran language server:

conda install fortls

6. Install the Emacs plugin:

sudo apt-get install elpa-pyvenv

7. Open Emacs’ initialization file, whose location should be

~/.emacs

8. Add the following code to the file.


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

(defun gfortran-run ()

  (interactive)

  (save-buffer)

  (unless visual-line-mode
    (visual-line-mode 1))

  (universal-argument)

  (compile "fpm run"))

(global-set-key (kbd "C-x C-r") 'gfortran-run)

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

(setenv "WORKON_HOME" "~/anaconda3/envs")

(pyvenv-mode 1)

(pyvenv-workon "fpm_env")

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

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

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

— Me@2022-12-01 09:18:59 AM

.

.

2022.12.04 Sunday (c) All rights reserved by ACHK

Why does the universe exist? 7.3

Euler problem 10.4

.

program main

  implicit none

  integer :: p_max, p_count, i, j
  integer(kind = 16) :: p_sum
  logical, ALLOCATABLE, DIMENSION(:) :: is_prime

  p_max = 2000000

  ALLOCATE(is_prime(p_max))

  is_prime = .true.

  is_prime(1) = .false.

  do i = 2, ceiling(sqrt(real(p_max)))
     if (is_prime(i)) then
        do j = i*i, p_max, i
           is_prime(j) = .false.
        end do
     end if
  end do

  p_count = 0
  p_sum = 0
  do i = 1, p_max
     if (is_prime(i)) then
        p_count = p_count + 1
        p_sum = p_sum + i
     end if
  end do

  print *, "p_count == ", p_count
  print *, "p_sum == ", p_sum

  DEALLOCATE(is_prime)

end program main

For a universe part, which is partial in space or in time, you can ask for its cause.

But for the universe as a whole, you cannot.

If the big bang is the first cause, you cannot ask for the cause of its existence.

Asking for the cause of the existence of the universe is the same as asking for the cause of the first cause.

— Me@2012-10-15 08:33:01 AM

— Me@2022-11-27 09:09:53 PM

.

.

2022.12.04 Sunday (c) All rights reserved by ACHK

Hacking Reality

Good marriage is better than no marriage.

No marriage is better than bad marriage.

— John T Reed

.

Your wife should be the exception of the (brutal) reality of this world.

If someone is just another reality, better not to marry her.

— Me@2022-11-10 01:21:15 AM

.

.

2022.12.03 Saturday (c) All rights reserved by ACHK

大恩養仇人

小恩養貴人

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

.

no good deed goes unpunished

Beneficial actions often go unappreciated or are met with outright hostility.

If they are appreciated, they often lead to additional requests.

— Wiktionary

.

記住,起點是,你有責任,去解決你自己的問題,但是,你並沒有責任,去解決其他人的問題。有時,間中幫助人,並不是因為責任,而是基於愛心或情義。而「有時候,在你沒有責任幫時,仍然選擇幫」,正正是人性光輝之處。

精確一點用字的話:必須做的工作,為之「責任」;可做可不做的,為之「愛心」或「情義」。

.

既然可幫可不幫,那樣,什麼時候應該幫呢?

大原則是:

1. 那是你自己的能力範圍以內。

2. 當事人有明確要求,或者情境預設,例如,當事人已昏迷,需立刻送院。

一個頭腦清醒的大人,如果沒有明確要求,你就沒有權力去幫助。越權就是無禮。

發乎情 止乎禮

禮者,人與人間之距離也。

.

那樣,在「有必要、應該幫」時,又應該幫到哪個程度呢?

「幫助別人」就有如「責備別人」,雖然有時必須,但是越小越好,越少越好。

「幫助別人」就有如「服用藥物」,雖然有時必須,但是越小越好,越少越好。

.

助人最小化,效果最大化。

— Me@2022-12-02 04:34:36 PM

.

.

2022.12.02 Friday (c) All rights reserved by ACHK

ParEdit

.

(autoload 'enable-paredit-mode
  "paredit" "Turn on pseudo-structural editing." t)

(add-hook 'emacs-lisp-mode-hook
      #'enable-paredit-mode)

(add-hook 'eval-expression-minibuffer-setup-hook   
      #'enable-paredit-mode)

(add-hook 'ielm-mode-hook
      #'enable-paredit-mode)

(add-hook 'lisp-mode-hook
      #'enable-paredit-mode)

(add-hook 'lisp-interaction-mode-hook
      #'enable-paredit-mode)

(add-hook 'scheme-mode-hook
      #'enable-paredit-mode)


(add-hook 'slime-repl-mode-hook
      (lambda () (paredit-mode +1)))

(defun override-slime-repl-bindings-with-paredit ()
   (define-key slime-repl-mode-map
     (read-kbd-macro paredit-backward-delete-key) nil))

(add-hook 'slime-repl-mode-hook
      'override-slime-repl-bindings-with-paredit)

.

— Me@2022-11-29 10:03:49 PM

.

.

2022.11.29 Tuesday (c) All rights reserved by ACHK

Ex 1.2-1 Stationary States

Quantum Methods with Mathematica

.

Assume a wavefunction of the form psi[x, t] == f[t] psi[x] and perform a separation of variables on the wave equation.

Show that f[t] = E^(-I w t) where h w is the separation constant. Try the built-in function DSolve.

Equate h w to the Energy by evaluating the [expected] value of hamiltonian[V] in the state psi[x, t].

~~~

Remove["Global`*"]


hbar := \[HBar]

H[V_] @ psi_  := -hbar^2/(2m) D[psi,{x,2}] + V psi



psi[x_,t_] := f[t] psi[x]

I hbar D [psi[x,t],t] == H[V] @ psi[x, t]

I hbar D [psi[x,t],t] / psi[x,t] == H[V] @ psi[x,t] / psi[x,t]

\displaystyle{i \hbar  \psi (x) f'(t)=V f(t) \psi (x)-\frac{\hbar ^2 f(t) \psi ''(x)}{2 m}}

\displaystyle{\frac{i \hbar  f'(t)}{f(t)}=\frac{V f(t) \psi (x)-\frac{\hbar ^2 f(t) \psi ''(x)}{2 m}}{f(t) \psi (x)}}

E1 := I hbar D [psi[x,t],t] / psi[x,t] == H[V] @ psi[x,t] / psi[x,t]

Simplify[E1]

\displaystyle{\frac{1}{2} \hbar  \left(\frac{\hbar  \psi ''(x)}{m \psi (x)}+\frac{2 i f'(t)}{f(t)}\right)=V}

E2 := - 1/2 hbar hbar (D[D[psi[x],x],x]/(m psi[x])) == hbar omega

DSolve[E2, psi[x], x]


E3 := 1/2 hbar 2 i D[f[t],t] / f[t] == hbar omega

DSolve[E3, f[t], t]

\displaystyle{\left\{\left\{\psi (x)\to c_1 \cos \left(\frac{\sqrt{2} \sqrt{m} \sqrt{\omega } x}{\sqrt{\hbar }}\right)+c_2 \sin \left(\frac{\sqrt{2} \sqrt{m} \sqrt{\omega } x}{\sqrt{\hbar }}\right)\right\}\right\}}

\displaystyle{\left\{\left\{f(t)\to c_1 e^{\frac{\omega  t}{i}}\right\}\right\}}


k

psi[x_] := c E^(I k x)

psi[x]

f[t_] := E^(-I omega t)

f[t]

psi[x_,t_] := f[t] psi[x]

psi[x,t]

\displaystyle{  \left\{k,c e^{i k x},e^{-i \omega  t},c e^{i k x-i \omega  t}\right\}  }

E4 := Conjugate[psi[x,t]] H[0] @ psi[x,t]

E4

E5 := Simplify[E4]

E5

k := Sqrt[2 m omega / hbar]

Refine[E5, {Element[{c, omega, m, t, hbar, k, x}, Reals]}]

\displaystyle{  \frac{c k^2 \hbar ^2 c^* \exp \left(-i \left(-(\omega  t-k x)^*-k x+\omega  t\right)\right)}{2 m}  }

\displaystyle{  = c^2 \omega  \hbar  }

E6 := Conjugate[psi[x,t]] psi[x,t]

Simplify[E6]

\displaystyle{  c c^* \exp \left(-i \left(-(\omega  t-k x)^*-k x+\omega  t\right)\right)  }

\displaystyle{  = c^2  }

.

\displaystyle{\begin{aligned}            \langle E \rangle     &= \frac{\int_{-\infty}^{\infty} \psi^* H_{V=0} \psi dx}{\int_{-\infty}^{\infty} \psi^* \psi dx} \\ \\     &= \frac{c^2 \omega  \hbar \int_{-\infty}^{\infty} dx}{c^2 \int_{-\infty}^{\infty} dx} \\ \\    &= \omega  \hbar \\    \end{aligned}}

.

— Me@2022-11-26 07:17:29 PM

.

.

2022.11.28 Monday (c) All rights reserved by ACHK

Why does the universe exist? 7.2

“There is nothing in that region of space”

and

“there is nothing outside the universe”

have different meanings.

.

there is nothing except quantum fluctuations in that region of space

= the best detector detects nothing but quantum fluctuations

.

there is nothing outside the universe

= whatever detected, label the whole collection as “the universe”

.

“There is nothing outside the universe” does not (!!!) mean that “we go outside the universe to keep searching, but find nothing”.

— Me@2012-10-15 08:33:01 AM

— Me@2022-11-27 09:09:53 PM

.

.

2022.11.28 Monday (c) All rights reserved by ACHK

Euler problem 8.3

Directory[]

mString := Import["n.txt"]

nString := StringDelete[mString, "\n" | "\r"]

nList := Map[FromDigits, Characters[nString]]

take13[lst_] := Times @@ Take[lst,13]

Fmax13n[lst_, n_] := If[Length[lst] < 13,
                        n,
                        With[{t13 = take13[lst]},
                            If[n > t13,
                                Fmax13n[Rest[lst], n],
                                Fmax13n[Rest[lst], t13]]]]

Fmax13n[nList, 0]

Wmax13n[lst_, n_] := Which[
                        Length[lst] < 13, n,
                        t13 = take13[lst];
                            n > t13, Wmax13n[Rest[lst], n],
                            True, Wmax13n[Rest[lst], t13]]

Wmax13n[nList, 0]

Fmax13n[nList, 0] - Wmax13n[nList, 0]

— colorized by palette fm

— Me@2022-11-24 05:51:56 PM

.

.

2022.11.24 Thursday (c) All rights reserved by ACHK

Problem 14.5d1.2.2

A First Course in String Theory

.

The generating function is an infinite product:

\displaystyle{ \begin{aligned} \alpha' M_L^2: \end{aligned}}

\displaystyle{\begin{aligned} &f_{L, NS+}(x) \\ &= a_{NS+} (r) x^r \\ &= \frac{1}{x} \prod_{r=1}^\infty \frac{(1 + x^{r-\frac{1}{2}})^{32}}{(1 - x^r)^8} \\ \end{aligned}}

.

To evaluate the infinite product, you can use Mathematica (or its official free version Wolfram Engine) with the following commands:

TeXForm[
    HoldForm[
        (1/x)*Product[
                 (1+x^(r-1/2))^32/(1-x^r)^8,
                 {r, 1, Infinity}]]]

f[x_] := (1/x)*Product[
                 (1+x^(r-1/2))^32/(1-x^r)^8,
                 {r, 1, Infinity}]

Print[f[x]]

TeXForm[f[x]]

TeXForm[Series[f[x], {x,0,3}]]
\displaystyle{\frac{1}{x}\prod _{r=1}^{\infty } \frac{\left(1+x^{r-\frac{1}{2}}\right)^{32}}{\left(1-x^r\right)^8}}


                     1        32
    QPochhammer[-(-------), x]
                  Sqrt[x]
------------------------------------
        1    32                    8
(1 + -------)   x QPochhammer[x, x]
     Sqrt[x]


\displaystyle{\frac{\left(-\frac{1}{\sqrt{x}};x\right)_{\infty }^{32}}{\left(\frac{1}{\sqrt{x}}+1\right)^{32} x (x;x)_{\infty }^8}}


\displaystyle{\frac{1}{x}+\frac{32}{\sqrt{x}}+504+5248 \sqrt{x}+40996 x+258624 x^{3/2}+1384320 x^2+O\left(x^{5/2}\right)}

\displaystyle{ \begin{aligned}  &f_{L, NS+}(x) \\  \end{aligned}}

\displaystyle{  \approx \frac{1}{x}+\frac{32}{\sqrt{x}}+504+5248 \, \sqrt{x}+40996 \, x+258624 \, x^{\frac{3}{2}}+1384320 \, x^{2}+6512384 \, x^{\frac{5}{2}} + ...}

— Me@2022-11-23 04:40:28 PM

.

.

2022.11.23 Wednesday (c) All rights reserved by ACHK

Why does the universe exist? 7.1

Why is there something instead of nothing?

Why is there the universe?

.

The existence of the universe is not a property of the universe itself.

Instead, it is a property of the system that the universe is in.

.

However, there is no bigger system that contains the universe, because by the definition of the word “universe”, the universe contains everything.

So the question “why is there the universe” should be transformed to “why is there something instead of nothing?”

.

For example, the watch exists because the watchmaker has made it.

Similarly, if someone has created the universe, we can say that that someone is the cause of the existence of the universe.

However, there is no “someone” outside the universe, because of the definition of the word “universe”.

The universe has no outside.

— Me@2012-10-15 08:33:01 AM

— Me@2022-11-21 09:41:23 PM

.

.

2022.11.22 Tuesday (c) All rights reserved by ACHK

還是覺得你最差

數學教育 1.2 | Personality | Break free | People don’t change | Choose around | Change the world, 2

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

.

每人都有對自己的最先責任,和最終責任。其他人對你,則沒有必然責任。你對其他人,亦沒有。

.

那是一個好重要的常識。那是 Stephen Covey 在他那本《First Things First》中教的。

記住,一個人(甲)並不能夠改變,另一個人(乙);除非,乙自己想改變。

當乙自己想改變時,不知道如何執行,你可以教他方法;或者執行時過份緩慢,你可以教他加速。

但是,如果乙本身沒有意願,去改變自己的話,那就一切已失去,不可以再追。

.

老師的責任,是把學生的學習動機,乘大一千倍。

只要有學習動機,無論是多麼小,乘大一千倍後,都可以威力驚人。

但是,如果學習動機是零的話,零乘一千還是零。

— Me@2010.01.29

— Me@2022-11-20 07:46:26 PM

.

.

2022.11.21 Monday (c) All rights reserved by ACHK

Importance, 2.2

Euler problem 8.2

.

.

import Data.Char

max13 lst = max13n lst 0
  where
    max13n lst n | (length lst) < 13 = n
                 | n > take13        = max13n (tail lst) n
                 | otherwise         = max13n (tail lst) take13
      where
        take13 = product (take 13 lst)

str <- readFile "n.txt"

max13 (map (fromIntegral . digitToInt) . concat . lines $ str)

.

— Me@2022-11-19 12:04:41 PM

.

.

2022.11.19 Saturday (c) All rights reserved by ACHK

Ex 2.0

Functional Differential Geometry

.

~~~


(define R2 (make-manifold R^n 2))

(define U (patch 'origin R2))


(define R2-rect (coordinate-system 'rectangular U))

(define R2-polar (coordinate-system 'polar/cylindrical U))


(define R2-rect-chi (chart R2-rect))

(define R2-rect-chi-inverse (point R2-rect))

(define R2-polar-chi (chart R2-polar))

(define R2-polar-chi-inverse (point R2-polar))


(show-expression
 ((compose R2-polar-chi R2-rect-chi-inverse)
  (up 'x_0 'y_0)))

(show-expression
 ((D (compose R2-rect-chi R2-polar-chi-inverse))
  (up 'r_0 'theta_0)))

(define R2->R (-> (UP Real Real) Real))

(define f
  (compose (literal-function 'f-rect R2->R) R2-rect-chi))



(define R2-rect-point (R2-rect-chi-inverse (up 'x_0 'y_0)))

(define corresponding-polar-point
  (R2-polar-chi-inverse
   (up (sqrt (+ (square 'x_0) (square 'y_0)))
       (atan 'y_0 'x_0))))


(f R2-rect-point)

(f corresponding-polar-point)

(show-expression
 (f R2-rect-point))

(show-expression
 (f corresponding-polar-point))

— Me@2022-11-18 11:22:36 AM

.

.

2022.11.18 Friday (c) All rights reserved by ACHK

Posted in FDG