diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..04e70ca --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "nimsaem.nimvscode" + ] +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..224af64 --- /dev/null +++ b/.vscode/launch.json @@ -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}/", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/linear.nimble b/linear.nimble new file mode 100644 index 0000000..83d7376 --- /dev/null +++ b/linear.nimble @@ -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" diff --git a/src/linear.exe b/src/linear.exe new file mode 100644 index 0000000..b34e548 Binary files /dev/null and b/src/linear.exe differ diff --git a/src/linear.nim b/src/linear.nim new file mode 100644 index 0000000..3a62c78 --- /dev/null +++ b/src/linear.nim @@ -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..((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..S): auto = + proc ff(v:Vec[N,R]) : Vec[N,S] = + var v2:Vec[N,S] + for i in 0..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 + diff --git a/src/linear/submodule.nim b/src/linear/submodule.nim new file mode 100644 index 0000000..d49b05e --- /dev/null +++ b/src/linear/submodule.nim @@ -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") diff --git a/tests/config.nims b/tests/config.nims new file mode 100644 index 0000000..3bb69f8 --- /dev/null +++ b/tests/config.nims @@ -0,0 +1 @@ +switch("path", "$projectDir/../src") \ No newline at end of file diff --git a/tests/test1.exe b/tests/test1.exe new file mode 100644 index 0000000..1759acb Binary files /dev/null and b/tests/test1.exe differ diff --git a/tests/test1.nim b/tests/test1.nim new file mode 100644 index 0000000..97f0b60 --- /dev/null +++ b/tests/test1.nim @@ -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))