Skip to content

Instantly share code, notes, and snippets.

@nikhedonia
Last active April 12, 2020 20:45
Show Gist options
  • Save nikhedonia/739d5fcb9b4f8d07b180a9ce6b8cfccf to your computer and use it in GitHub Desktop.
Save nikhedonia/739d5fcb9b4f8d07b180a9ce6b8cfccf to your computer and use it in GitHub Desktop.
type ^T Dual
when ^T: (static member (+): ^T * ^T -> ^T)
and ^T: (static member (-): ^T * ^T -> ^T)
and ^T: (static member (*): ^T * ^T -> ^T)
and ^T: (static member (/): ^T * ^T -> ^T)
(real: ^T, imag: ^T) =
member inline this.x = real
member inline this.y = imag
static member inline (+) (lhs: Dual< ^T>, rhs: Dual< ^T>) =
Dual< ^T>(lhs.x + rhs.x, lhs.y + rhs.y)
static member inline (-) (lhs: Dual< ^T>, rhs: Dual< ^T>) =
Dual< ^T>(lhs.x - rhs.x, lhs.y - rhs.y)
static member inline (*) (lhs: Dual< ^T>, rhs: Dual< ^T>) =
Dual< ^T>(lhs.x * rhs.x, lhs.x * rhs.y + lhs.y * rhs.x)
static member inline (/) (lhs: Dual< ^T>, rhs: Dual< ^T>) =
Dual< ^T>(
lhs.x / rhs.x,
(lhs.y * rhs.x + lhs.x * rhs.y) / (rhs.x * rhs.x))
static member inline (*) (lhs: Dual< ^T>, rhs: ^T) =
Dual< ^T>(lhs.x * rhs, lhs.y * rhs)
static member inline (*) (lhs: ^T, rhs: Dual< ^T>) =
Dual< ^T>(lhs * rhs.x, lhs * rhs.y)
static member inline (+) (lhs: Dual< ^T>, rhs: ^T) =
Dual< ^T>(lhs.x + rhs, lhs.y)
static member inline (-) (lhs: ^T, rhs: Dual< ^T>) =
Dual< ^T>(lhs - rhs.x, rhs.y)
let inline dual x =
let y = LanguagePrimitives.GenericOne
Dual (x, y)
// implicitly (x: int) -> int ???
let inline F x = 3 * x * x + 2 * x
let x = dual 2
//error FS0001: This expression was expected to have type
// 'int Dual'
//but here has type
// 'int'
let result : Dual<int> = F x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment