-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into clonk/improve-npy-array-interop
- Loading branch information
Showing
10 changed files
with
861 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
name: scinim CI | ||
on: | ||
push: | ||
paths: | ||
- 'tests/**' | ||
- '**' | ||
- 'scinim.nimble' | ||
- '.github/workflows/ci.yml' | ||
pull_request: | ||
paths: | ||
- 'tests/**' | ||
- '**' | ||
- 'scinim.nimble' | ||
- '.github/workflows/ci.yml' | ||
|
||
jobs: | ||
build: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
nim: | ||
- '1.6.x' | ||
- '2.0.x' | ||
- 'stable' | ||
os: | ||
- ubuntu-latest | ||
- windows-latest | ||
- macOS-latest | ||
name: '${{ matrix.nim }} (${{ matrix.os }})' | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
path: scinim | ||
|
||
- name: Setup nim | ||
uses: jiro4989/setup-nim-action@v1 | ||
with: | ||
nim-version: ${{ matrix.nim }} | ||
repo-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Setup MSYS2 (Windows) | ||
if: ${{matrix.target == 'windows'}} | ||
uses: msys2/setup-msys2@v2 | ||
with: | ||
path-type: inherit | ||
update: true | ||
install: base-devel git mingw-w64-x86_64-toolchain | ||
|
||
- name: Install dependencies (Ubuntu) | ||
if: ${{matrix.target == 'linux'}} | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install -y python-numpy python3-numpy | ||
- name: Install dependencies (OSX) | ||
if: ${{matrix.target == 'macos'}} | ||
run: | | ||
brew install numpy | ||
- name: Install dependencies (Windows) | ||
if: ${{matrix.target == 'windows'}} | ||
shell: msys2 {0} | ||
run: | | ||
pacman -Syu --noconfirm | ||
pacman -S --needed --noconfirm mingw-w64-x86_64-lapack | ||
pacman -S --needed --noconfirm mingw-w64-x86_64-python-numpy | ||
- name: Setup nimble & deps | ||
shell: bash | ||
run: | | ||
cd scinim | ||
nimble refresh -y | ||
nimble install -y | ||
- name: Run tests (Linux & OSX) | ||
if: ${{matrix.target != 'windows'}} | ||
shell: bash | ||
run: | | ||
cd scinim | ||
nimble -y test | ||
- name: Run tests (Windows) | ||
if: ${{matrix.target == 'windows'}} | ||
shell: msys2 {0} | ||
run: | | ||
cd scinim | ||
nimble -y test | ||
- name: Build docs | ||
if: > | ||
github.event_name == 'push' && github.ref == 'refs/heads/master' && | ||
matrix.target == 'linux' && matrix.branch == 'devel' | ||
shell: bash | ||
run: | | ||
cd scinim | ||
branch=${{ github.ref }} | ||
branch=${branch##*/} | ||
nimble doc --project --outdir:docs \ | ||
'--git.url:https://github.com/${{ github.repository }}' \ | ||
'--git.commit:${{ github.sha }}' \ | ||
"--git.devel:$branch" \ | ||
scinim.nim | ||
# Ignore failures for older Nim | ||
cp docs/{the,}index.html || true | ||
- name: Publish docs | ||
if: > | ||
github.event_name == 'push' && github.ref == 'refs/heads/master' && | ||
matrix.target == 'linux' && matrix.branch == 'devel' | ||
uses: crazy-max/ghaction-github-pages@v1 | ||
with: | ||
build_dir: scinim/docs | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import ./scinim/signals/signals | ||
import ./scinim/signals | ||
export signals | ||
|
||
import ./scinim/numpyarrays | ||
export numpyarrays | ||
|
||
import ./scinim/fuse_loops | ||
export fuse_loops |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import std / [macros, math] | ||
export math | ||
|
||
## This module contains a whole bunch of fun little sugar procs / templates / macros | ||
## to (mostly) help with writing math code. The most likely use case might be for | ||
## illustrative / explanatory code that is close to math in nature already. | ||
|
||
|
||
proc getTypeReplaceI(arg: NimNode): NimNode = | ||
if arg.len > 0: | ||
result = newTree(arg.kind) | ||
for ch in arg: | ||
result.add getTypeReplaceI(ch) | ||
else: | ||
case arg.kind | ||
of nnkIdent, nnkSym: | ||
if arg.strVal == "i": return newLit(0) | ||
else: return arg | ||
else: return arg | ||
|
||
proc Σ*[T](s: openArray[T]): T = s.sum | ||
proc Π*[T](s: openArray[T]): T = s.prod | ||
|
||
proc √*[T](x: T): T = sqrt(x) | ||
proc √*[T](x: openArray[T]): T = sqrt(x) | ||
|
||
template Σ_i*(frm, to: int, body: untyped): untyped = | ||
var res: int | ||
for i {.inject.} in frm ..< to: | ||
res += body | ||
res | ||
|
||
macro Σ_i*(col, body: untyped): untyped = | ||
let typ = getTypeReplaceI(body) | ||
let iId = ident"i" | ||
result = quote do: | ||
var res: typeof(`typ`) | ||
for `iId` in 0 ..< `col`.len: | ||
res += `body` | ||
res | ||
|
||
macro λ*(arg, body: untyped): untyped = | ||
## | ||
# XXX: Support multiple arguments! | ||
if arg.kind != nnkInfix or | ||
(arg.kind == nnkInfix and arg[0].kind in {nnkIdent, nnkSym} and arg[0].strVal != "->"): | ||
error("Unsupported operation in `λ`. The infix must be `->`, but is: " & arg[0].repr) | ||
let a = arg[1] | ||
let typ = arg[2] | ||
result = quote do: | ||
proc(`a`: `typ`): auto = `body` | ||
|
||
proc sliceTypes(n: NimNode, sl: Slice[int]): tuple[args, genTyps: NimNode] = | ||
var args = nnkFormalParams.newTree(ident"auto") | ||
var genTyps = nnkIdentDefs.newTree() | ||
for i in sl.a .. sl.b: | ||
let typ = ident($char('A'.ord + i - 1)) | ||
args.add nnkIdentDefs.newTree(n[i], | ||
typ, | ||
newEmptyNode()) | ||
genTyps.add typ | ||
genTyps.add newEmptyNode() | ||
genTyps.add newEmptyNode() | ||
genTyps = nnkGenericParams.newTree(genTyps) | ||
result = (args: args, genTyps: genTyps) | ||
|
||
proc generateFunc(arg: NimNode): NimNode = | ||
expectKind(arg, nnkAsgn) | ||
let lhs = arg[0] | ||
let rhs = arg[1] | ||
let fnName = lhs[0] | ||
let (fnArgs, genTyps) = sliceTypes(lhs, 1 ..< lhs.len) | ||
result = newProc(name = fnName, body = rhs) | ||
result[2] = genTyps | ||
result[3] = fnArgs | ||
|
||
macro mathScope*(args: untyped): untyped = | ||
expectKind(args, nnkStmtList) | ||
result = newStmtList() | ||
for arg in args: | ||
result.add generateFunc(arg) |
Oops, something went wrong.