» Publishers, Monetize your RSS feeds with FeedShow: More infos (Show/Hide Ads)

Date: Thursday, 12 Sep 2013 23:07

The F# Journal just published an article about Android development:

*"Xamarin's products can be used to run F# applications on non-Windows operating systems including Android and iOS. This article covers the use of MonoDroid to run an F# program on an Android device such as a Google Nexus table. The program is the same Stanford Bunny demo from a previous article ported to OpenGL ES 1.1..."**To read this article and more, subscribe to The F# Journal today!*

Date: Wednesday, 11 Sep 2013 12:27

The following program uses our F# for Visualization library to draw a color wheel:

open System.Windows

open FlyingFrog.Graphics

let brush(r, g, b) =

let f x = 256.0 * x |> max 0.0 |> min 255.0 |> byte

Media.SolidColorBrush(Media.Color.FromRgb(f r, f g, f b))

let polygon brush xys =

Shape([Interior brush], [ Contour.Closed[for x, y in xys -> line_to x y] ])

let wheel m n =

let a = System.Math.PI / float n

let ka, sa = cos a, sin a

let ta = (ka + sa) / (ka - sa)

let d = ka + sa * ta

let m = float m

Group

[ for e in 0.0..m-1.0 do

let r = 1.0 / (ka - sa) ** e

for i in 0..n-1 do

let t = a * (float(2*i) + e)

let k, s = r * cos t, r * sin t

yield

polygon (brush(float i / float n, 0.5*(1.0 + e/m), 0.5*(2.0 - e/m)))

[ k, s

d*k*ka - d*s*sa, d*s*ka + d*k*sa

ta*k, ta*s

d*k*ka + d*s*sa, d*s*ka - d*k*sa ] ]

View(wheel 12 16)

Date: Tuesday, 10 Sep 2013 15:07

The Fast Fourier Transform is the most important spectral method in computing and lies at the heart of many common algorithms including compressed formats for image, video and sound.

The following is a simple 15-line implementation of the FFT written in F#:

open System.Numerics

let fft a =

let rec loop n a =

if n < 2 then Array.init n a else

let e = loop (n/2) (fun i -> a(2*i))

let o = loop (n/2) (fun i -> a(2*i + 1))

let a = Array.create n Complex.Zero

let f k k' =

let t = 2. * System.Math.PI * float k / float n

a.[k] <- complex="" cos="" e.="" k="" o.="" o:p="" sin="" t="">

for k=0 to n/2 - 1 do

f k k

for k=n/2 to n - 1 do

f k (k - n/2)

a

loop (Array.length a) (Array.get a)

Note that this implementation works only for inputs with an integral-power-of-two number of samples. For a professional high-performance solution that handles any size input please buy our F# for Numerics software library.

The following array of complex numbers represents the spectrum of a low-frequency cosine wave:

let zs =

[|0; 1; 0; 0; 0; 0; 0; 1|]

|> Array.map (fun x -> Complex(float x, 0.0))

In order to make the output from F# Interactive comprehensible we shall provide our own custom pretty printer for complex numbers. Firstly, we wish to truncate small floating point numbers to zero:

let chop x =

if abs x < 1e-5 then 0.0 else x

Now we can write a function to convert the chopped real and imaginary components of a complex number into a string, skipping any zeros:

let stringOfComplex (z: Complex) =

match chop z.Real, chop z.Imaginary with

| 0.0, 0.0 -> "0"

| 0.0, y -> sprintf "%fi" y

| x, 0.0 -> sprintf "%f" x

| x, y -> sprintf "%f + %fi" x y

This function may be registered as the pretty printed for complex numbers with F# Interactive as follows:

fsi.AddPrinter stringOfComplex

The output of our fft function is then:

fft zs

val it : Complex [] =

[|2.000000; 1.414214; 0; -1.414214; -2.000000; -1.414214; 0; 1.414214|]

This is one complete cycle of a cosine wave (the lowest possible frequency with this sampling) with amplitude two. Note the appearance of 2×cos(pi/4) = √2.

Date: Monday, 09 Sep 2013 23:49

These ten one-liners are trending in other languages so here is an F# translation:

Multiply each item in a list by two

[for n in 1..10 -> 2*n]

Sum a list of numbers

Seq.sum [1..1000]

Verify if word exists as a substring in a string

Seq.exists tweet.Contains wordList

Read a file as a single string or line-by-line

System.IO.File.ReadAllText "data.txt"

System.IO.File.ReadLines "data.txt"

Happy birthday to you

for i in 1..4 do printfn "Happy Birthday %s" (if i=3 then "dear NAME" else "to You")

Filter list of numbers

let failed, passed = List.partition ((>) 60) [49; 58; 76; 82; 88; 90]

Load XML

