Skip to content

Instantly share code, notes, and snippets.

@andreasnoack
Created February 21, 2014 11:05
Show Gist options
  • Save andreasnoack/9132511 to your computer and use it in GitHub Desktop.
Save andreasnoack/9132511 to your computer and use it in GitHub Desktop.
GF(2) Gaussian elimination
zeror = Rational(gf2(0),gf2(1))
oner = Rational(gf2(1),gf2(1))
A = [oner oner;zeror oner];
b = [oner,oner]
x = A\b
# Show doesn't work for Rational{GF2}, but you can do
x[1].num
x[2].num
import Base: &,|,<,+,-,*,/,abs,and_int,box,convert,div,or_int,rem,promote_rule,show,udiv_int,ult_int,unbox,urem_int,xor_int
bitstype 8 GF2 <: Unsigned
convert(::Type{GF2}, x::GF2) = x
convert(::Type{GF2}, x::Uint8) = box(GF2,unbox(Uint8,x))
convert(::Type{GF2}, x::Number) = convert(GF2,uint8(x))
convert(::Type{Int8}, x::GF2) = box(Int8,unbox(GF2,x))
convert(::Type{Uint8}, x::GF2) = box(Uint8,unbox(GF2,x))
convert(::Type{Uint64}, x::GF2) = uint64(uint8(x))
gf2(x::Number) = convert(GF2,x)
promote_rule(::Type{GF2},::Type{Uint64}) = Uint64
show(io::IO, x::GF2) = int8(x)
+(x::GF2, y::GF2) = box(GF2,xor_int(unbox(GF2,x),unbox(GF2,y)))
-(x::GF2, y::GF2) = box(GF2,xor_int(unbox(GF2,x),unbox(GF2,y)))
*(x::GF2, y::GF2) = box(GF2,and_int(unbox(GF2,x),unbox(GF2,y)))
-(x::GF2) = x
abs(x::GF2) = x
div(x::GF2, y::GF2) = box(GF2,udiv_int(unbox(GF2,x),unbox(GF2,y)))
rem(x::GF2, y::GF2) = box(GF2,urem_int(unbox(GF2,x),unbox(GF2,y)))
<(x::GF2, y::GF2) = ult_int(unbox(GF2,x),unbox(GF2,y))
(&)(x::GF2, y::GF2) = box(GF2,and_int(unbox(GF2,x),unbox(GF2,y)))
(|)(x::GF2, y::GF2) = box(GF2,or_int(unbox(GF2,x),unbox(GF2,y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment