## Snippets tagged math

• ### 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).

30 people like this

Posted: 11 years ago by Oldrich Svec

• ### 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

11 people like this

Posted: 11 years ago by Ademar Gonzalez

• ### 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: 11 years ago by HP

• ### Prime testing

3 people like this

Posted: 10 years ago by d95danb

• ### 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: 10 years ago by Matt Wilson

• ### 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: 10 years ago by Kit Eason

• ### Simple Markov chains

Small example illustrating how to use Arrays and Sequences to simulate simple Markov chain and evaluate their stationary distribution.

4 people like this

Posted: 10 years ago by Mathias Brandewinder

• ### Checking for perfect squares

An implementation of John D. Cook's algorithm for fast-finding perfect squares: http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/

1 people like this

Posted: 9 years ago by Kit Eason

• ### Square root for integral types

Square root for integral types

5 people like this

Posted: 9 years ago by ildjarn

• ### Inverse error function (float)

let erfinv x = let mutable p = 0.0 let mutable w = -log((1.0-x)*(1.0+x)) // corrected sign if w < 5.000000 then w <- w - 2.500000 p <- 2.81022636e-08 p <- 3.43273939e-07 + p*w p <- -3.5233877e-06 + p*w p <- -4.39150654e-06 + p*w p <- 0.00021858087 + p*w p <- -0.00125372503 + p*w p <- -0.00417768164 + p*w p <- 0.246640727 + p*w p <- 1.50140941 + p*w else w <- sqrt w - 3.000000 p <- -0.000200214257 p <- 0.000100950558 + p*w p <- 0.00134934322 + p*w p <- -0.00367342844 + p*w p <- 0.00573950773 + p*w p <- -0.0076224613 + p*w p <- 0.00943887047 + p*w p <- 1.00167406 + p*w p <- 2.83297682 + p*w p*x

2 people like this

Posted: 9 years ago by Clever people

• ### Calculate PI using Monte Carlo

Estimate the value of PI using a Monte Carlo simulation.

5 people like this

Posted: 9 years ago by Phillip Trelford

• ### Great circle distance

Compute the great circle distance of 2 points

3 people like this

Posted: 8 years ago by Samuel Bosch

• ### Azimuthal equidistant projection

Simple and a more optimized implementation of the azimuthal equidistant projection. Input is expected in degrees.

3 people like this

Posted: 8 years ago by Samuel Bosch

• ### Generic number literal

A generic numeral G. It allows writing functions for arbitrary numeric type. The transformation is an efficient one, because it is implemented explicitly for every type. It combines the type classes technique of FsControl (https://github.com/gmpl/FsControl and http://nut-cracker.azurewebsites.net/typeclasses-for-fsharp) with numeric literals. But FsControl is removed to completely avoid unnecessary function calls.

4 people like this

Posted: 8 years ago by Daniel Fabian (@iceypoi)

• ### Generic Numeric Type Conversions

Function that converts numeric types using type inference. This is meant to complement the generic number literal G in helping to write generic numeric code. The implementation is similar to the generic number literal snippet. See http://fssnip.net/mv/

1 people like this

Posted: 6 years ago by Sami Perttu

• ### Lagrange formula

Lagrange's interpolation formula

7 people like this

Posted: 5 years ago by Ivan Padron

• ### Pascal's Triangle

Creates a jagged 2d list of Pascal's Triangle.

2 people like this

Posted: 2 years ago by Kayden Tebau

• ### Simple Sieve of Eratosthenes with sets

Simple implementation of the Sieve of Eratosthenes with set arithmetic.

3 people like this

Posted: 2 years ago by galacticdessert

• ### 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.

20 people like this

Posted: 11 years ago by Horacio Nuñez

• ### 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: 11 years ago by Antwan "A-Dubb" Wimberly

• ### Prime numbers - Sieve of Eratosthenes

Basic prime number generator

3 people like this

Posted: 10 years 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: 10 years ago by Ademar Gonzalez

• ### 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.

9 people like this

Posted: 10 years ago by Johann Deneux

• ### 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.

6 people like this

Posted: 10 years ago by Mathias Brandewinder

• ### Calculate PI using Monte Carlo

Simple walkthrough that demonstrates how to estimate the value of PI using Monte Carlo simulation. A few holes need to be filled in and then you can run & parallelize the sample!

4 people like this

Posted: 10 years ago by Tomas Petricek

• ### Slicing for Math.Net vectors and matrices

This snippet adds the 'GetSlice' operation to Math.Net vector and matrix types. This makes it possible to get sub-vectors and sub-matrices using the convenient syntax (For vectors 'vect.[start .. end]` or 'vect.[start ..]' and for matrices 'mat.[r1 .. r2, c1 .. c2]' and similar).

9 people like this

Posted: 9 years ago by Tomas Petricek

• ### Density of sparse matrix product

A quick exploration of the rather useless question "how does the density of the product of 2 sparse matrices look like?"

0 people like this

Posted: 9 years ago by Mathias Brandewinder

• ### DivRem Operator

Defines the operator /% as DivRem for int, int64 and bigint.

6 people like this

Posted: 9 years ago by Diego Frata

• ### DBSCAN

Density-based spatial clustering of applications with noise (DBSCAN) is a data clustering algorithm. For more information see http://en.wikipedia.org/wiki/DBSCAN. The implementation is based on the pseudocode in the article and the following C# code http://www.c-sharpcorner.com/uploadfile/b942f9/implementing-the-dbscan-algorithm-using-C-Sharp/ The implementation is not very functional but does the job. Added pwd by ignorance, the password is "fssnip" (without quotes)

1 people like this

Posted: 8 years ago by Samuel Bosch

• ### Convert integers to positive values

Convert negative and positive integers to a positive value by using an overlap and interleave scheme (more info: http://en.wikipedia.org/wiki/Golomb_coding).

1 people like this

Posted: 8 years ago by Samuel Bosch

• ### Azimuthal equidistant projection with measures

Simple version of the azimuthal equidistant projection (see also http://fssnip.net/lA) but with measures. This avoids mixing of degrees and radians and longitudes/x and latitudes/y

2 people like this

Posted: 8 years ago by Samuel Bosch

• ### Rational type for units of measure

A more idiomatic definition of a Rational type with custom operators.

1 people like this

Posted: 7 years ago by Tomas Petricek

• ### Euler

Euler method is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value.

1 people like this

Posted: 5 years ago by Ivan Padron

• ### 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.

4 people like this

Posted: 5 years ago by Tuomas Hietanen

• ### Pi - Using Nilakantha's accelerated series

Calculate the value of Pi, using the Nilakantha series

0 people like this

Posted: 2 years ago by Apurva