Skip to content

Instantly share code, notes, and snippets.

# compile without (!) `--gc:arc` to get a SIGSEGV
import typetraits
type
RawMutableView*[T] = distinct ptr UncheckedArray[T]
CpuStorage*[T] = ref CpuStorageObj[T]
CpuStorageObj[T] = object
raw_buffer*: seq[T]
Tensor[T] = object
buf*: CpuStorage[T] # if tensor stores a `seq` itself it works fine
import sequtils
type
CpuStorage*[T] = ref CpuStorageObj[T]
CpuStorageObj[T] = object
size*: int
raw_buffer*: ptr UncheckedArray[T]
Tensor[T] = object
buf*: CpuStorage[T]
@Vindaar
Vindaar / rand_ggplotnim.nim
Last active December 4, 2021 19:09
Tossing with ggplotnim
# Note by @Vindaar: this is just a modified version from here:
# https://forum.nim-lang.org/t/7102
# that uses ggplotnim
# Generate random pairs of non-equal numbers and test the routine's distribution
# License: MIT
# Usage:
# ./randpairs [1-6;c]
import os, random, hashes, tables, strutils, stats, ggplotnim
@Vindaar
Vindaar / realign_halo3_pattern.nim
Created July 7, 2020 18:11
Toying around with the garbled frames in the Halo 3 trailer for MCC on PC
import nimPNG, sequtils, strutils, os, cligen
const startY = 257
const shiftWidth = 78
const shiftHeight = 9
const switch = 548
proc main(input: string,
output: string,
start = startY,
import times
import tables
import random
import ggplotnim
import weave
import locks
const runs = 1_000
const sizes = 1_000
var lock: Lock

These benches ran on:

basti at DESKTOP-G5IDKB7 in ~/src λ neofetch
       _,met$$$$$gg.          basti@DESKTOP-G5IDKB7
    ,g$$$$$$$$$$$$$$$P.       ---------------------
  ,g$$P"     """Y$$.".        OS: Debian GNU/Linux 10 (buster) on Windows 10 x86_64
 ,$$P'              `$$$.     Kernel: 4.4.0-18362-Microsoft
',$$P       ,ggs.     `$$b:   Uptime: 1 hour, 11 mins
`d$$'     ,$P"'   .    $$$    Packages: 545 (dpkg)
import plotly, random, sequtils
import json # to modify json
import strformat # to format the tick texts
# let's assume the 28 correspond to 28 inches and we want to print the
# x axis values in pixels using a dpi of 72.27
var zs = newSeqWith(28, newSeq[float32](28))
for x in 0 ..< 28:
for y in 0 ..< 28:
zs[x][y] = rand(1.0)
@Vindaar
Vindaar / tKernelDensity.nim
Created April 27, 2020 21:16
KDE in Nim for `geom_density`
import ggplotnim, seqmath, sequtils, stats, algorithm, strutils
type
KernelKind = enum
knCustom = "custom"
knBox = "box"
knTriangular = "triangular"
knTrig = "trigonometric"
knEpanechnikov = "epanechnikov"
knGauss = "gauss"
@Vindaar
Vindaar / neural_spike_plot.org
Last active May 29, 2024 15:34
Raster plots for neuroscience in ggplotnim