Re: [sympy] defining a derivative

2016-10-17 Thread Riccardo Rossi
iterating a little more,

unfortunately the "doit" fails at the next bend...

this code

from sympy import *

u,B = symbols('u B')
e = B*u #here i use an expression instead of a function
s = Function('s')(e)
print(diff(s,u).doit())

is expected to give somethign like

B*Derivative(s(e),e)

instead it fails with the output













*rrossi@PCCB076:~/data/examples$ python3 test_derivatives2.py 
B*Subs(Derivative(s(_xi_1), _xi_1), (_xi_1,), (B*u,))Traceback (most recent 
call last):  File "test_derivatives2.py", line 8, in aaa = { 
Derivative(e,u):symbols('DeDu'), Derivative(s,e):symbols('DsDe') }  File 
"/home/rrossi/.local/lib/python3.5/site-packages/sympy/core/function.py", 
line 1070, in __new__Can\'t calculate %s%s derivative wrt %s.''' % 
(count, ordinal, v)))ValueError: Can't calculate 1st derivative wrt B*u.*i 
guess what i should really do is to subfunction the class as your propose. 
However i do not get how to make it to work. in my case "fdiff" is simply 
not being called

class FunctionWithDerivative(Function):
nargs = 3

#first argument  --> the expression
#second argument --> the variable it depends on
#second argument --> the derivative   
@classmethod
def eval(cls,expression,x,D):
print("- in eval -. Looks like here it is 
not creating the object ...")
cls.expression = expression
cls.x = x
cls.D = D
return cls.expression

def fdiff(self, argindex=1):
print("** inside fdiff ",self.args) 



u = symbols('u')
e = Function('e')(u)
s = FunctionWithDerivative(symbols('e'), symbols('e'), symbols('DsDe') )

print(diff(s,e).doit())

returns 

rrossi@PCCB076:~/data/examples$ python3 test_derivatives2.py 
- in eval -. Looks like here it is not creating the 
object ...
s =  s
diff(s,e).doit() =  0




*and what is the worst... fdiff is NOT being called...*regards
Riccardo



On Monday, October 17, 2016 at 8:26:04 AM UTC+2, Riccardo Rossi wrote:
>
> Dear Aaron,
>
> your suggestion did work, HOWEVER i had to upgrade sympy to 1.0. It did 
> nothing on 0.7.6 (the default one on ubuntu 16.04)
>
> only writing here to "document" this behaviour
>
> regards
> Riccardo 
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/69441429-2756-424a-ba4f-97a8cbdd36e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sympy] defining a derivative

2016-10-17 Thread Riccardo Rossi
Dear Aaron,

your suggestion did work, HOWEVER i had to upgrade sympy to 1.0. It did 
nothing on 0.7.6 (the default one on ubuntu 16.04)

only writing here to "document" this behaviour

regards
Riccardo 

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/8817f5c7-cf92-46ba-8a4d-d1e9d2a1484c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sympy] defining a derivative

2016-10-16 Thread Aaron Meurer
On Sun, Oct 16, 2016 at 4:59 AM, Riccardo Rossi  wrote:
> Dear Aaron,
>
> first of all thank you for answering.
>
> before i start subclassing, let me ask if i can do something easier: can i
> use "subs"?
>
> right now i am failing, but there might be some obvious error in what i
> do...
>
> in any case i also tried (and failed) with your suggestion, surely due to my
> lack of understanding of the sympy internals.
> here goes my code
>
> regards
> Riccardo
>
> from sympy import *
>
> u = symbols('u')
> der = symbols('der')
> e = Function('e')(u)
> s = Function('s')(e)
>
> print(s)
>
> Derivative(e,u)
>
> print(diff(e,u))
> print(diff(s,e))
> print(diff(s,u)) #here i would like "der" to be replaced within the chain
> rule

The chain rule is used. The output is

Derivative(e(u), u)*Subs(Derivative(s(_xi_1), _xi_1), (_xi_1,), (e(u),))

You can also call doit on this to get

Derivative(e(u), u)*Derivative(s(e(u)), e(u))

Your issue below is that there are two different ways of representing
ds/de, using subs and the direct way. The direct way is only returned
when using doit.  Honestly it would be simpler if diff always returned
a Derivative instance when it could. I opened
https://github.com/sympy/sympy/issues/11737 for this.

Aaron Meurer

