src/numericalnim/utils

Search:
Group by:
Source   Edit  

Utils

This module implements various procs that don't fit in any of the other modules.

Highlights:

  • meshgrid to produce meshed points.
  • linspace and arange to generate points.
  • chi2 to calculate χ²-test on data and fit.

Types

Vector[T] = object
  components*: seq[T]
  len*: int
Source   Edit  

Procs

proc `$`(v: Vector): string {.inline.}
Source   Edit  
proc `*`[T](d: float; v1: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `*`[T](v1, v2: Vector[T]): float {.inline.}
Source   Edit  
proc `*`[T](v1: Vector[T]; d: float): Vector[T] {.inline.}
Source   Edit  
proc `*.`[T](v1, v2: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `*.=`[T](v1: var Vector[T]; v2: Vector[T]) {.inline.}
Source   Edit  
proc `*=`[T](v1: var Vector[T]; d: float) {.inline.}
Source   Edit  
proc `+`[T](d: float; v1: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `+`[T](d: T; v1: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `+`[T](v1, v2: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `+`[T](v1: Vector[T]; d: float): Vector[T] {.inline.}
Source   Edit  
proc `+`[T](v1: Vector[T]; d: T): Vector[T] {.inline.}
Source   Edit  
proc `+=`[T](v1: var Vector[T]; d: float) {.inline.}
Source   Edit  
proc `+=`[T](v1: var Vector[T]; d: T) {.inline.}
Source   Edit  
proc `+=`[T](v1: var Vector[T]; v2: Vector[T]) {.inline.}
Source   Edit  
proc `-`[T](d: float; v1: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `-`[T](d: T; v1: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `-`[T](v1, v2: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `-`[T](v1: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `-`[T](v1: Vector[T]; d: float): Vector[T] {.inline.}
Source   Edit  
proc `-`[T](v1: Vector[T]; d: T): Vector[T] {.inline.}
Source   Edit  
proc `-=`[T](v1: var Vector[T]; d: float) {.inline.}
Source   Edit  
proc `-=`[T](v1: var Vector[T]; d: T) {.inline.}
Source   Edit  
proc `-=`[T](v1: var Vector[T]; v2: Vector[T]) {.inline.}
Source   Edit  
proc `/`[T](v1: Vector[T]; d: float): Vector[T] {.inline.}
Source   Edit  
proc `/.`[T](v1, v2: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc `/.=`[T](v1: var Vector[T]; v2: Vector[T]) {.inline.}
Source   Edit  
proc `/=`[T](v1: var Vector[T]; d: float) {.inline.}
Source   Edit  
proc `==`[T](v1, v2: Vector[T]): bool {.inline.}
Source   Edit  
proc `@`[T](v: Vector[T]): seq[T] {.inline.}
Source   Edit  
proc `@`[T](v: Vector[Vector[T]]): seq[seq[T]] {.inline.}
Source   Edit  
proc `@`[T](v: Vector[Vector[Vector[T]]]): seq[seq[seq[T]]] {.inline.}
Source   Edit  
proc `[]`[T](v: var Vector[T]; i: int): var T {.inline.}
Source   Edit  
proc `[]`[T](v: Vector[T]; i: int): T {.inline.}
Source   Edit  
proc `[]=`[T](v: var Vector[T]; i: int; value: T) {.inline.}
Source   Edit  
proc `^`[float](v: Vector[float]; power: float): Vector[float] {.inline.}
Returns a Vector object after raising each element to a power (float powers) Source   Edit  
proc `^`[float](v: Vector[float]; power: Natural): Vector[float] {.inline.}
Returns a Vector object after raising each element to a power (Natural number powers) Source   Edit  
proc abs[T](v1: Vector[T]): Vector[T] {.inline.}
Source   Edit  
proc arange(x1, x2, dx: float; includeStart = true; includeEnd = false): seq[
    float] {.inline, ...raises: [ValueError], tags: [], forbids: [].}
Generates points between x1 and x2 with spacing dx. By default, x2 is not included unless it ends up exactly on it. Set includeEnd = true if you want to include it. Source   Edit  
proc calcError[T](y_true, y: T): auto {.inline.}
Source   Edit  
proc checkVectorSizes(v1, v2: Vector) {.inline.}
Source   Edit  
proc chi2[T](yData, yFit, yError: seq[T] or Tensor[T]): T
Calculates the χ²-test on the fit of a curve to data points. Source   Edit  
proc clone[T](x: T): T {.inline.}
Source   Edit  
proc delete[T](s: var seq[T]; idx: seq[int])
Deletes the elements of seq s at indices idx. idx must not contain duplicates! Source   Edit  
proc dot[T](v1, v2: Vector[T]): float {.inline.}
Source   Edit  
proc findDuplicates[T](x: openArray[T]; isSorted: bool = false): seq[seq[int]]
Finds and returns the indices of duplicates in an openArray. 1, 1, 2, 1, 4, 5, 4 would yield @@[0, 1, 3], @[4, 6] for the duplicate 1's and 4's Source   Edit  
proc getIndexTable[T](x: openArray[T]): Table[T, seq[int]]
Source   Edit  
proc hermiteInterpolate[T](x: openArray[float]; t: openArray[float];
                           y, dy: openArray[T]): seq[T] {.inline.}
Hermite interpolate data points.
  • x is the points to evaluate the spline in
  • t, y, dy are the raw input values used to form the spline
Source   Edit  
proc hermiteSpline[T](x, x1, x2: float; y1, y2, dy1, dy2: T): T {.inline.}
Source   Edit  
proc isClose[T](y1, y2: T; tol: float = 0.001): bool {.inline.}
Source   Edit  
proc linspace(x1, x2: float; N: int): seq[float] {.inline, ...raises: [ValueError],
    tags: [], forbids: [].}
Generates N evenly spaced points between (and including) x1 and x2. Source   Edit  
proc mean_squared_error[T](v1, v2: Vector[T]): float {.inline.}
Source   Edit  
proc mean_squared_error[T](y_true, y: T): auto {.inline.}
Source   Edit  
proc meshgrid[T](ts: varargs[Tensor[T]]): Tensor[T]

Creates a Tensor with the coordinates on a meshgrid defined by the inputs. Each input is expected to be a 1D Tensor with the grid values in that dimension. If N inputs are given, a N-dimensional meshgrid will be created and returned in a (nPoints, N) Tensor.

Note: This is different from for example MATLAB's meshgrid which returns a N-dimensional Tensor instead.

Source   Edit  
proc meshgridFlat[T](x, y: Tensor[T]): (Tensor[T], Tensor[T])
Returns two flat (rank 1) tensors with the grid coordinates specified by x and y. Source   Edit  
proc newVector[T](components: openArray[T]): Vector[T] {.inline.}
Source   Edit  
proc norm(v1: Vector; p: int = 2): float64 {.inline.}
Calculate various norms of our Vector class Source   Edit  
proc removeDuplicates[Tx, Ty](x: seq[Tx]; y: seq[seq[Ty]]): tuple[x: seq[Tx],
    y: seq[seq[Ty]]]
Pure duplicates, ie exactly the same x and y values, are removed. If impure duplicate, same x but different y, exception will be raised. Source   Edit  
proc size[T](v: Vector[T]): int
Source   Edit  
proc sortAndTrimDataset[Tx, Ty](x: seq[Tx]; y: seq[seq[Ty]];
                                sortOrder: SortOrder = Ascending): tuple[
    x: seq[Tx], y: seq[seq[Ty]]]
Sorts x and y according to x and removes duplicate values. Source   Edit  
proc sortAndTrimDataset[Tx, Ty](x: seq[Tx]; y: seq[Ty];
                                sortOrder: SortOrder = Ascending): tuple[
    x: seq[Tx], y: seq[Ty]]
Sorts x and y according to x and removes duplicate values. Source   Edit  
proc sortDataset[T](X: openArray[float]; Y: openArray[T]): seq[(float, T)] {.
    inline.}
Source   Edit  
proc sortDataset[Tx, Ty](x: seq[Tx]; y: seq[seq[Ty]];
                         sortOrder: SortOrder = Ascending): tuple[x: seq[Tx],
    y: seq[seq[Ty]]]
Sorts seqs of according to the first seq in the openarray. Source   Edit  
proc sortDataset[Tx, Ty](x: seq[Tx]; y: seq[Ty];
                         sortOrder: SortOrder = Ascending): tuple[x: seq[Tx],
    y: seq[Ty]]
Source   Edit  
proc sum[T](v: Vector[T]): T {.inline.}
Source   Edit  
proc toTensor(v: Vector): Tensor[float] {.inline.}
Source   Edit  

Iterators

iterator items[T](v: Vector[T]): T
Source   Edit  
iterator mitems[T](v: var Vector[T]): var T
Source   Edit  
iterator pairs[T](v: Vector[T]): (int, T)
Source   Edit  

Templates

template `+.`[T](d: float; v1: Vector[T]): Vector[T]
Source   Edit  
template `+.`[T](v1: Vector[T]; d: float): Vector[T]
Source   Edit  
template `+.=`[T](v1: var Vector[T]; d: float)
Source   Edit  
template `-.`[T](d: float; v1: Vector[T]): Vector[T]
Source   Edit  
template `-.`[T](v1: Vector[T]; d: float): Vector[T]
Source   Edit  
template `-.=`[T](v1: var Vector[T]; d: float)
Source   Edit  
template benchmarkit[T](s: untyped; n = 100; msg = ""; answer: T;
                        onlyEfficiency = false): untyped
Source   Edit  
template timeit(s: untyped; n = 100; msg = ""): untyped
Source   Edit