Skip to content

Commit

Permalink
Replace trunc with round to fix small precision problems
Browse files Browse the repository at this point in the history
  • Loading branch information
jofrevalles committed Apr 26, 2024
1 parent 0b9fcd6 commit de5f8f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/ExprEvaluation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function convert_expr(expr, custom_operations, primitives_with_arity, gradient_f
if get(primitives_with_arity, expr, 1) == 0
return :(vars[$(QuoteNode(expr))])
elseif get(primitives_with_arity, expr, 1) == -1
return :(vars[$(QuoteNode(expr))][(vars[:x] + 0.5) * (width-1) + 1 |> trunc |> Int, (vars[:y] + 0.5) * (height-1) + 1 |> trunc |> Int])
return :(vars[$(QuoteNode(expr))][(vars[:x] + 0.5) * (width-1) + 1 |> round |> Int, (vars[:y] + 0.5) * (height-1) + 1 |> round |> Int])
else
return expr
end
Expand Down
20 changes: 10 additions & 10 deletions src/MathOperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function x_grad(func, vars, width, height; Δx = 1)
x_val = vars[:x]
Δx_scaled = Δx / (width - 1) # scale Δx to be proportional to the image width

idx_x = (x_val + 0.5) * (width - 1) + 1 |> trunc |> Int
idx_x = (x_val + 0.5) * (width - 1) + 1 |> round |> Int

# Evaluate function at x
center_val = func(merge(vars, Dict(:x => x_val)))
Expand All @@ -99,7 +99,7 @@ function y_grad(func, vars, width, height; Δy = 1)
y_val = vars[:y]
Δy_scaled = Δy / (height - 1) # scale Δy to be proportional to the image height

idx_y = (y_val + 0.5) * (height - 1) + 1 |> trunc |> Int
idx_y = (y_val + 0.5) * (height - 1) + 1 |> round |> Int

# Evaluate function at y
center_val = func(merge(vars, Dict(:y => y_val)))
Expand Down Expand Up @@ -133,8 +133,8 @@ function laplacian(func, vars, width, height; Δx = 1, Δy = 1)
Δx_scaled = Δx / (width - 1) # scale Δx to be proportional to the image width
Δy_scaled = Δy / (height - 1) # scale Δy to be proportional to the image height

idx_x = (x_val + 0.5) * (width - 1) + 1 |> trunc |> Int
idx_y = (y_val + 0.5) * (height - 1) + 1 |> trunc |> Int
idx_x = (x_val + 0.5) * (width - 1) + 1 |> round |> Int
idx_y = (y_val + 0.5) * (height - 1) + 1 |> round |> Int

center_val = func(merge(vars, Dict(:x => x_val, :y => y_val)))

Expand Down Expand Up @@ -190,8 +190,8 @@ function neighbor_min(func, vars, width, height; Δx = 1, Δy = 1)


if any([isa(v, Matrix) for v in values(vars)])
idx_x = (x_val + 0.5) * (width - 1) + 1 |> trunc |> Int
idx_y = (y_val + 0.5) * (height - 1) + 1 |> trunc |> Int
idx_x = (x_val + 0.5) * (width - 1) + 1 |> round |> Int
idx_y = (y_val + 0.5) * (height - 1) + 1 |> round |> Int

# Filter the iterations that are not in the matrix
range_x = filter(x -> 1 <= idx_x + x <= width, range_x)
Expand Down Expand Up @@ -232,8 +232,8 @@ function neighbor_max(func, vars, width, height; Δx = 1, Δy = 1)
range_y = -Δy:Δy

if any([isa(v, Matrix) for v in values(vars)])
idx_x = (x_val + 0.5) * (width - 1) + 1 |> trunc |> Int
idx_y = (y_val + 0.5) * (height - 1) + 1 |> trunc |> Int
idx_x = (x_val + 0.5) * (width - 1) + 1 |> round |> Int
idx_y = (y_val + 0.5) * (height - 1) + 1 |> round |> Int

# Filter the iterations that are not in the matrix
range_x = filter(x -> 1 <= idx_x + x <= width, range_x)
Expand Down Expand Up @@ -275,8 +275,8 @@ function neighbor_ave(func, vars, width, height; Δx = 1, Δy = 1)
range_y = -Δy:Δy

if any([isa(v, Matrix) for v in values(vars)])
idx_x = (x_val + 0.5) * (width - 1) + 1 |> trunc |> Int
idx_y = (y_val + 0.5) * (height - 1) + 1 |> trunc |> Int
idx_x = (x_val + 0.5) * (width - 1) + 1 |> round |> Int
idx_y = (y_val + 0.5) * (height - 1) + 1 |> round |> Int

# Filter the iterations that are not in the matrix
range_x = filter(x -> 1 <= idx_x + x <= width, range_x)
Expand Down

0 comments on commit de5f8f2

Please sign in to comment.