Tuesday, 26 May 2009

Downloadable demos from the F#.NET Journal

Articles published in the F#.NET Journal often culminate in fun graphical programs. Consequently, we have begun the process of uploading demos that everyone can download and enjoy. The first such demo, from the article Real-time Finite Element Materials simulation (15th June 2008), is now freely available for download from the F#.NET Journal web page.

This demo simulates a simple 2D material using finite element methods and the flexible object can be grabbed and pulled with the mouse in order to deform and even smash it:

The entire self-contained F# program is under 200 lines of code and uses Windows Presentation Foundation for easy and efficient real-time visualization.

Friday, 22 May 2009

F# for Numerics and F# for Visualization

We have completed the updates of our F# for Numerics and F# for Visualization libraries to work with the latest F# May 2009 CTP ( and the results are now available to our beta release scheme subscribers.

Although the latest version of F# contains many improvements and breaking changes, the updates turned out to be relatively straightforward and the resulting libraries passed all tests first time. Congratulations to the F# team for such a polished release! We recommend that all users upgrade to the latest F# for a better development experience.

Thursday, 21 May 2009

Pythagoras Tree

A Pythagoras tree is a simple vector graphics image:

The following F# program uses the F# for Visualization library to render this image:


open System.Windows.Media
open FlyingFrog.Graphics

let pi = System.Math.PI

let rotate t = RotateTransform(t / pi * 180.).Value

let branches m =
[ m * rotate(pi/2. - asin(4. / 5.)) * scale(4. / 5., 4. / 5.) *
translate(0., 1.);
m * translate(-1., 0.) * rotate(-pi/2. + asin(3. / 5.)) *
scale(3. / 5., 3. / 5.) * translate(1., 1.) ]

let line_loop xys = Contour.Closed [ for x, y in xys -> line_to x y ]

let square = line_loop [1., 0.; 1., 1.; 0., 1.; 0., 0.]

let shape brush m =
Shape([ Interior brush;
Stroke(Brushes.DarkGreen, { width = 0.01 }) ],
[ Contour.map (fun p -> p * m) square ])

let trunks brush ms = Group(List.map (shape brush) ms)

let rec tree n ms =
if n=0 then [trunks Brushes.Green ms] else
let ms' = List.collect branches ms
trunks Brushes.BurlyWood ms :: tree (n-1) ms'

let view = View(Group[])
for n = 1 to 12 do
view.Scene <- Group(tree n [Matrix.Identity])

Download and run the demo!.

F# released

Don Syme has announced the release of a new version of F# that replaces the old F# September 2008 CTP. This new release brings F# much more into alignment with the rest of .NET 4, in preparation for the first full product release of F# in Visual Studio 2010. The release is available either as part of the latest Visual Studio 2010 beta or as a separate F# May 2009 CTP that works with Visual Studio 2008.

Many of the improvements in the new version of F# are breaking changes that require existing software to be updated, such as the renaming of basic functions. For example, the List.fold_right function from OCaml has been renamed List.foldBack. We are in the process of bringing our F# for Visualization and F# for Numerics libraries up to date with respect to the latest F# CTP. Our forthcoming book F# for Technical Computing will not only cover the latest version of F# but also all of the latest library versions and new techniques for integration and interoperability.

F# for Numerics released

F# for Numerics has just been released. This includes a wealth of new features and improvements such as:

  • Numerical integration.
  • Bessel functions.
  • Skewness and kurtosis.
  • Series: natural numbers, Fibonacci, primes.
  • Seeding of Mersenne Twister with a uint32.
  • Constants: epsilon_float32 and delta32.

Subscribe to our beta release scheme today!

Wednesday, 20 May 2009

F# for Visualization released

F# for Visualization has just been released. This includes bug fixes, performance improvements and new examples.

Purely functional data structures: real-time queues and catenable lists

The F#.NET Journal just published an article about data structures:

"This is the second article in a series describing the design and implementation of some purely functional data structures. Real-time queues and catenable lists that provide reliable O(1) time complexity operations are described in detail. In particular, laziness is used to ensure that operations are performed efficiently even when the data structures are used persistently. This culminates in the creation of some useful new data structure implementations...."

To read this article and more, subscribe to The F#.NET Journal today!

Monday, 11 May 2009

Nice F# screencast by Jason Olson

Jason Olson has done a great beginners introduction to F# in the form of a video of him walking the viewer through F# using an interactive session.

In particular, Jason explains many of the basic features of the F# language that surprise beginners who have never seen an interactive statically-typed functional programming language before. This is recommended viewing for anyone struggling with basic features like patterns and curried functions.

Thursday, 7 May 2009

F# as a next-generation platform for high-performance interactive technical computing

We are now in the throws of writing a new book on F# called F# for Technical Computing that is due out in a few months. Amongst other things, we are generating diagrams for the book using our own F# for Visualization library and making extensive use of our F# for Numerics library and Microsoft's excellent Parallel Extensions to .NET Framework 3.5 June 2008 CTP in order to build a next-generation platform for high-performance interactive technical computing.

The following screenshot shows the real-time visualization of performance measurements over the array, list and set data structure implementations provided with the F# September 2008 CTP:

