Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @tamandua! I feel embarrassed to admit this is the first time I hear about the Fréchet derivative, thus I am not sure about what you are trying to achieve or how to test my code. However, I hope the following is useful for you.

On one hand, Sage's function() allows to define symbolic functions (referring to "function" in the mathematical sense). (Sorry about the repetitive words). In this particular case, I don't think you need such a thing, since the instructions you used to work with r0 work perfectly well with normal Python functions (this time, "function" in the sense of "subroutine"). For example, you could just define inside your innermost loop the following:

def r0(*args):
    return dependVar[i](*independVar)+ testfunction[i](*independVar) * eps

and that should do the trick.

On the other hand, it is also possible to define a symbolic function with an arbitrary number of arguments, as you request in your question. For completeness (and for other users with this same question), I am going to describe what you should do. First, define a Python subroutine that accepts an arbitrary number of arguments. This is basically the same as my previous bit of code, but I will use a different name in order to avoid name clash. For example,

def _r0(*args):
    return dependVar[i](*independVar)+ testfunction[i](*independVar) * eps

Now, you create a symbolic function r0 and indicate Sage that its values should be computed using the _r0 subroutine:

r0 = function('r0', eval_func=_r0)

That's it!

If you want to know more about Sage's function(), you can use the command function?, and you will be able to see the documentation of this command. In fact, it is way more advanced and complex than I can describe here. For example, you can actually define the symbolic function r0 and how to differentiate it symbolically.

In any case, here are the two versions of my code:

Please, let me know if this worked for you. I recommend you test it with different case, just to be sure. If it doesn't work, please, let me know so we can work an more adequate approach for your use!

I hope this helps!