Skip to content

Commit

Permalink
changed various API namings for v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Jun 29, 2019
1 parent f8e47df commit 49ce23b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DirectSum"
uuid = "22fd7b30-a8c0-5bf2-aabe-97783860d07c"
authors = ["Michael Reed"]
version = "0.2.5"
version = "0.3.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
108 changes: 62 additions & 46 deletions src/DirectSum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module DirectSum
# This file is part of DirectSum.jl. It is licensed under the GPL license
# Grassmann Copyright (C) 2019 Michael Reed

export VectorSpace, Signature, DiagonalForm, ℝ, , value, tangent
export VectorBundle, Signature, DiagonalForm, ℝ, , value, tangent
import Base: getindex, abs, @pure, +, *, ^, , , ,
import LinearAlgebra: det
using StaticArrays
Expand All @@ -21,19 +21,35 @@ const vio = ('∞','∅')
signbit(x...) = Base.signbit(x...)
signbit(x::Symbol) = false
signbit(x::Expr) = x.head == :call && x.args[1] == :-
PROD(x) = Base.prod(x)
SUB(x) = Base.:-(x)
SUB(x::Symbol) = :(-$x)
SUB(x::SArray) = Base.:-(x)
SUB(x::SArray{Tuple{M},T,1,M} where M) where T<:Any = broadcast(SUB,x)
conj(z) = Base.conj(z)
inv(z) = Base.inv(z)
/(a,b) = Base.:/(a,b)
-(x) = Base.:-(x)
-(a,b) = Base.:-(a,b)
-(x::Symbol) = :(-$x)
-(x::SArray) = Base.:-(x)
-(x::SArray{Tuple{M},T,1,M} where M) where T<:Any = broadcast(-,x)

for (OP,op) ((:∏,:*),(:∑,:+))
@eval begin
$OP(x...) = Base.$op(x...)
$OP(x::AbstractVector{T}) where T<:Any = $op(x...)
end
end

const PROD,SUM,SUB = ∏,∑,-

## Manifold{N}

abstract type Manifold{Indices} end

## VectorSpace{N}
## VectorBundle{N}

abstract type VectorSpace{Indices,Options,Metrics,Diff} end
abstract type VectorBundle{Indices,Options,Metrics,Diff} <: Manifold{Indices} end

## Signature{N}

struct Signature{Indices,Options,Signatures,Diff} <: VectorSpace{Indices,Options,Signatures,Diff}
struct Signature{Indices,Options,Signatures,Diff} <: VectorBundle{Indices,Options,Signatures,Diff}
@pure Signature{N,M,S,D}() where {N,M,S,D} = new{N,M,S,D}()
end

Expand Down Expand Up @@ -61,16 +77,16 @@ end

@inline getindex(vs::Signature,i::Vector) = [getindex(vs,j) for j i]
@inline getindex(vs::Signature,i::UnitRange{Int}) = [getindex(vs,j) for j i]
@inline getindex(vs::Signature{N,M,S,D} where S,i::Colon) where {N,M,D} = getindex(vs,1:N-(dualtype(vs)<0 ? 2D : D))
Base.firstindex(m::VectorSpace) = 1
Base.lastindex(m::VectorSpace{N}) where N = N
Base.length(s::VectorSpace{N}) where N = N
@inline getindex(vs::Signature{N,M,S,D} where S,i::Colon) where {N,M,D} = getindex(vs,1:N-(mixedmode(vs)<0 ? 2D : D))
Base.firstindex(m::VectorBundle) = 1
Base.lastindex(m::VectorBundle{N}) where N = N
Base.length(s::VectorBundle{N}) where N = N

@inline sig(s::Bool) = s ? '-' : '+'

function Base.show(io::IO,s::Signature)
print(io,'')
C,d = dualtype(s),diffmode(s)
C,d = mixedmode(s),diffmode(s)
N = ndims(s)-(d>0 ? (C<0 ? 2d : d) : 0)
hasinf(s) && print(io,vio[1])
hasorigin(s) && print(io,vio[2])
Expand All @@ -84,17 +100,17 @@ end

## DiagonalForm{N}

struct DiagonalForm{Indices,Options,Signatures,Diff} <: VectorSpace{Indices,Options,Signatures,Diff}
struct DiagonalForm{Indices,Options,Signatures,Diff} <: VectorBundle{Indices,Options,Signatures,Diff}
@pure DiagonalForm{N,M,S,D}() where {N,M,S,D} = new{N,M,S,D}()
end

@pure DiagonalForm{N,M,S}() where {N,M,S} = DiagonalForm{N,M,S,0}()

@pure diagonalform(V::DiagonalForm{N,M,S} where N) where {M,S} = dualtype(V)>0 ? SUB(diagonalform_cache[S]) : diagonalform_cache[S]
@pure diagonalform(V::DiagonalForm{N,M,S} where N) where {M,S} = mixedmode(V)>0 ? SUB(diagonalform_cache[S]) : diagonalform_cache[S]