There are several interesting aspects to this F# program:

  • Individual benchmarks for a given data structure are executed in parallel in order to obtain results faster without artificially degrading the measurements for other data structures via GC load.
  • The parallelism is so effective that 90% CPU usage is sustained across all 8 cores.
  • Parallel threads update the visualizations concurrently in real time safe in the knowledge that F# for Visualization handles all thread-related issues automatically and efficiently.
  • The randomized algorithms in the benchmarks are fuelled by the high-performance Mersenne Twister implementation in our F# for Numerics library.
  • The entire program is under 80 lines of code.
  • The resulting high-fidelity diagrams are ideal for inclusion in quality printed literature such as our full-color books.

Here is the entire source code:

open FlyingFrog
open FlyingFrog.Graphics
open FlyingFrog.Graphics.Typeset
let log2 x =
log x / log 2.0
let plot_mem =
Plot([], (0., 20.), (-25., 0.0),
Labels=(Char 'n', Sub(Char 't', Text "mem")))
let plot_mem_rel =
Plot([], (0., 20.), (0., 20.0),
Labels=(Char 'n', Fraction(Sub(Char 't', Text "set"), Sub(Char 't', Text "array"))))
let plot_iter =
Plot([], (0., 20.), (-30., -20.0),
Labels=(Char 'n', Sub(Char 't', Text "iter")))
let plot_foldl =
Plot([], (0., 20.), (-30., -20.0),
Labels=(Char 'n', Sub(Char 't', Text "foldl")))
let plot_foldr =
Plot([], (0., 20.), (-30., -20.0),
Labels=(Char 'n', Sub(Char 't', Text "foldr")))
let tss =
[for plot in 1 .. 4 ->
[for i in 1 .. 3 ->
[for i in 0 .. 101 -> float i, infinity]]]
let iters = 4001
let inline time f n m =
let rand = Random.MersenneTwister()
let t = System.Diagnostics.Stopwatch.StartNew()
let mutable j = 0
while t.ElapsedMilliseconds < 100L do
for k=0 to j do
f (abs(rand.int()) % n) m
j <- j + 1 float t.ElapsedMilliseconds / 1e3 / float j
let inline run d f mem iter foldl foldr =
System.Threading.Parallel.For(0, iters, fun i' ->
let i = iters * i' % 101
let n = 2. ** (float i / 100. * 20.) > int
let m = f n
let t' = time mem n m > log2
if snd tss.[0].[d].[i] > t' then
tss.[0].[d].[i] <- log2(float n), t'
for f, tss in [iter, tss.[1]; foldl, tss.[2]; foldr, tss.[3]] do
let t' = (time f n m / float n) > log2
if snd tss.[d].[i] > t' then
tss.[d].[i] <- log2(float n), t'
if i' % 100 = 0 then
plot_mem.Data <- [for ts in tss.[0] -> Data ts]
plot_mem_rel.Data <-
[Data(Array.map2 (fun (i, ts) (_, ta) -> i, 2. ** ts / 2. ** ta)
tss.[0].[0] tss.[0].[2])]
plot_iter.Data <- [for ts in tss.[1] -> Data ts]
plot_foldl.Data <- [for ts in tss.[2] -> Data ts]
plot_foldr.Data <- [for ts in tss.[3] -> Data ts]
let inline array_mem x (a: _ array) =
let rec aux i =
i < Array.length a && (a.[i] = x aux(i+1))
aux 0
run 2 (fun n -> Array.init n (fun i -> float i))
(fun n m -> ignore(array_mem (float n) m))
(fun n m -> Array.iter ignore m)
(fun n m -> Array.fold_left ( + ) 0.0 m > ignore)
(fun n m -> Array.fold_right ( + ) m 0.0 > ignore)
run 0 (fun n -> set (seq {for i in 0 .. n-1 -> float i}))
(fun n m -> ignore(Set.mem (float n) m))
(fun n m -> Set.iter ignore m)
(fun n m -> Set.fold_left ( + ) 0.0 m > ignore)
(fun n m -> Set.fold_right ( + ) m 0.0 > ignore)
run 1 (fun n -> [for i in 0 .. n-1 -> float i])
(fun n m -> ignore(list_mem (float n) m))
(fun n m -> List.iter ignore m)
(fun n m -> List.fold_left ( + ) 0.0 m > ignore)
(fun n m -> List.fold_right ( + ) m 0.0 > ignore)

Monday, 4 May 2009

Re: Microsoft canning F#

We went to Cambridge to meet Don Syme last week and attended a great lecture by Tom Wickham-Jones from Wolfram Research. I spoke to Don about the concerns among some of our customers regarding the future of F# and he assured me that they are focused entirely on forthcoming releases with a schedule that spans years. The next release, beta 1, is due out this month and a beta 2 is scheduled for release before the full 2010 release next year.

Purely functional data structures: streams and batched queues

The F#.NET Journal just published an article about data structures:

"This article is the first in a series describing the design and implementation of some very useful purely functional data structures. Lazy evaluation, lazy streams and batched queues are described in detail. In particular, new .NET interfaces, classes and objects are used factor the implementations of the data structures and various built-in interfaces are implemented in order to support equality, comparison and hashing of these new data structures...."

To read this article and more, subscribe to The F#.NET Journal today!