Skip to content

Commit

Permalink
Merge pull request #34 from bendudson/misc-docs
Browse files Browse the repository at this point in the history
Add some documentation on python-getattr
  • Loading branch information
bendudson committed Sep 11, 2020
2 parents 2d36bd5 + 14477fc commit 7b965b5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,20 @@ specifying the method, followed by any arguments:
#+RESULTS:
: 3

** Getting python attributes: =python-getattr=

The attributes of a python object can be accessed using the generic
functon =python-getattr=, with the python object as first argument and
a string as the name of the attribute:
#+BEGIN_SRC lisp
(py4cl:python-getattr '(1 2 3) "__doc__")
#+END_SRC


Note: Methods for this function can also be defined for lisp classes,
enabling python code to access attributes of lisp objects. See below
for details.

** Chaining python methods: =chain=

In python it is quite common to apply a chain of method calls, data
Expand Down
67 changes: 67 additions & 0 deletions docs/matplotlib.org
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,72 @@ in addition to the =np= and =plt= modules already loaded:
#+RESULTS:
: NIL

or a slightly more efficient method, using =remote-objects=:
#+BEGIN_SRC lisp
(defun f (x y)
(*
(+ 1 (* -0.5 x) (expt x 5) (expt y 3))
(exp (- 0 (* x x) (* y y)))))

(py4cl:remote-objects
(let* ((n 256)
(xy (np:meshgrid (np:linspace -3 3 n)
(np:linspace -3 3 n)))
(x (py4cl:chain xy 0))
(y (py4cl:chain xy 1)))

(plt:axes #(0.025 0.025 0.95 0.95))

(plt:contourf x y (py4cl:python-call (np:vectorize #'f) x y) 8 :alpha 0.75)

(let ((c (plt:contour x y (py4cl:python-call (np:vectorize #'f) x y) 8 :colors "black" :linewidth 0.5)))
(plt:clabel c :inline 1 :fontsize 10))))

(plt:xticks #())
(plt:yticks #())
(plt:show)
#+END_SRC

#+RESULTS:
: NIL

The majority of the time is spent in calling lisp for every point in (x,y). We could
just do the calculation in lisp using =array-operations=:
#+BEGIN_SRC lisp
(ql:quickload :array-operations)
#+END_SRC

#+RESULTS:
| :ARRAY-OPERATIONS |

#+BEGIN_SRC lisp
(defun f (x y)
(*
(+ 1 (* -0.5 x) (expt x 5) (expt y 3))
(exp (- 0 (* x x) (* y y)))))

(let* ((n 256)
(xy (np:meshgrid (np:linspace -3 3 n)
(np:linspace -3 3 n)))
(x (aref xy 0))
(y (aref xy 1))
(z (aops:vectorize (x y) (f x y))))

(plt:axes #(0.025 0.025 0.95 0.95))

(plt:contourf x y z 8 :alpha 0.75)

(let ((c (plt:contour x y z 8 :colors "black" :linewidth 0.5)))
(plt:clabel c :inline 1 :fontsize 10)))

(plt:xticks #())
(plt:yticks #())
(plt:show)
#+END_SRC

#+RESULTS:
: NIL

** 3D plots

#+CAPTION: A simple example of 3D plotting.
Expand Down Expand Up @@ -287,3 +353,4 @@ in addition to the =np= and =plt= modules already loaded:

#+RESULTS:
: NIL

0 comments on commit 7b965b5

Please sign in to comment.