>
> #list of replacements i would like to happen
> aaa = { Derivative(e,u):symbols('DeDu'), Derivative(s,e):symbols('DsDe') }
>
> print(diff(s,u).subs(aaa)) #here Derivative(s,e) is not substituted, i would
> like to get "se*eu" as a result
>
> #the reason for the failure to substitute is here ... how can i do this
> correctly?
> for iter in diff(s,u).atoms(Derivative):
> print("iter = ",iter)
> print(iter == Derivative(s,e))
> if iter in aaa:
> print ("found")
> else:
> print("not found")
>
>
>
>
>
> #
> ### TRYING THE SUGGESTION
> #
> class FunctionWithDerivative(Function):
> def __init__(self, x, D):
> super(FunctionWithDerivative, self).__init__()
> self.x = x #this would be the var the function depends on f(x)
> self.D = D #this would be the output i wish for
> Derivative(FunctionWithDerivative(x,D),x)
>
> def fdiff(self, argindex=1):
> """
> Return the first derivative of this function.
> """
> print("**",self.args)  NOT BEING CALLED!
>
> if len(self.args) == 1:
> if(self.args[0] == self.x):
> return self.D
> else:
> return 0
> else:
> raise ArgumentIndexError(self, argindex)
>
>
> u = symbols('u')
> e = Function('e')(u)
> s = FunctionWithDerivative(symbols('e'), symbols('DsDe') )
>
> print(s)
>
> Derivative(e,u)
>
> print(diff(e,u))
> print(diff(s,e))
> print(diff(s,u)) #here i would like "der" to be replaced within the chain
> rule
>
>
>
>
>
> On Friday, October 14, 2016 at 5:27:21 PM UTC+2, Aaron Meurer wrote:
>>
>> If you want to define advanced things you need to subclass from
>> Function rather than using symbols(cls=Function). For derivatives, you
>> should define fdiff, which should return the derivative of the
>> function without consideration of the chain rule. For example, search
>> for "fdiff" in this file to see some examples for exp, log, and
>> LambertW
>> https://github.com/sympy/sympy/blob/master/sympy/functions/elementary/exponential.py.
>>
>> Aaron Meurer
>>
>> On Fri, Oct 14, 2016 at 4:53 AM, Riccardo Rossi  wrote:
>> > Dear List,
>> >
>> > i am writing since i would like to define the output of the derivative
>> > of a
>> > function, and i don't have a clue of how to achieve it
>> >
>> > to explain what i wish to do, let's consider the following script
>> >
>> > from sympy import *
>> >
>> > u = symbols('u')
>> > der = symbols('der')
>> > e = symbols('e', cls=Function)(u)
>> > s = symbols('s', cls=Function)(e)
>> > Derivative(e,u) = der #essentially i would like to teach to sympy to use
>> > a
>> > symbol for the Derivative
>> > ---> but here i get "SyntaxError: can't assign to function call"
>> >
>> > print(diff(e,u))
>> > print(diff(s,e))
>> > print(diff(s,u)) #here i would like "der" to be replaced within the
>> > chain
>> > rule
>> >
>> > any suggestion would be very welcome...
>> >
>> > regards
>> > Riccardo
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "sympy" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to sympy+un...@googlegroups.com.
>> > To post to this group, send email to sy...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/sympy.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/sympy/a4511ef2-2a10-4dbe-b6a0-01fe2fc47a05%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> 

Re: [sympy] defining a derivative

2016-10-16 Thread Riccardo Rossi
Dear Aaron,

first of all thank you for answering.

before i start subclassing, let me ask if i can do something easier: can i 
use "subs"?

right now i am failing, but there might be some obvious error in what i 
do...

in any case i also tried (and failed) with your suggestion, surely due to 
my lack of understanding of the sympy internals.
here goes my code

regards
Riccardo

from sympy import *

u = symbols('u')
der = symbols('der')
e = Function('e')(u)
s = Function('s')(e)

print(s)

Derivative(e,u) 

print(diff(e,u))
print(diff(s,e))
print(diff(s,u)) #here i would like "der" to be replaced within the chain 
rule

#list of replacements i would like to happen
aaa = { Derivative(e,u):symbols('DeDu'), Derivative(s,e):symbols('DsDe') }

print(diff(s,u).subs(aaa)) #here Derivative(s,e) is not substituted, i 
would like to get "se*eu" as a result

#the reason for the failure to substitute is here ... how can i do this 
correctly?
for iter in diff(s,u).atoms(Derivative):
print("iter = ",iter)
print(iter == Derivative(s,e))   
if iter in aaa:
print ("found")
else:
print("not found")

  
  


