Share via


Overview: Introduction to Numerical Computing in F#

Applies to: Functional Programming

Authors Yin Zhu

Referenced Image

Get this book in Print, PDF, ePub and Kindle at manning.com. Use code “MSDN37b” to save 37%.

Summary: This article gives a brief summary of numerical computing using F#. It contains references to other documents that discuss the benefits of using F# for numerical computing and it also provides links to articles about several math libraries for .NET and F#.

This topic contains the following sections.

  • Introduction to Numerical Computing in .NET
  • Numerical Libraries for .NET and F#
  • Charting Libraries for F#
  • Summary
  • See Also

This article is associated with Real World Functional Programming: With Examples in F# and C# by Tomas Petricek with Jon Skeet from Manning Publications (ISBN 9781933988924, copyright Manning Publications 2009, all rights reserved). No part of these chapters may be reproduced, stored in a retrieval system, or transmitted in any form or by any means—electronic, electrostatic, mechanical, photocopying, recording, or otherwise—without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.

Introduction to Numerical Computing in .NET

The Microsoft .NET Framework is a great environment for developing desktop applications, web applications, and services, as well as mobile device programming. It is also gaining popularity in numerical computing as the computing paradigm is shifting towards rich, parallel, and cloud-based applications.

Numerical computing applications are no longer developed only in FORTRAN or C. A wide range of modern applications use numerical computing techniques to provide richer user interface or additional functionality. For example, an estore may use machine learning to implement a recommender system and a collaborative application may provide a video chat with face recognition. The .NET Framework is a great tool for developing rich applications like this.

Note

F# is a .NET language that is available in Visual Studio and is fully supported by Microsoft. It has a large community, including professionals working in finance, data mining, and numerical computing in general. F# started as a research project in Microsoft Research Cambridge led by Don Syme but later became a supported product developed in collaboration with Microsoft Developer Division.

Early successful applications of F# included a ranking system for XBox online games named TrueSkill and an ad-rating algorithm developed for the Bing search engine. Other scientific applications that used F# include genome sequencing and financial modeling.

This chapter discusses how to implement numeric algorithms for .NET using the F# language. It also explains why F# is a good language for this task and then shows how to use several numerical libraries that can be used in F#.

Choosing the F# Language

Why is F# suitable for numerical computing? There are two major reasons:

  • Succinctness—The F# language is very succinct, which makes it easy to express complex numerical algorithms. Features like lambda functions and discriminated unions provide excellent abstractions for numerical problems.

  • Types and units of measure—F# is a statically typed language and can catch many errors at compile-time. It also uses types to check units in physical or other numerical calculations.

  • Functional programming—Immutability and other concepts from functional programming make it easier to reason about programs and algorithms.

  • .NET Framework—There is a wide range of existing, high-quality numerical packages for the .NET Framework. Although many of them were made primarily for C#, it is very easy to use them from F#.

  • Interoperability—The .NET Framework makes it very easy to access native libraries, making it possible to call optimized native implementations of standard numerical routines or other native or COM components.

In general, the features described in the list above are either F# language features or functionality provided by the .NET runtime. The following two articles provide more information about F# and .NET interoperability:

Functional programming is in many ways different from imperative style used in languages like C, Python, R, or Matlab. The following articles discuss the key functional concepts and their effect when implementing parallel computations:

Numerical Libraries for .NET and F#

The core part of any numerical programming involves heavy numerical computations, but very few programmers actually need to implement these computations themselves. Most of the calculations including linear algebra, optimization, statistics and probability, and many others are already implemented in an existing library. The most common task in numerical computing is building application-specific layers on top of existing numerical libraries.

As a fully compatible .NET language, F# can use any library that is designed for the .NET Framework. However, there are also several libraries that are specifically designed for F# or that provide helpers that make it easier to use from F#. The following article surveys some of the F# and .NET numerical libraries:

The three of the libraries are discussed in more detail in this chapter. The next three sections summarize them, give a brief example and provide links to more resources.

Math.NET Numerics

Math.NET Numerics is an open-source library that implements a wide range of numerical methods and algorithms. It is designed to be used by any .NET language, but it provides additional extensions for F# that make it very easy to use. The following snippet shows a few simple operations with matrices:

