Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed May 2, 2024
1 parent 1cd0865 commit 98216f1
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 178 deletions.
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Documenter.makedocs(
"developer/contributing.md",
"developer/credits.md",
],
"API" => ["atoms.md", "reformulations.md"],
"atoms.md",
"reference.md",
"release_notes.md",
],
Expand Down
44 changes: 11 additions & 33 deletions docs/src/atoms.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
# Atoms

## AbsAtom

```@docs
Convex.AbsAtom
```

## ExpAtom

```@docs
Convex.ExpAtom
```

## LogAtom

```@docs
Convex.LogAtom
```

## LogSumExpAtom

```@docs
Convex.LogSumExpAtom
```

## MaxAtom

```@docs
Convex.MaAtom
```

## MinAtom

```@docs
Convex.MinAtom
Base.abs(::Convex.AbstractExpr)
Base.abs2(::Convex.AbstractExpr)
Base.exp(::Convex.AbstractExpr)
Convex.hinge_loss
Base.log(::Convex.AbstractExpr)
Convex.logisticloss
Convex.logsumexp
Base.min(::Convex.AbstractExpr, ::Convex.AbstractExpr)
Base.max(::Convex.AbstractExpr, ::Convex.AbstractExpr)
Convex.neg
Convex.pos
```
10 changes: 0 additions & 10 deletions docs/src/reformulations.md

This file was deleted.

21 changes: 8 additions & 13 deletions src/atoms/AbsAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file or at https://opensource.org/license/bsd-2-clause

"""
AbsAtom(x::AbstractExpr)
mutable struct AbsAtom <: AbstractExpr
children::Tuple{AbstractExpr}
size::Tuple{Int,Int}

The epigraph of \$|x|\$.
AbsAtom(x::AbstractExpr) = new((x,), x.size)
end

## Related constructors
"""
Base.abs(x::Convex.AbstractExpr)
* `Base.abs(x)`
* `Base.abs2(x)`
The epigraph of \$|x|\$.
## Examples
Expand Down Expand Up @@ -51,13 +53,6 @@ abs (convex; positive)
| :--- | :----------- | :-------- |
| Positive | depends on the sign of \$x\$ | Convex |
"""
mutable struct AbsAtom <: AbstractExpr
children::Tuple{AbstractExpr}
size::Tuple{Int,Int}

AbsAtom(x::AbstractExpr) = new((x,), x.size)
end

Base.abs(x::AbstractExpr) = AbsAtom(x)

head(io::IO, ::AbsAtom) = print(io, "abs")
Expand Down
34 changes: 15 additions & 19 deletions src/atoms/ExpAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file or at https://opensource.org/license/bsd-2-clause

"""
ExpAtom(x::AbstractExpr)
mutable struct ExpAtom <: AbstractExpr
children::Tuple{AbstractExpr}
size::Tuple{Int,Int}

The epigraph of \$e^x\$.
function ExpAtom(x::AbstractExpr)
if sign(x) == ComplexSign()
error(
"[ExpAtom] the argument should be real but it's instead complex",
)
end
return new((x,), x.size)
end
end

## Related constructors
"""
Base.exp(x::Convex.AbstractExpr)
* `Base.exp(x)`
The epigraph of \$e^x\$.
## Examples
Expand Down Expand Up @@ -50,20 +60,6 @@ exp (convex; positive)
| :--- | :----------- | :-------- |
| Positive | Nondecreasing | Convex |
"""
mutable struct ExpAtom <: AbstractExpr
children::Tuple{AbstractExpr}
size::Tuple{Int,Int}

function ExpAtom(x::AbstractExpr)
if sign(x) == ComplexSign()
error(
"[ExpAtom] the argument should be real but it's instead complex",
)
end
return new((x,), x.size)
end
end

Base.exp(x::AbstractExpr) = ExpAtom(x)

head(io::IO, ::ExpAtom) = print(io, "exp")
Expand Down
34 changes: 15 additions & 19 deletions src/atoms/LogAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file or at https://opensource.org/license/bsd-2-clause

