Skip to content

Commit

Permalink
Vector Subtraction for the vector type provider
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed May 2, 2012
1 parent fc75af5 commit cf7969f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/FSharpx.TypeProviders/VectorProvider.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ open FSharpx.TypeProviders.DSL

let dotProduct = Array.fold2 (fun acc x y -> acc + x * y) 0.
let add = Array.map2 (fun x y -> x + y)
let subtract = Array.map2 (fun x y -> x - y)

let vectorTy =
let missingValue = "@@@missingValue###"
Expand Down Expand Up @@ -50,6 +51,12 @@ let vectorTy =
newType
(fun args -> <@@ add (%%args.[0]:float array) (%%args.[1]:float array) @@>)
|> addXmlDoc "Calculates the sum with the given summand.")
|+!> (provideMethod
"Subtract"
["subtrahend", newType]
newType
(fun args -> <@@ subtract (%%args.[0]:float array) (%%args.[1]:float array) @@>)
|> addXmlDoc "Calculates the difference with the given subtrahend.")
|++!> (parameters
|> Seq.mapi (fun i name ->
provideProperty
Expand Down
13 changes: 11 additions & 2 deletions tests/FSharpx.TypeProviders.Tests/Vector.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ let ``Can calc the typed dot product for the 3D vector``() =
[<Test>]
let ``Can calc the typed sum for the 3D vector``() =
let v1 = Vector3D(1.,3.,-5.)
let v2 = Vector3D(4.,-2.,-1.)
let v2 = Vector3D(4.,-2.,-1.)
let sum = v1.Add(v2)

sum.A |> should equal 5.
sum.B |> should equal 1.
sum.C |> should equal -6.
sum.C |> should equal -6.

[<Test>]
let ``Can calc the typed difference for the 2D vector``() =
let v1 = Vector2D(1.,-5.)
let v2 = Vector2D(4.,-2.2)
let sum = v1.Subtract(v2)

sum.X |> should equal -3.
sum.Y |> should equal -2.8

0 comments on commit cf7969f

Please sign in to comment.