Skip to content

Commit

Permalink
Merge pull request #530 from eschnett/eschnett/random
Browse files Browse the repository at this point in the history
Provide `rand` function for TensorValues
  • Loading branch information
fverdugo authored Feb 2, 2021
2 parents 7ec005e + 078f836 commit cd5048d
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added
- `rand` function for `MultiValue` objects.

## [0.15.1] - 2021-01-22

### Added
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LineSearches = "d3d80556-e9d4-5f37-9878-2ab0fcc64255"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
10 changes: 10 additions & 0 deletions src/TensorValues/SymFourthOrderTensorValueTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ zero(::SymFourthOrderTensorValue{D,T,L}) where {D,T,L} = zero(SymFourthOrderTens
end
one(::SymFourthOrderTensorValue{D,T}) where {D,T} = one(SymFourthOrderTensorValue{D,T})

@generated function rand(rng::AbstractRNG,
::Random.SamplerType{<:SymFourthOrderTensorValue{D,T}}) where {D,T}
L=(D*(D+1)÷2)^2
quote
rand(rng, SymFourthOrderTensorValue{D,T,$L})
end
end
rand(rng::AbstractRNG,::Random.SamplerType{<:SymFourthOrderTensorValue{D,T,L}}) where {D,T,L} =
SymFourthOrderTensorValue{D,T}(Tuple(rand(rng, SVector{L,T})))

change_eltype(::Type{SymFourthOrderTensorValue{D,T1,L}},::Type{T2}) where {D,T1,T2,L} = SymFourthOrderTensorValue{D,T2,L}
change_eltype(::SymFourthOrderTensorValue{D,T1,L},::Type{T2}) where {D,T1,T2,L} = change_eltype(SymFourthOrderTensorValue{D,T1,L},T2)

Expand Down
10 changes: 10 additions & 0 deletions src/TensorValues/SymTensorValueTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ zero(::SymTensorValue{D,T,L}) where {D,T,L} = zero(SymTensorValue{D,T,L})
end
one(::SymTensorValue{D,T}) where {D,T} = one(SymTensorValue{D,T})

@generated function rand(rng::AbstractRNG,
::Random.SamplerType{<:SymTensorValue{D,T}}) where {D,T}
L=D*(D+1)÷2
quote
rand(rng, SymTensorValue{D,T,$L})
end
end
rand(rng::AbstractRNG,::Random.SamplerType{<:SymTensorValue{D,T,L}}) where {D,T,L} =
SymTensorValue{D,T}(Tuple(rand(rng, SVector{L,T})))

Mutable(::Type{<:SymTensorValue{D,T}}) where {D,T} = MMatrix{D,D,T}
Mutable(::SymTensorValue{D,T}) where {D,T} = Mutable(SymTensorValue{D,T})
mutable(a::SymTensorValue{D}) where D = MMatrix{D,D}(Tuple(get_array(a)))
Expand Down
12 changes: 12 additions & 0 deletions src/TensorValues/TensorValueTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ zero(::TensorValue{D1,D2,T}) where {D1,D2,T} = zero(TensorValue{D1,D2,T})
end
one(::TensorValue{D1,D2,T}) where {D1,D2,T} = one(TensorValue{D1,D2,T})

@generated function rand(rng::AbstractRNG,
::Random.SamplerType{<:TensorValue{D1,D2,T}}) where {D1,D2,T}
L=D1*D2
quote
rand(rng, TensorValue{D1,D2,T,$L})
end
end
function rand(rng::AbstractRNG,
::Random.SamplerType{<:TensorValue{D1,D2,T,L}}) where {D1,D2,T,L}
return TensorValue{D1,D2,T,L}(Tuple(rand(rng, SVector{L,T})))
end

Mutable(::Type{<:TensorValue{D1,D2,T}}) where {D1,D2,T} = MMatrix{D1,D2,T}
Mutable(::TensorValue{D1,D2,T}) where {D1,D2,T} = Mutable(TensorValue{D1,D2,T})
mutable(a::TensorValue{D1,D2}) where {D1,D2} = MMatrix{D1,D2}(a.data)
Expand Down
2 changes: 2 additions & 0 deletions src/TensorValues/TensorValues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ using Base: @propagate_inbounds, @pure
using Gridap.Helpers
using Gridap.Arrays
using LinearAlgebra
using Random

export MultiValue
export VectorValue
Expand Down Expand Up @@ -71,6 +72,7 @@ import Base: CartesianIndices
import Base: LinearIndices
import Base: adjoint
import Base: transpose
import Base: rand

import LinearAlgebra: det, inv, tr, cross, dot, norm
# Reexport from LinearAlgebra (just for convenience)
Expand Down
12 changes: 12 additions & 0 deletions src/TensorValues/ThirdOrderTensorValueTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ change_eltype(::T,::Type{T2}) where {T<:ThirdOrderTensorValue,T2} = change_eltyp
zero(::Type{<:ThirdOrderTensorValue{D1,D2,D3,T}}) where {D1,D2,D3,T} = ThirdOrderTensorValue{D1,D2,D3,T}(tfill(zero(T),Val{D1*D2*D3}()))
zero(::ThirdOrderTensorValue{D1,D2,D3,T}) where {D1,D2,D3,T} = zero(ThirdOrderTensorValue{D1,D2,D3,T})

@generated function rand(rng::AbstractRNG,
::Random.SamplerType{<:ThirdOrderTensorValue{D1,D2,D3,T}}) where {D1,D2,D3,T}
L=D1*D2*D3
quote
rand(rng, ThirdOrderTensorValue{D1,D2,D3,T,$L})
end
end
function rand(rng::AbstractRNG,
::Random.SamplerType{<:ThirdOrderTensorValue{D1,D2,D3,T,L}}) where {D1,D2,D3,T,L}
return ThirdOrderTensorValue{D1,D2,D3,T,L}(Tuple(rand(rng, SVector{L,T})))
end

Mutable(::Type{<:ThirdOrderTensorValue{D1,D2,D3,T}}) where {D1,D2,D3,T} = MArray{Tuple{D1,D2,D3},T}
Mutable(::ThirdOrderTensorValue{D1,D2,D3,T}) where {D1,D2,D3,T} = Mutable(ThirdOrderTensorValue{D1,D2,D3,T})
mutable(a::ThirdOrderTensorValue{D1,D2,D3}) where {D1,D2,D3} = MArray{Tuple{D1,D2,D3}}(a.data)
Expand Down
4 changes: 4 additions & 0 deletions src/TensorValues/VectorValueTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ convert(::Type{<:VectorValue{D,T}}, arg::VectorValue{D,T}) where {D,T} = arg
zero(::Type{<:VectorValue{D,T}}) where {D,T} = VectorValue{D,T}(tfill(zero(T),Val{D}()))
zero(::VectorValue{D,T}) where {D,T} = zero(VectorValue{D,T})

function rand(rng::AbstractRNG, ::Random.SamplerType{VectorValue{D,T}}) where {D,T}
return VectorValue{D,T}(Tuple(rand(rng, SVector{D,T})))
end

Mutable(::Type{VectorValue{D,T}}) where {D,T} = MVector{D,T}
Mutable(::VectorValue{D,T}) where {D,T} = Mutable(VectorValue{D,T})
mutable(a::VectorValue) = MVector(a.data)
Expand Down
20 changes: 20 additions & 0 deletions test/TensorValuesTests/TypesTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,26 @@ z = one(SymFourthOrderTensorValue{2,Int})
@test isa(z,SymFourthOrderTensorValue{2})
@test Tuple(z) == (1.,0.,0., 0.,0.5,0., 0.,0.,1.)

r = rand(VectorValue{3,Int})
@test isa(r,VectorValue{3,Int})
@test r rand(typeof(r))

r = rand(TensorValue{3,3,Int})
@test isa(r,TensorValue{3,3,Int,9})
@test r rand(typeof(r))

r = rand(ThirdOrderTensorValue{3,3,3,Int})
@test isa(r,ThirdOrderTensorValue{3,3,3,Int,27})
@test r rand(typeof(r))

r = rand(SymTensorValue{3,Int})
@test isa(r,SymTensorValue{3,Int,6})
@test r rand(typeof(r))

r = rand(SymFourthOrderTensorValue{3,Int})
@test isa(r,SymFourthOrderTensorValue{3,Int,36})
@test r rand(typeof(r))

# Conversions

a = @SVector ones(Int,3)
Expand Down

0 comments on commit cd5048d

Please sign in to comment.