gpt4 book ai didi

math - 你如何在 Nimrod 中使用矩阵?

转载 作者:行者123 更新时间:2023-12-04 15:44:36 25 4
gpt4 key购买 nike

我找到了 this GitHub 上的项目;它是为“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]

谢谢你们!

最佳答案

我想首先指出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 .不确定它们是否是您要找的东西,但至少它们看起来更新鲜。

关于math - 你如何在 Nimrod 中使用矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21416013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com