const diagonalform_cache = SVector[]
function DiagonalForm{N,M}(b::SVector{N}) where {N,M}
a = dualtype(M)>0 ? SUB(b) : b
a = mixedmode(M)>0 ? SUB(b) : b
if a diagonalform_cache
DiagonalForm{N,M,findfirst(x->x==a,diagonalform_cache)}()
else
Expand All @@ -115,7 +131,7 @@ DiagonalForm(s::String) = DiagonalForm(Meta.parse(s).args)

function Base.show(io::IO,s::DiagonalForm)
print(io,'')
C,d = dualtype(s),diffmode(s)
C,d = mixedmode(s),diffmode(s)
N = ndims(s)-(d>0 ? (C<0 ? 2d : d) : 0)
hasinf(s) && print(io,vio[1])
hasorigin(s) && print(io,vio[2])
Expand Down Expand Up @@ -160,56 +176,56 @@ end

# generic

@pure Base.ndims(::T) where T<:VectorSpace{N} where N = N
@pure Base.ndims(::T) where T<:VectorBundle{N} where N = N
@pure hasinf(M::Int) = M (1,3,5,7,9,11)
@pure hasorigin(M::Int) = M (2,3,6,7,10,11)
@pure dualtype(M::Int) = M 8:11 ? -1 : Int(M (4,5,6,7))
@pure hasinf(::T) where T<:VectorSpace{N,M} where N where M = hasinf(M)
@pure hasorigin(::T) where T<:VectorSpace{N,M} where N where M = hasorigin(M)
@pure dualtype(::T) where T<:VectorSpace{N,M} where N where M = dualtype(M)
@pure options(::T) where T<:VectorSpace{N,M} where N where M = M
@pure options_list(V::T) where T<:VectorSpace = hasinf(V),hasorigin(V),dualtype(V)
@pure value(::T) where T<:VectorSpace{N,M,S} where {N,M} where S = S
@pure diffmode(::T) where T<:VectorSpace{N,M,S,D} where {N,M,S} where D = D
@pure mixedmode(M::Int) = M 8:11 ? -1 : Int(M (4,5,6,7))
@pure hasinf(::T) where T<:VectorBundle{N,M} where N where M = hasinf(M)
@pure hasorigin(::T) where T<:VectorBundle{N,M} where N where M = hasorigin(M)
@pure mixedmode(::T) where T<:VectorBundle{N,M} where N where M = mixedmode(M)
@pure options(::T) where T<:VectorBundle{N,M} where N where M = M
@pure options_list(V::T) where T<:VectorBundle = hasinf(V),hasorigin(V),mixedmode(V)
@pure value(::T) where T<:VectorBundle{N,M,S} where {N,M} where S = S
@pure diffmode(::T) where T<:VectorBundle{N,M,S,D} where {N,M,S} where D = D

@pure det(s::Signature) = isodd(count_ones(value(s))) ? -1 : 1
@pure det(s::DiagonalForm) = PROD(diagonalform(s))

@pure abs(s::VectorSpace) = sqrt(abs(det(s)))
@pure abs(s::VectorBundle) = sqrt(abs(det(s)))

@pure hasorigin(V::VectorSpace, B::Bits) = hasinf(V) ? (Bits(2)&B)==Bits(2) : isodd(B)
@pure hasorigin(V::VectorBundle, B::Bits) = hasinf(V) ? (Bits(2)&B)==Bits(2) : isodd(B)

@pure function hasinf(V::T,A::Bits,B::Bits) where T<:VectorSpace
@pure function hasinf(V::T,A::Bits,B::Bits) where T<:VectorBundle
hasinf(V) && (isodd(A) || isodd(B))
end
@pure function hasorigin(V::T,A::Bits,B::Bits) where T<:VectorSpace
@pure function hasorigin(V::T,A::Bits,B::Bits) where T<:VectorBundle
hasorigin(V) && (hasorigin(V,A) || hasorigin(V,B))
end

@pure function hasinf2(V::T,A::Bits,B::Bits) where T<:VectorSpace
@pure function hasinf2(V::T,A::Bits,B::Bits) where T<:VectorBundle
hasinf(V) && isodd(A) && isodd(B)
end
@pure function hasorigin2(V::T,A::Bits,B::Bits) where T<:VectorSpace
@pure function hasorigin2(V::T,A::Bits,B::Bits) where T<:VectorBundle
hasorigin(V) && hasorigin(V,A) && hasorigin(V,B)
end