#r "System.Xml.Linq"; System.Xml.Linq.XDocument.Load "data.xml"

Find min/max of list of numbers

Seq.min [14; 35; -7; 46; 98]

Seq.max [14; 35; -7; 46; 98]

Parallel processing

Array.Parallel.map ((*) 2) [|1..100|]

Primes

Seq.filter (fun i -> Seq.forall (fun j -> i%j<>0) [2..i-1]) [2..50]

Date: Monday, 09 Sep 2013 12:06

A new version of our F# for Visualization library has been released. This library makes it easy to create interactive 2D and 3D graphs using F# including mathematically typeset labels and annotations.

Watch this tutorial video and see the pictures below:

A labelled plot of a sine wave:

Plot([Function sin; Data xys], (-6., 6.), Labels=(Text "x", Text "sin(x)"))

F# for Visualization provides a handy View function that tries to visualize any value of any type, such as an F# PowerPack matrix of arbitrary-precision rational numbers:

let A =

Matrix.Generic.ofSeq

[ for i in 0N..4N ->

[ for j in 0N..4N ->

1N / (i + j + 1N) ] ]

View A

In combination with mathematical typesetting, this allows us to build up and visualize complicated expressions:

View(Row[math A; Symbols.Times; math(inverse A); math "="; math(A*inverse A)])

Arbitrary 2D vector graphics can be drawn, such as the following doodle:

let styles = [ Stroke(Brushes.Red, {width = 0.001}) ]

let geometry =

let n = 50

[ for i in 0 .. n do

for j in 0 .. 1 do

let x, y = float i / float n, float j

yield Contour.Open(Point(x, y), [line_to y (1. - x)]) ]

View(Shape(styles, geometry))

Using a vector graphic as an Epilog allows it to be rendered with axes:

Plot([], (0., 1.), (0., 1.), Epilog=scene)

Plot a function of two variables in 3D:

let f x z =

let r = 5. * sqrt(x*x + z*z)

sin(r + 3. * x) / r

Plot3D(f, (-5., 5.), (-5., 5.), (-0.1, 1.))

Visualize a predefined mesh:

View(Shape3D(Polyhedra.Icosahedron.mesh, Brushes.White))

And many more features...

Watch this tutorial video and see the pictures below:

A labelled plot of a sine wave:

Plot([Function sin; Data xys], (-6., 6.), Labels=(Text "x", Text "sin(x)"))

F# for Visualization provides a handy View function that tries to visualize any value of any type, such as an F# PowerPack matrix of arbitrary-precision rational numbers:

let A =

Matrix.Generic.ofSeq

[ for i in 0N..4N ->

[ for j in 0N..4N ->

1N / (i + j + 1N) ] ]

View A

In combination with mathematical typesetting, this allows us to build up and visualize complicated expressions:

View(Row[math A; Symbols.Times; math(inverse A); math "="; math(A*inverse A)])

let styles = [ Stroke(Brushes.Red, {width = 0.001}) ]

let geometry =

let n = 50

[ for i in 0 .. n do

for j in 0 .. 1 do

let x, y = float i / float n, float j

yield Contour.Open(Point(x, y), [line_to y (1. - x)]) ]

View(Shape(styles, geometry))

Using a vector graphic as an Epilog allows it to be rendered with axes:

Plot([], (0., 1.), (0., 1.), Epilog=scene)

Plot a function of two variables in 3D:

let f x z =

let r = 5. * sqrt(x*x + z*z)

sin(r + 3. * x) / r

Plot3D(f, (-5., 5.), (-5., 5.), (-0.1, 1.))

Visualize a predefined mesh:

View(Shape3D(Polyhedra.Icosahedron.mesh, Brushes.White))

And many more features...

Date: Monday, 09 Sep 2013 12:06

A version of our F# for Numerics library optimized for .NET 4.x has been released. New features include:

Existing customers can download the latest .NET assembly and an F# script providing extensive worked examples for free.

As always, the new features are designed for ease of use and integrate seamlessly with our F# for Visualization library. Convex hull:

Delaunay triangulation:

Linear least squares best fit of a quadratic through a sine wave:

- Linear least-squares regression (curve fitting).
- 2D convex hulls.
- Delaunay triangulation.
- k-means algorithm for machine learning with example data.
- Longest common subsequence and Levenshtein edit distance.
- Root finding.
- Parallel aggregates: mapReduce, reduce, minBy, maxBy, tryPick, tryFindIndex, exists and forall.

Existing customers can download the latest .NET assembly and an F# script providing extensive worked examples for free.

As always, the new features are designed for ease of use and integrate seamlessly with our F# for Visualization library. Convex hull:

Delaunay triangulation:

Linear least squares best fit of a quadratic through a sine wave:

