Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LinearMaps callable #128

Merged
merged 4 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LinearMaps"
uuid = "7a12625a-238d-50fd-b39a-03d52299707e"
version = "3.0.1"
version = "3.1.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ The only requirement for a `LinearMap` is that it can act on a vector (by multip

| **Documentation** | **Build Status** |
|:-------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------:|
| [![][docs-stable-img]][docs-stable-url] [![][docs-dev-img]][docs-dev-url] | [![][build-img]][build-url] [![][codecov-img]][codecov-url] [![][license-img]][license-url] |

| [![stable docs][docs-stable-img]][docs-stable-url] [![dev docs][docs-dev-img]][docs-dev-url] | [![build status][build-img]][build-url] [![coverage][codecov-img]][codecov-url] [![license][license-img]][license-url] |

## Installation

Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"

[compat]
BenchmarkTools = "0.4, 0.5"
Documenter = "0.25"
Documenter = "0.25, 0.26"
Literate = "2"
5 changes: 5 additions & 0 deletions docs/src/history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Version history

## What's new in v3.1

* In Julia v1.3 and above, `LinearMap`-typed objects are callable on `AbstractVector`s:
For `L::LinearMap` and `x::AbstractVector`, `L(x) = L*x`.

## What's new in v3.0

* BREAKING change: Internally, any dependence on former `A*_mul_B!` methods is abandonned.
Expand Down
8 changes: 8 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ Arpack.eigs(Δ; nev=3, which=:LR)
KrylovKit.eigsolve(x -> Δ*x, size(Δ, 1), 3, :LR)
```

In Julia v1.3 and above, the last line can be simplified to

```julia
KrylovKit.eigsolve(Δ, size(Δ, 1), 3, :LR)
````

leveraging the fact that objects of type `L <: LinearMap` are callable.

## Philosophy

Several iterative linear algebra methods such as linear solvers or eigensolvers
Expand Down
12 changes: 12 additions & 0 deletions src/LinearMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,20 @@ convert_to_lmaps(A) = (convert_to_lmaps_(A),)

Compute the action of the linear map `A` on the vector `x`.

!!! compat "Julia 1.3"
In Julia versions v1.3 and above, objects `L` of any subtype of `LinearMap`
are callable in the sense that `L(x) = L*x` for `x::AbstractVector`.

## Examples
```jldoctest; setup=(using LinearAlgebra, LinearMaps)
julia> A=LinearMap([1.0 2.0; 3.0 4.0]); x=[1.0, 1.0];

julia> A*x
2-element Array{Float64,1}:
3.0
7.0

julia> A(x)
2-element Array{Float64,1}:
3.0
7.0
Expand All @@ -104,6 +113,9 @@ function Base.:(*)(A::LinearMap, x::AbstractVector)
check_dim_mul(A, x)
return mul!(similar(x, promote_type(eltype(A), eltype(x)), size(A, 1)), A, x)
end
if VERSION ≥ v"1.3"
(L::LinearMap)(x::AbstractVector) = L*x
end

"""
mul!(Y::AbstractVecOrMat, A::LinearMap, B::AbstractVector) -> Y
Expand Down
4 changes: 2 additions & 2 deletions src/kronecker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ holds. Upon vector multiplication, this rule is checked for applicability.
# Examples
```jldoctest; setup=(using LinearAlgebra, SparseArrays, LinearMaps)
julia> J = LinearMap(I, 2) # 2×2 identity map
LinearMaps.UniformScalingMap{Bool}(true, 2)
2×2 LinearMaps.UniformScalingMap{Bool} with scaling factor: true

julia> E = spdiagm(-1 => trues(1)); D = E + E' - 2I;

Expand Down Expand Up @@ -228,7 +228,7 @@ For convenience, one can also use `A ⊕ B` or `⊕(A, B, Cs...)` (typed as
# Examples
```jldoctest; setup=(using LinearAlgebra, SparseArrays, LinearMaps)
julia> J = LinearMap(I, 2) # 2×2 identity map
LinearMaps.UniformScalingMap{Bool}(true, 2)
2×2 LinearMaps.UniformScalingMap{Bool} with scaling factor: true

julia> E = spdiagm(-1 => trues(1)); D = LinearMap(E + E' - 2I);

Expand Down
3 changes: 3 additions & 0 deletions test/linearmaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ LinearAlgebra.mul!(y::AbstractVector, A::Union{SimpleFunctionMap,SimpleComplexFu
α = rand(ComplexF64); β = rand(ComplexF64)
v = rand(ComplexF64, 10); V = rand(ComplexF64, 10, 3)
w = rand(ComplexF64, 10); W = rand(ComplexF64, 10, 3)
if VERSION ≥ v"1.3"
F(v) == F*v
end
@test mul!(w, F, v) === w == F * v
@test_throws ErrorException F' * v
@test_throws ErrorException transpose(F) * v
Expand Down