|
|
|
@ -717,6 +717,90 @@ namespace MathNet.Numerics |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generate samples generated by the given computation.
|
|
|
|
/// </summary>
|
|
|
|
public static T[] Unfold<T, TState>(int length, Func<TState, Tuple<T, TState>> f, TState state) |
|
|
|
{ |
|
|
|
if (length < 0) |
|
|
|
{ |
|
|
|
throw new ArgumentOutOfRangeException("length"); |
|
|
|
} |
|
|
|
|
|
|
|
var data = new T[length]; |
|
|
|
for (int i = 0; i < data.Length; i++) |
|
|
|
{ |
|
|
|
Tuple<T, TState> next = f(state); |
|
|
|
data[i] = next.Item1; |
|
|
|
state = next.Item2; |
|
|
|
} |
|
|
|
return data; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generate an infinite sequence generated by the given computation.
|
|
|
|
/// </summary>
|
|
|
|
public static IEnumerable<T> UnfoldSequence<T, TState>(Func<TState, Tuple<T, TState>> f, TState state) |
|
|
|
{ |
|
|
|
while (true) |
|
|
|
{ |
|
|
|
Tuple<T, TState> next = f(state); |
|
|
|
state = next.Item2; |
|
|
|
yield return next.Item1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#if !NOSYSNUMERICS
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generate a Fibonacci sequence, including zero as first value.
|
|
|
|
/// </summary>
|
|
|
|
public static BigInteger[] Fibonacci(int length) |
|
|
|
{ |
|
|
|
if (length < 0) |
|
|
|
{ |
|
|
|
throw new ArgumentOutOfRangeException("length"); |
|
|
|
} |
|
|
|
|
|
|
|
var data = new BigInteger[length]; |
|
|
|
if (data.Length > 0) |
|
|
|
{ |
|
|
|
data[0] = BigInteger.Zero; |
|
|
|
} |
|
|
|
if (data.Length > 1) |
|
|
|
{ |
|
|
|
data[1] = BigInteger.One; |
|
|
|
} |
|
|
|
for (int i = 2; i < data.Length; i++) |
|
|
|
{ |
|
|
|
data[i] = data[i - 1] + data[i - 2]; |
|
|
|
} |
|
|
|
return data; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generate an infinite Fibonacci sequence, including zero as first value.
|
|
|
|
/// </summary>
|
|
|
|
public static IEnumerable<BigInteger> FibonacciSequence() |
|
|
|
{ |
|
|
|
BigInteger a = BigInteger.Zero; |
|
|
|
yield return a; |
|
|
|
|
|
|
|
BigInteger b = BigInteger.One; |
|
|
|
yield return b; |
|
|
|
|
|
|
|
while (true) |
|
|
|
{ |
|
|
|
a = a + b; |
|
|
|
yield return a; |
|
|
|
|
|
|
|
b = a + b; |
|
|
|
yield return b; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create random samples, uniform between 0 and 1.
|
|
|
|
/// Faster than other methods but with reduced guarantees on randomness.
|
|
|
|
@ -1016,5 +1100,7 @@ namespace MathNet.Numerics |
|
|
|
{ |
|
|
|
return distribution.Samples().Zip(distribution.Samples(), map); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|