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

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

Clojure

Rich Hickey developed Clojure because he wanted a modern Lisp for functional programming, symbiotic with the established Java platform, and designed for concurrency.

Clojure’s approach to concurrency is characterized by the concept of identities, which represent a series of immutable states over time. Since states are immutable values, any number of workers can operate on them in parallel, and concurrency becomes a question of managing changes from one state to another. For this purpose, Clojure provides several mutable reference types, each having well-defined semantics for the transition between states.

— Wikipedia on Clojure

2011.08.03 Wednesday ACHK