Ich habe this Projekt auf GitHub gefunden; es war der einzige Suchbegriff, der für "nimrod matrix" zurückgegeben wurde. Ich nahm die nackten Knochen davon und änderte es ein wenig, so dass es ohne Fehler kompiliert, und dann habe ich die letzten beiden Zeilen hinzugefügt, um eine einfache Matrix zu erstellen, und dann einen Wert ausgeben, aber die "Getter" -Funktion funktioniert nicht aus irgendeinem Grund. Ich habe die Anweisungen zum Hinzufügen von Eigenschaften angepasst here angepasst, aber etwas stimmt nicht.Wie benutzt man Matrizen in Nimrod?
Hier ist mein Code so weit. Ich würde gerne die GNU Scientific Library aus Nimrod verwenden, und ich dachte mir, dass dies der erste logische Schritt war.
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])
Dies ist der Fehler, den ich bekommen:
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]
Danke euch!
Ja, ich sah, dass es alt war, ich nahm nur die Grundlagen heraus und versuchte, an ihnen herumzuhantieren. Ich habe es gefunden, aber es hatte nicht das, was ich brauchte; Ich werde definitiv auf Linalg schauen. Vielen Dank! – cjohnson318
Noch einmal, zur Vollständigkeit, ich werde hinzufügen, dass der "Setter" 'proc" geändert werden muss in 'proc \' [] = \ '* [T] (x: var TMatrix [T], r, c: int, a: T) = '. – cjohnson318