我在这上找到了GitHub项目;它是唯一返回的"nimrod“搜索词。我把它的基本结构做了一些修改,这样编译就没有错误,然后我添加了最后两行来构建一个简单的矩阵,然后输出一个值,但是"getter“函数由于某些原因不能工作。我修改了添加属性的说明,找到了这里,但有些地方不对劲。
到目前为止这是我的密码。我想从尼姆罗德内部使用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]谢谢你们!
发布于 2014-01-29 09:27:59
我想首先指出,您所指的矩阵库已经三岁了。对于开发中的编程语言来说,由于更改需要花费大量时间,而且它不再使用当前的Nimrod版本进行编译
$ 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 search math命令显示了两个潜在的包:利纳格尔和可拓数学。不确定它们是否是你想要的,但至少它们看起来更新鲜。
https://stackoverflow.com/questions/21416013
复制相似问题