Quicklisp error

Common Lisp calculator, 2

.

1. To run the following code, you have to install the Quicklisp library manager by following the steps in my blog post titled cmu-infix.

.

2.1 To enable the library, in the Common Lisp REPL, run the code:

(ql:quickload :cmu-infix)

(named-readtables:in-readtable cmu-infix:syntax)

2.2 If you encounter the error message:

Lisp connection closed unexpectedly: connection broken by remote peer

2.2.1 empty the cache folder:

~/.cache/common-lisp/

2.2.2 Restart the Common Lisp REPL.

2.2.3 Reload the library.

.

3.1 The goal of the following code is to create our own REPL.

(defmacro infix-string-eval (a-string)
  `(eval (read-from-string
          (concatenate 'string
                       "#I (" ,a-string ")"))))

(defun read-repl-all ()
  (loop (print (write-to-string (read)))))

(defun my-repl ()
  (loop
    (let ((a-string (write-to-string (read))))
      (if (or (string-equal "(exit)" a-string)
              (string-equal "exit" a-string))
          (return)            
          (print
           (infix-string-eval
            (string-trim "|()" a-string))))))) 

3.2 In the Common Lisp REPL, run the code:

(my-repl)

— Me@2022-12-26 01:10:34 PM

.

The symbol

^

is the bitwise exclusive OR.

To create an exponentiation, we use

^^

— Me@2023-09-09 12:51:46 PM

.

— Me@2023-11-22 12:56:00 PM

.

.

2023.11.23 Thursday (c) All rights reserved by ACHK

Euler problem 17.2

import Data.Char ( digitToInt )

one :: [String]
one =
  [ "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",
    "twelve",
    "thirteen",
    "fourteen",
    "fifteen",
    "sixteen",
    "seventeen",
    "eighteen",
    "nineteen"
  ]

ty :: [String]
ty =
  [ "twenty",
    "thirty",
    "forty",
    "fifty",
    "sixty",
    "seventy",
    "eighty",
    "ninety"
  ]

english :: Int -> [Char]
english x
  | x == 0 = []
  | x < 20 = one !! (x - 1)
  | x >= 20 && x < 100 =
      ty !! (firstDigit x - 2)
        ++ " "
        ++ english (x - firstDigit x * 10)
        
  | x < 1000 && x `mod` 100 == 0 =
      one !! (firstDigit x - 1)
        ++ " hundred"
      
  | x > 100 && x <= 999 =
      one !! (firstDigit x - 1)
        ++ " hundred and "
        ++ english (x - firstDigit x * 100)
        
  | x == 1000 = "one thousand"
  | otherwise = "error"
  where
    firstDigit = digitToInt . head . show

removeSpace :: [Char] -> [Char]
removeSpace = filter (`notElem` " ")

engCat :: [Int] -> [Char]
engCat = concatMap english

e17 :: Int
e17 = length . removeSpace $ engCat [1..1000]

— based on Haskell official

λ> e17
21124

— colorized by palette fm

— Me@2023-11-07 11:24:03 PM

.

.

2023.11.08 Wednesday (c) All rights reserved by ACHK

KDE Wifi

Sometimes, KDE Plasma keeps turning off wifi during every startup. To solve this problem:

1. Type this command to the terminal:

sudo emacs \ 
/etc/systemd/system/unb-wifi.service

1.1 If you like, you can replace

emacs

with your favourite editor’s name in this command.

1.2 The goal of this command is to create the new text file

unb-wifi.service

.

2.1 Copy and paste the following code to the text file.

[Unit]
Description=Unblock WiFi Devices

[Service]
Type=oneshot
ExecStart=/usr/sbin/rfkill unblock wifi
ExecStart=/usr/bin/nmcli radio wifi on
ExecStop=
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

2.2 Save the file and exit the editor.

.

3.1 Execute this command in bash terminal:

sudo systemctl enable \ 
unb-wifi.service

If you get any errors, try this instead:

sudo systemctl enable \ 
/etc/systemd/system/unb-wifi.service

3.2 Then, execute also:

sudo systemctl start \ 
unb-wifi.service

If you get any errors, try this instead:

sudo systemctl start \ 
/etc/systemd/system/unb-wifi.service

3.3 If you still get errors, restart your computer.

Check if the wifi is already on.

— Me@2023-11-05 12:56:05 PM

.

.

2023.11.06 Monday (c) All rights reserved by ACHK

Euler problem 17.1

(defun remove-char (lst string)
  (cond ((not lst) string)
        ('t (remove-char (cdr lst)
                         (remove (car lst)
                                 string)))))

(defmacro english-reduced (n)
  `(length (remove-char (list #\- #\space)
                        (format nil "~r" ,n))))

(defmacro english-and (n)
  `(cond
     ((= (mod n 100) 0) (english-reduced ,n))
     ((> ,n 100) (+ 3 (english-reduced ,n)))
     ('t (english-reduced ,n))))

(defun e17 ()
  (labels ((e17-iter (n acc)
             (cond ((<= n 0) acc)
                   ('t (e17-iter (- n 1)
                                 (+ acc
                                    (english-and n)))
                       ))))
    (e17-iter 1000 0)))

CL-USER> (e17)
21124

— Me@2023-10-26 06:36:38 PM

.

.

2023.10.26 Thursday (c) All rights reserved by ACHK

1.8.4 Noether’s Theorem

1. …

2. However, there are more general symmetries that no coordinate system can fully express. For example, motion in a central potential is spherically symmetric … , but the expression of the Lagrangian for the system in spherical coordinates exhibits symmetry around only one axis.

3. …

4. A continuous symmetry is a parametric family of symmetries. Noether proved that for any continuous symmetry there is a conserved quantity.

— Structure and Interpretation of Classical Mechanics

.

.

2023.10.23 Monday ACHK

Picasa

This is a file from the Wikimedia Commons.

Picasa was a cross-platform image organizer and image viewer for organizing and editing digital photos, integrated with a now defunct photo-sharing website, originally created by a company named Lifescape (which at that time was incubated by Idealab) in 2002.

— Wikipedia on Picasa

— Me@2023-10-10 08:09:55 PM

.

.

2023.10.10 Tuesday (c) All rights reserved by ACHK

Euler problem 16.1

(defun digits (n)
  (labels
      ((f-iter (m acc)
         (cond ((= m 0) acc)
               ('t (f-iter (floor m 10)
                           (cons (mod m 10) acc))))))
    (f-iter n NIL)))

(defmacro sum (lst)
  `(reduce #'+ ,lst))

(sum (digits (expt 2 1000)))

— Me@2023-09-17 01:04:49 AM

.

.

2023.09.17 Sunday (c) All rights reserved by ACHK

Ex 1.30 Driven spherical pendulum, 2

Structure and Interpretation of Classical Mechanics

.

What symmetry(ies) can you have?

Find coordinates that express the symmetry.

What is conserved?

Give [the] analytic expression(s) for the conserved quantity(ies).

~~~

[guess]

(define ((F->C F) local)
  (->local (time local)
           (F local)
           (+ (((partial 0) F) local)
              (* (((partial 1) F) local)
                 (velocity local)))))

(define (L-driven m R qs U)
  (compose
   (L-rect m R qs U)
   (F->C (sf->rf qs))))

(let* ((U (U-gravity 'g 'm))
       (xs (lambda (t) 0))
       (ys (lambda (t) 0))
       (zs (literal-function 'z_s))
       (qs (up xs ys zs))
       (L (L-driven 'm 'R qs U))
       (q (up (literal-function 'r)
              (literal-function 'theta)
              (literal-function 'phi)
              (literal-function 'lambda))))
  (show-expression
   ((compose L (Gamma q)) 't)))
(+ (* 1/2 m (expt (r t) 2)
            (expt (sin (theta t)) 2)
            (expt ((D phi) t) 2))
   (* -1 m ((D z_s) t)
           (r t)
           (sin (theta t))
           ((D theta) t))
   (* 1/2 m (expt (r t) 2)
            (expt ((D theta) t) 2))
   (* -1 g m (cos (theta t)) (r t))
   (* m ((D r) t)
        ((D z_s) t)
        (cos (theta t)))
   (* (expt R 2) (lambda t))
   (* -1 g m (z_s t))
   (* 1/2 m (expt ((D r) t) 2))
   (* 1/2 m (expt ((D z_s) t) 2))
   (* -1 (lambda t) (expt (r t) 2)))

\displaystyle{    \begin{aligned}    L &= \frac{1}{2} m \left( \dot r^2  + r^2 \dot \theta^2  + r^2 (\sin \theta)^2 \dot \phi^2  + \dot z_s^2 \right) \\    &    - m g \left( r \cos \theta  + z_s(t) \right)  - m \dot z_s \left( r \dot \theta \sin \theta  - \dot r \cos \theta \right)    \\    &    - \lambda(t) (r^2 - R^2) \\    \end{aligned}    }

(let* ((U (U-gravity 'g 'm))
       (xs (lambda (t) 0))
       (ys (lambda (t) 0))
       (zs (literal-function 'z_s))
       (qs (up xs ys zs))
       (L (L-driven 'm 'R qs U))
       (q (up (literal-function 'r)
              (literal-function 'theta)
              (literal-function 'phi)
              (literal-function 'lambda))))
  (show-expression
   (((Lagrange-equations L) q) 't)))

(let* ((U (U-gravity 'g 'm))
       (xs (lambda (t) 0))
       (ys (lambda (t) 0))
       (zs (literal-function 'z_s))
       (qs (up xs ys zs))
       (L (L-driven 'm 'R qs U))
       (q (up (literal-function 'r)
              (literal-function 'theta)
              (literal-function 'phi)
              (literal-function 'lambda))))
  (show-expression
   ((compose (Lagrangian->energy L) (Gamma q)) 't)))

(let* ((U (U-gravity 'g 'm))
       (xs (lambda (t) 0))
       (ys (lambda (t) 0))
       (zs (literal-function 'z_s))
       (qs (up xs ys zs))
       (L (L-driven 'm 'R qs U))
       (q (up (literal-function 'r)
              (literal-function 'theta)
              (literal-function 'phi)
              (literal-function 'lambda))))
  (show-expression
   ((compose ((partial 1) L) (Gamma q)) 't)))

The \displaystyle{\varphi} component of the force is zero because \displaystyle{\varphi} does not appear in the Lagrangian (it is a cyclic variable). The corresponding momentum component is conserved. Compute the momenta:

(let* ((U (U-gravity 'g 'm))
       (xs (lambda (t) 0))
       (ys (lambda (t) 0))
       (zs (literal-function 'z_s))
       (qs (up xs ys zs))
       (L (L-driven 'm 'R qs U))
       (q (up (literal-function 'r)
              (literal-function 'theta)
              (literal-function 'phi)
              (literal-function 'lambda))))
  (show-expression
   ((compose ((partial 2) L) (Gamma q)) 't)))

[guess]

— Me@2023-08-20 05:02:09 PM

.

.

2023.09.15 Friday (c) All rights reserved by ACHK

Common Lisp calculator, 1

To run the following code, you have to install the Quicklisp library manager by following the steps in my blog post titled cmu-infix.

(ql:quickload :infix-math)

(asdf:load-system "infix-math/calc")

(infix-math/calc:calc)

This problem of using this calculator is that spaces are required both before and after an operator:

wrong:

1+1

correct:

1 + 1

— Me@2022-12-26 10:19:05 AM

.

.

2023.09.09 Saturday (c) All rights reserved by ACHK

Lagrange’s equations Debugged

\displaystyle{\frac{d}{dt} \left( \frac{\partial L(t, q, \dot q)}{\partial \dot q} \Bigg|_{\begin{aligned}   q &= w(t) \\  \dot q &= \frac{d w(t)}{dt} \\   \end{aligned}} \right) - \frac{\partial L}{\partial q}\Bigg|_{\begin{aligned}   q &= w(t) \\  \dot q &= \frac{d w(t)}{dt} \\   \end{aligned}}  = 0}

This equation is complete. It has meaning independent of the context and there is nothing left to the imagination. The earlier equations require the reader to fill in lots of detail that is implicit in the context. They do not have a clear meaning independent of the context.

\displaystyle{\frac{d}{dt} \frac{\partial L}{\partial \dot q} - \frac{\partial L}{\partial q} = 0}

— Functional Differential Geometry

.

.

2023.09.05 Tuesday ACHK

Euler problem 15.1

— Poorly Drawn Lines

.

(defun factorial (n)
  (labels
      ((f-iter (m acc)
         (cond ((= m 0) acc)
               ('t (f-iter (1- m)
                           (* m acc))))))
    (f-iter n 1)))

(defun binomial (n r)
  (labels
      ((b-iter (m acc)
         (cond ((< m (1+ (- n r))) acc)
               ('t (b-iter (1- m)
                           (* m acc))))))
    (/ (b-iter n 1) (factorial r))))

.

— Me@2023-08-26 11:27:59 AM

.

.

2023.08.27 Sunday (c) All rights reserved by ACHK

Ex 1.30 Driven spherical pendulum, 1

Structure and Interpretation of Classical Mechanics

.

A spherical pendulum is a massive bob, subject to uniform gravity, that may swing in three dimensions, but remains at a given distance from the pivot.

Formulate a Lagrangian for a spherical pendulum, driven by vertical motion of the pivot.

~~~

How come [the equations]?

Maybe just using the above equation but set the r constant. But I have
to add a something in order to realize the moving center.

— Me@2006

.

[guess]

(define (KE-particle m v)
  (* 1/2 m (square v)))

(define ((extract-particle pieces) local i)
  (let* ((indices (apply up
                         (iota pieces
                               (* i pieces))))
         (extract (lambda (tuple)
                    (vector-map
                     (lambda (i) (ref tuple i))
                     indices))))
    (up (time local)
        (extract (coordinate local))
        (extract (velocity local)))))

(define (U-constraint R qs q lambd)
  (* lambd
     (- (square (- q qs))
        (square R))))

(define ((U-gravity g m) q)
  (let ((z (ref q 2)))
    (* m g z)))

(define ((L-rect m R qs U) local)
  (let* ((extract (extract-particle 3))

         (p (extract local 0))
         (t (time p))
         (q (coordinate p))
         (v (velocity p))

         (lambd (ref (coordinate local) 3)))

    (- (KE-particle m v)
       (U q)
       (U-constraint R (qs t) q lambd))))

(let* ((U (U-gravity 'g 'm))
       (xs (lambda (t) 0))
       (ys (lambda (t) 0))
       (zs (literal-function 'z_s))
       (qs (up xs ys zs))
       (L (L-rect 'm 'R qs U))
       (q-rect (up (literal-function 'x)
                   (literal-function 'y)
                   (literal-function 'z)
                   (literal-function 'lambda))))
  (show-expression
   ((compose L (Gamma q-rect)) 't)))
(+ (* (expt R 2) (lambda t))
   (* -1 g m (z t))
   (* 1/2 m (expt ((D x) t) 2))
   (* 1/2 m (expt ((D y) t) 2))
   (* 1/2 m (expt ((D z) t) 2))
   (* -1 (lambda t) (expt (z_s t) 2))
   (* 2 (lambda t) (z_s t) (z t))
   (* -1 (lambda t) (expt (z t) 2))
   (* -1 (lambda t) (expt (y t) 2))
   (* -1 (lambda t) (expt (x t) 2)))

\displaystyle{  L_r = \frac{1}{2} m \left| \dot {\vec r} (t) \right|^2  - mg z(t)  - \lambda(t) \left( \left| \vec r(t) - \vec r_s(t) \right|^2 - R^2 \right)  }

(define ((sf->rf qs) state-with-force)
  (let* ((extract (extract-particle 3))

         (p (extract state-with-force 0))
         (t (time p))
         (q (coordinate p))

         (lambd (ref (coordinate
                      state-with-force) 3))

         (r (ref q 0))
         (theta (ref q 1))
         (phi (ref q 2))

         (xs (ref qs 0))
         (ys (ref qs 1))
         (zs (ref qs 2))

         (x (+ (xs t)
               (* r (sin theta) (cos phi))))
         (y (+ (ys t)
               (* r (sin theta) (sin phi))))
         (z (+ (zs t)
               (* r (cos theta)))))

    (up x y z lambd)))

(let* ((xs (literal-function 'x_s))
       (ys (literal-function 'y_s))
       (zs (literal-function 'z_s))
       (qs (up xs ys zs))
       (q (up (literal-function 'r)
              (literal-function 'theta)
              (literal-function 'phi)
              (literal-function 'lambda))))
  (show-expression
   ((compose (sf->rf qs) (Gamma q))
    't)))

(define ((F->C F) local)
  (->local (time local)
           (F local)
           (+ (((partial 0) F) local)
              (* (((partial 1) F) local)
                 (velocity local)))))

(define (L-driven m R qs U)
  (compose
   (L-rect m R qs U)
   (F->C (sf->rf qs))))

[guess]

— Me@2023-08-20 05:02:09 PM

.

.

2023.08.23 Wednesday (c) All rights reserved by ACHK

Sequential speed



--recurse-paths
-r

recurse into directories


--symlinks
-y

store symbolic links as the link instead of the referenced file

.

zip -r -y /dest_folder/the_zip_file.zip /source_folder/

source_folder is the folder to be zipped.

dest_folder is the destination folder for storing the zip file.

After zipping, the resulting file is the_zip_file.zip, which is a zipped version of the source_folder.

— Me@2023-08-03 09:41:29 AM

.

.

2023.08.07 Monday (c) All rights reserved by ACHK

Euler problem 14.2

import Data.Array ( listArray, (!) )
import Data.List ( elemIndex )

collatzLengthList m = collatzLengthArray
  where
    collatzLengthArray
      = listArray (1,m) $ 1:map collatzLength [2..m]
      where
        collatzLength n
          | b<=m = 1 + collatzLengthArray ! b
          | otherwise = 1 + collatzLength b
          where
            b
              | even n = n `div` 2 
              | otherwise = 3*n+1

e14 :: (Maybe Int, Integer)
e14 = (n, m)
  where
    m = maximum cArray
    cArray = collatzLengthList p
    p = 1000000
    n = elemIndex m cList
      where
        cList = 0:[cArray ! x | x <- [1..p]]

λ> :set +s
λ> e14
(Just 837799,525)
(1.74 secs, 2,193,219,920 bytes)

— Me@2023-07-25 11:55:41 PM

.

.

2023.07.26 Wednesday (c) All rights reserved by ACHK

Memoize

Euler problem 14.1.2

.

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

(defun max-item (lst)
  (loop :for item :in lst
        :maximize item))

(defmacro r-nth (n lst)
  `(nth (- (length ,lst) ,n) ,lst))

(defun collatz-length (n c)
  (cond ((= n 1) c) 
        ((evenp n) (collatz-length (/ n 2) (1+ c)))
        ('t (collatz-length (1+ (* 3 n)) (1+ c)))))

(defun memoize (fn)
  (let ((cache (make-hash-table :test #'equal)))
    #'(lambda (&rest args)
        (multiple-value-bind (val win)
            (gethash args cache)
          (if win
              val
              (setf (gethash args cache)
                    (apply fn args)))))))

(setf (fdefinition 'collatz-length)
      (memoize #'collatz-length))

(time (max-item (mapcar #'(lambda (x)
                            (collatz-length x 1))
                        (range 1000001 :min 1))))

;; Heap exhausted (no more space for allocation).
;; 109314048 bytes available, 171127632 requested.

;; PROCEED WITH CAUTION.
;;    [Condition of type
;;       SB-KERNEL::HEAP-EXHAUSTED-ERROR]

(time (max-item (mapcar #'(lambda (x)
                            (collatz-length x 1))
                        (range 250001 :min 1))))

;; Evaluation took:
;; 1.380 seconds of real time

(time (max-item (mapcar #'(lambda (x)
                            (collatz-length x 1))
                        (range 250001 :min 1))))

;; Evaluation took:
;; 0.068 seconds of real time

— Me@2023-07-14 02:54:38 PM

.

.

2023.07.14 Friday (c) All rights reserved by ACHK

Ex 1.29 A particle of mass m slides off a horizontal cylinder, 2.2

Structure and Interpretation of Classical Mechanics

.

A particle of mass m slides off a horizontal cylinder of radius R in a uniform gravitational field with acceleration g. If the particle starts close to the top of the cylinder with zero initial speed, with what angular velocity does it leave the cylinder?

~~~

(define ((KE m g R) local)
  (let ((t (time local))
        (thetadot (velocity local)))    
    (* 1/2 m (square R) (square thetadot))))

(define ((PE m g R) local)
  (let ((t (time local))
        (theta (coordinate local))
        (thetadot (velocity local)))    
    (- (* m g R (- 1 (cos theta))))))

(define L (- KE PE))

(show-expression
 ((L 'm 'g 'R)
  (->local 't
           'theta
           'thetadot)))

(show-expression
 (((Lagrange-equations
    (L 'm 'g 'R))
   (literal-function 'theta))
  't))


This derivation is wrong, because the constraint force is missing.

.

(define ((F->C F) local)
  (->local (time local)
           (F local)
           (+ (((partial 0) F) local)
              (* (((partial 1) F) local)
                 (velocity local)))))

(define (q->r local)
  (let ((q (coordinate local)))
    (let ((r (ref q 0))
          (theta (ref q 1))
          (lambd (ref q 2)))
      (let ((x (* r (sin theta)))
            (y (* r (cos theta))))
        (up x y lambd)))))

(show-expression
 (q->r
  (->local 't
           (up 'r 'theta 'lambda)
           (up 'rdot 'thetadot 'lambdadot))))

(define (KE m vx vy)
  (* 1/2 m (+ (square vx) (square vy))))

(define ((T-rect m) local)
  (let ((q (coordinate local))
        (v (velocity local)))
    (let ((xdot (ref v 0))
          (ydot (ref v 1)))
      (KE m xdot ydot))))

(show-expression
 ((T-rect 'm)
  (up 't
      (up 'x 'y 'lambda)
      (up 'xdot 'ydot 'lambdadot))))

(show-expression
 ((T-rect 'm)
  ((F->C q->r)
   (->local 't
            (up 'r 'theta 'lambda)
            (up 'rdot 'thetadot 'lambdadot)))))

(define ((U-rect g m) local)
  (let* ((q (coordinate local))
         (y (ref q 1)))
    (* g (+ (* m y)))))

(define (L-rect g m)
  (- (T-rect m) (U-rect g m)))

(define (L g m)
  (compose
   (L-rect g m) (F->C q->r)))

(show-expression
 ((L 'g 'm)
  (->local 't
           (up 'r 'theta 'lambda)
           (up 'rdot 'thetadot 'lambdadot))))

(show-expression
 (((Lagrange-equations
    (L 'g 'm))
   (up
    (literal-function 'r)
    (literal-function 'theta)
    (literal-function 'lambda)))
  't))

(define ((U-constraint R) local)
  (let* ((q (coordinate local))
         (x (ref q 0))
         (y (ref q 1))
         (lambd (ref q 2))
         (r_sq (+ (square x) (square y))))
    (* lambd (- r_sq (square R)))))

(define ((U2-constraint R) local)
  (let* ((q (coordinate local))
         (x (ref q 0))
         (y (ref q 1))
         (lambd (ref q 2))
         (r_sq (+ (square x) (square y))))
    (* lambd (- (sqrt r_sq) R))))
(define (L-rect-constraint g m R)
  (- (T-rect m) (+ (U-rect g m) (U-constraint R))))

(define (L2-rect-constraint g m R)
  (- (T-rect m) (+ (U-rect g m) (U2-constraint R))))

(show-expression
 ((L-rect-constraint 'g 'm 'R)
  ((F->C q->r)
   (->local 't
            (up 'r 'theta 'lambda)
            (up 'rdot 'thetadot 'lambdadot)))))

(show-expression
 ((L2-rect-constraint 'g 'm 'R)
  ((F->C q->r)
   (->local 't
            (up 'r 'theta 'lambda)
            (up 'rdot 'thetadot 'lambdadot)))))

(show-expression
 (((Lagrange-equations
    (compose (L-rect-constraint 'g 'm 'R) (F->C q->r)))
   (up
    (literal-function 'r)
    (literal-function 'theta)
    (literal-function 'lambda)))
  't))

(show-expression
 (((Lagrange-equations
    (compose (L2-rect-constraint 'g 'm 'R) (F->C q->r)))
   (up
    (literal-function 'r)
    (literal-function 'theta)
    (literal-function 'lambda)))
  't))

(show-expression
 (((Lagrange-equations
    (compose (L-rect-constraint 'g 'm 'R) (F->C q->r)))
   (up
    (lambda (t) 'R)
    (literal-function 'theta)
    (literal-function 'lambda)))
  't))

— Me@2023-07-12 10:00:19 PM

.

.

2023.07.13 Thursday (c) All rights reserved by ACHK

Shadow

Euler problem 14.1.1

.

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

(defun col (n c)
  (cond ((= n 1) c)     
        ((evenp n) (col (/ n 2) (1+ c)))
        ('t (col (1+ (* 3 n)) (1+ c)))))

(defun max-item (lst)
  (loop :for item :in lst
        :maximize item))

(defun e14 ()
  (let* ((c-lst
           (mapcar #'(lambda (x)
                       (col x 1))
                   (range 1000001 :min 1)))
         (max-len
           (max-item c-lst))
         (max-index
           (1+ (position max-len c-lst))))    
    (list max-index max-len)))

(time (e14))

;; Evaluation took:
;; 1.084 seconds of real time
  
;; (837799 525)

— Me@2023-07-03 12:33:40 AM

.

.

2023.07.03 Monday (c) All rights reserved by ACHK

Ex 1.29 A particle of mass m slides off a horizontal cylinder, 2.1

Structure and Interpretation of Classical Mechanics

.

A particle of mass m slides off a horizontal cylinder of radius R in a uniform gravitational field with acceleration g. If the particle starts close to the top of the cylinder with zero initial speed, with what angular velocity does it leave the cylinder?

~~~

.

Kinetic energy:

\displaystyle{\begin{aligned}          T(r(t), \theta(t))     &= \frac{1}{2} m \left( v_x^2 + v_y^2 \right) \\         &= \frac{1}{2} m \left[ \left( \frac{d}{dt} r \sin \theta \right)^2 + \left( \frac{d}{dt} r \cos \theta \right)^2 \right] \\         &= \frac{1}{2} m \left[     \left( \dot r \sin \theta + r \dot \theta \cos \theta \right)^2   + \left( \dot r \cos \theta - r \dot \theta \sin \theta \right)^2   \right] \\         &= \frac{1}{2} m \left[ \dot r^2 + r^2 \dot \theta^2  \right] \\           \end{aligned}}

Potential energy:

\displaystyle{\begin{aligned}          V(r(t), \theta(t)) &= mgr \cos \theta \\        \end{aligned}}

Constraint:

\displaystyle{\begin{aligned}          \phi(t) &= r(t) - R \\         &= 0 \\        \end{aligned}}

.

The Lagrangian:

\displaystyle{\begin{aligned}          L(r(t), \theta(t)) &= T - V + \lambda \phi \\    &= \frac{1}{2} m ( \dot r^2 + r^2 \dot \theta^2 ) - mg r \cos \theta + \lambda (r - R) \\      \end{aligned}}

The Lagrange equations:

\displaystyle{ \begin{aligned}     \frac{d}{dt} \left( \frac{\partial L}{\partial \dot r} \right) - \frac{\partial L}{\partial r} &= 0 \\     \frac{d}{dt} \left( \frac{\partial L}{\partial \dot \theta} \right) - \frac{\partial L}{\partial \theta} &= 0 \\     \frac{d}{dt} \left( \frac{\partial L}{\partial \dot \lambda} \right) - \frac{\partial L}{\partial \lambda} &= 0 \\     \end{aligned}}

.

\displaystyle{ \begin{aligned}     \frac{d}{dt} \left( \frac{\partial L}{\partial \dot r} \right) - \frac{\partial L}{\partial r} &= 0 \\     \frac{d}{dt} m \dot r - \left( m r \dot \theta^2 - mg \cos \theta + \lambda \right) &= 0 \\     m \ddot r &= m r \dot \theta^2 - mg \cos \theta + \lambda   \\     \end{aligned}}

.

\displaystyle{ \begin{aligned}     \frac{d}{dt} \left( \frac{\partial L}{\partial \dot \theta} \right) - \frac{\partial L}{\partial \theta} &= 0 \\     \frac{d}{dt} \left( m r^2 \dot \theta \right) - mgr \sin \theta &= 0 \\     m \left( 2 r \dot r \dot \theta + r^2 \ddot \theta \right) - mgr \sin \theta &= 0 \\     \end{aligned}}

.

\displaystyle{ \begin{aligned}     \frac{d}{dt} \left( \frac{\partial L}{\partial \dot \lambda} \right) - \frac{\partial L}{\partial \lambda} &= 0 \\     - ( r - R ) &= 0 \\     r(t) &= R \\     \end{aligned}}

.

By the constraint \displaystyle{r(t) = R},

\displaystyle{ \begin{aligned}   \dot r(t) &= 0 \\   \ddot r(t) &= 0 \\   \end{aligned}}

— Me@2023-06-25 07:47:21 PM

.

.

2023.06.28 Wednesday (c) All rights reserved by ACHK

Emacs theme

(custom-set-variables
 '(cua-mode t nil (cua-base))
 '(custom-enabled-themes '(leuven))
 '(global-display-line-numbers-mode t))

(custom-set-faces
 '(default ((t (:family "DejaVu Sans Mono"
                        :foundry "PfEd"
                        :slant normal
                        :weight normal
                        :height 120
                        :width normal)))))

(prefer-coding-system 'utf-8)

(global-visual-line-mode t)

— Me@2023-06-18 01:27:37 PM

.

.

2023.06.18 Sunday (c) All rights reserved by ACHK