ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 30 Jul 2012 04:44:57 -0500how to import a math command "df(x,y)/dx + int_a^b f(x,z) f(y,z) dz" in sage when f is an unknown function of two variables in 3d space?https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/Hi,
As a newbie to sage and python I asked a question here around two days ago, and had a chat about it in sage's IRC channel, however, after I learned more from the guys therein and checking different situations by myself I understood my previous question somewhat nonsense, so I am now editing it!
I am working on a multipoint theory of turbulence in fluid mechanics, so I have functions that are simultaneously defined on multi point of space, like f(x,y,z; x1,y1,z1; ), however these functions --which are my main unknowns to be found from a set of many (I mean from some ten to maybe some millions!) coupled nonlinear integro-partial differential equations-- sometimes live as integrand to some integrals and sometimes are differentiated and the important thing is that their arguments vary from place to place in an equation and/or in different equations. Maybe I should classify my difficulties into two cases:
1. how to define many functions automatically and symbolically without specifying their arguments?
2. how to not specify the arguments to a function and at the same time be able to differentiate it or take its integral?
the answer to the first equation according to what I have read in this website and has been told from the guys in IRC channel is either of the followings:
reset
var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3')
for k in range(10):
function('f%s' %k, nargs=2)
# one can even refuse to determine the number of arguments and let it open by omitting nargs!
or
reset
var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3')
for k in range(10):
s = 'def f%d(*args):\n print args\n' % k
exec(s)
at the end of each of these codes one would have 10 functions f0,f1, ,f9 which can be given their arguments and be worked with. For example one can write f2(x,y)+f2(y,z1) which introduce once x and y and another time y and z1 to the same function f2 in one logical line in sage! This is why the second problems seems not to be solvable using the above methods as e.g. f2.diff(x) is nonsense as long as a specific law is not defined for f2, like f(x,y)=2*x*y^3 ! And it is nonsense to sage as it gives error when one tries to write f2.diff() before defining such a prescribed law for f2, it says function object and NewSymbolicFunction object have no attribute 'diff'
The only way that I can think about is to give my functions a set of dummy variables and replace them with the variables of interest in each equation or differentiation and integration, however, I am not sure it would work as the function is unknown and I don't know if the procedure of many-times changing their variables would affect what is found at the end or not?
----------------------------------------------
And now a second question, I wanted --if possible-- to make my equations more readable in sage code --as is on the paper-- by working with vectors as my functions argument, e.g. something like this:
reset
var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3')
R0=[x,y,z,t]
R1=[x1,y1,z1,t1]
R2=[x2,y2,z2,t2]
R3=[x3,y3,z3,t3]
R=[R0,R1,R2,R3]
to then write my titled math command something like this:
d f1(R0)/d R0[0] + int int int int f2(R0,R1) f2(R1,R2) dR1
here in this example f1 has a single spatio-temporal vector as its arguments whereas f2 has two of such spatio-temporal vectors as its arguments ! also note that dR1 under integral means dx1*dy1*dz1*dt1 !
Best Regards and thanks in advance
**EDIT.**
I tried the following code:
reset
var('x,y,z,t')
for i in range(4):
function('U%s' %i, x,y,z,t)
but still trying e.g. "diff(U0,x)" gives the error that 'NewSymbolicFunction' object has no attribute 'diff' ! This time I had my functions' arguments defined at the very function definition! Ok, I already knew that defining the arguments should work properly, so I looked to the help pages again and found out that the code below works:
var('x,y')
f=function('f',x,y)
f.diff(x,1,y,3)
so that everything lies in "f=" in function definition being absent for my in-loop-defined-functions! So any idea now? Maybe if this problem is resolved even the previous codes will work, that is, without specifying the arguments to the functions!Fri, 20 Jul 2012 21:07:41 -0500https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/Answer by owari for <div class="snippet"><p>Hi,</p>
<p>As a newbie to sage and python I asked a question here around two days ago, and had a chat about it in sage's IRC channel, however, after I learned more from the guys therein and checking different situations by myself I understood my previous question somewhat nonsense, so I am now editing it!</p>
<p>I am working on a multipoint theory of turbulence in fluid mechanics, so I have functions that are simultaneously defined on multi point of space, like f(x,y,z; x1,y1,z1; ), however these functions --which are my main unknowns to be found from a set of many (I mean from some ten to maybe some millions!) coupled nonlinear integro-partial differential equations-- sometimes live as integrand to some integrals and sometimes are differentiated and the important thing is that their arguments vary from place to place in an equation and/or in different equations. Maybe I should classify my difficulties into two cases:</p>
<ol>
<li><p>how to define many functions automatically and symbolically without specifying their arguments?</p></li>
<li><p>how to not specify the arguments to a function and at the same time be able to differentiate it or take its integral?</p></li>
</ol>
<p>the answer to the first equation according to what I have read in this website and has been told from the guys in IRC channel is either of the followings:</p>
<pre><code>reset
var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3')
for k in range(10):
function('f%s' %k, nargs=2)
# one can even refuse to determine the number of arguments and let it open by omitting nargs!
</code></pre>
<p>or</p>
<pre><code>reset
var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3')
for k in range(10):
s = 'def f%d(*args):\n print args\n' % k
exec(s)
</code></pre>
<p>at the end of each of these codes one would have 10 functions f0,f1, ,f9 which can be given their arguments and be worked with. For example one can write f2(x,y)+f2(y,z1) which introduce once x and y and another time y and z1 to the same function f2 in one logical line in sage! This is why the second problems seems not to be solvable using the above methods as e.g. f2.diff(x) is nonsense as long as a specific law is not defined for f2, like f(x,y)=2<em>x</em>y^3 ! And it is nonsense to sage as it gives error when one tries to write f2.diff() before defining such a prescribed law for f2, it says function object and NewSymbolicFunction object have no attribute 'diff' </p>
<p>The only way that I can think about is to give my functions a set of dummy variables and replace them with the variables of interest in each equation or differentiation and integration, however, I am not sure it would work as the function is unknown and I don't know if the procedure of ...<span class="expander"> <a>(more)</a></span></p></div> https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/?answer=13862#post-id-13862Well, my tries to work with dummy variables failed, but the code below solved the problem carefully: using the Lambda operator! Actually, it is also commented in the book "Dive into Python" that "You can use a lambda function without even assigning it to a variable" and this was what I exactly needed, not in defining my functions but in defining the operators acting on them, at least I guess so!
var('x,y,z')
f=function('f1', x,y)
h= lambda function1,function2: integral(function1(x,y)*function2(x,z),x,-oo,oo)
eq1= f1(y,z)==h(f1,f1)
show(eq1)
However, in the same book it was commented that "lambda functions are a matter of style. Using them is never required; anywhere you could use them, you could define a separate normal function and use that instead." So that, it shouldn't be the only way, but I couldn't define another function that does the same anyhow!?
Sat, 28 Jul 2012 10:12:06 -0500https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/?answer=13862#post-id-13862Comment by Jason Grout for <p>Well, my tries to work with dummy variables failed, but the code below solved the problem carefully: using the Lambda operator! Actually, it is also commented in the book "Dive into Python" that "You can use a lambda function without even assigning it to a variable" and this was what I exactly needed, not in defining my functions but in defining the operators acting on them, at least I guess so!</p>
<pre><code>var('x,y,z')
f=function('f1', x,y)
h= lambda function1,function2: integral(function1(x,y)*function2(x,z),x,-oo,oo)
eq1= f1(y,z)==h(f1,f1)
show(eq1)
</code></pre>
<p>However, in the same book it was commented that "lambda functions are a matter of style. Using them is never required; anywhere you could use them, you could define a separate normal function and use that instead." So that, it shouldn't be the only way, but I couldn't define another function that does the same anyhow!?</p>
https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/?comment=19323#post-id-19323This is the equivalent of the above code without the lambdas: [here](http://aleph.sagemath.org/?z=eJxFjT0OwjAMhXefwltsZAYzIuUwFcS0EopFSKHt6TFDwdOzvvfzGhqlRVbZEoNlm-ulT14pmSbBAAzXYjjSTlR2deIzAsa10udWcaq93Npw_1vpmz_8_PFuLIsc3cWdoTw0oynFOOccE9GtDM_R3xSMP1sQMVY=)Mon, 30 Jul 2012 04:44:57 -0500https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/?comment=19323#post-id-19323Answer by owari for <div class="snippet"><p>Hi,</p>
<p>As a newbie to sage and python I asked a question here around two days ago, and had a chat about it in sage's IRC channel, however, after I learned more from the guys therein and checking different situations by myself I understood my previous question somewhat nonsense, so I am now editing it!</p>
<p>I am working on a multipoint theory of turbulence in fluid mechanics, so I have functions that are simultaneously defined on multi point of space, like f(x,y,z; x1,y1,z1; ), however these functions --which are my main unknowns to be found from a set of many (I mean from some ten to maybe some millions!) coupled nonlinear integro-partial differential equations-- sometimes live as integrand to some integrals and sometimes are differentiated and the important thing is that their arguments vary from place to place in an equation and/or in different equations. Maybe I should classify my difficulties into two cases:</p>
<ol>
<li><p>how to define many functions automatically and symbolically without specifying their arguments?</p></li>
<li><p>how to not specify the arguments to a function and at the same time be able to differentiate it or take its integral?</p></li>
</ol>
<p>the answer to the first equation according to what I have read in this website and has been told from the guys in IRC channel is either of the followings:</p>
<pre><code>reset
var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3')
for k in range(10):
function('f%s' %k, nargs=2)
# one can even refuse to determine the number of arguments and let it open by omitting nargs!
</code></pre>
<p>or</p>
<pre><code>reset
var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3')
for k in range(10):
s = 'def f%d(*args):\n print args\n' % k
exec(s)
</code></pre>
<p>at the end of each of these codes one would have 10 functions f0,f1, ,f9 which can be given their arguments and be worked with. For example one can write f2(x,y)+f2(y,z1) which introduce once x and y and another time y and z1 to the same function f2 in one logical line in sage! This is why the second problems seems not to be solvable using the above methods as e.g. f2.diff(x) is nonsense as long as a specific law is not defined for f2, like f(x,y)=2<em>x</em>y^3 ! And it is nonsense to sage as it gives error when one tries to write f2.diff() before defining such a prescribed law for f2, it says function object and NewSymbolicFunction object have no attribute 'diff' </p>
<p>The only way that I can think about is to give my functions a set of dummy variables and replace them with the variables of interest in each equation or differentiation and integration, however, I am not sure it would work as the function is unknown and I don't know if the procedure of ...<span class="expander"> <a>(more)</a></span></p></div> https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/?answer=13842#post-id-13842Ok,
According to the codes I gave in my last edit above I checked the following code and it worked:
reset
var('x,y,z,t')
U=[U0,U1,U2,U3]
for i in range(4):
U[i]=function('U%s' %i, x,y,z,t)
now trying the command "`show( diff(U[0],x,1,y,3) + integral(U[0],x,-oo,oo) )`" works nicely! However, it is not beautiful to use the command "`U=[U0,U1,U2,U3]`" when the number of unknowns gets large, so that I rewrote the above code like the following and it worked again:
reset
var('x,y,z,t')
U=[]
for i in range(4):
U.append(function('U%s' %i, x,y,z,t))
Also I checked and see if nargs=2, as an example, was used instead of specifying the arguments, it was then not going to give anything as result other than an error. So still I think one should use dummy variables to define the functions, then try substituting them by real variables whenever needed, it is only important to know the number of each function argument (I am not sure if one can also define more than required, if not sure exactly how many may be required, and after differentiation and integration assume the extra ones as parameter and equate them with unity, I mean at the end!)Mon, 23 Jul 2012 05:10:42 -0500https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/?answer=13842#post-id-13842Answer by owari for <div class="snippet"><p>Hi,</p>
<p>As a newbie to sage and python I asked a question here around two days ago, and had a chat about it in sage's IRC channel, however, after I learned more from the guys therein and checking different situations by myself I understood my previous question somewhat nonsense, so I am now editing it!</p>
<p>I am working on a multipoint theory of turbulence in fluid mechanics, so I have functions that are simultaneously defined on multi point of space, like f(x,y,z; x1,y1,z1; ), however these functions --which are my main unknowns to be found from a set of many (I mean from some ten to maybe some millions!) coupled nonlinear integro-partial differential equations-- sometimes live as integrand to some integrals and sometimes are differentiated and the important thing is that their arguments vary from place to place in an equation and/or in different equations. Maybe I should classify my difficulties into two cases:</p>
<ol>
<li><p>how to define many functions automatically and symbolically without specifying their arguments?</p></li>
<li><p>how to not specify the arguments to a function and at the same time be able to differentiate it or take its integral?</p></li>
</ol>
<p>the answer to the first equation according to what I have read in this website and has been told from the guys in IRC channel is either of the followings:</p>
<pre><code>reset
var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3')
for k in range(10):
function('f%s' %k, nargs=2)
# one can even refuse to determine the number of arguments and let it open by omitting nargs!
</code></pre>
<p>or</p>
<pre><code>reset
var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3')
for k in range(10):
s = 'def f%d(*args):\n print args\n' % k
exec(s)
</code></pre>
<p>at the end of each of these codes one would have 10 functions f0,f1, ,f9 which can be given their arguments and be worked with. For example one can write f2(x,y)+f2(y,z1) which introduce once x and y and another time y and z1 to the same function f2 in one logical line in sage! This is why the second problems seems not to be solvable using the above methods as e.g. f2.diff(x) is nonsense as long as a specific law is not defined for f2, like f(x,y)=2<em>x</em>y^3 ! And it is nonsense to sage as it gives error when one tries to write f2.diff() before defining such a prescribed law for f2, it says function object and NewSymbolicFunction object have no attribute 'diff' </p>
<p>The only way that I can think about is to give my functions a set of dummy variables and replace them with the variables of interest in each equation or differentiation and integration, however, I am not sure it would work as the function is unknown and I don't know if the procedure of ...<span class="expander"> <a>(more)</a></span></p></div> https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/?answer=13857#post-id-13857Also about using spatio-temporal vectors instead of single scalar coordinate variables, if one use in his function definition a code like:
f=function('f',R0,R1)
or use instead
f=function('f',*R0,*R1)
both will yield into error, but the correct syntax is to use the following code as much as I tried:
f=function('f',*R0+R1)
hope that it would help
Thu, 26 Jul 2012 01:36:34 -0500https://ask.sagemath.org/question/9168/how-to-import-a-math-command-dfxydx-int_ab-fxz-fyz-dz-in-sage-when-f-is-an-unknown-function-of-two-variables-in-3d-space/?answer=13857#post-id-13857