How to create a simple graphic using Common Lisp? - common-lisp

I would like to create a graphic, some points and lines, possibly with colours, out of lists of numbers. What would be the simplest and efficient solution?
I suppose that a possibility would be to use the library imago but I was not very sucessfull with it until now when trying simple examples from https://quickdocs.org/imago like:
;; Create 400x100 px red RGB image
(imago:make-rgb-image 400 100 (imago:make-color 255 0 0))
Symbol "MAKE-GRAYSCALE-IMAGE" not found in the IMAGO package.
How could this work? Do you have any working example I could take a look at?

I made a library called Vectometry for this, and I find it pretty useful. It's vector-based, not pixel-based, and it only outputs PNG files, so its suitability depends on what kind of output you want.
Here's an example:
* (ql:quickload "vectometry")
...
* (in-package :vectometry)
#<PACKAGE "VECTOMETRY">
* (defun red-box (output-file)
(let ((canvas (box 0 0 400 100)))
(with-box-canvas canvas
(set-fill-color (rgb-color 1.0 0.0 0.0))
(clear-canvas)
(save-png output-file))))
RED-BOX
* (red-box "~/red.png")
#p"/path/to/home/directory/red.png"
It follows a postscript- or pdf-style imaging model, with move-to, line-to, curve-to, perspective transforms, etc., so if you are familiar with those operations, it should not be too tricky to use.

