Skip to content

Commit

Permalink
Merge branch 'main' into clonk/improve-npy-array-interop
Browse files Browse the repository at this point in the history
  • Loading branch information
Clonkk committed Jun 16, 2024
2 parents 165fbd3 + 95e301c commit 71bcc31
Show file tree
Hide file tree
Showing 10 changed files with 861 additions and 4 deletions.
115 changes: 115 additions & 0 deletions .github/workflows/ci.yml
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 }}
5 changes: 4 additions & 1 deletion scinim.nim
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
10 changes: 8 additions & 2 deletions scinim.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ license = "MIT"
backend = "cpp"

# Dependencies
requires "nim >= 1.4.0"
requires "nim >= 1.6.0"
requires "threading"
requires "arraymancer >= 0.7.3"
requires "arraymancer >= 0.7.32"
requires "polynumeric >= 0.2.0"
requires "nimpy >= 0.2.0"

task test, "Run all tests":
exec "nim c -r tests/tnumpyarrays.nim"
exec "nim c -r --gc:orc tests/tnumpyarrays.nim"
exec "nim cpp -r tests/tnumpyarrays.nim"
exec "nim cpp -r --gc:orc tests/tnumpyarrays.nim"
81 changes: 81 additions & 0 deletions scinim/experimental/sugar.nim
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)
Loading

0 comments on commit 71bcc31

Please sign in to comment.