README 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # -*- mode: org -*-
  2. * Description
  3. This is a port of the 'MIT Scmutils' library to Chez Scheme. On top
  4. of providing a set of of procedures for numerical computations, the
  5. library provides a set of generic operators extending many
  6. mathematical functions and operators to work with
  7. - symbolic values,
  8. - vectors, matrices and structures (to represent, e.g., tensors)
  9. - physical quantities (values with units),
  10. - ...
  11. ** Examples
  12. Load the libraries in the REPL (see below for further details):
  13. #+BEGIN_SRC scheme
  14. (import (scmutils base)
  15. (scmutils generic)
  16. (scmutils calculus))
  17. (start-scmutils-print!)
  18. #+END_SRC
  19. You can compute the derivative of a scalar or vector valued
  20. function evaluated at point 'x with:
  21. #+BEGIN_SRC scheme
  22. ((D (up cos sin)) 'x)
  23. #+END_SRC
  24. where 'up' denotes a contravariant vector which is represented by
  25. a Scheme vector and 'D' is the generic derivative operator.
  26. The operator 'D' is also used to compute the gradient of functions
  27. of multiple arguments:
  28. #+BEGIN_SRC scheme
  29. (define (g x y)
  30. (sin (* x y)))
  31. ((D g) 'x0 'y0)
  32. #+END_SRC
  33. The result is a 'down' tuple' representing a covariant vector which
  34. can be contracted with a contravariant vector to obtain the
  35. approximate change in the function 'g':
  36. #+BEGIN_SRC scheme
  37. (* ((D g) 'x0 'y0)
  38. (up 'dx 'dy))
  39. #+END_SRC
  40. You can work with 'structures' (see refman) and with matrices:
  41. #+BEGIN_SRC scheme
  42. (let ((M (matrix-by-rows (list 'a 0) (list 0 'b))))
  43. (determinant M))
  44. #+END_SRC
  45. For numerical computations the library includes procedures to solve
  46. differential equations, doing numerical integration, root finding
  47. and single- and multi-variable minimization among other things:
  48. #+BEGIN_SRC scheme
  49. (minimize (square cos) 0 :pi)
  50. #+END_SRC
  51. You can work with units as well:
  52. #+BEGIN_SRC scheme
  53. (/ (& 30 &centi &meter) :c)
  54. #+END_SRC
  55. with ':c' the speed of light in m/s (one of several built-in
  56. constants).
  57. * Build
  58. To build the libraries run "make". By default it looks for a Chez
  59. Scheme binary called "chez-scheme". If the name of your binary is
  60. different, you can specify it as follows (for all targets):
  61. make CHEZ=my-chez-scheme-bin
  62. * Install
  63. By default "make install" will install the libraries under the
  64. prefix "/usr/local". You can specify a different installation
  65. prefix directory as follows:
  66. make install PREFIX=/my-prefix/path
  67. This will install all compiled libraries (*.so files) to
  68. $PREFIX/lib/csvX.Y-site, where X.Y is Chez's version. If in
  69. addition you want to install the source files, use the 'install-src'
  70. target. They are installed in the same directory as the .so files.
  71. * Use
  72. The library consists of the following sub-libraries:
  73. - (scmutils base): defines the fundamental part of the library.
  74. Currently we export most defined functions, even many internal
  75. ones. In the future we may clean up the list of exported
  76. symbols.
  77. - (scmutils generic): exports the generic operators such as '+',
  78. '*', ... with names equal to standard Scheme procedures.
  79. - (scmutils mechanics): exports the functions described in the book
  80. 'Structure and Interpretation of Classical Mechanics' by
  81. G. J. Sussman and J. Wisdom.
  82. Note that, because Chez Scheme doesn't provide a built-in
  83. graphics library, the plotting functions described in the book
  84. are not available. As an alternative, interfacing with 'Gnuplot'
  85. from Chez Scheme is pretty straight-forward.
  86. - (scmutils calculus): exports the functions described in the book
  87. 'Functional Differential Geometry' by G. J. Sussman and
  88. J. Wisdom.
  89. Note that the (scmutils mechanics) library defines a version of
  90. 'Lie-derivative' which is less general than the one defined in
  91. (scmutils calculus). Be sure to use the latter for calculus
  92. purposes.
  93. If you want to load everything you can just import all of above
  94. libraries in the indicated order, making sure to import
  95. 'Lie-derivative' from (scmutils calculus) and not from (scmutils
  96. mechanis).
  97. Scmutils includes a custom REPL to drop internal tag information.
  98. To start it execute (start-scmutils-print!), to stop it use
  99. (stop-scmutils-print!). The custom REPL automatically simplify
  100. expression results before printing them.
  101. Once the custom REPL is active and if you have LaTeX installed, the
  102. result of the last expression can be nicely displayed with the full
  103. mathematical formatting capability of LaTeX with the command '(de)'.
  104. The original MIT/GNU Scheme Scmutils includes several global
  105. variables, usually identified by names starting and ending with
  106. '*'. Because the R6RS Scheme standard doesn't allow libraries to
  107. export mutable variables, we changed most of them to 'parameters'.
  108. * Limitations
  109. MIT/GNU Scheme has a very powerful condition system, including the
  110. 'restart' feature found in Common-Lisp. This allows one to extend
  111. Scheme without having to implement a new interpreter. This feature
  112. is used in Scmutils to allows, e.g., a vector of functions in
  113. operator position.
  114. The condition system of R6RS Scheme is more limited and doesn't
  115. provide 'restarts'. As a consequence, sometimes one has to
  116. explicitly use the generic version of 'apply', called 'g:apply',
  117. instead of the Scheme native one. As an example, the following
  118. code produces an error of type "attempt to apply non-procedure":
  119. #+BEGIN_SRC scheme
  120. (((d (literal-manifold-function 'f-rect R2-rect))
  121. (coordinate-system->vector-basis R2-rect))
  122. ((point R2-rect) (up 'x0 'y0)))
  123. #+END_SRC
  124. To fix the problem one has to explicitly use 'g:apply' as shown
  125. below:
  126. #+BEGIN_SRC scheme
  127. (g:apply
  128. ((d (literal-manifold-function 'f-rect R2-rect))
  129. (coordinate-system->vector-basis R2-rect))
  130. (list ((point R2-rect) (up 'x0 'y0))))
  131. #+END_SRC
  132. This should only be necessary in code that you type at the REPL or
  133. in user defined functions.