-
Notifications
You must be signed in to change notification settings - Fork 0
/
algebraic.jl
178 lines (115 loc) · 3.29 KB
/
algebraic.jl
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
baremodule algebraic
#must import operators since baremodule doesnt (AND that is the intention)
import Base.+
import Base.*
import Base.-
import Base./
abstract Algebraic {T}
abstract Binop {T} <: Algebraic {T}
abstract Semigroup {T} <: Binop {T}
abstract Monoid {T} <: Semigroup {T}
abstract Group {T} <: Monoid {T}
abstract BinopAdditive {T} <: Binop {T}
abstract SemigroupAdditive {T} <: Semigroup {T}
abstract MonoidAdditive {T} <: Monoid {T}
abstract GroupAdditive {T} <: Group {T}
abstract BinopMultipicative {T} <: Binop {T}
abstract SemigroupMultipicative {T} <: Semigroup {T}
abstract MonoidMultipicative {T} <: Monoid {T}
abstract GroupMultipicative {T} <: Group {T}
#doesnt work
#abstract GroupAddMul{T} <: Union(GroupAdditive{T},GroupMultipicative{T} )
# abstract FieldAbs {T} <: GroupAddMul {T}
#abstract FieldAbs {T} <: GroupAdditive {T}
#abstract FieldAbs {T} <: GroupMultipicative {T}
#abstract FieldAbs {T}
# doesn't work verified
# julia> subtypes(GroupMultipicative)
# 1-element Array{Any,1}:
# Groupm{T}
abstract FieldAbs {T} <: GroupAdditive {T},GroupMultipicative {T}
type Binopa {T} <: BinopAdditive {T}
v::T
end
type Binopm {T}<: BinopMultipicative {T}
v::T
end
#+(a::Binopa, b::Binopa)=Binopa(a.v+b.v)
# +(a::Binop {T}, b::Binop {T})=Binopa{T}(a.v+b.v)
# *(a::Binop {T}, b::Binop {T})=Binopm{T}(a.v*b.v)
+(a::Binop , b::Binop )=Binopa(a.v+b.v)
*(a::Binop , b::Binop )=Binopm(a.v*b.v)
#like adding an integer n times m+m+m=3*m
*(n::Integer , a::Binop )=Binopa(n*a.v)
#multiplying an element by itself n times
^( a::Binop,n::Integer )=Binopm(a.v^n)
type Semigroupa{T} <: SemigroupAdditive {T}
v::T
end
type Semigroupm{T} <: SemigroupMultipicative {T}
v::T
end
+(a::Semigroup , b::Semigroup )=Semigroupa(a.v+b.v)
*(a::Semigroup , b::Semigroup )=Semigroupm(a.v*b.v)
type Monoida{T} <: MonoidAdditive {T}
zero::T
v::T
end
type Monoidm{T} <: MonoidMultipicative {T}
unit::T
v::T
end
getId(a::Monoida)=a.zero
getId(a::Monoidm)=a.unit
+(a:: Monoid , b:: Monoid )=Monoida(getId(a),a.v+b.v)
*(a:: Monoid , b:: Monoid )= Monoidm(getId(a),a.v*b.v)
type Groupa{T} <: GroupAdditive {T}
zero::T
v::T
end
type Groupm{T} <: GroupMultipicative {T}
unit::T
v::T
end
getId(a::Groupa)=a.zero
getId(a::Groupm)=a.unit
+(a:: Group , b:: Group )=Groupa(getId(a),a.v+b.v)
*(a:: Group , b:: Group )= Groupm(getId(a),a.v*b.v)
-(a:: Group , b:: Group )=Groupa(getId(a),a.v-b.v)
-(a:: Group )=Groupa(getId(a),-a.v)
#/(a:: Group , b:: Group )= Groupm(getId(a),a.v/b.v)
/(a:: Group , b:: Group )= Groupm(a.unit,a.v/b.v)
#/(a:: Group )= Groupm(getId(a),getId(a)/a.v)
/(a:: Group )= Groupm(a.unit,a.unit/a.v)
type Field{T} <: FieldAbs {T}
#need additive and multiplicative, 0 and 1
zero::T
unit::T
v::T
end
getZero(a::Field)=a.zero
getUnit(a::Field)=a.unit
+(a:: FieldAbs , b:: FieldAbs )=Field(getZero(a),getUnit(a),a.v+b.v)
*(a:: FieldAbs , b:: FieldAbs )= Field(getZero(a),getUnit(a),a.v*b.v)
-(a:: FieldAbs , b:: FieldAbs )=Field(getZero(a),getUnit(a),a.v-b.v)
-(a:: FieldAbs )=Field(getZero(a),getUnit(a),-a.v)
/(a:: FieldAbs , b:: FieldAbs )= Field(getZero(a),getUnit(a),a.v/b.v)
/(a:: FieldAbs )= Field(getZero(a),getUnit(a)/a.v)
export
Binopa,
Binopm,
Semigroupa,
Semigroupm,
Monoida,
Monoidm,
Groupa,
Groupm,
Field,
getZero,
getUnit,
getId,
+,
*,
-,
/
end