【问题标题】:How do you use matrices in Nimrod?你如何在 Nimrod 中使用矩阵?
【发布时间】:2014-01-28 20:23:54
【问题描述】:

我在 GitHub 上找到了this 项目;它是为“nimrod 矩阵”返回的唯一搜索词。我把它的基本部分改了一点,使它编译没有错误,然后我添加了最后两行来构建一个简单的矩阵,然后输出一个值,但是“getter”函数不起作用由于某些原因。我修改了添加属性的说明here,但有些地方不对。

到目前为止,这是我的代码。我想在 Nimrod 中使用 GNU 科学图书馆,我认为这是合乎逻辑的第一步。

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] )

这是我得到的错误:

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]

谢谢你们!

【问题讨论】:

    标签: math matrix nim-lang


    【解决方案1】:

    我想首先指出the matrix library you refer to 已经三岁了。对于由于更改而需要大量时间开发的编程语言,并且它不再使用当前的Nimrod git version进行编译:

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

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

    proc `[]`*[T](x: TMatrix[T], r,c: int): T =
    

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

    for x in 0 .. <2:
      for y in 0 .. <2:
        echo "x: ", x, " y: ", y, " = ", m[x,y]
    

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

    x: 0 y: 0 = 1
    x: 0 y: 1 = 2
    x: 1 y: 0 = 3
    x: 1 y: 1 = 4
    

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

    【讨论】:

    • 是的,我看到它已经很旧了,我只是把基础知识拿出来试着摆弄它们。我找到了 extmath,但它没有我需要的东西;我一定会看看linalg。谢谢!
    • 再次感谢,为了完整起见,我将添加“setter”proc 需要更改为 proc `[]=`*[T](x: var TMatrix[T], r,c: int, a: T) =
    猜你喜欢
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2021-04-09
    • 2015-10-22
    相关资源
    最近更新 更多