let rnd = new System.Random()
let vec = vector [ for i in 0 .. 99 -> float i / 100.0]
let a = DenseMatrix.init 500 500 (fun _ _ -> rnd.NextDouble())
let a' = a.Inverse()

The library provides a function vector for creating vectors using the F# list comprehension syntax. It also implements a module DenseMatrix that is similar to modules for working with other collections in F# (such as Array2D). The matrix type includes extension methods for a wide range of linear algebra operations. In addition, the library also includes extensive functionality for probabilistic modeling. For more information, visit the project homepage and read a tutorial available in this chapter:

F# PowerPack and F# MathProvider

The F# PowerPack is a library developed by the F# team that implements additional functionality for the F# language. It includes a basic set of math libraries that provide an F#-friendly programming interface. The F# MathProvider is an open-source project that builds on the F# PowerPack types and implements wrappers for native linear algebra libraries. The following snippet demonstrates the F# PowerPack types:

let a = matrix [ [ 1.0; 2.0; 3.0 ]
                 [ 10.0; 20.0; 30.0 ] ];;
a.[0 .., 2 .. 2] <- matrix [ [4.0]; [40.0] ];;

let square = a.[0 .. , 0 .. 1];;
let square' = square.Transpose

The F# PowerPack implements types representing vectors and matrices (for floating point numbers as well as generic). They can be created using the list comprehension syntax and modified using the slicing notation provided by F#. For example, the syntax a.[0 .., 0 .. 1] refers to a part of matrix containing all rows in the first two columns. The types provide basic linear algebra operations, but additional operations are available thanks to the F# MathProvider. More information can be found on the websites of the two projects and in the following tutorial:

Microsoft Sho

Microsoft Sho is a noncommercial computing platform developed by Microsoft Research. It consists of an interactive programming environment based on IronPython and an extensive set of linear algebra and optimization libraries. They can be used from any .NET language including F#:

let a = new DoubleArray(10, 10)
a.FillRandom(new Random(1))
let svd = new SVD(a)
let a' = svd.U * svd.D * svd.V.T

The Sho library implements five specialized matrix types (Boolean, Integer, single float, double float, and complex). The DoubleArray type from the previous snippet is one of them. The linear algebra operations (such as SVD decomposition) are performed using native Intel MKL library licensed from Intel. More information can be found on the project website and in the following tutorial:

Charting Libraries for F#

Another important aspect of working with data is visualization. The easiest option for creating charts in F# is to use a set of classes (Microsoft Chart Controls) that is available in .NET Framework 4.0 (and can be downloaded separately for version 3.5). The library can be used for creating charts interactively using F# Interactive and for embedding charts in Web or Windows applications.

The library follows the object-oriented design. When using C#, charts can also be created using the Windows Forms Designer. F# provides a lightweight wrapper called FSharpChart that makes creating charts significantly easier:

Aside from the Microsoft Chart Controls library and the FSharpChart wrapper, it is also possible to use a wide range of other libraries. The Microsoft Sho library provides its own charting wrapper, and F# applications can easily call, for example, Excel or gnuplot:

To download the code snippets shown in this article, go to https://code.msdn.microsoft.com/Chapter-4-Numerical-3df3edee

Summary

In this overview, we surveyed various language features of F# and their applications in numerical computing. Many language features of F# make writing numerical computing applications easier than conventional languages such as C or FORTRAN. We have also shown the application-rich side of numerical computing using F#.

See Also

This article is based on Real World Functional Programming: With Examples in F# and C#. Book chapters related to the content of this article are:

  • Chapter 10: “Efficiency of data structures” explains how to write efficient programs in F#. The chapter covers advanced functional techniques and explains how and when to use arrays and functional lists.

  • Book Chapter 12: “Sequence expressions and alternative workflows” contains detailed information on processing in-memory data (such as lists and seq<'T> values) using higher-order functions and F# sequence expressions.

  • Book Chapter 13: “Asynchronous and data-driven programming” explains how asynchronous workflows work and uses them to write an interactive script that downloads a large dataset from the Internet.

Previous article: How to: Access User Interface Elements Dynamically

Next article: Writing Succinct and Correct Numerical Computations with F#