Date: Tuesday, 03 Sep 2013 01:30

The F#.NET Journal just published an article about optical character recognition:

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

*"Optical character recognition is a family of algorithms designed to convert scanned images of text into sequences of characters. This article describes a simple technique to segment an image of hand-written numbers into individual digits and use a simple metric to identify each digit..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Thursday, 29 Aug 2013 13:34

.NET 4 introduced the System.Numerics namespace that includes the BigInteger and Complex number types. The BigInteger type can be used to construct a Rational type that can represent arbitrary-precision rational numbers. A simple implementation that just exposes construction with cancellation of the greatest common divisor (GCD) from numerator and denominator as well as the four basic arithmetic operations is remarkably elegant when written in F#:

type Rational(p: BigInteger, q: BigInteger) =

let rec gcd a (b: BigInteger) =

if b.IsZero then a else

gcd b (a % b)

let fixSign(p: BigInteger, q: BigInteger) =

if q.Sign > 0 then p, q else -p, -q

let p, q =

if q.IsZero then raise(System.DivideByZeroException())

let g = gcd q p

fixSign(p/g, q/g)

member __.Numerator = p

member __.Denominator = q

static member (+) (m: Rational, n: Rational) =

Rational(m.Numerator*n.Denominator + n.Numerator*m.Denominator,

m.Denominator*n.Denominator)

static member (-) (m: Rational, n: Rational) =

Rational(m.Numerator*n.Denominator - n.Numerator*m.Denominator,

m.Denominator*n.Denominator)

static member (*) (m: Rational, n: Rational) =

Rational(m.Numerator*n.Numerator, m.Denominator*n.Denominator)

static member (/) (m: Rational, n: Rational) =

Rational(m.Numerator*n.Denominator, m.Denominator*n.Numerator)

In practice, we are likely to want more functionality such as support for equality, comparison, hashing and pretty printing as well as helper functions like an extra constructor that allows ordinary int arguments. The following implementation provides all of these capabilities:

[<StructuredFormatDisplayAttribute("{AsString}")>]

type Rational(p: BigInteger, q: BigInteger) =

let rec gcd a (b: BigInteger) =

if b.IsZero then a else

gcd b (a % b)

let fixSign(p: BigInteger, q: BigInteger) =

if q.Sign > 0 then p, q else -p, -q

let p, q =

if q.IsZero then raise(System.DivideByZeroException())

let g = gcd q p

fixSign(p/g, q/g)

new(p: int, q: int) = Rational(BigInteger p, BigInteger q)

member __.Numerator = p

member __.Denominator = q

override __.ToString() =

if q.IsOne then p.ToString() else sprintf "%A/%A" p q

interface System.IEquatable<Rational> with

member q1.Equals q2 =

q1.Numerator = q2.Numerator &&

q1.Denominator = q2.Denominator

override q1.Equals q2 =

(q1 :> System.IEquatable<_>).Equals(q2 :?> Rational)

interface System.IComparable<Rational> with

member q1.CompareTo q2 =

compare

(q1.Numerator * q2.Denominator)

(q2.Numerator * q1.Denominator)

interface System.IComparable with

member q1.CompareTo q2 =

(q1 :> System.IComparable<_>).CompareTo(q2 :?> Rational)

member q.AsString = q.ToString()

override q.GetHashCode() = hash(q.Numerator, q.Denominator)

static member (+) (m: Rational, n: Rational) =

Rational(m.Numerator*n.Denominator + n.Numerator*m.Denominator,

m.Denominator*n.Denominator)

static member (-) (m: Rational, n: Rational) =

Rational(m.Numerator*n.Denominator - n.Numerator*m.Denominator,

m.Denominator*n.Denominator)

static member (*) (m: Rational, n: Rational) =

Rational(m.Numerator*n.Numerator, m.Denominator*n.Denominator)

static member (/) (m: Rational, n: Rational) =

Rational(m.Numerator*n.Denominator, m.Denominator*n.Numerator)

An even more comprehensive solution might want to provide parsing from the fractional form (e.g. 2/3) and perhaps even from decimal notation.

Let’s have a look at some examples. The fraction 5/10 is simplified to:

> Rational(5, 10);;

val it : Rational = 1/2

We can multiple ½ by 2/3 to obtain 1/3:

> Rational(1, 2) * Rational(2, 3);;

val it : Rational = 1/3

We can divide ½ by 2/3 to obtain ¾:

> Rational(1, 2) / Rational(2, 3);;

val it : Rational = 3/4

And we can compare rationals, in this case observing that 2/5>1/3:

> compare (Rational(2, 5)) (Rational(1, 3));;

val it : int = 1

Rational numbers are provided by the F# PowerPack which is compatible with our own F# for Numerics and F# for Visualization libraries. For example, see the visualization of a matrix of rationals.

