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

Incorrect Jacobian generated when Order of assignment to Outputs is changed #479

Closed
Nirhar opened this issue Aug 3, 2022 · 1 comment · Fixed by #1003
Closed

Incorrect Jacobian generated when Order of assignment to Outputs is changed #479

Nirhar opened this issue Aug 3, 2022 · 1 comment · Fixed by #1003
Assignees
Milestone

Comments

@Nirhar
Copy link
Contributor

Nirhar commented Aug 3, 2022

Minimum reproducible example:

void h(float a, float output[]){
  output[1]=a;
  output[0]=a*a;  
}
int main(){
  auto dh = clad::jacobian(h);
  float a=3;
  float output[2]={0};
  float jc[2]={0};
  dh.execute(a,output,jc);
  printf("Result = {%.2f, %.2f}",jc[0],jc[1]);
}

The above example prints: Result = {0.00, 6.00}, which is wrong

However if the order of assignment to outputs is correctly given:

void h(float a, float output[]){
  output[0]=a*a;  
  output[1]=a;
}
int main(){
  auto dh = clad::jacobian(h);
  float a=3;
  float output[2]={0};
  float jc[2]={0};
  dh.execute(a,output,jc);
  printf("Result = {%.2f, %.2f}",jc[0],jc[1]);
}

The result is correct: Result = {6.00, 1.00}

@Nirhar
Copy link
Contributor Author

Nirhar commented Aug 3, 2022

The issue seems to be in this line, which expects sequential ordering of references to the output array

@vgvassilev vgvassilev added this to the v1.6 milestone Feb 7, 2024
@vgvassilev vgvassilev assigned PetroZarytskyi and unassigned vaithak Jul 11, 2024
@vgvassilev vgvassilev modified the milestones: v1.6, v1.7 Jul 11, 2024
PetroZarytskyi added a commit to PetroZarytskyi/clad that referenced this issue Jul 24, 2024
On master, when creating jacobians, we assume that output elements are used in the ascending order. i.e.
```
output[0] = ...;
output[1] = ...;
```
is supported but
```
output[1] = ...;
output[0] = ...;
```
is not supported. We should not make such assumptions.
Fixes vgvassilev#479.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants