F# 比较两个数组是否等于指定的精度级别

Bea*_*ker 2 arrays precision f# multidimensional-array

这是我试图翻译的 C# 代码:

public bool equals(Matrix matrix, int precision)
    {

        if (precision < 0)
        {
            throw new MatrixError("Precision can't be a negative number.");
        }

        double test = Math.Pow(10.0, precision);
        if (double.IsInfinity(test) || (test > long.MaxValue))
        {
            throw new MatrixError("Precision of " + precision
                    + " decimal places is not supported.");
        }

        precision = (int)Math.Pow(10, precision);

        for (int r = 0; r < this.Rows; r++)
        {
            for (int c = 0; c < this.Cols; c++)
            {
                if ((long)(this[r, c] * precision) != (long)(matrix[r, c] * precision))
                {
                    return false;
                }
            }
        }

        return true;
    }
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所拥有的:

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]
    ///The number of Rows in this Matrix.
    member this.Rows = rows

    ///The number of Columns in this Matrix.
    member this.Cols = cols

    member this.Equals(matrix:Matrix, precision:int) =
     if(precision < 0) then raise (new ArgumentOutOfRangeException("Precision can't be a negative number."))
     let (test:double) = Math.Pow(10.0, double(precision))
     if(System.Double.IsInfinity(test) || (test > double(System.Int32.MaxValue))) then raise (new ArgumentOutOfRangeException("Precision of " + precision.ToString() + " decimal places is not supported."))
     let precision = int(Math.Pow(10.0, double(precision)))
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,到目前为止我所写的内容都充满了类型转换,这可能意味着我的代码没有按应有的方式编写。未完成的部分需要方法返回第一个元素,当计算到一定精度时返回 false。我确信必须有一些优雅的 F# 代码来实现这一目标,但显然我距离它还很远。我试图弄清楚 Array2D 类是否有某种方法可以让我做到这一点,但我无法找到它(如果有的话)。我知道 PowerPack Matrix 类并且最终会使用它,但现在我正在尝试通过将我理解的 C# 代码转换为 F# 来学习 F#。显然说起来容易做起来难。:) 我相信我已经在我创建的类型中添加了所有相关的 F# 代码。如果我遗漏了什么,请告诉我。

Tom*_*cek 5

一种优雅而高级的编写方式可能不会非常有效,那就是使用惰性序列表达式:

seq { for r in 0 .. this.Rows - 1 do
        for c in 0 .. this.Cols - 1 do
          if <your condition goes here> then
            yield false}
|> Seq.forall id
Run Code Online (Sandbox Code Playgroud)

false这个想法是,一旦矩阵中的第一个元素符合条件,序列就会生成。然后Seq.forall函数立即返回false(并停止迭代序列)。

在实践中,您可能需要使用递归函数来实现它以提高效率。这不是特别好(因为在 F# 中无法跳出循环),但您不应该经常需要这样的代码:

let rec loopRows r = 
  let rec loopCols c = 
    if c = this.Cols then true
    elif <your condition goes here> then false
    else loopCols (c + 1)
  if r = this.Rows then true       // Processed all rows
  elif not (loopCols 0) then false // Nonequal element in this row
  else loopRows (r + 1)            // Continue looping

loopRows 0
Run Code Online (Sandbox Code Playgroud)