[racket] Function composition in Racket

From: Neil Toronto (neil.toronto at gmail.com)
Date: Tue Oct 16 16:35:41 EDT 2012

On 10/16/2012 12:02 PM, Michael Wilber wrote:
> Does surface3d and isosurface3d from racket/plot do what you want?
>
> file:///usr/share/racket/doc/plot/renderer3d.html?q=isosurface#(def._((lib._plot/main..rkt)._isosurface3d))

In particular:

#lang racket

(require plot)

(define (f x y)
   (+ 2 (* 2 x) (* 5 y) (* 1/2 x y y) (* 6 x x)))

(plot3d (surface3d f -1 1 -1 1))
(plot3d (contour-intervals3d f -1 1 -1 1 #:label "f(x,y)"))

(define (g x y z)
   (+ (* x y) (* y z) (* z x)))

(define c 0.25)

(plot3d (isosurface3d g c -1 1 -1 1 -1 1))
(plot3d (isosurfaces3d g -1 1 -1 1 -1 1 #:label "g(x,y,z)"))


Isosurfaces are visualized using marching cubes, whose patent finally 
ran out a few years ago.

> Gregory Woodhouse <gregwoodhouse at me.com> writes:
>> I'm intrigued. I suppose pattern based macros could be used to implement
 >> operations like + and * [...]

I was thinking more generics. Generic `+' and `*' could evaluate 
polynomials at runtime, and generic `syntax+' and `syntax*' could emit 
code that does the same.

>> and passing to the field of quotients should  formally be no different
 >> from rational arithmetic.

Exactly. Reducing polynomial fractions in a generic way might be tricky, 
but it looks fun. :)

>> Are you interested in  Chebyshev polynomials for a particular reason
 >> (e.g, applications to differential equations) or as part of a more
 >> general mathematics library?

Both. Chebyshev polynomials have some nice properties when used to 
approximate non-polynomials; in particular, they're really close to 
minimax-error approximations. `math/special-functions' already uses a 
private Chebyshev polynomial implementation extensively. For many R->R 
functions, I only have to chop up their domains and plonk a Chebyshev 
approximation down for each piece.

Chebyshev and other orthogonal polynomial bases come up repeatedly in 
solutions to differential equations. Multidimensional polynomials can be 
represented by sparse polynomials over other polynomial rings. When I 
considered making the private polynomial API public, I studied up on 
these things a bit, and realized it would be cleaner, more likely 
correct, and more generally useful if I parameterized polynomials over 
arbitrary rings and bases.

 >> What would be interesting is yo have s nice way of representing
 >> algebraic structures in Racket so that standard constructs like
>> algebraic extensions such as Q(i) don't have to be strongly coupled
 >> with the definition of the base ring/field. Maybe I'm asking for
 >> functors (in the mathematical sense) and categories.

That's exactly what I need. I had been thinking I'd wait until generics 
found their way into Typed Racket. But I might try defining them untyped 
(where generics work right now) and importing them using 
`require/typed'. I have no idea whether it will work. :D

Neil ⊥


Posted on the users mailing list.