Snippets in category Mathematics

  • Form changing color

    Windows Forms tutorial showing how to create form with button and how to register an event handler. When the button is clicked, the form changes its background color.

    29 people like this
    Posted: 1 years ago by Tomas Petricek

  • Support slicing operator

    The snippet shows how to support slicing in a type. Slicing allows you to get for example a 2D sub-matrix of a matrix and is implemented by adding GetSlice member.

    37 people like this
    Posted: 1 years ago by Tomas Petricek

  • Array shuffle

    Shuffle an array

    27 people like this
    Posted: 1 years ago by Laurent

  • Random Type with a few modifications

    The modified Random type is built on top of System.Random type. It has a member Seed which returns a seed and NextFloat has the same overloads as NextInt (Next in System.Random). The members should further support units of measure.

    14 people like this
    Posted: 1 years ago by Oldrich Svec

  • Distance between two 3D segments

    The function segmentSegment takes 2 segments (starting and ending points) and computes the shortest distance between them. The function returns a starting and ending point of the shortest segment between the two segments. The function uses a triple type but can be easily rewritten to work with any other type (vector etc).

    23 people like this
    Posted: 1 years ago by Oldrich Svec

  • Sequence Random Permutation

    A generic function that randomly permutes the elements of a sequence.

    29 people like this
    Posted: 1 years ago by Taha Hachana

  • inline pow

    LanguagePrimitives help create inline functions

    23 people like this
    Posted: 1 years ago by Dmitri Pavlenkov

  • Function to get all possible combinations

    Function to get all possible combinations of list items. There are some Euler problems (like 77 & 78) to get total amounts. But e.g. for some card game implementations you will need the real items.

    27 people like this
    Posted: 1 years ago by Tuomas Hietanen

  • Miller–Rabin primality test

    Miller–Rabin primality test is an algorithm which determines whether a given number is probable prime. For more information go to http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test

    18 people like this
    Posted: 1 years ago by Cesar Mendoza

  • All combinations of list elements

    For a given list, find all possible combinations of elements of the list (not just k-combinations). The result is a list of lists with each element representing one combination. Order of elements is not taken into account.

    14 people like this
    Posted: 1 years ago by Alexander Rautenberg

  • Cartesian product of n lists

    Cartesian product of a variable number of lists. Input is a list of lists of which the cartesian product is to be constructed; output is a list that contains the elements of the product set, as lists.

    11 people like this
    Posted: 1 years ago by Alexander Rautenberg

  • Pascal's Triangle

    Returns the pascal triangle in a 2D list . This snippet computes rows translating the 'visual' method taught at school into Lists and the usage of map2 function. It takes almost 5 seconds to compute 5000 rows.

    12 people like this
    Posted: 1 years ago by Horacio Nuñez

  • A Lazy fixed-point combinator

    x = f(x) encoded in F#

    14 people like this
    Posted: 1 years ago by Nick Palladinos

  • Primitive Pythagorean triples

    Primitive Pythagorean triples generator. It uses an Algorithm found on Wolfram MathWorld and the F# PowerPack matrix library.

    31 people like this
    Posted: 1 years ago by Cesar Mendoza

  • Gaussian Random Sequence

    Normalized Random sequence generator conforming to the user supplied mean and sigma utilizing a "seed factory" instead of the default time of day. The Gaussian sequence is based on the central limit theory, averages together the flat distribution from the random generator built into .NET. Two examples of using normalRand are given to create infinite sequences of white and brown(ian) noise.

    32 people like this
    Posted: 1 years ago by Tony Lee

  • Graham scal algorithm for finding the convex hull of a sequence of 2D points

    finds the points lying on the convex hull of the given set of points and returns those points in clockwise direction, starting at the point with minimum y-value Remarks: it's a more or less direct implementation of the algorithm named after Ronald Graham that is explained on http://en.wikipedia.org/wiki/Graham_scan you can switch the definition Point for a proper type of your liking - e.g. System.Drawing.Point

    30 people like this
    Posted: 1 years ago by Carsten König

  • QR-decomoposition of a square-matrix using the Gram-Schmidt method

    shows a simple implementation of a vector and matrix type together with a QR-decomposition using the Gram-Schmidt method. The algorithms themselfes are rather easy but I think the implementation of the types and the computations using recursive techniques might be interessting

    2 people like this
    Posted: 1 years ago by Carsten König

  • How to write a financial contract

    Implements the theory from 'How to write a financial contract' by S.L Peyton Jones and J-M Eber

    7 people like this
    Posted: 1 years ago by Ademar Gonzalez

  • Combinatorial functions

    Here is my F# take on some combinatorial functions from the book "Introduction to Functional Programming" by Richard Bird and Philip Wadler.

    5 people like this
    Posted: 1 years ago by Cesar Mendoza

  • Permutations

    computes the list of all permutations of a list for example the permutations of [1;2;3] will be [1;2;3]; [1;3;2]; [2;1;3]; [2;3;1]; [3;1;2]; [3;2;1]

    2 people like this
    Posted: 1 years ago by Carsten König

  • Very Fast Permutations

    I spent a lot of time this week profiling different permutation functions from various places on the internet. The following was by far the fastest:

    4 people like this
    Posted: 1 years ago by Rick Minerich

  • Computation Builder for Cartesian Products

    Sample framework for computing Cartesian products using a computation builder.

    7 people like this
    Posted: 1 years ago by TechNeilogy

  • Moving Average

    Given a period and a sequence of values, calculates the moving average of the input. E.g., given a period of 4 and a sequence of { 2.0, 8.0, 12.0, 10.0, 6.0, 4.0, 14.0 }, the result of this function is { 8.0, 9.0, 8.0, 8.5 }.

    7 people like this
    Posted: 12 months ago by Brett V. Forsgren

  • Calculating the angle between the hour and minute hand

    This is a response to a Google interview question that someone encountered. A friend of mine was recently hired there and I've heard some thrilling stories of their interview process. So every now and then, I plan to code up a response to one of the hoards of interview questions they have.

    0 people like this
    Posted: 10 months ago by Antwan "A-Dubb" Wimberly

  • Calculating the angle between the hour and minute hand redux

    This snippet introduces a further subtlety into the previous posting, whereby the intra-hour movement of the hour hand is captured.

    0 people like this
    Posted: 10 months ago by HP

  • Permutation and Combination

    Permutation and Combination using ListBuilder.

    8 people like this
    Posted: 9 months ago by zecl

  • Prime numbers - Sieve of Eratosthenes

    Basic prime number generator

    1 people like this
    Posted: 8 months ago by d95danb

  • Prime testing

    Simple check if a number is prime. See also http://fssnip.net/2w.

    0 people like this
    Posted: 8 months ago by d95danb

  • RSK algorithm

    Implements a bijective mapping between permutations and pairs of standard Young tableaux, both having the same shape. http://en.wikipedia.org/wiki/Robinson%E2%80%93Schensted_correspondence

    4 people like this
    Posted: 7 months ago by Ademar Gonzalez

  • Compute CC.Net build statistics

    Written against CruiseControl.NET v1.5. Queries a CruiseControl.NET server for a project list and then computes min, max, average, and standard deviation of the build durations based on the statistics.csv CC.NET maintains.

    3 people like this
    Posted: 6 months ago by Matt Wilson

  • Calculate PI number

    PI number calculation based on the wikipedia page(http://en.wikipedia.org/wiki/Pi#cite_note-59). I used Newton's , Machine's and Ramanujan's formula. (updated: line 21: Seq.take => Seq.truncate)

    2 people like this
    Posted: 6 months ago by nagat01

  • A fast sieve of Eratosthenes

    A prime Eratosthenes' sieve, using a bit array and sieving only odd composites to conserve memory and keep things fast.

    2 people like this
    Posted: 6 months ago by Arjen Kopinga

  • Folding over prime factors

    Let's have some fun with higher order functions and instead of folding over a list, fold over the prime factors of a number. It can be optimized further by dividing out real primes instead of numbers of the form 6k+/-1, but it's not embarrassingly slow.

    0 people like this
    Posted: 6 months ago by Arjen Kopinga

  • Random Subset

    A function that takes a random subset from a seq<'T>.

    0 people like this
    Posted: 5 months ago by Taha Hachana

  • XNA's Vector3 with units of measure

    A vector type with units of measure built on top of XNA's Vector3. Not complete, the point is mainly to show how to use generic units of measure to adapt an existing type.

    7 people like this
    Posted: 4 months ago by Johann Deneux

  • Discrete Fréchet Distance

    Compute the Discrete Fréchet Distance between two arrays (which may be of different lengths). Based on the 1994 algorithm by Thomas Eiter and Heikki Mannila. Not extensively tested, so use at your peril! (This version with some small fixes.)

    0 people like this
    Posted: 3 months ago by Kit Eason

  • Ninety-Nine F# Problems - Problems 31 - 41 - Arithmetic]

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    3 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Matrix

    Matrix

    2 people like this
    Posted: 3 months ago by Matrix

  • A simple sieve

    A simple implementation for the sieve of Eratosthenes.

    3 people like this
    Posted: 2 months ago by Gab_km

  • SIngle Life Annuity

    A single life annuity function in F# including supporting functions such as probability of survival, pure endowment and discounted interest rate calculation.

    0 people like this
    Posted: 1 months ago by Kevin Roche

  • Single Life Annuity

    A single life annuity function in F# including supporting functions such as probability of survival, pure endowment and discounted interest rate calculation. Should add that I'd be interested in making this more "functional"; at the moment it seems like a chunk of procedural code to me - any advice would be very welcome.

    3 people like this
    Posted: 1 months ago by Kevin Roche

  • Bayesian Monte Carlo of Let's Make a Deal

    This code illustrates Bayes' Theorem in action on the Let's Make a Deal problem, which several authors have used to illustrate Bayes' Theorem. (It's easy to search the internet for further explanation.) Run with the audit option to audit up to the first 100 games. Running without audit is faster and can simulate a couple billion games.

    3 people like this
    Posted: 1 months ago by Jack Fox

  • Monotone Chain Convex Hull Algorithm

    Andrew's Monotone Chain Convex Hull algorithm: given points in 2 dimensions, determine their convex hull by constructing the upper and lower hull.

    3 people like this
    Posted: 1 months ago by Mathias Brandewinder