Skip to content

Commit

Permalink
multiplication by scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
hakkelt committed Nov 19, 2019
1 parent be3fe0e commit fb25c1f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
38 changes: 21 additions & 17 deletions src/BuildCompTree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ Base.:+(FO1::FunOp, FO2::FunOp) = begin
FunctionOperatorComposite(FO1, FO2, :+)
end

Base.:+(FO1::FunOp, S::LinearAlgebra.UniformScaling) = begin
assertAddDimScaling(FO1, S)
FunctionOperatorComposite(FO1, createScalingForAddSub(FO1, S), :+)
Base.:+(FO::FunOp, S::LinearAlgebra.UniformScaling) = begin
assertAddDimScaling(FO, S)
FunctionOperatorComposite(FO, createScalingForAddSub(FO, S), :+)
end

Base.:+(S::LinearAlgebra.UniformScaling, FO2::FunOp) = begin
assertAddDimScaling(FO2, S)
FunctionOperatorComposite(createScalingForAddSub(FO2, S), FO2, :+)
Base.:+(S::LinearAlgebra.UniformScaling, FO::FunOp) = begin
assertAddDimScaling(FO, S)
FunctionOperatorComposite(createScalingForAddSub(FO, S), FO, :+)
end

Base.:-(FO1::FunOp, FO2::FunOp) = begin
Expand All @@ -30,14 +30,14 @@ Base.:-(FO1::FunOp, FO2::FunOp) = begin
FunctionOperatorComposite(FO1, FO2, :-)
end

Base.:-(FO1::FunOp, S::LinearAlgebra.UniformScaling) = begin
assertAddDimScaling(FO1, S)
FunctionOperatorComposite(FO1, createScalingForAddSub(FO1, S), :-)
Base.:-(FO::FunOp, S::LinearAlgebra.UniformScaling) = begin
assertAddDimScaling(FO, S)
FunctionOperatorComposite(FO, createScalingForAddSub(FO, S), :-)
end

Base.:-(S::LinearAlgebra.UniformScaling, FO2::FunOp) = begin
assertAddDimScaling(FO2, S)
FunctionOperatorComposite(createScalingForAddSub(FO2, S), FO2, :-)
Base.:-(S::LinearAlgebra.UniformScaling, FO::FunOp) = begin
assertAddDimScaling(FO, S)
FunctionOperatorComposite(createScalingForAddSub(FO, S), FO, :-)
end

Base.:*(FO1::FunOp, FO2::FunOp) = begin
Expand All @@ -52,17 +52,21 @@ Base.:*(FO::FunctionOperator, S::LinearAlgebra.UniformScaling{Bool}) =
Base.:*(FO::FunctionOperatorComposite, S::LinearAlgebra.UniformScaling{Bool}) =
FunctionOperatorComposite(FO, name = getName(FO) * " * I")

Base.:*(FO1::FunOp, S::LinearAlgebra.UniformScaling) =
FunctionOperatorComposite(FO1, createScalingForMult(FO1, S, FO1.inDims), :*)

Base.:*(S::LinearAlgebra.UniformScaling{Bool}, FO::FunctionOperator) =
FunctionOperator(FO, name = "I * " * getName(FO))

Base.:*(S::LinearAlgebra.UniformScaling{Bool}, FO::FunctionOperatorComposite) =
FunctionOperatorComposite(FO, name = "I * " * getName(FO))

Base.:*(S::LinearAlgebra.UniformScaling, FO2::FunOp) =
FunctionOperatorComposite(createScalingForMult(FO2, S, FO2.outDims), FO2, :*)
Base.:*(FO::FunOp, S::LinearAlgebra.UniformScaling) =
FunctionOperatorComposite(FO, createScalingForMult(FO, S, FO.inDims), :*)

Base.:*(S::LinearAlgebra.UniformScaling, FO::FunOp) =
FunctionOperatorComposite(createScalingForMult(FO, S, FO.outDims), FO, :*)

Base.:*(FO::FunOp, λ::Number) = FO **I)

Base.:*::Number, FO::FunOp) =*I) * FO

# Adjoint operator creates a new FunctionOperatorComposite object, toggles the adjoint field and
# switches the input and output dimension constraints (and also voids plan for FunctionOperatorComposite)
Expand Down
8 changes: 6 additions & 2 deletions test/functionality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ using FunctionOperators, LinearAlgebra, Test
@test Op₁' * I * (ones(10,10)*8) == ones(10,10)*2
@test I * Op₁ * (ones(10,10)*2) == ones(10,10)*8
@test I * Op₁' * (ones(10,10)*8) == ones(10,10)*2
@test Op₁ * 2 * (ones(10,10)*2) == ones(10,10)*64
@test Op₁' * 2 * (ones(10,10)*4) == ones(10,10)*2
@test 2 * Op₁ * (ones(10,10)*2) == ones(10,10)*16
@test 2 * Op₁' * (ones(10,10)*8) == ones(10,10)*4
@test Op₃ * Op₁ * (ones(10,10)*2) == ones(10,10)*8 .* w
@test (Op₃ * Op₁)' * (ones(10,10)*8 .* w) == Op₁' * Op₃' * (ones(10,10)*8 .* w)
@test (Op₃ * Op₁)' * (ones(10,10)*8 .* w) == ones(10,10)*2
Expand Down Expand Up @@ -62,9 +66,9 @@ using FunctionOperators, LinearAlgebra, Test
manual_tests()
end
@testset "With automatic reshape" begin
FO_settings.auto_reshape = true
FunctionOperators_global_settings.auto_reshape = true
manual_tests()
FO_settings.auto_reshape = false
FunctionOperators_global_settings.auto_reshape = false
end
end
@testset "Adjoint of addition/substraction" begin
Expand Down

2 comments on commit fb25c1f

@hakkelt
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
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 updated: JuliaRegistries/General/5491

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.1.0 -m "<description of version>" fb25c1f512433522ff3f4034a61052c80f562d3a
git push origin v0.1.0

Please sign in to comment.