Skip to content

Commit

Permalink
chore: sync commit working on GenSolver comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper Bollen authored and Casper Bollen committed Sep 10, 2023
1 parent 38c98d6 commit bbff7c7
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<None Include="Scripts/GentaEquations.fsx" />
<None Include="Scripts\OrderEquations.fsx" />
<None Include="Scripts\Tests.fsx" />
<None Include="Scripts\Increment.fsx" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Informedica.Utils.Lib\Informedica.Utils.Lib.fsproj" />
Expand Down
89 changes: 89 additions & 0 deletions src/Informedica.GenSolver.Lib/Scripts/Increment.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

#r "nuget: Unquote"

#load "load.fsx"

open MathNet.Numerics
open Swensen.Unquote

open Informedica.Utils.Lib.BCL

let x = [2N..2N..10N]
let y = [3N..3N..12N]


let calc op x y =
x
|> List.allPairs y
|> List.map (fun (x1, x2) -> x1 |> op <| x2)
|> List.sort
|> List.distinct


let minIncrMaxToList min incr max : BigRational list =
incr
|> List.fold (fun acc i ->
let min = min |> BigRational.toMinMultipleOf i
let max = max |> BigRational.toMaxMultipleOf i
[min..i..max] @ acc
) []
|> List.sort
|> List.distinct


let calcIncr op x y =
let r =
x
|> List.allPairs y
|> List.map (fun (x1, x2) -> x1 |> op <| x2)

r |> List.min,
r
|> List.toArray
|> Informedica.GenUnits.Lib.Array.removeBigRationalMultiples
|> Array.toList,
r |> List.max


let calcIncrDiv = calcIncr (/)
let calcIncrMul = calcIncr (*)
let calcIncrAdd = calcIncr (+)
let calcIncrSub = calcIncr (-)


calcIncrMul x y |> fun (min, incr, max) -> minIncrMaxToList min incr max
calcIncrDiv x y
calcIncrAdd x y
calcIncrSub x y



test <@
let x = [3N..3N..12N]
let y = [2N..2N..10N]
let exp = calc (*) x y
let act = calcIncrMul x y |> fun (min, incr, max) -> minIncrMaxToList min incr max
exp <> act &&
Set.isSubset (exp |> Set.ofList) (act |> Set.ofList)
@>


test <@
let x = [3N..3N..12N]
let y = [2N..2N..10N]
let exp = calc (/) x y
let act = calcIncrDiv x y |> fun (min, incr, max) -> minIncrMaxToList min incr max
exp <> act &&
Set.isSubset (exp |> Set.ofList) (act |> Set.ofList)
@>


test <@
let x = [3N..3N..12N]
let y = [2N..2N..10N]
let exp = calc (+) x y
let act = calcIncrAdd x y |> fun (min, incr, max) -> minIncrMaxToList min incr max
exp <> act &&
Set.isSubset (exp |> Set.ofList) (act |> Set.ofList)
@>

67 changes: 65 additions & 2 deletions src/Informedica.GenSolver.Lib/Variable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ module Variable =
module Increment =


/// <summary>
/// Create an `Increment` from a `ValueUnit`.
/// </summary>
/// <param name="vu">The ValueUnit</param>
/// <returns>An `Increment`</returns>
/// <exception cref="Exceptions.ValueRangeEmptyIncrementException">When the `ValueUnit` is empty</exception>
/// <remarks>
/// Filters out negative values and removes multiples.
/// </remarks>
let create vu =
vu
|> ValueUnit.filter ((<) 0N)
Expand All @@ -63,25 +72,78 @@ module Variable =
Exceptions.ValueRangeEmptyIncrement |> raiseExc []


let map f (Increment incr) = incr |> f |> create
/// <summary>
/// Maps a function over the ValueUnit of `Increment`.
/// </summary>
/// <param name="f">The function to map</param>
/// <returns>A new `Increment`</returns>
let map f (Increment vu) = vu |> f |> create


/// <summary>
/// Convert an `Increment` to a `ValueUnit`.
/// </summary>
let toValueUnit (Increment vu) = vu


let setUnit u = map (ValueUnit.convertTo u)
/// <summary>
/// Convert the Unit of an `Increment` to **u**.
/// </summary>
/// <param name="u">The unit to convert to</param>
let convertToUnit u = map (ValueUnit.convertTo u)


/// <summary>
/// Get the smallest increment value
/// </summary>
/// <returns>
/// The smallest increment value as a ValueUnit option.
/// </returns>
let minElement =
toValueUnit >> ValueUnit.minValue


/// <summary>
/// Check if 'incr1' equals 'incr2'
/// </summary>
/// <param name="incr1">The first increment</param>
/// <param name="incr2">The second increment</param>
/// <example>
/// <remarks>
/// Uses ValueUnit equality, so 1000 milligram equals 1 gram
/// </remarks>
/// <code>
/// let incr1 = Increment.create ( [| 1000N |] |> ValueUnit.create Units.Mass.milliGram)
/// let incr2 = Increment.create ( [| 1N |] |> ValueUnit.create Units.Mass.gram)
/// incr1 |> Increment.eqs incr2
/// // returns true, i.e. 1000 milligram equals 1 gram
/// </code>
/// </example>
let eqs incr1 incr2 =
let vu1 = incr1 |> toValueUnit
let vu2 = incr2 |> toValueUnit
vu1 =? vu2


/// <summary>

Check warning on line 128 in src/Informedica.GenSolver.Lib/Variable.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This XML comment is invalid: unknown parameter 'incr1'

Check warning on line 128 in src/Informedica.GenSolver.Lib/Variable.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This XML comment is invalid: unknown parameter 'incr2'

Check warning on line 128 in src/Informedica.GenSolver.Lib/Variable.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This XML comment is invalid: unknown parameter 'incr1'

Check warning on line 128 in src/Informedica.GenSolver.Lib/Variable.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This XML comment is invalid: unknown parameter 'incr2'

Check warning on line 128 in src/Informedica.GenSolver.Lib/Variable.fs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

This XML comment is invalid: unknown parameter 'incr1'

Check warning on line 128 in src/Informedica.GenSolver.Lib/Variable.fs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

This XML comment is invalid: unknown parameter 'incr2'
/// Get the intersection of two `Increment`.
/// </summary>
/// <param name="incr1">The first increment</param>
/// <param name="incr2">The second increment</param>
/// <returns>
/// The intersection of the two increments.
/// </returns>
/// <remarks>
/// Returns the ValueUnit with Unit of the first increment.
/// </remarks>
/// <example>
/// <code>
/// let incr1 = Increment.create ( [| 2000N; 3000N |] |> ValueUnit.create Units.Mass.milliGram)
/// let incr2 = Increment.create ( [| 3N; 5N |] |> ValueUnit.create Units.Mass.gram)
/// incr1 |> Increment.intersect incr2
/// // returns Increment (ValueUnit ([|3000N|], Mass (MilliGram 1N)))
/// </code>
/// </example>
let intersect (Increment incr1) (Increment incr2) =
incr1 |> ValueUnit.intersect incr2 |> create

Expand Down Expand Up @@ -534,6 +596,7 @@ module Variable =
create


// TODO: this is not right!!
let minIncrMaxToValueSet min incr max =
let min =
min
Expand Down

0 comments on commit bbff7c7

Please sign in to comment.