@pure function hasorigininf(V::T,A::Bits,B::Bits) where T<:VectorSpace
@pure function hasorigininf(V::T,A::Bits,B::Bits) where T<:VectorBundle
hasinf(V) && hasorigin(V) && hasorigin(V,A) && isodd(B) && !hasorigin(V,B) && !isodd(A)
end
@pure function hasinforigin(V::T,A::Bits,B::Bits) where T<:VectorSpace
@pure function hasinforigin(V::T,A::Bits,B::Bits) where T<:VectorBundle
hasinf(V) && hasorigin(V) && isodd(A) && hasorigin(V,B) && !isodd(B) && !hasorigin(V,A)
end

@pure function hasi2o(V::T,A::Bits,B::Bits) where T<:VectorSpace
@pure function hasinf2origin(V::T,A::Bits,B::Bits) where T<:VectorBundle
hasinf2(V,A,B) && hasorigin(V,A,B)
end
@pure function haso2i(V::T,A::Bits,B::Bits) where T<:VectorSpace
@pure function hasorigin2inf(V::T,A::Bits,B::Bits) where T<:VectorBundle
hasorigin2(V,A,B) && hasinf(V,A,B)
end

@pure function dualbits(V::T) where T<:VectorSpace
@pure function diffmask(V::T) where T<:VectorBundle
d = diffmode(V)
if dualtype(V)<0
if mixedmode(V)<0
v = ((one(Bits)<<d)-1)<<(ndims(V)-2d)
w = ((one(Bits)<<d)-1)<<(ndims(V)-d)
return d<0 ? (typemax(Bits)-v,typemax(Bits)-w) : (v,w)
Expand All @@ -218,9 +234,9 @@ end
d<0 ? typemax(Bits)-v : v
end

@pure function dualcheck(V::T,A::Bits,B::Bits) where T<:VectorSpace
d,db = diffmode(V),dualbits(V)
v = dualtype(V)<0 ? db[1]|db[2] : db
@pure function diffcheck(V::T,A::Bits,B::Bits) where T<:VectorBundle
d,db = diffmode(V),diffmask(V)
v = mixedmode(V)<0 ? db[1]|db[2] : db
hi = hasinf2(V,A,B) && !hasorigin(V,A,B)
ho = hasorigin2(V,A,B) && !hasinf(V,A,B)
(hi || ho) || (d0 && count_ones((A&v)&(B&v))0)
Expand All @@ -236,19 +252,19 @@ export metric

# dual involution

@pure dual(V::T) where T<:VectorSpace = dualtype(V)<0 ? V : V'
@pure dual(V::T,B,M=Int(N/2)) where T<:VectorSpace{N} where N = ((B<<M)&((1<<N)-1))|(B>>M)
@pure dual(V::T) where T<:VectorBundle = mixedmode(V)<0 ? V : V'
@pure dual(V::T,B,M=Int(N/2)) where T<:VectorBundle{N} where N = ((B<<M)&((1<<N)-1))|(B>>M)

@pure flip_sig(N,S::Bits) = Bits(2^N-1) & (~S)

@pure function Base.adjoint(V::Signature{N,M,S,D}) where {N,M,S,D}
C = dualtype(V)
C = mixedmode(V)
C < 0 && throw(error("$V is the direct sum of a vector space and its dual space"))
Signature{N,doc2m(hasinf(V),hasorigin(V),Int(!Bool(C))),flip_sig(N,S),D}()
end

@pure function Base.adjoint(V::DiagonalForm{N,M,S,D}) where {N,M,S,D}
C = dualtype(V)
C = mixedmode(V)
C < 0 && throw(error("$V is the direct sum of a vector space and its dual space"))
DiagonalForm{N,doc2m(hasinf(V),hasorigin(V),Int(!Bool(C))),S,D}()
end
Expand Down
42 changes: 21 additions & 21 deletions src/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,30 @@ end

# index sets
index_limit = 20
const ndigits_cache = Vector{SVector}[]
const ndigits_extra = Dict{Bits,SVector}[]
@pure ndigits_calc(b,N) = SVector{N+1,Int}(digits(b,base=2,pad=N+1))
@pure function ndigits(b,N)
const digits_fast_cache = Vector{SVector}[]
const digits_fast_extra = Dict{Bits,SVector}[]
@pure digits_fast_calc(b,N) = SVector{N+1,Int}(digits(b,base=2,pad=N+1))
@pure function digits_fast(b,N)
if N>index_limit
n = N-index_limit
for k length(ndigits_extra)+1:n
push!(ndigits_extra,Dict{Bits,SVector{k+1,Int}}())
for k length(digits_fast_extra)+1:n
push!(digits_fast_extra,Dict{Bits,SVector{k+1,Int}}())
end
!haskey(ndigits_extra[n],b) && push!(ndigits_extra[n],b=>ndigits_calc(b,N))
@inbounds ndigits_extra[n][b]
!haskey(digits_fast_extra[n],b) && push!(digits_fast_extra[n],b=>digits_fast_calc(b,N))
@inbounds digits_fast_extra[n][b]
else
for k length(ndigits_cache)+1:min(N,index_limit)
push!(ndigits_cache,[ndigits_calc(d,k) for d 0:1<<(k+1)-1])
for k length(digits_fast_cache)+1:min(N,index_limit)
push!(digits_fast_cache,[digits_fast_calc(d,k) for d 0:1<<(k+1)-1])
GC.gc()
end
@inbounds ndigits_cache[N][b+1]
@inbounds digits_fast_cache[N][b+1]
end
end