Date: Wednesday, 28 Aug 2013 03:02

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

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

*"The Computer Language Benchmarks Game contains solutions to many problems written in many different languages. This article walks through the design and implementation of a solution to the k-nucleotide problem that is 3.6x faster than the solution listed on that site at the time of writing..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Wednesday, 14 Aug 2013 03:39

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

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

*"Microsoft's F# programming language is a close relative of the OCaml programming language from INRIA. This article is the second in a series comparing and contrasting the two languages and their environments with a focus on the implementations of equality, comparison and hashing as well as OCaml's exotic higher-order module system..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Monday, 05 Aug 2013 01:14

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

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

*"In 1962, John McCarthy published the definition of Lisp 1.5 whilst at MIT. This article follows that document in order to write a simple Lisp parser and evaluator and studies some example inputs..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Thursday, 01 Aug 2013 02:40

The F#.NET Journal just published an article about programming languages:

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

*"Microsoft's F# programming language is a close relative of the OCaml programming language from INRIA. This article is the first in a series comparing and contrasting the two languages and their environments with a focus on the basic syntax and type systems in the two languages..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Sunday, 21 Jul 2013 00:14

The F#.NET Journal just published an article about 3D printing:

The following print was obtained using the Slic3r CAM software:And this print was obtained using the custom GCode described in this article: To read this article and more, subscribe to The F#.NET Journal today!

*"A variety of consumer-level desktop 3D printers have been released over the past couple of years. Although the hardware is impressive the current generation of software leaves much to be desired, including the CAD, CAM and driver software. This article takes a look at a hollow cylinder printed using the Slic3r CAM software commonly used today and walks through the design and implementation of bespoke CAM software written in F# that produces a much higher quality print in a fraction of the time..."*The following print was obtained using the Slic3r CAM software:And this print was obtained using the custom GCode described in this article: To read this article and more, subscribe to The F#.NET Journal today!

Date: Thursday, 04 Jul 2013 00:01

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

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

*".NET introduced a variety of useful concurrency primitives including the barrier, which blocks a number of threads until they have all reached the same point whereupon the barrier rolls and all of the threads are allowed to progress. This article examines the asynchronous equivalent, a concurrency primitive that provides the same semantics by suspending asynchronous workflows rather than blocking threads..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Wednesday, 05 Jun 2013 22:48

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

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

*"The Arduino is a popular family of hobbyist microcontroller boards. These devices make it easy to connect sensors and actuators to a PC and control them via the serial port using a variety of programming languages including F#. This article shows how a simple circuit can be controlled from F# in order to visualize the properties of a transistor interactively..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Tuesday, 21 May 2013 03:40

The F#.NET Journal just published an article about reflection and serialization:

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

*"This article builds upon the recent article about reflecting values of non-recursive F# types in order to create a simple but effective and efficient XML serialization library..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Monday, 13 May 2013 01:51

The F#.NET Journal just published an article about windows programming:

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

*"Long running programs such as daemons are conventionally run as services under the Windows operating system. Windows services can be started automatically when a machine boots up. Logging is conventionally accomplished by writing to an Event Log. This article walks through an example windows service that creates and uses its own event log and provides a TCP echo server..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Friday, 10 May 2013 11:27

The F#.NET Journal just published an article about meta programming:

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

*"Reflection has many practical applications but is significantly harder using the built-in API that it can be if consideration is restricted to F# types as they are used in messages. This article describes a simple library that allows non-recursive F# types and values of them to be converted to and from an abstract representation in the form of a union type. This makes it much easier to write fast serialization libraries for a variety of different serialized formats..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Sunday, 05 May 2013 01:33

The F#.NET Journal just published an article about GUI programming:

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

*"There are many parallels between types and the built-in WPF controls. An editable boolean can be represented by a check box. An editable enum can be represented by a combobox. An editable string by a text box and so on. Editable sequences are notably absent from the set of built-in WPF controls. This article describes the design and implementation of a WPF control that allows an arbitrary sequence to be edited..."*To read this article and more, subscribe to The F#.NET Journal today!

Date: Wednesday, 13 Mar 2013 18:25

The F#.NET Journal just published an article about GUI programming:

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

*"There are many parallels between types and the built-in WPF controls. An editable boolean can be represented by a check box. An editable enum can be represented by a combobox. An editable string by a text box and so on. Editable sequences are notably absent from the set of built-in WPF controls. This article describes the design and implementation of a WPF control that allows an arbitrary sequence to be edited..."*To read this article and more, subscribe to The F#.NET Journal today!

**Previous page - Next page**

**»**© All content and copyrights belong to their respective authors.

**«**

**»**© FeedShow - Online RSS Feeds Reader