F# numeric associations

Posted by b1g3ar5 on Stack Overflow See other posts from Stack Overflow or by b1g3ar5
Published on 2012-07-05T16:46:39Z Indexed on 2012/07/06 9:16 UTC
Read the original article Hit count: 249

Filed under:
|

I have a numeric association for a custom type as follows:

let DiffNumerics =
{ new INumeric<Diff> with
      member op.Zero = C 0.0
      member op.One = C 1.0
      member op.Add(a,b) = a + b
      member op.Subtract(a,b) = a - b
      member op.Multiply(a,b) = a * b
      member ops.Negate(a) = Diff.negate a
      member ops.Abs(a) = Diff.abs a
      member ops.Equals(a, b) = ((=) a b)
      member ops.Compare(a, b) = Diff.compare a b
      member ops.Sign(a) = int (Diff.sign a).Val
      member ops.ToString(x,fmt,fmtprovider) =  failwith "not implemented"
      member ops.Parse(s,numstyle,fmtprovider) = failwith "not implemented"
}

GlobalAssociations.RegisterNumericAssociation(DiffNumerics)

It works fine in f# interactive, but crashes when I run, because .ElementOps is not filled correctly for a matrix of these types. Any ideas why this might be?

EDIT:

In fsi, the code

let A = dmatrix [[Diff.C 1.;Diff.C 2.;Diff.C 3.];[Diff.C 4.;Diff.C 5.;Diff.C 6.]]
let B = matrix [[1.;2.;3.];[4.;5.;6.]]

gives:

> A.ElementOps;;
val it : INumeric<Diff> = FSI_0003.NewAD+DiffNumerics@258
> B.ElementOps;;
val it : INumeric<float> = Microsoft.FSharp.Math.Instances+FloatNumerics@115
> 

in the debugger A.ElementOps shows:

'(A).ElementOps' threw an exception of type 'System.NotSupportedException'

and, for the B matrix:

Microsoft.FSharp.Math.Instances+FloatNumerics@115

So somehow the DiffNumerics isn't making it to the compiled program.

© Stack Overflow or respective owner

Related posts about F#

Related posts about numeric