Skip to content

Commit

Permalink
add geomspace
Browse files Browse the repository at this point in the history
  • Loading branch information
DoganCK committed Mar 13, 2023
1 parent e705070 commit e28b4c8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/FSharp.Stats/Array.fs
Expand Up @@ -526,3 +526,17 @@ module ArrayExtension =
Seq.linspace(start,stop,Num.Value,includeEndpoint) |> Array.ofSeq
else
Seq.linspace(start,stop,IncludeEndpoint=includeEndpoint) |> Array.ofSeq

/// <summary>
/// Creates a geometric array of floats with values between a given interval
/// </summary>
/// <param name="start">start value (is included)</param>
/// <param name="stop">end value (by default is included)</param>
/// <param name="Num">sets the number of elements in the array. Defaults to 50.</param>
/// <param name="IncludeEndpoint">If false, the array does not contain the stop value. Defaults to true.</param>
static member geomspace(start:float,stop:float,?Num:int,?IncludeEndpoint:bool) : float array =
let includeEndpoint = defaultArg IncludeEndpoint true
let num = defaultArg Num 50

Seq.geomspace (start, stop ,num, includeEndpoint)
|> Array.ofSeq
15 changes: 15 additions & 0 deletions src/FSharp.Stats/List.fs
Expand Up @@ -211,3 +211,18 @@ module ListExtension =
Seq.linspace(start,stop,Num.Value,includeEndpoint) |> List.ofSeq
else
Seq.linspace(start,stop,IncludeEndpoint=includeEndpoint) |> List.ofSeq

/// <summary>
/// Creates a geometric list of floats with values between a given interval
/// </summary>
/// <param name="start">start value (is included)</param>
/// <param name="stop">end value (by default is included)</param>
/// <param name="Num">sets the number of elements in the list. Defaults to 50.</param>
/// <param name="IncludeEndpoint">If false, the list does not contain the stop value. Defaults to true.</param>
static member geomspace(start:float,stop:float,?Num:int,?IncludeEndpoint:bool) : float list =
let includeEndpoint = defaultArg IncludeEndpoint true
let num = defaultArg Num 50

Seq.geomspace (start, stop ,num, includeEndpoint)
|> List.ofSeq

24 changes: 24 additions & 0 deletions src/FSharp.Stats/Seq.fs
Expand Up @@ -1240,6 +1240,30 @@ module SeqExtension =
ceil dif
Seq.init (int num) (fun i -> float i + start)

/// <summary>
/// Creates a geometric seq float with values between a given interval
/// </summary>
/// <param name="start">start value (is included)</param>
/// <param name="stop">end value (by default is included)</param>
/// <param name="Num">sets the number of elements in the seq. Defaults to 50.</param>
/// <param name="IncludeEndpoint">If false, the seq does not contain the stop value. Defaults to true.</param>
static member inline geomspace (start:float, stop:float, ?Num:int, ?IncludeEndpoint:bool) : seq<float> =
if start <= 0. || stop <= 0. then
failwith "Geometric space can only take positive values."

let includeEndpoint = defaultArg IncludeEndpoint true
let num = defaultArg Num 50

let logStart = System.Math.Log start
let logStop = System.Math.Log stop

let logStep =
match includeEndpoint with
| true -> (logStop - logStart) / (float num - 1.)
| false -> (logStop - logStart) / (float num)

Seq.init num (fun x -> (System.Math.Exp (logStart + float x * logStep)))


// // ########################################################################
// /// A module which implements functional matrix operations.
Expand Down
14 changes: 14 additions & 0 deletions src/FSharp.Stats/Vector.fs
Expand Up @@ -424,4 +424,18 @@ module VectorExtension =
Seq.linspace(start,stop,Num.Value,includeEndpoint) |> Vector.ofSeq
else
Seq.linspace(start,stop,IncludeEndpoint=includeEndpoint) |> Vector.ofSeq

/// <summary>
/// Creates a geometric vector of floats with values between a given interval.
/// </summary>
/// <param name="start">start value (is included)</param>
/// <param name="stop">end value (by default is included)</param>
/// <param name="Num">sets the number of elements in the vector. Defaults to 50.</param>
/// <param name="IncludeEndpoint">If false, the vector does not contain the stop value. Defaults to true.</param>
static member geomspace(start:float,stop:float,?Num:int,?IncludeEndpoint:bool) : vector =
let includeEndpoint = defaultArg IncludeEndpoint true
let num = defaultArg Num 50

Seq.geomspace (start, stop ,num, includeEndpoint)
|> Vector.ofSeq

0 comments on commit e28b4c8

Please sign in to comment.