在类型中使用F#索引属性
我正在尝试将下面的C#转换为F#:在类型中使用F#索引属性,f#,types,indexed-properties,F#,Types,Indexed Properties,我正在尝试将下面的C#转换为F#: 公共类矩阵 { 双[,]矩阵; 公共int Cols { 得到 { 返回此.matrix.GetUpperBound(1)+1; } } 公共整数行 { 得到 { 返回此.matrix.GetUpperBound(0)+1; } } 公共矩阵(双[,]源矩阵) { this.matrix=新的双精度[sourceMatrix.GetUpperBound(0)+1,sourceMatrix.GetUpperBound(1)+1]; for(int r=0;r
公共类矩阵
{
双[,]矩阵;
公共int Cols
{
得到
{
返回此.matrix.GetUpperBound(1)+1;
}
}
公共整数行
{
得到
{
返回此.matrix.GetUpperBound(0)+1;
}
}
公共矩阵(双[,]源矩阵)
{
this.matrix=新的双精度[sourceMatrix.GetUpperBound(0)+1,sourceMatrix.GetUpperBound(1)+1];
for(int r=0;r
这就是我到目前为止所做的:
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]
类型矩阵(sourceMatrix:double[,])=
设mutable(matrix:double[,])=Array2D.create(sourceMatrix.GetUpperBound(0)+1)(sourceMatrix.GetUpperBound(1)+1)0.0
成员:本项目
使用get(x,y)=矩阵。[(x,y)]
设置(x,y)value=matrix。[(x,y)]关于第一个问题,您希望使用matrix。[x,y]
而不是matrix。[(x,y)]
-您的矩阵由两个整数索引,而不是由整数元组索引(尽管它们在概念上类似)
这里有一些大致相当于你的C#:
关于第一个问题,您希望使用矩阵。[x,y]
而不是矩阵。[(x,y)]
-您的矩阵由两个整数索引,而不是由一个整数元组索引(尽管它们在概念上相似)
这里有一些大致相当于你的C#:
类型矩阵(sourceMatrix:double[,])=
让matrix=Array2D.copy sourceMatrix
成员:本项目
用get(x,y)=矩阵[x,y]
并设置(x,y)值=矩阵。[x,y]类型矩阵(sourceMatrix:double[,])=
让matrix=Array2D.copy sourceMatrix
成员:本项目
用get(x,y)=矩阵[x,y]
然后设置(x,y)value=matrix。[x,y]回答得很好,并且很好地理解了readonly,这是您正确的假设。原来的C#不是我的,我忽略了这一点。同时擅长C和F一定很不错如果有人感兴趣的话,我在这个问题上添加了一个后续问题:很好的答案,很好地理解了readonly,这是您的正确假设。原来的C#不是我的,我忽略了这一点。同时擅长C和F一定很不错如果有人感兴趣的话,我在这个问题上添加了一个后续问题:
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]
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
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
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