Differentiating implicit functions #360
Replies: 3 comments
-
|
Hi @vegardjervell - could you please try the following: dual z_func(dual a){
const auto rootfun = [&](dual z_) -> dual {return pow(z_, 2.) - a;};
const auto rootfun_z = [&](dual z_) -> dual {return 2*z_;};
dual f0, f1;
dual z = 1;
do {
f0 = rootfun(z);
f1 = rootfun_z(z); // instead of derivative(rootfun, wrt(z), at(z)), which breaks derivative propagation because the result is not dual but an order lower (double)
z -= f0 / f1;
} while (abs(f0) > 1e-6);
return z;
}
int main(){
dual a = 4.;
auto [z0, z1] = derivatives(z_func, wrt(a), at(a));
std::cout << "Z : " << z0 << ", " << z1 << std::endl; // Should be (2, 0.25)
return 0;
}Hopefully this compiles - cannot check now. Explanation in the comment above. You are "consuming" the |
Beta Was this translation helpful? Give feedback.
-
|
Thank you, your example compiles and works, but has the issue that it requires differentiating but ended up with some follow-up questions:
It definitely feels like there's a lot going on under the hood here that I need to account for, but I'm having a really hard time figuring out what the system to it is. Any help would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
-
|
I've done some more digging @allanleal, and I'm starting to wonder if this should be raised as an issue. I'm not sure about the details of how this is implemented, but by comparing to my own (very bare-bones) implementation of hyperdual numbers it appears that the different "parts" of the To give an example: Gives the output As you can see, the derivative computed inside What is your take on this? For the time being it looks to me like I'll have to resort to my own implementation of hyperduals for these use-cases, is this something that should be raised as an issue? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm having some issues with differentiating a function$z(a)$ , which is mathematically given by something of the form $f(z, a) = 0$ . As a minimal example to demonstrate the issue I'll say that we can have $z(a) = \sqrt{a}$ , implemented as a function taking $a$ as the argument and returning the solution to $z^2 - a = 0$ , such as
The issue is that the derivative comes out completely wrong (I get
z1 = -59.462). I'm wondering if I've misunderstood something regarding howdualshould be used for this, as I get the expected behaviour if I implementz_funcasand wouldn't think that the internals of
z_funcshould matter, as long as I'm usingduals all the way through. I've changing the order of theduals (usingdual2nd), but get the same result.Beta Was this translation helpful? Give feedback.
All reactions