#
### TRYING THE SUGGESTION
#
class FunctionWithDerivative(Function):
def __init__(self, x, D):
super(FunctionWithDerivative, self).__init__()
self.x = x #this would be the var the function depends on f(x)
self.D = D #this would be the output i wish for 
Derivative(FunctionWithDerivative(x,D),x)

def fdiff(self, argindex=1):
"""
Return the first derivative of this function.
"""
print("**",self.args)  NOT BEING CALLED!

if len(self.args) == 1:
if(self.args[0] == self.x):
return self.D
else:
return 0
else:
raise ArgumentIndexError(self, argindex)


u = symbols('u')
e = Function('e')(u)
s = FunctionWithDerivative(symbols('e'), symbols('DsDe') )

print(s)

Derivative(e,u) 

print(diff(e,u))
print(diff(s,e))
print(diff(s,u)) #here i would like "der" to be replaced within the chain 
rule





On Friday, October 14, 2016 at 5:27:21 PM UTC+2, Aaron Meurer wrote:
>
> If you want to define advanced things you need to subclass from 
> Function rather than using symbols(cls=Function). For derivatives, you 
> should define fdiff, which should return the derivative of the 
> function without consideration of the chain rule. For example, search 
> for "fdiff" in this file to see some examples for exp, log, and 
> LambertW 
> https://github.com/sympy/sympy/blob/master/sympy/functions/elementary/exponential.py.
>  
>
>
> Aaron Meurer 
>
> On Fri, Oct 14, 2016 at 4:53 AM, Riccardo Rossi  > wrote: 
> > Dear List, 
> > 
> > i am writing since i would like to define the output of the derivative 
> of a 
> > function, and i don't have a clue of how to achieve it 
> > 
> > to explain what i wish to do, let's consider the following script 
> > 
> > from sympy import * 
> > 
> > u = symbols('u') 
> > der = symbols('der') 
> > e = symbols('e', cls=Function)(u) 
> > s = symbols('s', cls=Function)(e) 
> > Derivative(e,u) = der #essentially i would like to teach to sympy to use 
> a 
> > symbol for the Derivative 
> > ---> but here i get "SyntaxError: can't assign to function call" 
> > 
> > print(diff(e,u)) 
> > print(diff(s,e)) 
> > print(diff(s,u)) #here i would like "der" to be replaced within the 
> chain 
> > rule 
> > 
> > any suggestion would be very welcome... 
> > 
> > regards 
> > Riccardo 
> > 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "sympy" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to sympy+un...@googlegroups.com . 
> > To post to this group, send email to sy...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sympy. 
> > To view this discussion on the web visit 
> > 
> https://groups.google.com/d/msgid/sympy/a4511ef2-2a10-4dbe-b6a0-01fe2fc47a05%40googlegroups.com.
>  
>
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/939b99f4-b75e-4f76-992e-28a92015e926%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sympy] defining a derivative

2016-10-14 Thread Aaron Meurer
If you want to define advanced things you need to subclass from
Function rather than using symbols(cls=Function). For derivatives, you
should define fdiff, which should return the derivative of the
function without consideration of the chain rule. For example, search
for "fdiff" in this file to see some examples for exp, log, and
LambertW 
https://github.com/sympy/sympy/blob/master/sympy/functions/elementary/exponential.py.

Aaron Meurer

On Fri, Oct 14, 2016 at 4:53 AM, Riccardo Rossi  wrote:
> Dear List,
>
> i am writing since i would like to define the output of the derivative of a
> function, and i don't have a clue of how to achieve it
>
> to explain what i wish to do, let's consider the following script
>
> from sympy import *
>
> u = symbols('u')
> der = symbols('der')
> e = symbols('e', cls=Function)(u)
> s = symbols('s', cls=Function)(e)
> Derivative(e,u) = der #essentially i would like to teach to sympy to use a
> symbol for the Derivative
> ---> but here i get "SyntaxError: can't assign to function call"
>
> print(diff(e,u))
> print(diff(s,e))
> print(diff(s,u)) #here i would like "der" to be replaced within the chain
> rule
>
> any suggestion would be very welcome...
>
> regards
> Riccardo
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+unsubscr...@googlegroups.com.
> To post to this group, send email to sympy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/a4511ef2-2a10-4dbe-b6a0-01fe2fc47a05%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2Bn_jrwzkANbe6tSF_EdrM2LeZrFDjEyVLA7puoNvCCuA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.