你如何在Nimrod中使用矩阵?

cjo*_*318 6 math matrix nimrod nim-lang

我在GitHub上找到了这个项目; 这是"nimrod矩阵"唯一返回的搜索词.我拿了它的骨头并稍微更改它以便编译没有错误,然后我添加最后两行来构建一个简单的矩阵,然后输出一个值,但"getter"函数不起作用由于某些原因.我修改了添加此处的属性的说明,但有些事情是不对的.

到目前为止,这是我的代码.我想在Nimrod中使用GNU Scientific Library,我认为这是第一个合乎逻辑的步骤.

type 
  TMatrix*[T] = object
    transposed: bool
    dataRows: int
    dataCols: int
    data: seq[T]

proc index[T](x: TMatrix[T], r,c: int): int {.inline.} = 
  if r<0  or  r>(x.rows()-1):
    raise newException(EInvalidIndex, "matrix index out of range")
  if c<0  or  c>(x.cols()-1):
    raise newException(EInvalidIndex, "matrix index out of range")
  result = if x.transposed: c*x.dataCols+r else: r*x.dataCols+c

proc rows*[T](x: TMatrix[T]): int {.inline.} = 
  ## Returns the number of rows in the matrix `x`.
  result = if x.transposed: x.dataCols else: x.dataRows

proc cols*[T](x: TMatrix[T]): int {.inline.}  = 
  ## Returns the number of columns in the matrix `x`.
  result = if x.transposed: x.dataRows else: x.dataCols

proc matrix*[T](rows, cols: int, d: openarray[T]): TMatrix[T] = 
  ## Constructor.  Initializes the matrix by allocating memory
  ## for the data and setting the number of rows and columns
  ## and sets the data to the values specified in `d`.
  result.dataRows = rows
  result.dataCols = cols
  newSeq(result.data, rows*cols)
  if len(d)>0:
    if len(d)<(rows*cols):
      raise newException(EInvalidIndex, "insufficient data supplied in matrix     constructor")

    for i in countup(0,rows*cols-1):
      result.data[i] = d[i]

proc `[][]`*[T](x: TMatrix[T], r,c: int): T = 
  ## Element access.  Returns the element at row `r` column `c`.
  result = x.data[x.index(r,c)]

proc `[][]=`*[T](x: var TMatrix[T], r,c: int, a: T) = 
  ## Sets the value of the element at row `r` column `c` to
  ## the value supplied in `a`.
  x.data[x.index(r,c)] = a

var m = matrix( 2, 2, [1,2,3,4] )

echo( $m[0][0] )
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

c:\program files (x86)\nimrod\config\nimrod.cfg(36, 11) Hint: added path:       'C:\Users\H127\.babel\libs\' [Path]
Hint: used config file 'C:\Program Files (x86)\Nimrod\config\nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: mat [Processing]
mat.nim(48, 9) Error: type mismatch: got (TMatrix[int], int literal(0))
but expected one of: 
system.[](a: array[Idx, T], x: TSlice[Idx]): seq[T]
system.[](a: array[Idx, T], x: TSlice[int]): seq[T]
system.[](s: string, x: TSlice[int]): string
system.[](s: seq[T], x: TSlice[int]): seq[T]
Run Code Online (Sandbox Code Playgroud)

谢谢你们!

Grz*_*icz 2

我想首先指出,您提到的矩阵库已有三年历史。对于正在开发的编程语言来说,由于变化需要花费很多时间,并且它不再使用当前的Nimrod git 版本进行编译:

$ nimrod c matrix
...
private/tmp/n/matrix/matrix.nim(97, 8) Error: ']' expected
Run Code Online (Sandbox Code Playgroud)

它在双数组访问器上失败,这似乎改变了语法。我猜您创建双[][]访问器的尝试是有问题的,它可能是不明确的:您是访问对象的双数组访问器还是访问第一个括号返回的嵌套数组?我必须将其更改proc为以下内容:

proc `[]`*[T](x: TMatrix[T], r,c: int): T =
Run Code Online (Sandbox Code Playgroud)

更改之后,您还需要更改访问矩阵的方式。这是我得到的:

for x in 0 .. <2:
  for y in 0 .. <2:
    echo "x: ", x, " y: ", y, " = ", m[x,y]
Run Code Online (Sandbox Code Playgroud)

基本上,您不需要指定两个括号访问,而是在单个括号内传递所有参数。该代码生成:

x: 0 y: 0 = 1
x: 0 y: 1 = 2
x: 1 y: 0 = 3
x: 1 y: 1 = 4
Run Code Online (Sandbox Code Playgroud)

关于寻找 Nimrod 的软件,我建议您使用Nimrod 的包管理器 Nimble。安装后,您可以搜索可用和维护的软件包。该命令nimble search math显示两个潜在的包:linaglextmath。不确定它们是否是您要找的,但至少它们看起来更新鲜