In order to use the new constructors, you have to use a more recent version of the library. You need to git clone the repository inside ~/quicklisp/local-projects/, and if you already loaded the system:
(delete-package :imago)
Also it can be useful to clean your cache:
$ find ~/.cache/common-lisp -type d -name "imago"
and delete the content.
Then, if you reload the library, it should be the newer version:
(ql:quickload :imago :verbose t)
Alternatively, you can keep your version and make class instances explictly:
(make-instance 'imago:grayscale-image ...)
The rest should work as described in the documentation and examples.

Related

Plothraw PARIGP (or similar) doesn't work (latexit crash)

I'm a new user of PARI/GP, and after writing my script, I wanted to make a graph of it. As my function take an integer and return a number, it's closer to a sequence. Actually, I didn't know how to do it, so I read the documentation of PARI/GP, and after that I made some test in order to obtain a graph from a list.
After reading an answer in stackoverflow (Plotting multiple lists in Pari), I wanted to test with the following code:
plothraw([0..200], apply(i->cos(i*3*Pi/200), [0..200]), 0);
But when I do it, it tries to open something on latexit, but then it crash and give me a problem report.
I didn't even know that I had an app named latextit, maybe it was install during the installation of PARI/GP. Anyway, how can I fix this?
PARI/GP definitely doesn't install latexit.
The way hi-res graphics work on the Win32 version of PARI/GP is to write down an Enhanced Metafile (.EMF) in a temp directory and ask the system to
"open" it. When you installed latexit it probably created an association in the registry to let it open .EMF files
i3Pi does not mean what you think, it just creates a new variable with that name. You want i * 3 * Pi instead.
The following constructions both work in my setup
plothraw([0..200], apply(i->cos(i*3*Pi/200), [0..200]), 0);
plothraw([0..200], apply(i->cos(i*3*Pi/200), [0..200]), 1);
(the second one being more readable because a red line is drawn between successive points; I have trouble seeing the few tiny blue dots)
Instead of apply, you can use a direct constructor as in
vector(201, i, cos((i-1) * 3 * Pi / 200))
which of course can be computed more efficiently as
real( powers(exp(3*I*Pi/200), 200) )
(of course, it doesn't matter here, but compare both commands at precision \p10000 or so ...)

how to get Disk Space used/free/total in Common Lisp

Seems that there is no standard functions to get used/free/total of Disk space in Common Lisp.
There is statvfs.h in Linux/Mac and GetDiskFreeSpaceEx function in Windows for C/C++.
Personally, it would call an executable to do that using a library.
Calling df -h with IOLib for example.
But this is not portable (particularly IOLib, but there are other libraries), and you have to parse the output of the commands.
That's one reason I love programs which have "machine readable" outputs: you can glue them up programmatically (à la shell script).
Another way would be to actually call these c function, using cffi or uffi (ffi standing for foreign function interface), but I haven't used neither, so I can't say much about it.
Oh, search on quickdocs.org, there is probably a library exactly for that, or maybe just to access the OS's API.
I wrote a new project cl-diskspace using statvfs to get disk total/free/available space in Common Lisp. Support Mac/Linux/Windows.
Update: 2015-07-11 now support Windows! Thanks to pjb, Guthur, Fare, |3b|
Install cl-diskspace with QuickLisp:
$ git clone https://github.com/muyinliu/cl-diskspace.git
$ cp -r cl-diskspace ~/quicklisp/local-projects/
Load cl-diskspace with QuickLisp:
(ql:quickload 'cl-diskspace)
Usage of cl-diskspace:
Get disk space information
(diskspace:disk-space "/")
Will get something like this:
127175917568
16509661184
16247517184
Means that the total space is 118.44G, free space is 15.38G and available space is 15.13G
Get disk total space
(diskspace:disk-total-space "/")
Will get something like this:
127175917568
Get disk free space
(diskspace:disk-free-space "/")
Will get something like this:
16509661184
Get disk available space
(diskspace:disk-available-space "/")
Will get something like this:
16247517184

Why "Reference to undefined global `Moduletest'" in OCaml?

I wrote
let fact x =
let result = ref 1 in
for i = 1 to x do
result := !result * i;
Printf.printf "%d %d %d\n" x i !result;
done;
!result;;
in a file named "Moduletest.ml", and
val fact : int -> int
in a file named "Moduletest.mli".
But, why don't they work?
When I tried to use in ocaml,
Moduletest.fact 3
it told me:
Error: Reference to undefined global `Moduletest'
What's happening?
Thanks.
OCaml toplevel is linked only with a standard library. There're several options on how to make other code visible to it:
copy-pasting
evaluating from the editor
loading files #use directive
making custom toplevel
loading with ocamlfind
Copy-pasting
This self-describing, you just copy code from some source and paste it into toplevel. Don't forget that toplevel won't evaluate your code until you add ;;
Evaluating from the editor
Where the editor is of course Emacs... Well, indeed it can be any other capable editor, like vim for example. This method is an elaboration of the previous, where the editor is actually responsible for copying and pasting the code for you. In Emacs you can evaluate the whole file with C-c C-b command, or you can narrow it to a selected area with C-c C-r, and the most granular is to use C-c C-e, i.e., evaluate an expression. Although it is slightly buggy.
Loading with #use directive.
This directive accepts a filename, and it will essentially copy and paste the code from the file. Notice, that it won't create a file-module for you/ For example, if you have file test.ml with this contents:
(* file test.ml *)
let sum x y = x + y
then loading it with the #use directive, will actually bring to your scope, sum value:
# #use "test.ml";;
# let z = sum 2 2
You mustn't to qualify sum with Test., because no Test module is actually created. #use directive merely copies the contents of the file to the toplevel. Nothing more.
Making custom toplevels
You can create your own toplevel with your code compiled in. It is an advanced theme, so I will skip it.
Loading libraries with ocamlfind
ocamlfind is a tool that allows you to find and load libraries, installed on your system, into your toplevel. By default, toplevel is not linked with any code except standard library. Even, not all parts of the library are actually linked, e.g., Unix module is not available, and needed to be loaded explicitly. There're primitive directives that can load any library, like #load and #include, but they are not for a casual user, especially if you have excellent ocamlfind at your disposal. Before using it, you need to load it, since it is also not available by default. The following command, will load ocamlfind and add few new directives:
# #use "topfind";;
In a process of loading it will show you a little hint on how to use it. The most interesting directive, that is added is #require. It accepts a library name, and loads (i.e., links) its code into toplevel:
# #require "unix";;
This will load a unix library. If you're not sure, about the name of the library you can always view all libraries with a #list command. The #require directive is clever and it will automatically load all dependencies of the library.
If you do not want to type all this directives every time you start OCaml top-level, then you cam create .ocamlinit file in your home directory, and put them there. This file will be loaded automatically on a top-level startup.
I have tested your code and it looks fine. You should "load" it from the OCaml toplevel (launched from the same directory as your .ml and .mli files) in the following way:
# #use "Moduletest.ml";;
val fact : int -> int = <fun>
# fact 4;;
4 1 1
4 2 2
4 3 6
4 4 24
- : int = 24

Emacs auto-complete for ESS in tooltip instead of buffer

I understand from the following resources:
http://www.emacswiki.org/emacs/ESSAuto-complete
http://www.emacswiki.org/emacs/AutoComplete
Emacs autocomplete-mode extension for ESS and R
Is it possible to get code completion for R in Emacs ESS similar to what is available in Rstudio?
...that I should have access to tooltips for auto-complete help in emacs when using ESS for R development. The last link additionally specifies that it should work out of the box with the latest ESS, and:
From version 12.03 ESS integrates out of the box with auto-complete
package.
Three sources ‘ac-source-R-args’, ‘ac-source-R-objects’ and
‘ac-source-R’ are included in ESS distribution. The latest combines
previous two and makes them play nicely together.
I know that in general, my emacs tooltips are possible because they appear correctly with my python jedi setup in emacs.
However, my emacs auto-complete does not work. Instead, there is a buffer at the bottom:
instead of this, with the help superimposed on the working buffer:
And I have the following in my init.el:
(require 'auto-complete)
(setq ess-use-auto-complete t)
(ess-toggle-underscore nil)
I have the following versions of:
ess 20131207.1141 installed No description available.
ess-R-data-view 20130509.458 installed Data viewer for GNU R
ess-R-object-popup 20130302.336 installed popup description of R object
auto-complete 20140208.653 installed Auto Completion for GNU Emacs
What am I missing?
UPDATE 1
Here's my init for ESS stuff. This produces completions in a buffer, but not in a tooltip, where some configuration options from the manual have been added. (I have now tried all permutations of commenting and not commenting all of these):
(require 'auto-complete)
(global-auto-complete-mode)
(require 'auto-complete-config)
(ac-config-default)
(require 'ess-site)
(setq ess-use-auto-complete t)
(setq ac-auto-start 2)
(setq ac-auto-show-menu 0.2)
(ess-toggle-underscore nil)
UPDATE 2
Similar question here, but the same solution is suggested which did not actually result in tooltips on my setup. But it suggests to me this might be a platform issue? The poster there was also on Ubuntu 12.04 as I am---is it possible the solutions that worked for VitoshKa, Alex Vorobiev, and Iqbal Ansari were on a different platform? Are you guys using OS X?
UPDATE 3
Maybe other diagnostics:
ac-source-R is a variable defined in `ess-r-d.el'.
Its value is ((prefix . ess-ac-start)
(candidates . ess-ac-candidates)
(document . ess-ac-help))
Documentation:
Combined ad-completion source for R function arguments and R objects
ac-source-R-objects is a variable defined in `ess-r-d.el'.
Its value is ((prefix . ess-symbol-start)
(candidates . ess-ac-objects)
(document . ess-ac-help-object))
Documentation:
Auto-completion source for R objects
ac-source-R-args is a variable defined in `ess-r-d.el'.
Its value is ((prefix . ess-ac-start-args)
(candidates . ess-ac-args)
(document . ess-ac-help-arg))
Documentation:
Auto-completion source for R function arguments
[back]
where the variables for ess-ac[TAB] are only...
Click <mouse-2> on a completion to select it.
In this buffer, type RET to select the completion near point.
Possible completions are:
ess-ac-R-argument-suffix
ess-ac-sources
So maybe the problem is my ESS install is lacking all ess-ac-* things defined above, like ess-ac-args, etc?
My auto-complete configuration also has
(require 'auto-complete-config)
(ac-config-default)
and after that setting ess-use-auto-complete makes ESS show the popup menus.
So as per your comments it turned out that auto-complete was not enabled in your emacs config. So just enable auto-complete by adding something like this in you init file
(load "auto-complete")
(global-auto-complete-mode)
Also for ESS you will need to set ess-use-auto-complete to t something like following would do
(setq ess-use-auto-complete t)
EDIT: ess-use-auto-complete is by default set to t so this step is not really needed.
Also since you found this option confusing I would recommend you to open an issue on the github repo so that the maintainers can improve the documentation (or code) whichever can reduce the confusion.
Auto-complete has a lot of configuration options do read the manual when you get time.
Glad I could help
Don't confuse eldoc with auto-completion. Eldoc is what you have in your screenshot, it shows all arguments of a function without being intrusive. Auto-completion pops up when you start typing stuff and activates after ac-auto-start characters.

AFP Dijkstra's Shortest Path Algorithm

For the AFP entry Dijkstra's Shortest Path Algorithm, both the proof outline and proof document were nonexistent *. Unfortunately, I did not find an IsaMakefile either to build those documents locally. What is the best way to get those documents?
Another question, as the Dijkstra.thy depends on a lot of other theories, is there a way to load everything faster?
*) It is fixed now.
(There seems to be something broken at AFP right now, please tell the editors about it.)
In general, you can download the sources of AFP entries and produce the documents yourself like this:
Get and unpack all AFP sources -- downloading separate entries is offered as well, but then you have to disentangle dependencies manually.
Invoke isabelle build like this:
isabelle build -d afp-2013-03-02 -o document=pdf -v Dijkstra_Shortest_Path
Here afp-2013-03-02 is the directory that was obtained by unpacking the current AFP sources.
See also the Isabelle System manual about "Isabelle sessions and build management", which is all new in Isabelle2013.
See isabelle build -b there to make things load faster, by producing persistent heap images from sessions.
The links in the AFP entry were indeed broken and should now be fixed again, sorry about that.
As Makarius writes, the AFP new uses Isabelle's new build system, i.e. has a ROOT file for each entry that can be used to check the associated theories and build the document.
Makarius' answer is pretty much the official way to do it, although I would additionally recommend setting up the AFP as a component. This gives you the following steps:
Download the AFP to e.g. ~/afp
Set it up as component e.g. by adding ~/afp to ~/.isabelle/Isabelle2013/components (see also AFP as a component)
build the entry with
isabelle afp_build Dijkstra_Shortest_Path
You can also have jEdit build the heap image for you. If the AFP is setup as a component (see the other answers for that), just start jEdit with
isabelle jedit -d '$AFP' -l Dijkstra_Shortest_Path
and jEdit will select Dijkstra_Shortest_Path as base logic and (re)build it if necessary.
If you make regular use of the AFP, it might be useful to add the AFP path by default. For this, create a file ROOTS in $ISABELLE_HOME_USER with the line $AFP in it (or add this line, if the file already exists).

Resources