tag:blogger.com,1999:blog-130737952015-01-27T08:23:12.933-05:00SchemingProgramming notes and ideas from an astrophysicist.Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.comBlogger80125tag:blogger.com,1999:blog-13073795.post-40769456384435545642010-06-04T18:30:00.003-04:002010-06-04T18:50:15.570-04:00Loop-Unrolling Macros in ClojureI've been fooling around with clojure a bit lately. Something fun: a dotimes-like macro, but it completely unrolls your loop at compile time:
(defmacro dotimes-unroll
"Just like dotimes, but completely unrolls the given loop. The
number of iterations must read as a number."
[[i n] & body]
(assert (number? n))
(if (= n 0)
`nil
`(let [~i ~(- n 1)]
(dotimes-unroll [~i ~(- nWill Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-69963139054957517562010-03-04T17:47:00.003-05:002010-03-04T17:48:55.049-05:00Distribution for the Ratio of Two Uniform DeviatesWeird: the distribution for the ratio of two uniform deviates is constant for ratios smaller than 1, and falls like r^2 for larger ratios. p(r) = 1/2 if r < 1, and 1/(2r^2) if r >= 1 when r = a/b, where a,b ~ U(0, A). What's up with that?Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-53869859364028797262010-01-09T22:36:00.002-05:002010-01-09T22:45:58.317-05:00Another Library: 1-D Root Finding in Ocaml...and here's another library for 1-D root finding (non-linear equation solving) in OCaml. Get it with git clone git://github.com/farr/ocaml-solve.git I'm particularly proud of this one for two reasons:
All the algorithms, even the ones requiring derivatives, use bracketing. If a step in the algorithm would fall outside the brackets for the root, the algorithms perform a bisection step Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-72884214015769862212010-01-09T18:12:00.003-05:002010-01-09T18:19:02.945-05:00Polynomials in OCamlI just posted some code on GitHub to manipulate polynomials in one variable in OCaml. It's pretty simplistic, and only works for polynomials whose coefficients are floating-point real numbers (so beware of roundoff error!), but I have found it to be useful in many quick-and-dirty situations. To grab it, use git clone git://github.com/farr/ocaml-poly.gitWill Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-83010651847483173872009-01-08T13:13:00.005-05:002009-01-08T13:39:34.230-05:00Profiling Using MzScheme and Errortrace
Lately I've needed to do some profiling in PLT Scheme, and the usual process in DrScheme hasn't been working for me. If you don't know, here's the usual process:
Language --> Choose Language
Show Details
Debugging and Profiling
Run Program
View --> Show Profile
Robby suggested using the errortrace library directly from mzscheme. It worked perfectly. Here's how you can duplicate my successWill Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com1tag:blogger.com,1999:blog-13073795.post-4767235355529759862008-12-03T13:05:00.002-05:002008-12-03T13:36:18.632-05:00Interpolation in OCaml
I just wrote some code to do polynomial interpolation in O'Caml. The full darcs repository is at http://web.mit.edu/farr/www/onumerics/; here's the guts of the code. Eventually (depending on my time and level of continued interest), this may turn into a more general O'Caml numerical library. As usual, it's released under the GPL.
(* interp.ml: Interpolation.
Copyright (C) 2008 WillWill Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-18898203569579506612008-06-11T20:20:00.001-04:002008-06-11T20:30:29.307-04:00Very Basic Matrix Library for PLT Scheme
I just uploaded to PLaneT a very simple matrix library. The library provides a matrix datastructure, iterators over matrices and vectors, algebraic operations on matrices, and matrix-vector and matrix-matrix products. There is also an initial module import language which provides all of scheme except the +, -, * and / operations, which instead are generalized for all reasonable combinations ofWill Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com2tag:blogger.com,1999:blog-13073795.post-71135487866026422712007-09-07T16:10:00.000-04:002007-09-07T16:17:17.053-04:00Quick Note on Continuations to comp.lang.schemeI just posted a quick note on continuations to c.l.s. I'm afraid it's pretty basic, and doesn't really do the topic justice, but maybe the original poster will find it helpful. I'm noting it here because no ever seems to explain continuations the way I did in my note (at least, I haven't seen any explanations like this), which uses "return points" to illustrate what continuations do. I don't Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com3tag:blogger.com,1999:blog-13073795.post-55877220854451570982007-08-15T10:57:00.000-04:002007-08-15T11:25:58.505-04:00One More Reason to Love Module Systems and Hygiene
I just released an Automatic Differentiation PlaneT Package, and am now converting my numerical relativity code to use it. The package exports procedures like +, -, =, etc, which are modified from the standard ones so it can compute derivatives of numerical procedures which use them. Of course, these modified procedures are slower than their standard cousins.
My numerical relativity code Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com1tag:blogger.com,1999:blog-13073795.post-60247877766834392682007-08-15T10:38:00.000-04:002007-08-15T10:44:36.373-04:00Automatic Differentiation PlaneT Package ReleasedI just released a PlaneT Package which, when used as a PLT Scheme module's initial language import, allows to compute derivatives of functions written in the module exactly. (I've written about the technique here and in the posts which follow.) I'm going to use this code to compute some derivatives of gravitational actions in code which would be extremely complicated to do by hand. Enjoy!Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-1037657343709701802007-08-08T09:13:00.000-04:002007-08-08T14:20:19.069-04:00Persistency and Lazy Memoization in a Pairing Heap
I just released a pairing heap PlaneT package. Because the datastructure is both persistent and has amortized space bounds, it requires a neat trick to implement, which I will discuss below.
A pairing heap is an adaptive datastructure for a collection of objects which supports the following operations [edit: You can also read about an alternative implementation here in the Scheme Cookbook.Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com3tag:blogger.com,1999:blog-13073795.post-39527566680760883252007-08-06T13:10:00.000-04:002007-08-06T13:19:05.928-04:00I Just Voted "Yes" on R6RS
Yep, I did. I'm including my explanation below, because I think that R6RS has come under a lot of fire lately. (By the way, for those who say, "why doesn't the standard come with a small base, and then lots of libraries attached to it?", it does! <snark>Try reading it, why don't you.</snark>)
Even if you don't agree with my choice, don't forget to vote if you signed up (just one of many Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com1tag:blogger.com,1999:blog-13073795.post-89760184315594476052007-07-23T16:32:00.000-04:002007-07-23T16:43:55.471-04:00A Question (and an Answer, sort of) About Scheme FFIs
Recently, I received the following email from Raoul Duke:
hi,
I'm trying to learn up on what Scheme has the easiest FFI for talking
to C (more ideally C++, but I assume nobody really does that). I've
been reading up and it seems like PLT isn't bad. I came across your
blog - might you have any opinions or recommendations about PLT vs.
Chicken vs. Bigloo vs. Gambit vs. etc.?
many thanks!
Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com5tag:blogger.com,1999:blog-13073795.post-82665639295205620722007-06-17T13:10:00.000-04:002007-06-17T13:47:51.116-04:00N-Body Utilities Coming Real Soon Now (TM)
It's been a while since I posted here, but never fear: I've been busy. I've been working on an updated set of N-Body utilities for another paper examining the efficiency of our algorithm (see astro-ph/0611416, to be published in ApJ 663 (2007) 1420) relative to drift-kick-drift leapfrog in a treecode. I've got code up and running, and am beginning simulations to compare the two algorithms. Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com2tag:blogger.com,1999:blog-13073795.post-74788973370516285092007-04-29T14:55:00.000-04:002007-04-29T15:07:34.841-04:00Pure Scheme Code for SO(3,1)
Jens Axel Soegaard wrote a comment on my last post asking for the Scheme code for comparison, so here it is. Note that the code provides a (moderately efficient, but conses too much) matrix inversion routine in pure Scheme (you want to look at the matrix module, for matrix-inverse)---some people may find that useful independently. Sorry it's so long, but there's a lot of background stuff to Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com5tag:blogger.com,1999:blog-13073795.post-80493718251980392912007-04-28T21:16:00.000-04:002007-04-28T22:00:43.835-04:00Why Use the FFI for Simple Numerical Operations in MzScheme?
Brad Lucier (of Numerical Partial Differential Equations in Scheme fame) posted a comment on my last post announcing the code for numerical manipulation of Lorentz group elements which reads:
I'm surprised that you decided to use the C FFI to manipulate 4x4 matrices; they are so small that it would seem better just to use mzscheme's numerical facilities which are getting better all the time.
Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com4tag:blogger.com,1999:blog-13073795.post-31550757011296532922007-04-28T18:03:00.000-04:002007-04-28T18:16:29.691-04:00SO(3,1) (Lorentz Group) Manipulations from PLT Scheme
I just released a PlaneT package which manipulates elements of the Lorentz group (here). Get it using
(require (planet "SO31.ss" ("wmfarr" "SO31.plt" 1 0)))
At the moment, it only runs on Mac OS 10.3 or 10.4, ppc, because it uses some C code I wrote (for efficiency), and I haven't had the patience to understand all the hoops I need to jump through to get PLT Scheme to compile this to a Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com2tag:blogger.com,1999:blog-13073795.post-37703233525159770612007-04-26T14:45:00.000-04:002007-04-26T14:54:46.051-04:00Linear Algebra in PLT Scheme
Last night, I posted a library which uses BLAS and LAPACK from within PLT Scheme to perform simple numerical linear algebra.
In doing so, I've learned a lot about the FFI for PLT Scheme. The conclusion: it's great! In particular, the "custom _fun types", discussed here are tremendously helpful! Particularly when interfacing to Fortran libraries, where every argument to a function must be Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com1tag:blogger.com,1999:blog-13073795.post-35461782757440093232007-04-09T11:50:00.000-04:002007-04-09T11:53:54.552-04:00Pearls Before BreakfastNothing to do with Scheme, but a really neat article here in the Washington Post. I'm really busy now, but I'm glad I took the time out to read this article. Found via the fascinating blog of Terrence Tao.Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-79104041609424229182007-03-03T20:31:00.000-05:002007-03-03T20:32:36.457-05:00Eager Comprehensions for ArraysI just submitted a package to PlaneT for eager comprehensions (SRFI-42) for arrays (SRFI-25). It's a pretty short module, so I've also pasted it below.
#| array-ec.ss: SRFI-42 comprehensions for SRFI-25 arrays.
Copyright (C) 2007 Will M. Farr
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-10984533806090793342007-02-18T22:20:00.000-05:002007-02-18T22:30:52.052-05:00Barnes-Hut Tree Code Submitted to PlaneTI've just submitted some new code to PlaneT which implements Barnes-Hut Trees in PLT Scheme. It's surprisingly (to me) fast, creating a 100K-body tree in about 30 seconds on my old PowerBook G4 800 Mhz.
This is just another step in my code clean-up in preparation for comparing our algorithm to the standard leapfrog algorithm of cosmological codes.Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com1tag:blogger.com,1999:blog-13073795.post-1568949992733788622007-02-18T03:07:00.000-05:002007-02-18T03:23:50.259-05:00N-Body Simulation Initial ConditionsI just packaged up some code for PlaneT which creates gravitational N-body initial conditions. I assume it'll appear in the next few days. It's not very extensive as of yet (only does cold and hot constant spatial density spherical distributions and the venerable Plummer model), but that's really all I've needed over the past year or so to test my algorithms.
The paper is undergoing its final Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-1163628047998776272006-11-15T17:00:00.000-05:002006-11-15T17:00:48.656-05:00The Paper is Out!
It's out! The paper of the century---get your copy before they're sold out at the newsstand. (If you're into N-body simulation methods, that is.)
Seriously: I'm very glad to have it out the door. The ball is now in ApJ's court, and I can get back to focusing on my thesis (for which I just submitted a title: "Numerical Relativity from a Gauge Theory Perspective").
Section 6.2.1 uses Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-1162950249860043092006-11-07T20:36:00.000-05:002006-11-07T20:44:10.386-05:00SRFI-42 Comprehensions for SRFI-4 Vectors
I spend quite a bit of time working with SRFI-4 homogeneous numeric vectors. Below is some code which adds some SRFI-4-vector generators and comprehensions to SRFI-42's Eager Comprehensions. I am impressed at the modularity in SRFI-42!
#|
Derived (almost verbatim) from the code for vector comprehensions in the SRFI-42 reference implementation. The copyright on that code is:
Copyright (C) Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0tag:blogger.com,1999:blog-13073795.post-1161710180745700422006-10-24T13:16:00.000-04:002006-10-24T13:17:35.096-04:00Updated Scheme Code for Derivatives
I've updated my functional differential geometry code to use the same techniques I've been writing about these past few days in OCaml for computing derivatives. It should (haven't rigorously tested this) improve the performance of computing structure constants (which involve the commutator of two tangent vectors in the group, and therefore taking second derivatives, and therefore multiple tags Will Farrhttp://www.blogger.com/profile/11756898910041903896noreply@blogger.com0