2011-03-05 162 views
1

我想下面的C#转换为F#:在类型使用F#索引属性

public class Matrix 
    { 
     double[,] matrix; 

public int Cols 
     { 
      get 
      { 
       return this.matrix.GetUpperBound(1) + 1; 
      } 
     } 

public int Rows 
     { 
      get 
      { 
       return this.matrix.GetUpperBound(0) + 1; 
      } 
     } 

     public Matrix(double[,] sourceMatrix) 
     { 
     this.matrix = new double[sourceMatrix.GetUpperBound(0) + 1, sourceMatrix.GetUpperBound(1) + 1]; 
     for (int r = 0; r < this.Rows; r++) 
     { 
      for (int c = 0; c < this.Cols; c++) 
      { 
       this[r, c] = sourceMatrix[r, c]; 
      } 
     } 
     } 

     public double this[int row, int col] 
     { 
     get 
     { 
      return this.matrix[row, col]; 
     } 
     set 
     { 
      this.matrix[row, col] = value; 
     } 
     } 
    } 

这是我到目前为止有:以上

type Matrix(sourceMatrix:double[,]) = 
let mutable (matrix:double[,]) = Array2D.create (sourceMatrix.GetUpperBound(0) + 1) (sourceMatrix.GetUpperBound(1) + 1) 0.0 
member this.Item 
    with get(x, y) = matrix.[(x, y)] 
    and set(x, y) value = matrix.[(x, y)] <- value 
do 
    for i = 0 to matrix.[i].Length - 1 do 
    for j = (i + 1) to matrix.[j].Length - 1 do 
     this.[i].[j] = matrix.[i].[j] 

我的类型似乎有两个问题我不知道如何解决。第一个是矩阵。[(x,y)]预计有类型`a []但是类型为double [,]。第二种是类型定义在成员和接口定义之前必须有let/do绑定。这个问题是我试图填充do块中的索引属性,这意味着我必须先创建它。

由于提前,

鲍勃

回答

6

关于你的第一个问题,你想用matrix.[x,y]而不是matrix.[(x,y)] - 你的矩阵是由两个整数索引,而不是整数的元组(虽然这些概念类似)。

这里的东西大致相当于你的C#:

type Matrix(sourceMatrix:double[,]) = 
    let rows = sourceMatrix.GetUpperBound(0) + 1 
    let cols = sourceMatrix.GetUpperBound(1) + 1 
    let matrix = Array2D.zeroCreate<double> rows cols 
    do 
    for i in 0 .. rows - 1 do 
    for j in 0 .. cols - 1 do 
     matrix.[i,j] <- sourceMatrix.[i,j] 
    member this.Rows = rows 
    member this.Cols = cols 
    member this.Item 
    with get(x, y) = matrix.[x, y] 
    and set(x, y) value = matrix.[x, y] <- value 

这是假设你的矩阵实际上不能重新分配(例如,在C#中你张贴,你可以做出你matrixreadonly - 除非还有其他隐藏的代码)。因此,行和列的数量可以在构造函数中计算一次,因为矩阵的条目可能会更改,但其大小不会更改。

但是,如果你希望你的代码更直译,你可以给你新建成的情况下(在这种情况下this)名称:

type Matrix(sourceMatrix:double[,]) as this = 
    let mutable matrix = Array2D.zeroCreate<double> (sourceMatrix.GetUpperBound(0) + 1) (sourceMatrix.GetUpperBound(1) + 1) 
    do 
    for i in 0 .. this.Rows - 1 do 
    for j in 0 .. this.Cols - 1 do 
     this.[i,j] <- sourceMatrix.[i,j] 
    member this.Rows = matrix.GetUpperBound(0) + 1 
    member this.Cols = matrix.GetUpperBound(1) + 1 
    member this.Item 
    with get(x, y) = matrix.[x, y] 
    and set(x, y) value = matrix.[x, y] <- value 
+0

对不起,我只是编辑它,粘贴在错误的构造函数中。 – Beaker 2011-03-05 21:48:20

+0

优秀的答案,以及只读的这是你的一个正确的假设。原来的C#不是我的,我忽略了这一点。对C#和F#都很好。 :) – Beaker 2011-03-05 23:35:46

+0

如果有人感兴趣,我添加了这个问题的后续问题:http://stackoverflow.com/questions/5212570/adding-overloaded-constructors-to-implicit-f-type – Beaker 2011-03-06 19:07:13

2
type Matrix(sourceMatrix:double[,]) = 
    let matrix = Array2D.copy sourceMatrix 
    member this.Item 
     with get(x, y) = matrix.[x, y] 
     and set(x, y) value = matrix.[x, y] <- value