-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadratic_extensions.nim
67 lines (57 loc) · 2.27 KB
/
quadratic_extensions.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
include prelude
import sugar, macros, math
type QuadraticExtension*[T; D:static T] = object
x: T
y: T
type ZZQ*[D:static ZZ] = QuadraticExtension[ZZ, D]
type ZZ_i* = ZZQ[initZZ(-1)]
type QQQ*[D:static QQ] = QuadraticExtension[QQ, D]
type QQ_i* = QQQ[initFrac(initZZ(-1),initZZ(-1))]
template zero*[T,D](R:typedesc[QuadraticExtension[T,D]]):R =
result.x = T.zero
result.y = T.zero
template one*[T,D](R:typedesc[QuadraticExtension[T,D]]):R =
result.x = T.one
result.y = T.zero
template sqrt*(a:static[int]):ZZQ[a] =
result.x = ZZ.zero
result.y = ZZ.one
func `$`*[T,D](a:QuadraticExtension[T,D]):string =
when D == -T.one:
return replace($a.x & " + " & $a.y & "i", "+ -", "- ")
else:
when len $D > 1:
return replace($a.x & " + " & $a.y & "√(" & $D & ")", "+ -", "- ")
else:
return replace($a.x & " + " & $a.y & "√" & $D, "+ -", "- ")
func conjugate*[T,D](a:QuadraticExtension[T,D]):QuadraticExtension[T,D] {.inline.} =
result.x = a.x
result.y = -a.y
func norm*[T,D](a:QuadraticExtension[T,D]):T {.inline.} =
a.x*a.x - a.y*a.y*D
func norm*(a:ZZi):ZZ {.inline.} =
a.x*a.x + a.y*a.y
func norm*(a:QQi):QQ {.inline.} =
a.x*a.x + a.y*a.y
func `+`*[T,D](a,b:QuadraticExtension[T,D]):QuadraticExtension[T,D] {.inline.} =
result.x = a.x + b.x
result.y = a.y + b.y
func `+=`*[T,D](a:var QuadraticExtension[T,D], b:QuadraticExtension[T,D]):QuadraticExtension[T,D] {.inline.} =
a = a + b
func `-`*[T,D](a,b:QuadraticExtension[T,D]):QuadraticExtension[T,D] {.inline.} =
result.x = a.x - b.x
result.y = a.y - b.y
func `-=`*[T,D](a:var QuadraticExtension[T,D], b:QuadraticExtension[T,D]):QuadraticExtension[T,D] {.inline.} =
a = a - b
func `-`*[T,D](a:QuadraticExtension[T,D]):QuadraticExtension[T,D] {.inline.} =
result.x = -a.x
result.y = -a.y
func `*`*[T,D](a,b:QuadraticExtension[T,D]):QuadraticExtension[T,D] {.inline.} =
result.x = a.x * b.x + D * a.y * b.y
result.y = a.y * b.x + a.x * b.y
func `*=`*[T,D](a:var QuadraticExtension[T,D], b:QuadraticExtension[T,D]):QuadraticExtension[T,D] {.inline.} =
a = a * b
func inv*[T,D](a:QuadraticExtension[T,D]):QuadraticExtension[T,D] {.inline.} =
let den = a.norm
result.x = a.x / den
result.y = -a.y / den