mathutil.scm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; Derived Generic Operators
  21. (declare (usual-integrations))
  22. ;;; FBE start
  23. ;; (define ratnum?
  24. ;; (access ratnum?
  25. ;; (->environment '(runtime number))))
  26. (define ratnum? rational?)
  27. (define (g:cube x)
  28. (g:* x x x))
  29. (define (g:log10 x)
  30. (g:/ (g:log x) (g:log 10)))
  31. (define (g:log2 x)
  32. (g:/ (g:log x) (g:log 2)))
  33. (define (g:exp10 x)
  34. (g:expt 10 x))
  35. (define (g:exp2 x)
  36. (g:expt 2 x))
  37. ;;; See numbers.scm
  38. (define (g:tan x)
  39. (g:/ (g:sin x) (g:cos x)))
  40. (define (g:cot x)
  41. (g:/ (g:cos x) (g:sin x)))
  42. (define (g:sec x)
  43. (g:/ :one (g:cos x)))
  44. (define (g:csc x)
  45. (g:/ :one (g:sin x)))
  46. (define (g:tanh x)
  47. (g:/ (g:sinh x) (g:cosh x)))
  48. (define (g:sech x)
  49. (g:/ :one (g:cosh x)))
  50. (define (g:csch x)
  51. (g:/ :one (g:sinh x)))
  52. (define (g:asinh z)
  53. (g:log (g:+ z (g:sqrt (g:+ :one (g:square z))))))
  54. (define (g:acosh z)
  55. (g:* :two
  56. (g:log (g:+ (g:sqrt (g:/ (g:+ z :one) :two))
  57. (g:sqrt (g:/ (g:- z :one) :two))))))
  58. (define (g:atanh z)
  59. (g:/ (g:- (g:log (g:+ :one z))
  60. (g:log (g:- :one z)))
  61. :two))
  62. (define (g:arg-shift f . shifts)
  63. (define (g . xs)
  64. (g:apply f (map g:+ xs shifts)))
  65. g)
  66. (define (g:arg-scale f . scales)
  67. (define (g . xs)
  68. (g:apply f (map g:* xs scales)))
  69. g)
  70. (define (g:sigma f low high)
  71. (if (fix:> low high)
  72. 0
  73. (let lp ((i (fix:+ low 1)) (sum (f low)))
  74. (if (fix:> i high)
  75. sum
  76. (lp (fix:+ i 1) (g:+ sum (f i)))))))
  77. ;;; The generalized selector:
  78. (define (g:ref x . selectors)
  79. (ref-internal x selectors))
  80. (define* ((component . selectors) x)
  81. (ref-internal x selectors))
  82. (define (ref-internal x selectors)
  83. (cond ((null? selectors) x)
  84. ((procedure? x)
  85. (if (operator? x)
  86. (make-operator (compose (lambda (y)
  87. (ref-internal y selectors))
  88. x)
  89. `(compose (component ,@selectors)
  90. ,(operator-name x))
  91. (operator-subtype x))
  92. (compose (lambda (y)
  93. (ref-internal y selectors))
  94. x)))
  95. (else
  96. (let ((i (car selectors)) (js (cdr selectors)))
  97. (cond ((exact-integer? i)
  98. (cond ((vector? x)
  99. (ref-internal
  100. (vector-ref x (adjust-index i (vector-length x)))
  101. js))
  102. ((structure? x)
  103. (ref-internal (s:ref x (adjust-index i (s:length x)))
  104. js))
  105. ((matrix? x)
  106. (if (null? js)
  107. (cond ((column-matrix? x)
  108. (ref-internal
  109. (matrix-ref x
  110. (adjust-index i (m:num-rows x))
  111. 0)
  112. js))
  113. ((row-matrix? x)
  114. (ref-internal
  115. (matrix-ref x
  116. 0
  117. (adjust-index i
  118. (m:num-cols x)))
  119. js))
  120. (else
  121. (error "Not enuf indices -- REF" x i js)))
  122. (ref-internal
  123. (matrix-ref x
  124. (adjust-index i (m:num-rows x))
  125. (adjust-index (car js)
  126. (m:num-cols x)))
  127. (cdr js))))
  128. ((series? x)
  129. (ref-internal (stream-ref (series->stream x) i) js))
  130. ((stream-pair? x)
  131. (ref-internal (stream-ref x i) js))
  132. ((list? x)
  133. (ref-internal
  134. (list-ref x (adjust-index i (length x)))
  135. js))
  136. ((string? x)
  137. (if (not (null? js))
  138. (error "String has no substructure -- REF" x i js))
  139. (string-ref x (adjust-index i (string-length x))))
  140. (else
  141. (error "Unknown compound -- G:REF" x i))))
  142. ((and (pair? i)
  143. (exact-integer? (car i))
  144. (pair? (cdr i))
  145. (exact-integer? (cadr i)))
  146. (cond ((vector? x)
  147. (ref-internal
  148. (subvector x
  149. (adjust-index (car i) (vector-length x))
  150. (adjust-end (cadr i) (vector-length x)))
  151. js))
  152. ((structure? x)
  153. (ref-internal
  154. (s:structure
  155. (s:type x)
  156. (subvector (s:->vector x)
  157. (adjust-index (car i) (s:length x))
  158. (adjust-end (cadr i) (s:length x))))
  159. js))
  160. ((matrix? x)
  161. (if (null? js)
  162. (cond ((column-matrix? x)
  163. (ref-internal
  164. (m:submatrix x
  165. (adjust-index (car i)
  166. (m:num-rows x))
  167. (adjust-end (cadr i)
  168. (m:num-rows x))
  169. 0
  170. 1)
  171. js))
  172. ((row-matrix? x)
  173. (ref-internal
  174. (m:submatrix x
  175. 0
  176. 1
  177. (adjust-index (car i)
  178. (m:num-cols x))
  179. (adjust-end (cadr i)
  180. (m:num-cols x)))
  181. js))
  182. (else
  183. (error "Not enuf indices -- REF" x i js)))
  184. (ref-internal
  185. (m:submatrix x
  186. (adjust-index (car i) (m:num-rows x))
  187. (adjust-end (cadr i) (m:num-rows x))
  188. (adjust-index (caar js)
  189. (m:num-cols x))
  190. (adjust-end (cadar js)
  191. (m:num-cols x)))
  192. (cdr js))))
  193. ((list? x)
  194. (ref-internal
  195. (sublist x
  196. (adjust-index (car i) (length x))
  197. (adjust-end (cadr i) (length x)))
  198. js))
  199. ((string? x)
  200. (if (not (null? js))
  201. (error "String has no substructure -- REF" x i js))
  202. (substring x
  203. (adjust-index (car i) (string-length x))
  204. (adjust-end (cadr i) (string-length x))))
  205. (else
  206. (error "Unknown compound -- G:REF" x i))))
  207. (else
  208. (error "Unknown selector type -- REF" x i js)))))))
  209. (define (adjust-index i n)
  210. (if (fix:< i 0)
  211. (let ((j (fix:+ n i)))
  212. (if (fix:< j 0)
  213. (error "Bad index -- REF" i))
  214. j)
  215. (begin
  216. (if (not (fix:< i n))
  217. (error "Bad index -- REF" i))
  218. i)))
  219. (define (adjust-end i n)
  220. (let ((n (fix:+ n 1)))
  221. (if (fix:< i 0)
  222. (let ((j (fix:+ n i)))
  223. (if (fix:< j 0)
  224. (error "Bad index -- REF" i))
  225. j)
  226. (begin
  227. (if (not (fix:< i n))
  228. (error "Bad index -- REF" i))
  229. i))))
  230. (define (g:size x)
  231. (cond ((vector? x) (vector-length x))
  232. ((matrix? x) (matrix-size x))
  233. ((structure? x) (s:length x))
  234. ((series? x) #f)
  235. ((stream-pair? x) #f)
  236. ((list? x) (length x))
  237. ((string? x) (string-length x))
  238. (else
  239. (error "Unknown compound -- G:size" x))))
  240. ;;; Generic composition duplicates composition in utils
  241. (define (g:compose . fs)
  242. (define (lp fs)
  243. (cond ((null? (cdr fs)) (car fs))
  244. (else (g:compose-2 (car fs) (lp (cdr fs))))))
  245. (cond ((null? fs) g:identity)
  246. ((null? (cdr fs)) (car fs))
  247. (else
  248. (g:compose-bin (lp (butlast fs))
  249. (car (last-pair fs))))))
  250. (define (g:identity x) x)
  251. (define (g:compose-2 f g)
  252. (cond ((pair? g)
  253. (lambda x
  254. (g:apply f
  255. (map (lambda (gi)
  256. (g:apply gi x))
  257. g))))
  258. (else
  259. (lambda x
  260. (f (g:apply g x))))))
  261. (define (g:compose-bin f g)
  262. (cond ((and (pair? g) (not (structure? g)))
  263. (let ((a
  264. (a-reduce joint-arity
  265. (map g:arity g))))
  266. (cond ((equal? a *at-least-zero*)
  267. (lambda x
  268. (g:apply f
  269. (map
  270. (lambda (gi)
  271. (g:apply gi x))
  272. g))))
  273. ((equal? a *exactly-zero*)
  274. (lambda ()
  275. (g:apply f
  276. (map (lambda (gi)
  277. (gi))
  278. g))))
  279. ((equal? a *at-least-one*)
  280. (lambda (x . y)
  281. (g:apply f
  282. (map (lambda (gi)
  283. (g:apply gi x y))
  284. g))))
  285. ((equal? a *exactly-one*)
  286. (lambda (x)
  287. (g:apply f
  288. (map (lambda (gi)
  289. (gi x))
  290. g))))
  291. ((equal? a *at-least-two*)
  292. (lambda (x y . z)
  293. (g:apply f
  294. (map (lambda (gi)
  295. (g:apply gi x y z))
  296. g))))
  297. ((equal? a *exactly-two*)
  298. (lambda (x y)
  299. (g:apply f
  300. (map (lambda (gi)
  301. (gi x y))
  302. g))))
  303. ((equal? a *at-least-three*)
  304. (lambda (u x y . z)
  305. (g:apply f
  306. (map (lambda (gi)
  307. (g:apply gi u x y z))
  308. g))))
  309. ((equal? a *exactly-three*)
  310. (lambda (x y z)
  311. (g:apply f
  312. (map (lambda (gi)
  313. (gi x y z))
  314. g))))
  315. ((equal? a *one-or-two*)
  316. (lambda* (x #:optional y)
  317. (if (default-object? y)
  318. (g:apply f
  319. (map (lambda (gi)
  320. (gi x))
  321. g))
  322. (g:apply f
  323. (map (lambda (gi)
  324. (gi x y))
  325. g)))))
  326. (else
  327. (lambda x
  328. (g:apply f
  329. (map
  330. (lambda (gi)
  331. (g:apply gi x))
  332. g)))))))
  333. (else
  334. (let ((a (g:arity g)))
  335. (cond ((equal? a *at-least-zero*)
  336. (lambda x
  337. (g:apply f
  338. (list (g:apply g x)))))
  339. ((equal? a *exactly-zero*)
  340. (lambda ()
  341. (g:apply f
  342. (list (g:apply g '())))))
  343. ((equal? a *at-least-one*)
  344. (lambda (x . y)
  345. (g:apply f
  346. (list (g:apply g x y)))))
  347. ((equal? a *exactly-one*)
  348. (lambda (x)
  349. (g:apply f
  350. (list (g:apply g (list x))))))
  351. ((equal? a *at-least-two*)
  352. (lambda (x y . z)
  353. (g:apply f
  354. (list (g:apply g x y z)))))
  355. ((equal? a *exactly-two*)
  356. (lambda (x y)
  357. (g:apply f
  358. (list (g:apply g (list x y))))))
  359. ((equal? a *at-least-three*)
  360. (lambda (u x y . z)
  361. (g:apply f
  362. (list (g:apply g u x y z)))))
  363. ((equal? a *exactly-three*)
  364. (lambda (x y z)
  365. (g:apply f
  366. (list (g:apply g (list x y z))))))
  367. ((equal? a *one-or-two*)
  368. (lambda* (x #:optional y)
  369. (if (default-object? y)
  370. (g:apply f
  371. (list (g:apply g (list x))))
  372. (g:apply f
  373. (list (g:apply g (list x y)))))))
  374. (else
  375. (lambda x
  376. (g:apply f
  377. (list (g:apply g x))))))))))