2011-03-05 10 views
1

:내가 F 번호에 다음 C#으로 변환하기 위해 노력하고있어

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] 

내 타입이 위 갖고있는 것 같아요 :

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; 
     } 
     } 
    } 

이것은 내가 지금까지 무엇을 가지고 두 가지 문제를 해결하는 방법을 모르겠습니다. 첫 번째 것은 그 행렬입니다. [(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#에서 당신이 게시 한 예 (당신이 당신의 matrix 필드 readonly를 만들었을 수도 재 할당 할 수없는 가정은 -하지 않는 한 숨겨진 추가 코드가 있습니다.) 따라서 행렬의 항목은 변경 될 수 있지만 크기는 변경되지 않으므로 생성자에서 행과 열의 수를 한 번 계산할 수 있습니다.

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

미안 해요, 난 그냥 그것을 편집 붙여 넣기 : 당신은 당신의 코드를보다 직역을 원하는 경우

그러나, 당신은 당신의 새로 건설 된 예에게 (이 경우 this)의 이름을 제공 할 수 있습니다 잘못된 생성자에서. – Beaker

+0

우수 답변 및 귀하의 올바른 가정이었던 읽기 전용 캐치. 원래 C#은 제 것이 아니었고 저는 그것을 간과했습니다. C#과 F # 모두 잘하는 것이 좋을 것입니다. :) – Beaker

+0

관심있는 사람이 있으면 후속 질문을 추가했습니다. http://stackoverflow.com/questions/5212570/adding-overloaded-constructors-to-implicit-f-type – Beaker

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 
관련 문제