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

Add new LazyBinaryOperator pow #32

Merged
merged 1 commit into from
Dec 19, 2023
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
22 changes: 21 additions & 1 deletion src/LazyOperators/LazyOperators.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
module LazyOperators

import Base:
*, /, +, -, max, min, sqrt, abs, tan, sin, cos, tanh, sinh, cosh, atan, asin, acos, zero
*,
/,
+,
-,
^,
max,
min,
sqrt,
abs,
tan,
sin,
cos,
tanh,
sinh,
cosh,
atan,
asin,
acos,
zero,
one,
materialize

using LinearAlgebra
import LinearAlgebra: dot, transpose, tr
Expand Down
10 changes: 9 additions & 1 deletion src/LazyOperators/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Define rules for binary operators
###############################################################

const LazyBinaryOp = (:*, :/, :+, :-, :max, :min, :dot)
const LazyBinaryOp = (:*, :/, :+, :-, :^, :max, :min, :dot)

for f in LazyBinaryOp
@eval ($f)(a::AbstractLazy, b::AbstractLazy) = LazyOperator($f, a, b)
Expand Down Expand Up @@ -31,6 +31,7 @@
:asin,
:acos,
:zero,
:one,
)

for f in LazyUnaryOp
Expand Down Expand Up @@ -65,6 +66,13 @@
Base.:/(a::NullOperator, b) = a
Base.:/(::NullOperator, ::NullOperator) = NullOperator()

# For binary `^`:
Base.:^(a, ::NullOperator) = error("Undefined")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why defining a method that returns error rather than not defining anything and let julia raise an error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if we don't raise an error explicitly we will forget that the method was not defined on purpose.

Base.:^(a::NullOperator, b) = a
Base.:^(::NullOperator, ::NullOperator) = NullOperator() # or "I" ?
Base.:^(a::AbstractLazy, ::NullOperator) = error("Undefined")

Check warning on line 73 in src/LazyOperators/algebra.jl

View check run for this annotation

Codecov / codecov/patch

src/LazyOperators/algebra.jl#L70-L73

Added lines #L70 - L73 were not covered by tests
Base.:^(a::NullOperator, b::AbstractLazy) = a

###############################################################
# Define rules with `broadcasted`
###############################################################
Expand Down
2 changes: 2 additions & 0 deletions src/LazyOperators/mapover.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
Base.:+(::NullOperator, b::AbstractMapOver) = b
Base.:-(a::AbstractMapOver, ::NullOperator) = a
Base.:-(::NullOperator, b::AbstractMapOver) = -b
Base.:^(::AbstractMapOver, ::NullOperator) = error("Undefined")
Base.:^(::NullOperator, ::AbstractMapOver) = NullOperator()

Check warning on line 106 in src/LazyOperators/mapover.jl

View check run for this annotation

Codecov / codecov/patch

src/LazyOperators/mapover.jl#L105-L106

Added lines #L105 - L106 were not covered by tests
Base.max(a::AbstractMapOver, ::NullOperator) = a
Base.max(::NullOperator, b::AbstractMapOver) = b
Base.min(a::AbstractMapOver, ::NullOperator) = a
Expand Down
15 changes: 15 additions & 0 deletions test/operator/test_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,19 @@
a = assemble_linear(l, V)
@test all(a .≈ [-3.0, 3.0, -3.0, 3.0, -3.0, -3.0, 3.0, 3.0])
end

@testset "Pow" begin
mesh = one_cell_mesh(:quad)
u = PhysicalFunction(x -> 3.0)
v = PhysicalFunction(x -> 2.0)
cinfo = CellInfo(mesh, 1)
cpoint = Bcube.CellPoint(SA[0.1, 0.3], cinfo, Bcube.ReferenceDomain())

@test Bcube.materialize(u * u, cinfo)(cpoint) ≈ 9
@test Bcube.materialize(u^2, cinfo)(cpoint) ≈ 9
@test Bcube.materialize(u^v, cinfo)(cpoint) ≈ 9

a = Bcube.NullOperator()
@test a^u == a
end
end
Loading