"""
LogAtom(x::AbstractExpr)
mutable struct LogAtom <: AbstractExpr
children::Tuple{AbstractExpr}
size::Tuple{Int,Int}

The hypograph of \$\\log(x)\$.
function LogAtom(x::AbstractExpr)
if sign(x) == ComplexSign()
error(
"[LogAtom] the argument should be real but it's instead complex",
)
end
return new((x,), x.size)
end
end

## Related constructors
"""
Base.log(x::Convex.AbstractExpr)
* `Base.log(x)`
The hypograph of \$\\log(x)\$.
## Examples
Expand Down Expand Up @@ -50,20 +60,6 @@ log (concave; real)
| :--- | :----------- | :-------- |
| None | Nondecreasing | Concave |
"""
mutable struct LogAtom <: AbstractExpr
children::Tuple{AbstractExpr}
size::Tuple{Int,Int}

function LogAtom(x::AbstractExpr)
if sign(x) == ComplexSign()
error(
"[LogAtom] the argument should be real but it's instead complex",
)
end
return new((x,), x.size)
end
end

Base.log(x::AbstractExpr) = LogAtom(x)

head(io::IO, ::LogAtom) = print(io, "log")
Expand Down
42 changes: 15 additions & 27 deletions src/atoms/LogSumExpAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file or at https://opensource.org/license/bsd-2-clause

"""
LogSumExpAtom(x::AbstractExpr)
mutable struct LogSumExpAtom <: AbstractExpr
children::Tuple{AbstractExpr}
size::Tuple{Int,Int}

The epigraph of \$\\log\\left(\\sum_i e^{x_i}\\right)\$.
function LogSumExpAtom(x::AbstractExpr)
if sign(x) == ComplexSign()
error(
"[LogSumExpAtom] the argument should be real but it's instead complex",
)
end
return new((x,), (1, 1))
end
end

## Related constructors
"""
logsumexp(x::Convex.AbstractExpr)
* [`logsumexp`](@ref)
* [`logisticloss`](@ref)
The epigraph of \$\\log\\left(\\sum_i e^{x_i}\\right)\$.
## Examples
Expand All @@ -36,27 +45,6 @@ logsumexp (convex; real)
| :--- | :----------- | :-------- |
| None | Nondecreasing | Convex |
"""
mutable struct LogSumExpAtom <: AbstractExpr
children::Tuple{AbstractExpr}
size::Tuple{Int,Int}

function LogSumExpAtom(x::AbstractExpr)
if sign(x) == ComplexSign()
error(
"[LogSumExpAtom] the argument should be real but it's instead complex",
)
end
return new((x,), (1, 1))
end
end

"""
logsumexp(x::AbstractExpr)
The epigraph of \$\\log\\left(\\sum_i e^{x_i}\\right)\$.
See [`LogSumExpAtom`](@ref).
"""
logsumexp(x::AbstractExpr) = LogSumExpAtom(x)

head(io::IO, ::LogSumExpAtom) = print(io, "logsumexp")
Expand Down
58 changes: 28 additions & 30 deletions src/atoms/MaxAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file or at https://opensource.org/license/bsd-2-clause

"""
MaxAtom(x::AbstractExpr, y::AbstractExpr)
mutable struct MaxAtom <: AbstractExpr
children::Tuple{AbstractExpr,AbstractExpr}
size::Tuple{Int,Int}

The hypograph of \$max(x, y)\$.
function MaxAtom(x::AbstractExpr, y::AbstractExpr)
if sign(x) == ComplexSign() || sign(y) == ComplexSign()
error(
"[MaxAtom] both the arguments should be real instead they are $(sign(x)) and $(sign(y))",
)
end
sz = if x.size == y.size
x.size
elseif x.size == (1, 1)
y.size
elseif y.size == (1, 1)
x.size
else
error(
"[MaxAtom] got different sizes for x as $(x.size) and y as $(y.size)",
)
end
return new((x, y), sz)
end
end

## Related constructors
"""
Base.max(x::Convex.AbstractExpr, y::Convex.AbstractExpr)
Base.max(x::Convex.AbstractExpr, y::Convex.Value)
Base.max(x::Convex.Value, y::Convex.AbstractExpr)
* `Base.max(x, y)`
The hypograph of \$max(x, y)\$.
## Examples
Expand Down Expand Up @@ -38,31 +61,6 @@ max (convex; real)
└─ [1; 2; 3;;]
```
"""
mutable struct MaxAtom <: AbstractExpr
children::Tuple{AbstractExpr,AbstractExpr}
size::Tuple{Int,Int}

function MaxAtom(x::AbstractExpr, y::AbstractExpr)
if sign(x) == ComplexSign() || sign(y) == ComplexSign()
error(
"[MaxAtom] both the arguments should be real instead they are $(sign(x)) and $(sign(y))",
)
end
sz = if x.size == y.size
x.size
elseif x.size == (1, 1)
y.size
elseif y.size == (1, 1)
x.size
else
error(
"[MaxAtom] got different sizes for x as $(x.size) and y as $(y.size)",
)
end
return new((x, y), sz)
end
end

Base.max(x::AbstractExpr, y::AbstractExpr) = MaxAtom(x, y)

Base.max(x::AbstractExpr, y::Value) = max(x, constant(y))
Expand Down
Loading

0 comments on commit 98216f1

Please sign in to comment.