# Function structure for ForwardDiff jacobian

**URL:** https://discourse.julialang.org/t/function-structure-for-forwarddiff-jacobian/93991
**Category:** General Usage
**Tags:** forwarddiff
**Created:** [February 3, 2023, 2:22pm UTC](https://discourse.julialang.org/t/function-structure-for-forwarddiff-jacobian/93991 "2023-02-03T14:22:00Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![rakshith95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakshith95/32/44606_2.png) [@rakshith95](https://discourse.julialang.org/u/rakshith95)
#### Post date: [February 3, 2023, 2:22pm UTC](https://discourse.julialang.org/t/function-structure-for-forwarddiff-jacobian/93991/1 "2023-02-03T14:22:00Z")

</div>

Hello,  
I’m working with images, and I’m seeing some strange behaviour on Jacobian computation, so I suspect my function formulation is incorrect.

I have two patches in an image, and my function `F` which returns a vector of differences in intensities between the patches.  
The difference vector which is returned is computed as `image[y] - image[g(y, x)]` where `x` are the variables, and `y` are some coordinates.

Now, if I try computing the jacobian of `F` wrt the variables of `x` with ForwardDiff, my Jacobian is a matrix of all zeros, implying that there is no relation between the function vector, and the input variables.

Is the way I am formulating this incorrect?

The function code:

```julia
function F_photometric(x̂::AbstractVector, A_data::AbstractVector, u_data::AbstractVector, imagebgr::AbstractVecOrMat, region1_coordinates::AbstractMatrix, image_dims::AbstractVector)
    # A_diff = x̂[1:4] - A_data
    # u_diff = x̂[5:8] - u_data
    â = reshape(x̂[1:4],2,2)
    
    region1_coordinates_int = Int.(round.(region1_coordinates[1:2,:]))
    region2_coordinates = x̂[7:8] .+ â*(region1_coordinates_int .- x̂[5:6])  
    region2_coordinates_int = Int.(round.(region2_coordinates))
    
    region1_indices = Array{Int64}(undef, size(region1_coordinates)[2])
    region2_indices = Array{Int64}(undef, size(region2_coordinates)[2])
    for j=1:size(region1_coordinates)[2]
        region1_indices[j] = convert_2Dto1D_index(image_dims,region1_coordinates_int[:,j])
        region2_indices[j] = convert_2Dto1D_index(image_dims,region2_coordinates_int[:,j])
    end

    region1_bgr = imagebgr[region1_indices]
    region2_bgr = imagebgr[region2_indices]
    photo_diff = region1_bgr - region2_bgr
    # photo_diff_norm = reduce(vcat, photo_diff)

    # print(norm(photo_diff),"\t")
    return photo_diff
end
  
Jₒ = ForwardDiff.jacobian(x̂ -> F(x̂, A_data, u_data, image_bgr, region1, img_dims), x̂)

```