const indices_cache = Dict{Bits,Vector}()
@pure indices(b::Bits) = findall(digits(b,base=2).==1)
@pure function indices_calc(b::Bits,N::Int)
d = ndigits(b,N)
d = digits_fast(b,N)
l = length(d)
a = Int[]
for i 1:l
Expand All @@ -105,8 +105,8 @@ end
return @inbounds indices_cache[b]
end

@pure shift_indices(V::T,b::Bits) where T<:VectorSpace = shift_indices!(V,copy(indices(b,ndims(V))))
function shift_indices!(s::T,set::Vector{Int}) where T<:VectorSpace{N,M} where N where M
@pure shift_indices(V::T,b::Bits) where T<:VectorBundle = shift_indices!(V,copy(indices(b,ndims(V))))
function shift_indices!(s::T,set::Vector{Int}) where T<:VectorBundle{N,M} where N where M
if !isempty(set)
k = 1
hasinf(s) && set[1] == 1 && (set[1] = -1; k += 1)
Expand All @@ -116,7 +116,7 @@ function shift_indices!(s::T,set::Vector{Int}) where T<:VectorSpace{N,M} where N
end
return set
end
@deprecate(shift_indices(s::VectorSpace,set::Vector{Int}),shift_indices!(s::VectorSpace,set::Vector{Int}))
@deprecate(shift_indices(s::VectorBundle,set::Vector{Int}),shift_indices!(s::VectorBundle,set::Vector{Int}))

# printing of indices
@inline function printindex(i,l::Bool=false,e::String=pre[1],t=i>36,j=t ? i-26 : i)
Expand All @@ -131,10 +131,10 @@ end
!((B || C || D) && A) && printindices(io,a,l,e)
B && printindices(io,b,l,f)
end
@pure printindices(io::IO,V::T,e::Bits,label::Bool=false) where T<:VectorSpace = printlabel(io,V,e,label,pre...)
@pure printindices(io::IO,V::T,e::Bits,label::Bool=false) where T<:VectorBundle = printlabel(io,V,e,label,pre...)

@inline function printlabel(io::IO,V::T,e::Bits,label::Bool,vec,cov,duo,dif) where T<:VectorSpace
N,D,C,db = ndims(V),diffmode(V),dualtype(V),dualbits(V)
@inline function printlabel(io::IO,V::T,e::Bits,label::Bool,vec,cov,duo,dif) where T<:VectorBundle
N,D,C,db = ndims(V),diffmode(V),mixedmode(V),diffmask(V)
if C < 0
es = e & (~(db[1]|db[2]))
n = Int((N-2D)/2)
Expand All @@ -152,7 +152,7 @@ end
end
end

function indexparity!(ind::Vector{Int},s::VectorSpace{N,M} where N) where M
function indexparity!(ind::Vector{Int},s::VectorBundle{N,M} where N) where M
k = 1
t = false
while k < length(ind)
Expand All @@ -171,7 +171,7 @@ function indexparity!(ind::Vector{Int},s::VectorSpace{N,M} where N) where M
return t, ind, false
end

@noinline function indexparity(V::VectorSpace,v::Symbol)::Tuple{Bool,Vector,VectorSpace,Bool}
@noinline function indexparity(V::VectorBundle,v::Symbol)::Tuple{Bool,Vector,VectorBundle,Bool}
vs = string(v)
vt = vs[1:1]pre[1]
Z=match(Regex("([$(pre[1])]([0-9a-vx-zA-VX-Z]+))?([$(pre[2])]([0-9a-zA-Z]+))?"),vs)
Expand All @@ -181,7 +181,7 @@ end
end
length(ef) == 0 && (return false,Int[],V,true)
let W = V,fs=false
C = dualtype(V)
C = mixedmode(V)
X = C0 && ndims(V)<4sizeof(Bits)+1
X && (W = C>0 ? V'V : VV')
V2 = (vt (vt ? C0 : C>0)) ? V' : V
Expand Down
Loading

2 comments on commit 49ce23b

@chakravala
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register() #1

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/1646

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" 49ce23b064a18aa00fffb9e25235e1b08cc994d8
git push origin v0.3.0

Please sign in to comment.