-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d44a817
Showing
9 changed files
with
146 additions
and
0 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,5 @@ | ||
{ | ||
"recommendations": [ | ||
"nimsaem.nimvscode" | ||
] | ||
} |
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,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}" | ||
} | ||
] | ||
} |
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,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 not shown.
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,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 | ||
|
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,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") |
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 @@ | ||
switch("path", "$projectDir/../src") |
Binary file not shown.
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,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)) |