Skip to content

Commit

Permalink
Renaming (cont.) (#16)
Browse files Browse the repository at this point in the history
* Rename Affine->FullAffine, AFF->Affine, AF->Aff

* Correct imports

* Correct imports

* Fix Project.toml and bump version

* Update .yml files

* Add constructors with 1 or several intervals and tests

* Fix tests etc

* update test and add missing methods

Co-authored-by: David Sanders <[email protected]>
  • Loading branch information
mforets and dpsanders authored Mar 9, 2020
1 parent d00775d commit 4d9c298
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 344 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ os:
- osx

julia:
- 1.0
- 1.1
- 1.2
- nightly

notifications:
Expand Down
8 changes: 5 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
name = "AffineArithmetic"
uuid = "2e89c364-fad6-56cb-99bd-ebadcd2cf8d2"
authors = ["dpsanders"]
version = "0.1.0"
version = "0.2.0"

[deps]
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
IntervalArithmetic = "≥ 0.15.0"
julia = "≥ 1.0.0"
IntervalArithmetic = "0.16"
Polynomials = "0.6"
StaticArrays = "0.12"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- julia_version: 1
- julia_version: 1.1
- julia_version: 1.2
- julia_version: nightly

platform:
Expand Down
157 changes: 0 additions & 157 deletions src/AFF.jl

This file was deleted.

124 changes: 5 additions & 119 deletions src/AffineArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,126 +7,12 @@ import Base: +, -, *, /, ^, ==,
show


export Affine, reset_affine_index
include("aff.jl")
include("affine.jl")
include("full_affine.jl")

include("AF1.jl")
include("AF.jl")
include("AFF.jl")
export AF1, AF, AFF

const affine_index = [1] # which affine vector index to use

reset_affine_index() = affine_index[1] = 1

@doc """
An affine quantity for affine arithmetic.
The usual way to create an affine quantity is from an interval `X`, via `Affine(X)`.
"""
struct Affine{T<:AbstractFloat}
c::T # mid-point
γ::Vector{T} # error terms
end


function show(io::IO, C::Affine)
print(io, "", C.c, "; ", C.γ, "")
end

==(C::Affine, D::Affine) = C.c == D.c && C.γ == D.γ

@doc """
Affine(X::Interval)
Construct a new `Affine` quantity from an `Interval`.
"""
function Affine(X::Interval)
c = mid(X)
r = radius(X)

index = affine_index[1]
affine_index[1] += 1

γ = zeros(index)
γ[end] = r

return Affine(c, γ)
end

# conversion of numerical constant to affine:
Affine(c::Real) = Affine(c, Float64[])


range(C::Affine) = C.c + sum(abs.(C.γ))*(-1..1)

range(X::Interval) = X

# morally:
# +(C::Affine, D::Afine) = Affine(C.c + D.c, C.γ + D.γ)

for op in (:+, :-)
@eval function $op(C::Affine, D::Affine)
k = length(C.γ)
l = length(D.γ)

# account for unequal lengths:
m = min(k, l)
common_γ = $op(C.γ[1:m], D.γ[1:m])

if l >= k
γ = [ common_γ; D.γ[m+1:l] ]
else
γ = [ common_γ; C.γ[m+1:k] ]
end

Affine($op(C.c, D.c), γ)
end
end

+(C::Affine, α::Real) = Affine(C.c + α, C.γ)
+::Real, C::Affine) = C + α

-(C::Affine, α::Real) = Affine(C.c - α, C.γ)
-::Real, C::Affine) = Affine- C.c, [-x for x in C.γ])

function *(C::Affine, D::Affine)

c = C.c
d = D.c

k = length(C.γ)
l = length(D.γ)

# account for unequal lengths:
m = min(k, l)
common_γ = d * C.γ[1:m] + c * D.γ[1:m]

error_bound = sum(abs.(C.γ)) * sum(abs.(D.γ))

if l >= k
γ = [ common_γ; c*D.γ[m+1:l]; error_bound ]
else
γ = [ common_γ; d*C.γ[m+1:k]; error_bound ]
end

# |γ|₁ = sum(abs.(γ))

Affine(C.c * D.c, γ)
end

*::Real, C::Affine) = Affine*C.c, α*C.γ)
*(C::Affine, α::Real) = α * C

^(x::Affine, n::Integer) = Base.power_by_squaring(x, n)

Base.literal_pow(::typeof(^), x::Affine{T}, ::Val{p}) where {T,p} = x^p

eltype(C::Affine{T}) where T = T
zero(C::Affine{T}) where T = Affine(zero(T))
zero(::Type{Affine{T}}) where T = Affine(zero(T))

one(C::Affine{T}) where T = Affine(one(T))
one(::Type{Affine{T}}) where T = Affine(one(T))

#one(C::Affine)
export Affine, affine
export FullAffine, reset_affine_index

end
Loading

0 comments on commit 4d9c298

Please sign in to comment.