Open ThePauliPrinciple opened 4 years ago
Rather than a different kind of derivative I think what we need is a way to generalise Function
so that we can compose it with other kinds of symbols such as Indexed
, MatrixSymbol
etc. That is I want to declare a symbol that is both Indexed
or IndexedBase
and a Function
at the same time. Or I want to declare a symbol that is both a MatrixSymbol
and a Function
representing a matrix that is a function of something else.
Your suggestion would be nice way to solve it and this is what I tried first but it seems to me that it requires a complete redesign of both indexed and function, but would love to see a suggestion how to practically implement this.
W.r.t. other kind of derivative: the functionality in this class could also be added to the base derivative class, it only adds functionality through a keyword.
Although the classes like https://reference.wolfram.com/language/ref/Dt.html would be useful, the biggest concern I have is that we can end up incompatible classes of derivatives.
I am surprised that this is not implemented. Basically one can already generate an indexed function like this:
xx, nn = sympy.symbols("x n")
YY = sympy.IndexedBase(sympy.Function("y")(xx))
But then the derivative fails as a result calculated as follows is equal to zero
result = sympy.diff(YY[nn], xx)
The following calculates the derivative correctly but is no longer indexed.
result = sympy.diff(YY, xx)
That is reproducible and imho a bug in the current release.
Tested on Arch with python 3.8.5 and sympy 1.6.2
one can already generate an indexed function
That's not really an indexed function. What you have is this:
In [31]: f = Function('f')
In [32]: x = Symbol('x')
In [33]: n = Symbol('n')
In [34]: F = IndexedBase(f(x))
In [35]: F
Out[35]: f(x)
In [36]: F[n]
Out[36]: f(x)[n]
Here F
is an indexed with f(x)
as its "symbol". The symbol is treated as an opaque parameter so the fact that it is a function of x
does not mean that F
itself is considered to be a function of x
. The indexed will only recognise something that matches its symbol exactly:
In [46]: F[n].diff(x)
Out[46]: 0
In [47]: F[n].diff(F[n])
Out[47]: 1
Oh I see. Thanks for the explanation. Quite misleading then. I would suggest that before the new feature is introduced (if it will) a corresponding warning should be printed when attempting to index a function.
Some symbols have implicit dependencies on other symbols that is not kept track of in sympy. For example, if one defines an indexed y[i]=x[i]**2, then a derivative of y[i] w.r.t. x[i] should exist. Currently sympy provides to option for this to the best of my ability. On can use Derivative, however, one can then not evaluate the derivative of something like y[i]=exp(x[i]) as .doit() will simply result in 0.
I suggest a new ImplicitDerivative, which is a subclass of Derivative and takes an "implicit" keywork argument. This specifies implicit dependencies such as above in a dictionairy, where the key is any sympy object and the value is a list of sympy objects on which it depends.
Please give any suggestions
Todo: using .doit() twice somehow fails and I am not sure how to fix it Some way to specify that a sympy object depends on any object it is derived to (e.g. by giving None instead of a list in the implicit dict and correct parsing) cleanup of code