Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rotu committed Jul 6, 2022
0 parents commit d44a817
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"nimsaem.nimvscode"
]
}
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/<your program>",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
12 changes: 12 additions & 0 deletions linear.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Package

version = "0.1.0"
author = "dan"
description = "Dan's magic linear algebra library"
license = "MIT"
srcDir = "src"


# Dependencies

requires "nim >= 1.6.6"
Binary file added src/linear.exe
Binary file not shown.
74 changes: 74 additions & 0 deletions src/linear.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

import std/sequtils
import std/sugar

type scalar=float64

type
VecSpace = object
dimension: int
name: string



proc `==`*[N:static[int],R] (x: Vec[N,R], y: Vec[N,R]): bool =
for i in 0..<N:
if x.coords[i] != y.coords[i]:
return false
return true


func toVector *[N:static[int], S](x:array[N,S]): Vec[N,S] =
Vec[N,S](coords: x)

func zero*[N:static[int], R](): Vec[N,R]=
let ar :array[N,R]
return ar.toVector

type
Semiring*[N] = object
Zero: N
One: N
`+`: (x:N)->((y:N)->N)
`*`: (x:N)->((y:N)->N)

type V3f = Vec[3,float32]
type V4d = Vec[4,float64]

proc map *[N:static[int],R,S](v:Vec[N,R], fn:R->S): Vec[N,S] =
for i in 0..<N:
result.coords[i]=fn(v.coords[i])

proc fpar *[N:static[int],R,S](fn:R->S): auto =
proc ff(v:Vec[N,R]) : Vec[N,S] =
var v2:Vec[N,S]
for i in 0..<N:
v2.coords[i] = fn(v.coords[i])
return ff




# proc elementwise*[S:type, T:type](fn:S->T): auto =
# proc fn2[N:static[int]](v:Vec[N,S]) : Vec[N,T] =
# result:Vec[N,T]
# for i in 0..N:
# result[i] = fn(v[i])
# return fn2

func inner[N:static[int],R](x:Vec[N,R], y:Vec[N,R]): R =
result:R
for i in 0..N:
result += x[i]*y[i] # todo: conjugate


func nonzero *(x:int): bool =
return x != 0

func nonzero *[S=float32, n](x:Vec[n,S]) =
return x.any( x => x != 0)

func `+=`[V:Vec](x: var V; y: V) =
for (i,a) in y.pairs:
x[i]+=a

12 changes: 12 additions & 0 deletions src/linear/submodule.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is just an example to get you started. Users of your library will
# import this file by writing ``import linear/submodule``. Feel free to rename or
# remove this file altogether. You may create additional modules alongside
# this file as required.

type
Submodule* = object
name*: string

proc initSubmodule*(): Submodule =
## Initialises a new ``Submodule`` object.
Submodule(name: "Anonymous")
1 change: 1 addition & 0 deletions tests/config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
switch("path", "$projectDir/../src")
Binary file added tests/test1.exe
Binary file not shown.
26 changes: 26 additions & 0 deletions tests/test1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This is just an example to get you started. You may wish to put all of your
# tests into a single file, or separate them into multiple `test1`, `test2`
# etc. files (better names are recommended, just make sure the name starts with
# the letter 't').
#
# To run these tests, simply execute `nimble test`.

import unittest
import sugar

import linear

test "can create":
var y: Vec[3,float32]
check y == y

let x: Vec[4,float] = [1.3,2.1,3.1,0.4].toVector

test "elementwise":
var x:Vec[3,float32]
let x2 = map(x,l=>l+1)

echo x
echo x2

echo (fpar(r:float32 => r + 1)(x))

0 comments on commit d44a817

Please sign in to comment.