[sage-support] Re: substitution in list
Besides the pure python options pointed above, if your list is filled by symbolic expressions, you can also turn it into a vector and do the substitutions straightforwardly. Given x = list(var('x', n=10)) ep = list(var('epsilon', n=10)) a random sublist my_list = [x[randint(0,8)] for _ in range(6)] and a substitution rule subs_rule = dict(zip(x[4:], ep)) You can make my_vec_list = vector(my_list).subs(subs_rule) and turn back to a list my_list2 = list(my_vec_list) Em terça-feira, 28 de dezembro de 2021 às 15:03:51 UTC-3, Nils Bruin escreveu: > That's a python question. See for instance, > > > https://stackoverflow.com/questions/2582138/finding-and-replacing-elements-in-a-list > > If you scroll down, there are some suggestions there that deal with > multiple replacements as well > > On Tuesday, 28 December 2021 at 06:09:35 UTC-8 cyrille piatecki wrote: > >> First I have seen that perhaps my question has an answer in AskSagemath >> but currently it doesn't answer. >> >> Suppose I have a list of variables x_1, x_2, x_3,...x_n >> >> I have some sub-list of variables say [x_4, x_1, x_6...]. But whenever >> x_4, x_5,x_6 belong to this list I want the substitution x_4 ->epsilon_0, >> x_5 -> epsilon_1... (this is an only an example. Is there a way to do this >> in the same way that we have a substitution in a fonction in Sagemath? >> > -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/ded1b03d-32b5-4514-a019-2d8d6dcc9e7bn%40googlegroups.com.
[sage-support] Re: substitution in list
That's a python question. See for instance, https://stackoverflow.com/questions/2582138/finding-and-replacing-elements-in-a-list If you scroll down, there are some suggestions there that deal with multiple replacements as well On Tuesday, 28 December 2021 at 06:09:35 UTC-8 cyrille piatecki wrote: > First I have seen that perhaps my question has an answer in AskSagemath > but currently it doesn't answer. > > Suppose I have a list of variables x_1, x_2, x_3,...x_n > > I have some sub-list of variables say [x_4, x_1, x_6...]. But whenever > x_4, x_5,x_6 belong to this list I want the substitution x_4 ->epsilon_0, > x_5 -> epsilon_1... (this is an only an example. Is there a way to do this > in the same way that we have a substitution in a fonction in Sagemath? > -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/764fe9f5-d8b3-4033-afcf-183474f5e318n%40googlegroups.com.
[sage-support] Re: substitution of lambda variable
On Thursday, May 28, 2009 at 9:31:46 PM UTC+5:30, Jason Grout wrote: Mike Hansen wrote: > Hello, > > On Thu, May 28, 2009 at 8:38 AM, Paul Sargentwrote: >> # Subs for lambda (have to use "lambda", but that's a keyword) lambda is a reserved keyword in Python, which means you can't use it in a python statement that you are writing. You'll notice below that Mike never writes lambda on a line (he only writes the string "lambda"). Yeah, I know; it's frustrating for me in linear algebra to not use lambda for eigenvalues. Can we declare lambda as a variable? I want to use it in eigenvalues (linear algebra). like: l = var("lambda"); It'll give error. So how can I declare it as a variable. -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.
[sage-support] Re: substitution of lambda variable
On Wednesday, September 16, 2015 at 1:54:40 AM UTC-7, Mandeep Singh wrote: > > Can we declare lambda as a variable? I want to use it in eigenvalues > (linear algebra). > like: > l = var("lambda"); > It'll give error. So how can I declare it as a variable. > This works. I don't know if it will keep working: sage: l=SR.symbol('lambda') sage: l lambda sage: l^2+l+1 lambda^2 + lambda + 1 -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.
[sage-support] Re: Substitution in infinite polynomial rings
Hi Bruno, I am sorry that I (as author of the InfinitePolynomialRing stuff) did not answer before. On 2014-04-16, BJ brunojo...@gmail.com wrote: The output looks something like this: [-e_8 + e_4^2, -e_10 + e_6*e_4, -1382*e_12 + 2205*e_8*e_4 + 500*e_6^2 - 1323*e_4^3, -10*e_14 + 21*e_10*e_4 + 22*e_8*e_6 - 33*e_6*e_4^2, 45606*e_12*e_4 + 65000*e_10*e_6 + 42042*e_8^2 - 63063*e_8*e_4^2 - 71500*e_6^2*e_4, 126126*e_14*e_4 + 212828*e_12*e_6 + 309582*e_10*e_8 - 154791*e_10*e_4^2 - 378378*e_8*e_6*e_4 - 71500*e_6^3, 858000*e_14*e_6 + 1337776*e_12*e_8 - 501666*e_12*e_4^2 + 760500*e_10^2 - 1287000*e_10*e_6*e_4 - 693693*e_8^2*e_4 - 786500*e_8*e_6^2] These list elements should be interpreted as relations between the variables e_i (for even i), so that, for instance, the first one should be interpreted as e_8 = e_4^2. Moreover, I know that these relations should allow me to inductively express each e_k in terms of e_4 and e_6. The original purpose of InfinitePolynomialRing is to implement the algorithm of Aschenbrenner and Hillar for the computation of symmetric Gröbner bases. This means: You would be in a situation where any permutation of indices applied to a relation between the e[i] results in another relation between the e[i]. In your example, you could easily deduce that all coefficients would be trivial if you would allow to freely permute indices: sage: SI = M.ideal(L) sage: SG = SI.groebner_basis() sage: SG [e_1] Do you have any permutation action at all on your coefficients? Say, if you have a relation then shifting all coefficients by 1 will result in another relation? If I recall correctly, it is not implemented yet, but it should be possible (without being able to guarantee termination of the algorithm, though) to consider a subgroup of the full permutation group. For instance, the second relation shows that e_10 = e_4e_6. The third relation -1382*e_12 + 2205*e_8*e_4 + 500*e_6^2 -1323*e_4^3 = 0 allows me (using a pen and paper...) to express e_12 only in terms of e_4 and e_6, by using e_8 = e_4^2. What I would like is to be able, for each k in the appropriate range, to obtain a polynomial f_k(u,v) with rational coefficients, such that f_k(e_4, e_6) = e_k. Well, if you have any *finite* list of relations and no index permutations on it, then you could simply use classical (non-symmetric) Gröbner bases. In fact, by your construction, all element of your list L are classical polynomials. Hence, it should be easy to get a polynomial ring with the variables involved in this list, and hopefully such that e_je_i for ji, because then the Gröbner basis will tell you how to express e_k in terms of e_4,e_6 for k6. Let's try: sage: I = ideal(L) sage: I Ideal (-e_8 + e_4^2, -e_10 + e_6*e_4, -1382*e_12 + 2205*e_8*e_4 + 500*e_6^2 - 1323*e_4^3, -10*e_14 + 21*e_10*e_4 + 22*e_8*e_6 - 33*e_6*e_4^2, 45606*e_12*e_4 + 65000*e_10*e_6 + 42042*e_8^2 - 63063*e_8*e_4^2 - 71500*e_6^2*e_4, 126126*e_14*e_4 + 212828*e_12*e_6 + 309582*e_10*e_8 - 154791*e_10*e_4^2 - 378378*e_8*e_6*e_4 - 71500*e_6^3, 858000*e_14*e_6 + 1337776*e_12*e_8 - 501666*e_12*e_4^2 + 760500*e_10^2 - 1287000*e_10*e_6*e_4 - 693693*e_8^2*e_4 - 786500*e_8*e_6^2) of Multivariate Polynomial Ring in e_14, e_12, e_10, e_8, e_6, e_4, e_0 over Rational Field sage: G [e_14 - e_6*e_4^2, e_12 - 250/691*e_6^2 - 441/691*e_4^3, e_10 - e_6*e_4, e_8 - e_4^2, e_6^3 + 38367/5500*e_6*e_4^3, e_6^2*e_4 + 1617/2000*e_4^4, e_6*e_4^4, e_4^5] sage: P = G.ring() sage: P Multivariate Polynomial Ring in e_14, e_12, e_10, e_8, e_6, e_4, e_0 over Rational Field sage: P('e_10').reduce(G) e_6*e_4 sage: P('e_12').reduce(G) 250/691*e_6^2 + 441/691*e_4^3 sage: P('e_14').reduce(G) e_6*e_4^2 Does this solve your problem? I am very sorry for the late answer. Best regards, Simon -- You received this message because you are subscribed to the Google Groups sage-support group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.
[sage-support] Re: Substitution in infinite polynomial rings
On Wednesday, April 16, 2014 4:16:30 PM UTC-7, BJ wrote: I have the following code, which produces a list of polynomials in the infinite number of variables e_0, e_1, ... M.e = InfinitePolynomialRing(QQ, implementation=sparse) However, I've been having a lot of trouble figuring out how to do substitutions in these polynomial rings in order to get what I want. I think the following should be the recommended way of doing this, but currently it doesn't work: sage: f = e[1]^2+e[2]^3 sage: f.subs({e[1]: 2}) #this doesn't work 4+e_2^3 the following does work: sage: f(e_1=2) 4+e_2^3 but it's flawed: sage: f(e_4=2) KeyError: 'e_4' The problem seems to be that the standard subs routines expect the parent to have a finite, predetermined sequence of generators. That's of course not the case for your rings. I think InfinitePolynomialRing has to override more of the methods involved. -- You received this message because you are subscribed to the Google Groups sage-support group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.
[sage-support] Re: Substitution in infinite polynomial rings
On Thursday, April 17, 2014 9:39:09 AM UTC-7, Nils Bruin wrote: but it's flawed: sage: f(e_4=2) KeyError: 'e_4' And also flawed in a different way: sage: f(e_2=e[4]) TypeError: unsupported operand parent(s) for '+': 'Multivariate Polynomial Ring in e_4, e_2, e_1 over Rational Field' and 'Multivariate Polynomial Ring in e_4, e_0 over Rational Field -- You received this message because you are subscribed to the Google Groups sage-support group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.
[sage-support] Re: substitution
On 01/09/2013 09:27 AM, Michael Beeson wrote: sage: K.d,e,p,g,m,f,u,v,j,N = FractionField(PolynomialRing(QQ,10,'depgmfuvjN')) sage: R.s = K[] sage: w=u sage: u=0 sage: w u Why doesn't Sage answer 0 for the value of w here? More generally, if I have some complicated expression and I assign a value to one of its variables, Sage knows the value I've just assigned, but it doesn't propagate into the value of previously defined expressions using that variable. Please comment and please tell me how to force the substitution to take effect in previously defined expressions. You can not have this kind of thing. w is not a pointer to u. w in this case contains a copy of u. -- You received this message because you are subscribed to the Google Groups sage-support group. To post to this group, send email to sage-support@googlegroups.com. To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support?hl=en.
Re: [sage-support] Re: substitution
Your first line defines a function field of several variables and assigns values to the identifiers d,p,e, etc so that in particular, u is assigned a value which is the 6th generator of that field. When you set w=u you have a new identifier called w whose value is a copy of u's value, so is also equal to the 6th geberator of the field. Then you set u=0 whcih just gives a new value to the identifier u, namely 0; this has no effect on w! Before, both u and w had the same value; now they don't. You can substitute values for all the variables in a different way. After w=(some expression in d,e,...,N) you can evaluate w([d0,e0,...,N0]), but you would have to give values to all the variables (which could be themselves, e.g. to evaluate at u=0 you could do w([d,e,p,g,m,f,0,v,j,N]). This is probably differnt from what happens with a symbolic manipulation system such as Maple, or even Sage's own symbolic ring -- but someone else would have to tell you about that, as I do not use it. John Cremona On 9 January 2013 08:04, P Purkayastha ppu...@gmail.com wrote: On 01/09/2013 09:27 AM, Michael Beeson wrote: sage: K.d,e,p,g,m,f,u,v,j,N = FractionField(PolynomialRing(QQ,10,'depgmfuvjN')) sage: R.s = K[] sage: w=u sage: u=0 sage: w u Why doesn't Sage answer 0 for the value of w here? More generally, if I have some complicated expression and I assign a value to one of its variables, Sage knows the value I've just assigned, but it doesn't propagate into the value of previously defined expressions using that variable. Please comment and please tell me how to force the substitution to take effect in previously defined expressions. You can not have this kind of thing. w is not a pointer to u. w in this case contains a copy of u. -- You received this message because you are subscribed to the Google Groups sage-support group. To post to this group, send email to sage-support@googlegroups.com. To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support?hl=en. -- You received this message because you are subscribed to the Google Groups sage-support group. To post to this group, send email to sage-support@googlegroups.com. To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support?hl=en.
[sage-support] Re: substitution in sums and multiplications
Hi Burcin, On Jan 25, 4:41 pm, Burcin Erocal bur...@erocal.org wrote: ... In your example, (k1 + k2) is not a subexpression of f, so there is nothing to substitute. In other words, (k1+k2)*2 is automatically turned into 2*k1+2*k2, but (k1+k2)^2 is not turned into k1^2+2*k1*k2+k2^2 ? Why? Cheers, Simon -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
[sage-support] Re: Substitution
Thanks. Unfortunately, for my particular example, it didn't work as is -- | Sage Version 4.2, Release Date: 2009-10-24 | | Type notebook() for the GUI, and license() for information.| -- sage: alpha=700 sage: mu1=0.1 sage: mu2=0.2 sage: pI=0.8 sage: l=5 sage: r=1.0 sage: b=r+l sage: c=r*mu1+l*(1-pI)*mu2 sage: a=lambda x: alpha*x-mu1+mu2 sage: f=lambda x: (a(x)*b-c+sqrt((a(x)*b-c)^2+4.0*a(x)*b*r*mu1))/(2*a (x)*mu1) sage: g=lambda x: (r+l-mu1*f(x))/mu2 sage: prev=lambda x: f(x)/(f(x)+g(x)) sage: k=lambda x: diff(prev(x),x) sage: k(x=0.03) --- TypeError Traceback (most recent call last) /Applications/sage/ipython console in module() /Applications/sage/ipython console in lambda(x) /Applications/sage/local/lib/python2.6/site-packages/sage/calculus/ functional.pyc in derivative(f, *args, **kwds) 133 if not isinstance(f, Expression): 134 f = SR(f) -- 135 return f.derivative(*args, **kwds) 136 137 diff = derivative /Applications/sage/local/lib/python2.6/site-packages/sage/symbolic/ expression.so in sage.symbolic.expression.Expression.derivative (sage/ symbolic/expression.cpp:11427)() /Applications/sage/local/lib/python2.6/site-packages/sage/misc/ derivative.so in sage.misc.derivative.multi_derivative (sage/misc/ derivative.c:2175)() /Applications/sage/local/lib/python2.6/site-packages/sage/symbolic/ expression.so in sage.symbolic.expression.Expression._derivative (sage/ symbolic/expression.cpp:11721)() TypeError: argument symb must be a symbol I had to type sage: k(x).subs(x=0.03) 0.0262047639227205 By the way, there is something still puzzling me. The equivalent Maple code gives a value of .883. Who should I believe ? -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
[sage-support] Re: Substitution
This simple minded approach seems to work for me: sage: a(x)=2*x sage: b(x)=a(x)^2 sage: g(x)=diff(b(x),x) sage: g(2) 16 -Alasdair On Jan 21, 2:14 am, Stochastix laurent.decreusef...@gmail.com wrote: I have the following problem : I have two nested functions a=lambda x: 2*x b=lambda x: a(x)^2 Everything goes well as long as I want to compute the derivative of b but how can I evaluate this derivative at x=2. I tried g=lambda x: diff(b(x),x) g(x) returns 8*x as expected but g(2)=0. In other words, how can we substitute x=2 to the expression 8*x ? I guess this is somewhere in the docs but I didn't find where. -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
[sage-support] Re: Substitution
Hi, I think what Minh was trying to say is that these lines: Stochastix wrote: sage: a=lambda x: alpha*x-mu1+mu2 sage: f=lambda x: (a(x)*b-c+sqrt((a(x)*b-c)^2+4.0*a(x)*b*r*mu1))/(2*a (x)*mu1) sage: g=lambda x: (r+l-mu1*f(x))/mu2 sage: prev=lambda x: f(x)/(f(x)+g(x)) sage: k=lambda x: diff(prev(x),x) should instead be written as a = alpha*x-mu1+mu2 f = (a(x)*b-c+sqrt((a(x)*b-c)^2+4.0*a(x)*b*r*mu1))/(2*a(x)*mu1) ... Or, if you prefer, a(x) = alpha*x-mu1+mu2 f(x) = ... etc. When you define functions this way, they become elements of the 'SymbolicRing' which Sage has a lot of tools to deal with. Defining them with 'lambda' makes them pure Python functions, which are not as flexible. sage: k(x).subs(x=0.03) 0.0262047639227205 By the way, there is something still puzzling me. The equivalent Maple code gives a value of .883. Who should I believe ? If you fix the errors as above, do you still get different answers in Maple and Sage? Best, Jason -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
[sage-support] Re: Substitution
Stochastix wrote: I have the following problem : I have two nested functions a=lambda x: 2*x b=lambda x: a(x)^2 Everything goes well as long as I want to compute the derivative of b but how can I evaluate this derivative at x=2. I tried g=lambda x: diff(b(x),x) g(x) returns 8*x as expected but g(2)=0. What is happening is that since g is a lambda function, g(something) is literally returning the result of diff(b(something), something). So g(x) is returning the result of the command diff(b(x),x), and g(2) is returning the result of diff(b(2),2). The key is not using lambda in this case. Other posts have shown how to do what you want. Thanks, Jason -- Jason Grout -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
[sage-support] Re: substitution
On Wed, 7 Oct 2009 13:20:30 -0700 (PDT) ma...@mendelu.cz ma...@mendelu.cz wrote: BTW: When looked at this problem, the notation D[0](alpha)(r)^2 seems to be unusual to me. Is it possible to fix sage so that it prints derivatives of functions in one variable as usual: alpha'(r) ? There is a patch attached to #6344 to change printing to alpha'(r) in latex mode. The syntax alpha'(r) would not be accepted on the command line without modifying the preparser, so normal printing is still a variant of the D notation. We can switch to using the diff notation when the function takes only one argument which is a symbol. As Maple does here: maple: diff(f(x),x); diff(f(x),x) maple: diff(f(x,x),x); D[1](f)(x,x)+D[2](f)(x,x) maple: diff(f(x^2),x); 2*D(f)(x^2)*x And is it possible to get sin^2(x) ans (alpha'(r))^2 in LaTeX output instead of sin(x)^2 and y'(r)^2? I don't think sin^2(x) is possible, (y'(r))^2 can be done. If we switch to that notation, I can make the necessary changes in pynac. Thanks. Burcin --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution
Sorry I should make my question clearer. I'm not trying to substitute for sigma^2. What I want to do is replace the various constants multiplying the terms in sigma with a single constant. For example I get the following term in sigma^2: -1/2*(1/R^2 - beta_0/(R^2*beta))*sigma(r)^2 And I want to get: -epsilon*sigma(r)^2 And basically I want to do this for each power or derivative of sigma in the expression I get. I've tried using subs(), but for some reason for my case it doesn't work, but for a simple example I tried it does and I don't see the difference. When I try var('epsilon') (-1/2*(1/R^2 - beta_0/(R^2*beta))*sigma(r)^2).subs(-1/2*(1/R^2 - beta_0/(R^2*beta))==epsilon) I get back the expression unchanged. But when I try: var('a b c') ((a+b)*x^2).subs((a+b)==c) the substitution does work. What's the difference? On Oct 7, 4:20 pm, ma...@mendelu.cz ma...@mendelu.cz wrote: On 7 říj, 16:21, Ranjit rjcha...@gmail.com wrote: I'm trying to figure out how to do a simple substitution. I've the following code: var('r beta beta_0 R a h') psi=function('psi',r) sigma=function('sigma',r) H_0=function('H_0',r) H_grad = lambda psi : (1/2)*(beta/beta_0)*R^2*(psi.diff(r))^2 H_0(psi) = -(1/2)*(beta/beta_0-1)*psi^2 + beta*a*psi^4 + beta*h*psi H_1=H_grad((1/R)*(beta_0/beta)^(1/2)*sigma) H=H_1+H_0((1/R)*(beta_0/beta)^(1/2)*sigma) H=H.simplify_radical().expand().collect(sigma^2) H Let's say I want to rewrite the coefficient on sigma^2 as simply epsilon, how can I do that? perhaps like this (I used alpha indstead of epsilon) alpha=function('alpha',r) H.subs_expr(sigma==sqrt(alpha)).subs_expr(diff(sigma)==diff(sqrt (alpha))) Not exactly what you want, but similar. BTW: When looked at this problem, the notation D[0](alpha)(r)^2 seems to be unusual to me. Is it possible to fix sage so that it prints derivatives of functions in one variable as usual: alpha'(r) ? And is it possible to get sin^2(x) ans (alpha'(r))^2 in LaTeX output instead of sin(x)^2 and y'(r)^2? Robert Marik --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution
perhaps, no subst is necessary: var('r beta beta_0 R a h') psi=function('psi',r) sigma=function('sigma',r) H_0=function('H_0',r) H_grad = lambda psi : (1/2)*(beta/beta_0)*R^2*(psi.diff(r))^2 H_0(psi) = -(1/2)*(beta/beta_0-1)*psi^2 + beta*a*psi^4 + beta*h*psi H_1=H_grad((1/R)*(beta_0/beta)^(1/2)*sigma) H=H_1+H_0((1/R)*(beta_0/beta)^(1/2)*sigma) H=H.simplify_radical().expand().collect(sigma^2) show(H) coef_sigma=(H.coeff(sigma^2)) show(coef_sigma) show(H) H=H-(coef_sigma-epsilon)*sigma^2 show(H) H=H.collect(sigma^2) H R.M. On 8 říj, 15:27, Ranjit rjcha...@gmail.com wrote: Sorry I should make my question clearer. I'm not trying to substitute for sigma^2. What I want to do is replace the various constants multiplying the terms in sigma with a single constant. For example I get the following term in sigma^2: -1/2*(1/R^2 - beta_0/(R^2*beta))*sigma(r)^2 And I want to get: -epsilon*sigma(r)^2 --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution
Ranjit a écrit : I'm trying to figure out how to do a simple substitution. I've the following code: var('r beta beta_0 R a h') psi=function('psi',r) sigma=function('sigma',r) H_0=function('H_0',r) H_grad = lambda psi : (1/2)*(beta/beta_0)*R^2*(psi.diff(r))^2 H_0(psi) = -(1/2)*(beta/beta_0-1)*psi^2 + beta*a*psi^4 + beta*h*psi H_1=H_grad((1/R)*(beta_0/beta)^(1/2)*sigma) H=H_1+H_0((1/R)*(beta_0/beta)^(1/2)*sigma) H=H.simplify_radical().expand().collect(sigma^2) H The formula.subs_expr (equation) doesn't recognize all sub-product or all sub-sum, but recognize ONE term in a sum, even a complex one, so I test with succes the code : coef = H.coefficient(sigma(r), 2) H.subs_expr(coef*sigma(r)^2==epsilon*sigma(r)^2) epsilon*sigma(r)^2 + sqrt(beta)*sqrt(beta_0)*h*sigma(r)/R + a*beta_0^2*sigma(r)^4/(R^4*beta) + 1/2*D[0](sigma)(r)^2 Do I answer to your question ? F. --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution
This works, though it would have been nicer if epsilon actually equalled what it was replacing. I just tried (-1/2*(1/R^2 - beta_0/(R^2*beta))*sigma(r)^2).subs((1/R^2 - beta_0/ (R^2*beta))==epsilon) which works, where (-1/2*(1/R^2 - beta_0/(R^2*beta))*sigma(r)^2).subs((1/2)*(1/R^2 - beta_0/(R^2*beta))==epsilon) does not. So I guess there's an upper limit to the number of operations that the expression being replaced can have to work in subs (). On Oct 8, 12:52 pm, Francois Maltey fmal...@nerim.fr wrote: Ranjit a écrit : I'm trying to figure out how to do a simple substitution. I've the following code: var('r beta beta_0 R a h') psi=function('psi',r) sigma=function('sigma',r) H_0=function('H_0',r) H_grad = lambda psi : (1/2)*(beta/beta_0)*R^2*(psi.diff(r))^2 H_0(psi) = -(1/2)*(beta/beta_0-1)*psi^2 + beta*a*psi^4 + beta*h*psi H_1=H_grad((1/R)*(beta_0/beta)^(1/2)*sigma) H=H_1+H_0((1/R)*(beta_0/beta)^(1/2)*sigma) H=H.simplify_radical().expand().collect(sigma^2) H The formula.subs_expr (equation) doesn't recognize all sub-product or all sub-sum, but recognize ONE term in a sum, even a complex one, so I test with succes the code : coef = H.coefficient(sigma(r), 2) H.subs_expr(coef*sigma(r)^2==epsilon*sigma(r)^2) epsilon*sigma(r)^2 + sqrt(beta)*sqrt(beta_0)*h*sigma(r)/R + a*beta_0^2*sigma(r)^4/(R^4*beta) + 1/2*D[0](sigma)(r)^2 Do I answer to your question ? F. --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution
On 8 říj, 19:41, Ranjit rjcha...@gmail.com wrote: does not. So I guess there's an upper limit to the number of operations that the expression being replaced can have to work in subs (). Perhaps true, but IMHO very risky guess which is not true, if pattern matching in Maxima is behind the scene when doing substitution. Robert Marik --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution
On 7 říj, 16:21, Ranjit rjcha...@gmail.com wrote: I'm trying to figure out how to do a simple substitution. I've the following code: var('r beta beta_0 R a h') psi=function('psi',r) sigma=function('sigma',r) H_0=function('H_0',r) H_grad = lambda psi : (1/2)*(beta/beta_0)*R^2*(psi.diff(r))^2 H_0(psi) = -(1/2)*(beta/beta_0-1)*psi^2 + beta*a*psi^4 + beta*h*psi H_1=H_grad((1/R)*(beta_0/beta)^(1/2)*sigma) H=H_1+H_0((1/R)*(beta_0/beta)^(1/2)*sigma) H=H.simplify_radical().expand().collect(sigma^2) H Let's say I want to rewrite the coefficient on sigma^2 as simply epsilon, how can I do that? perhaps like this (I used alpha indstead of epsilon) alpha=function('alpha',r) H.subs_expr(sigma==sqrt(alpha)).subs_expr(diff(sigma)==diff(sqrt (alpha))) Not exactly what you want, but similar. BTW: When looked at this problem, the notation D[0](alpha)(r)^2 seems to be unusual to me. Is it possible to fix sage so that it prints derivatives of functions in one variable as usual: alpha'(r) ? And is it possible to get sin^2(x) ans (alpha'(r))^2 in LaTeX output instead of sin(x)^2 and y'(r)^2? Robert Marik --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution of lambda variable
Mike Hansen wrote: Hello, On Thu, May 28, 2009 at 8:38 AM, Paul Sargent psa...@gmail.com wrote: # Subs for lambda (have to use lambda, but that's a keyword) sage: e1.subs(lambda = 3) File ipython console, line 1 e1.subs(lambda = Integer(3)) ^ SyntaxError: invalid syntax For some reason I don't understand, whilst .solve() will take the symbolic variable as a parameter to solve for (e.g. t), .subs() requires the variable name (e.g. theta). Often these are the same, but not always, and in the case where I have a variable lambda it causes a syntax error. lambda is a reserved keyword in Python, which means you can't use it in a python statement that you are writing. You'll notice below that Mike never writes lambda on a line (he only writes the string lambda). Yeah, I know; it's frustrating for me in linear algebra to not use lambda for eigenvalues. Jason Any way to get around this? Is this the intended syntax for subs(), as it seems a little confused? The subs() method can take various types of input -- you can look a the docstring to see examples of these. Here's one way to do what you want: sage: l = var(lambda) sage: t = var(theta) sage: e1 = t == l^2 sage: e2 = e1.solve(l) sage: (e2[0].subs({t:5}), e2[1].subs({t:5})) (lambda == -sqrt(5), lambda == sqrt(5)) sage: sage: e1.subs({l:3}) theta == 9 In Sage 4.0, you'll be able to do sage: sage: e1.subs(l==3) theta == 9 --Mike -- Jason Grout --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Substitution with symbolic equations
Not sure if this is what you are after, but the following would give you the solution: sage: solve([e1,e2],c,a) [[c == (d - b*e)/e, a == d/e]] You can give n equations to solve and solve for n variables. Solve will insert one into another automatically. An equation has a different syntax to a substitution rule, so if you want to substitute what is in an equation, you could write sage: e2(a=eq1.rhs()) Not sure if there is a better way of transforming equations into substitution rules. Perhaps the dict() function could help? You can substitute a dictionary of variables, e.g. sage: vars = dict(a = b + c) sage: e2.subs(vars) However, again the dict() arguments have a single equal sign, not a double one as an equation. I'm sure someone can explain this much more elegantly. Regards, Stan Paul Sargent wrote: Hi, I keep running into a road block which I think means either I'm missing something simple, or I'm thinking about things the wrong way. I'm fairly new to sage, and CAS in general, so either is possible. Here's a simple example of what I'm doing. Lets give ourselves two symbolic equations: sage: var(a b c d e) sage: e1 = a == b + c sage: e2 = d == e * a Now, lets say I want to know what c is in terms of b, d e. By hand I'd substitute e1 in e2, and then solve for c. sage: e3=e2.subs(a=b+c) sage: solve(e3, c) [c == (d - b*e)/e] All fine, but note that I had to enter e1 into the substitution explicitly. I've yet to find a way of substituting one symbolic equation into another. Is there a way? Where am I going wrong? Thanks Paul --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Substitution with symbolic equations
Hello, On Fri, May 15, 2009 at 5:06 AM, Paul Sargent psa...@gmail.com wrote: Lets give ourselves two symbolic equations: sage: var(a b c d e) sage: e1 = a == b + c sage: e2 = d == e * a Now, lets say I want to know what c is in terms of b, d e. By hand I'd substitute e1 in e2, and then solve for c. sage: e3=e2.subs(a=b+c) sage: solve(e3, c) [c == (d - b*e)/e] All fine, but note that I had to enter e1 into the substitution explicitly. I've yet to find a way of substituting one symbolic equation into another. Is there a way? Where am I going wrong? In Sage 4.0 which will be released within the week, you'll be able to do the following: sage: var(a b c d e) (a, b, c, d, e) sage: e1 = a == b + c sage: e2 = d == e * a sage: e3 = e2.subs(e1); e3 d == (b + c)*e --Mike --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Substitution with symbolic equations
On 15 May 2009, at 16:33, Mike Hansen wrote: In Sage 4.0 which will be released within the week, you'll be able to do the following: sage: var(a b c d e) (a, b, c, d, e) sage: e1 = a == b + c sage: e2 = d == e * a sage: e3 = e2.subs(e1); e3 d == (b + c)*e Well, that's what I call service! ;-) Seriously, that'll make some of the manipulations I'm doing flow a lot better. --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution problem
On Mar 30, 6:07 pm, Jason Grout jason-s...@creativetrax.com wrote: V wrote: Hi, I'm fairly new to sage with some background in maxima. My workbook is shared at http://www.sagenb.org/home/pub/410/ the last three lines show the error I get. Basically, I derive an equilibrium condition that I would like to use in the previous stage of my game (solving by backwards induction), but maxima fails (line -2) with simplifying after the substitution is made, even though the substitution is successful (line -3). I expect to receive the line in the last paragraph as defined by D1p. Is this a bug or did I made a mistake? In this line, solve([q1==q1.substitute(q2=sol2[0][1].right()).simplify_full()], q1) you are trying to solve for q1, but q1 is not a variable, it's the expression: f2^alpha*p1^(alpha - 1)*p2^(beta - alpha*beta)*q2^alpha*A^(1 - alpha)/f1 Did you mean to solve for a variable in the solve statement above? The expression in the solve line would be a demand function where q stands for quantity. But as it is derived from a later stage of the game, q is present on the right hand side as well, and I would like to express the demand function explicitly, that is I have q=f(q,x) where x are all the other parameters and q is defined implicitly, and I would like to have q=g(x). I thought solve would do this for me, even though I already understand why it doesn't, I still don't know how to do it. Could you give me a hint, please? Thanks, V --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution problem
V wrote: On Mar 30, 6:07 pm, Jason Grout jason-s...@creativetrax.com wrote: V wrote: Hi, I'm fairly new to sage with some background in maxima. My workbook is shared at http://www.sagenb.org/home/pub/410/ the last three lines show the error I get. Basically, I derive an equilibrium condition that I would like to use in the previous stage of my game (solving by backwards induction), but maxima fails (line -2) with simplifying after the substitution is made, even though the substitution is successful (line -3). I expect to receive the line in the last paragraph as defined by D1p. Is this a bug or did I made a mistake? In this line, solve([q1==q1.substitute(q2=sol2[0][1].right()).simplify_full()], q1) you are trying to solve for q1, but q1 is not a variable, it's the expression: f2^alpha*p1^(alpha - 1)*p2^(beta - alpha*beta)*q2^alpha*A^(1 - alpha)/f1 Did you mean to solve for a variable in the solve statement above? The expression in the solve line would be a demand function where q stands for quantity. But as it is derived from a later stage of the game, q is present on the right hand side as well, and I would like to express the demand function explicitly, that is I have q=f(q,x) where x are all the other parameters and q is defined implicitly, and I would like to have q=g(x). I thought solve would do this for me, even though I already understand why it doesn't, I still don't know how to do it. Could you give me a hint, please? I see. Disclaimer: I don't know the mathematics that you are doing, so forgive me if I mess things up below... When you do q1==q1.subs(...), what the computer sees is: f2^alpha*p1^(alpha - 1)*p2^(beta - alpha*beta)*q2^alpha*A^(1 - alpha)/f1 == f1^(alpha^2 - 1)*p1^((alpha - alpha^2)*beta + alpha - 1)*p2^((1 - alpha)*beta + alpha^2 - alpha)*q1^alpha^2*A^(1 - alpha^2) This doesn't seem like what you want; you want the left side to be a single variable, and the right side to have q1 be that same single variable. So maybe you can do something like: sage: var('q') sage: q==q1.substitute(q2=sol2[0][1].right()).simplify_full().subs(q1=q) q == f1^(alpha^2 - 1)*p1^((alpha - alpha^2)*beta + alpha - 1)*p2^((1 - alpha)*beta + alpha^2 - alpha)*q^alpha^2*A^(1 - alpha^2) sage: solve(q==q1.substitute(q2=sol2[0][1].right()).simplify_full().subs(q1=q), q) [q == f1^(alpha^2 - 1)*p1^((alpha - alpha^2)*beta + alpha - 1)*p2^((1 - alpha)*beta + alpha^2 - alpha)*q^alpha^2*A^(1 - alpha^2)] So it doesn't look like maxima is able to handle this. Jason --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution problem
This is exactly what I wanted. Thanks for your help! :) I'll look up the maxima manuals to see if I can force it to think harder. Thanks anyway! Have a nice day! V On Mar 31, 3:35 pm, Jason Grout jason-s...@creativetrax.com wrote: V wrote: On Mar 30, 6:07 pm, Jason Grout jason-s...@creativetrax.com wrote: V wrote: Hi, I'm fairly new to sage with some background in maxima. My workbook is shared at http://www.sagenb.org/home/pub/410/ the last three lines show the error I get. Basically, I derive an equilibrium condition that I would like to use in the previous stage of my game (solving by backwards induction), but maxima fails (line -2) with simplifying after the substitution is made, even though the substitution is successful (line -3). I expect to receive the line in the last paragraph as defined by D1p. Is this a bug or did I made a mistake? In this line, solve([q1==q1.substitute(q2=sol2[0][1].right()).simplify_full()], q1) you are trying to solve for q1, but q1 is not a variable, it's the expression: f2^alpha*p1^(alpha - 1)*p2^(beta - alpha*beta)*q2^alpha*A^(1 - alpha)/f1 Did you mean to solve for a variable in the solve statement above? The expression in the solve line would be a demand function where q stands for quantity. But as it is derived from a later stage of the game, q is present on the right hand side as well, and I would like to express the demand function explicitly, that is I have q=f(q,x) where x are all the other parameters and q is defined implicitly, and I would like to have q=g(x). I thought solve would do this for me, even though I already understand why it doesn't, I still don't know how to do it. Could you give me a hint, please? I see. Disclaimer: I don't know the mathematics that you are doing, so forgive me if I mess things up below... When you do q1==q1.subs(...), what the computer sees is: f2^alpha*p1^(alpha - 1)*p2^(beta - alpha*beta)*q2^alpha*A^(1 - alpha)/f1 == f1^(alpha^2 - 1)*p1^((alpha - alpha^2)*beta + alpha - 1)*p2^((1 - alpha)*beta + alpha^2 - alpha)*q1^alpha^2*A^(1 - alpha^2) This doesn't seem like what you want; you want the left side to be a single variable, and the right side to have q1 be that same single variable. So maybe you can do something like: sage: var('q') sage: q==q1.substitute(q2=sol2[0][1].right()).simplify_full().subs(q1=q) q == f1^(alpha^2 - 1)*p1^((alpha - alpha^2)*beta + alpha - 1)*p2^((1 - alpha)*beta + alpha^2 - alpha)*q^alpha^2*A^(1 - alpha^2) sage: solve(q==q1.substitute(q2=sol2[0][1].right()).simplify_full().subs(q1=q), q) [q == f1^(alpha^2 - 1)*p1^((alpha - alpha^2)*beta + alpha - 1)*p2^((1 - alpha)*beta + alpha^2 - alpha)*q^alpha^2*A^(1 - alpha^2)] So it doesn't look like maxima is able to handle this. Jason --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution problem
V wrote: Hi, I'm fairly new to sage with some background in maxima. My workbook is shared at http://www.sagenb.org/home/pub/410/ the last three lines show the error I get. Basically, I derive an equilibrium condition that I would like to use in the previous stage of my game (solving by backwards induction), but maxima fails (line -2) with simplifying after the substitution is made, even though the substitution is successful (line -3). I expect to receive the line in the last paragraph as defined by D1p. Is this a bug or did I made a mistake? In this line, solve([q1==q1.substitute(q2=sol2[0][1].right()).simplify_full()], q1) you are trying to solve for q1, but q1 is not a variable, it's the expression: f2^alpha*p1^(alpha - 1)*p2^(beta - alpha*beta)*q2^alpha*A^(1 - alpha)/f1 Did you mean to solve for a variable in the solve statement above? Jason --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Substitution
Hi, On Fri, Mar 13, 2009 at 7:09 PM, hpon peter.norli...@gmail.com wrote: Hi, What is the easiest way to make a mathematical substitution in Sage? For example: We have eqn1 = F == a + b I'm not sure I understand what you are trying to do in this line. If you just want to define F as the sum of a and b, use var('a b') # to tell Sage that a and b are symbolic variables F = a + b and would like to use a = 0 and b = 3 to calculate F. Now F is a symbolic expression, and you can perform substitutions easily as follows: F.substitute(a=0, b=3) # and Sage will answer: 3 If you do F.substitute? you will get the documentation for the substitution method, with more examples of usage. Best, Alex -- Alex Ghitza -- Lecturer in Mathematics -- The University of Melbourne -- Australia -- http://www.ms.unimelb.edu.au/~aghitza/ --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Substitution
Excellent. Thank you Alex! /hpon On 13 Mar, 09:16, Alex Ghitza aghi...@gmail.com wrote: Hi, On Fri, Mar 13, 2009 at 7:09 PM, hpon peter.norli...@gmail.com wrote: Hi, What is the easiest way to make a mathematical substitution in Sage? For example: We have eqn1 = F == a + b I'm not sure I understand what you are trying to do in this line. If you just want to define F as the sum of a and b, use var('a b') # to tell Sage that a and b are symbolic variables F = a + b and would like to use a = 0 and b = 3 to calculate F. Now F is a symbolic expression, and you can perform substitutions easily as follows: F.substitute(a=0, b=3) # and Sage will answer: 3 If you do F.substitute? you will get the documentation for the substitution method, with more examples of usage. Best, Alex -- Alex Ghitza -- Lecturer in Mathematics -- The University of Melbourne -- Australia --http://www.ms.unimelb.edu.au/~aghitza/ --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
One of the issues right now is that sage's piecewise is a completely separate class then the rest of the calculus library. I think it should descend from symbolic expression and be on the same level as, e.g. sin and addition (and probably even have a pynac counterpart). Letting the operands be the conditions (as symbolic equations, giving much more flexibility then we now have) and the corresponding expressions (as symbolic equations) would allow it to fit into the symbolic ring nicely. This would have the advantage that one could leverage all the generic SR functionality like subs, pow, composition, etc. without having to re-implement it for every method that's missing. Exactly. In fact, that is how Piecewise works in sympy. So I think pynac has to know how to properly dispatch stuff like .subs(), .series() and other things into the Piecewise class (after it is integrated into the calculus). And I know it is possible to do that with Cython. Btw, how about pattern matching? Will you extend the C++ code in ginac to handle Piecewise functions? The other option is to reimplement the .subs(), .series() and pattern matching in Python. Burcin, please don't take my comments as sarcastic --- I am really interested how to handle this and how to cooperate as much as possible. Also, how will you approach to fix the bug in pynac series I reported here: http://groups.google.com/group/sage-devel/msg/a59f3c5d0586766b E.g. will you fix ginac, or rather reimplement it in Cython/Python? You can actually still use Maxima for that, so that's not a problem in fact. And later just reimplement it in Python/Cython. Ondrej --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
On Nov 24, 2008, at 5:04 AM, Ondrej Certik wrote: On Mon, Nov 24, 2008 at 1:25 PM, David Joyner [EMAIL PROTECTED] wrote: On Mon, Nov 24, 2008 at 6:19 AM, Ondrej Certik [EMAIL PROTECTED] wrote: Hi, when I use regular expressions, I can use .subs(): sage: e = x+y sage: e.subs(x=y) 2*y but not with Piecewise: sage: var(h H x y) (h, H, x, y) sage: u = Piecewise([((0, h), x/h), ((h, H), 1)]) I don't think variable endpoints are implemented in Sage's Piecewise. Maybe one could add a method=sympy option to use sympy's Piecewise in place of the Maxima interface? Well, even fixed endpoints doesn't seem to work properly: sage: u = Piecewise([((0, 1), x/h), ((1, 2), 1)]) sage: u**2 -- - TypeError Traceback (most recent call last) /home/ondra/ext/sage-3.1.4-debian32-IntelXeon-x86-i686-Linux/ipython console in module() /home/ondra/ext/sage/local/lib/python2.5/site-packages/sage/rings/ integer.so in sage.rings.integer.Integer.__pow__ (sage/rings/integer.c:9379)() TypeError: unsupported operand type(s) for ** or pow(): 'instance' and 'int' I don't see how pynac is relevant here. Pynac will only provide a different internal format for representing symbolic expressions, the piecewise class will still be the same. Well, if you want to do stuff like u**2, where u is piecewise, or use the .subs() that is implemented in the symbolic engine, you can either implement everything by hand (e.g. implement .subs(), implement __pow__, ...), but that's imho not the way, or, you can integrate it inside the symbolic engine, so that you can manipulate with it like with any other function -- that's the approach we took with sympy. +1 You can open a trac ticket if you need this functionality. It is safe to change piecewise.py, and add a .subs() method. Since the user interface for the pynac expressions will be almost the same as the current ones, it should still work when the underlying representation changes. I think it should be discussed more first. My vision is that the symbolic engine should treat everything as first class citizens --- e.g. if you make series expansion, it does the right thing. One of the issues right now is that sage's piecewise is a completely separate class then the rest of the calculus library. I think it should descend from symbolic expression and be on the same level as, e.g. sin and addition (and probably even have a pynac counterpart). Letting the operands be the conditions (as symbolic equations, giving much more flexibility then we now have) and the corresponding expressions (as symbolic equations) would allow it to fit into the symbolic ring nicely. This would have the advantage that one could leverage all the generic SR functionality like subs, pow, composition, etc. without having to re-implement it for every method that's missing. - Robert --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
On Tue, Nov 25, 2008 at 3:15 AM, Ondrej Certik [EMAIL PROTECTED] wrote: One of the issues right now is that sage's piecewise is a completely separate class then the rest of the calculus library. I think it Just for the record, this is because Piecewise was implemented long long ago by David Joyner, probably a year before any of the rest of the calculus library was implemented.It definitely needs to be somehow integrated better with the calculus code as explained here. should descend from symbolic expression and be on the same level as, e.g. sin and addition (and probably even have a pynac counterpart). Letting the operands be the conditions (as symbolic equations, giving much more flexibility then we now have) and the corresponding expressions (as symbolic equations) would allow it to fit into the symbolic ring nicely. This would have the advantage that one could leverage all the generic SR functionality like subs, pow, composition, etc. without having to re-implement it for every method that's missing. Exactly. In fact, that is how Piecewise works in sympy. So I think pynac has to know how to properly dispatch stuff like .subs(), .series() and other things into the Piecewise class (after it is integrated into the calculus). And I know it is possible to do that with Cython. Btw, how about pattern matching? Will you extend the C++ code in ginac to handle Piecewise functions? The other option is to reimplement the .subs(), .series() and pattern matching in Python. Burcin, please don't take my comments as sarcastic --- I am really interested how to handle this and how to cooperate as much as possible. Also, how will you approach to fix the bug in pynac series I reported here: http://groups.google.com/group/sage-devel/msg/a59f3c5d0586766b E.g. will you fix ginac, or rather reimplement it in Cython/Python? You can actually still use Maxima for that, so that's not a problem in fact. And later just reimplement it in Python/Cython. Ondrej -- William Stein Associate Professor of Mathematics University of Washington http://wstein.org --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
On Tue, Nov 25, 2008 at 3:13 PM, William Stein [EMAIL PROTECTED] wrote: On Tue, Nov 25, 2008 at 3:15 AM, Ondrej Certik [EMAIL PROTECTED] wrote: One of the issues right now is that sage's piecewise is a completely separate class then the rest of the calculus library. I think it Just for the record, this is because Piecewise was implemented long long ago by David Joyner, probably a year before any of the rest of the calculus library was implemented.It definitely needs to be somehow integrated better with the calculus code as explained here. I'll try working on this some over the winter break, ie, in 2-3 weeks. should descend from symbolic expression and be on the same level as, e.g. sin and addition (and probably even have a pynac counterpart). Letting the operands be the conditions (as symbolic equations, giving much more flexibility then we now have) and the corresponding expressions (as symbolic equations) would allow it to fit into the symbolic ring nicely. This would have the advantage that one could leverage all the generic SR functionality like subs, pow, composition, etc. without having to re-implement it for every method that's missing. Exactly. In fact, that is how Piecewise works in sympy. So I think pynac has to know how to properly dispatch stuff like .subs(), .series() and other things into the Piecewise class (after it is integrated into the calculus). And I know it is possible to do that with Cython. Btw, how about pattern matching? Will you extend the C++ code in ginac to handle Piecewise functions? The other option is to reimplement the .subs(), .series() and pattern matching in Python. Burcin, please don't take my comments as sarcastic --- I am really interested how to handle this and how to cooperate as much as possible. Also, how will you approach to fix the bug in pynac series I reported here: http://groups.google.com/group/sage-devel/msg/a59f3c5d0586766b E.g. will you fix ginac, or rather reimplement it in Cython/Python? You can actually still use Maxima for that, so that's not a problem in fact. And later just reimplement it in Python/Cython. Ondrej -- William Stein Associate Professor of Mathematics University of Washington http://wstein.org --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
On Tue, 25 Nov 2008 12:15:41 +0100 Ondrej Certik [EMAIL PROTECTED] wrote: One of the issues right now is that sage's piecewise is a completely separate class then the rest of the calculus library. I think it should descend from symbolic expression and be on the same level as, e.g. sin and addition (and probably even have a pynac counterpart). Letting the operands be the conditions (as symbolic equations, giving much more flexibility then we now have) and the corresponding expressions (as symbolic equations) would allow it to fit into the symbolic ring nicely. This would have the advantage that one could leverage all the generic SR functionality like subs, pow, composition, etc. without having to re-implement it for every method that's missing. Exactly. In fact, that is how Piecewise works in sympy. So I think pynac has to know how to properly dispatch stuff like .subs(), .series() and other things into the Piecewise class (after it is integrated into the calculus). And I know it is possible to do that with Cython. Btw, how about pattern matching? Will you extend the C++ code in ginac to handle Piecewise functions? Ok. Now I see what you mean, first let me explain why I failed to understand you before. Sage treats piecewise functions as functions, and in Sage, once you have a function, it remains a function even when you do arithmetic with it, unless you apply it to values and get an expression. E.g., sage: var('x,y,z,n') (x, y, z, n) sage: f(x) = 2*x^n sage: f x |-- 2*x^n sage: g = f + y; g x |-- y + 2*x^n sage: g.subs(x=z) 2*z^n + y sage: e = g(x,z); e y + 2*x^n sage: type(e) class 'sage.calculus.calculus.SymbolicArithmetic' So, when we were talking about piecewise functions, I thought that would also stay as a piecewise function object all along. No matter what arithmetic you do with it. When you start having piecewise (or conditional?) expressions, things do get complicated as you have been trying to point out all this time. :) Note that we already call python functions from c++ in pynac. Just look at the sage.symbolic.function.SFunction class. You can specify python functions for evaluation, derivation, conjugation, etc. just as ginac lets you do this with c++ functions. sage: from sage.symbolic.function import function as nfunction sage: var('x,y',ns=1) (x, y) sage: def npow(x, power_param=None): return x*power_param sage: foo = nfunction(foo, 1, power_func=npow) sage: foo(y)^(x+y) (x + y)*y sage: def nseries(*args, **kwds): return sum(args[0]^i for i in range(kwds['order'])) sage: foo = nfunction(foo, 1, series_func=nseries) sage: foo(y).series(y, 5) y^4 + y^3 + y^2 + y + 1 Once a couple more glitches are fixed, this should allow people to implement new special functions using only python. It should be straightforward to add a new class to ginac, which also dispatches substitution and pattern matching to custom functions as well. I'll have to think more about the best way to do this. As you might have noticed from the lack of progress on the pynac front, I don't have much time to spend on pynac these days. I will have at least a week to work on this in december, than more january. I hope to have most of the lower level stuff sorted out by then, so we can start moving things over to pynac. The other option is to reimplement the .subs(), .series() and pattern matching in Python. Burcin, please don't take my comments as sarcastic --- I am really interested how to handle this and how to cooperate as much as possible. Thanks for pointing these out. I don't use symbolics much, and was stuck with the idea of piecewise functions for some reason. I guess I hadn't looked at your initial example carefully. So piecewise functions are on my todo list now: http://wiki.sagemath.org/symbolics/pynac_todo Please suggest anything else that you think should be in there. I don't use symbolics enough to know about different use cases and common problems. Your experience here is definitely needed. Also, how will you approach to fix the bug in pynac series I reported here: http://groups.google.com/group/sage-devel/msg/a59f3c5d0586766b E.g. will you fix ginac, or rather reimplement it in Cython/Python? This was already on my todo list, see item 1/gamma(-1) = 0 in the above link. I will fix ginac, as this annoys me too. This is also on the ginac todo list (see the 4th item): http://www.ginac.de/ToDo.html You can actually still use Maxima for that, so that's not a problem in fact. And later just reimplement it in Python/Cython. I would like to use pynac as much as possible and avoid calling maxima. I don't think I need to reimplement anything in cython to fix this specific problem though. I just need to go around and modify a few eval functions to take infinity into account. Covering all the possible cases might take a couple of days, but it's definitely within reach. In fact, I already did something similar to get around ginac's shortcuts in
[sage-support] Re: substitution in and integration of piecewise functions
On Tue, Nov 25, 2008 at 11:00 PM, Burcin Erocal [EMAIL PROTECTED] wrote: On Tue, 25 Nov 2008 12:15:41 +0100 Ondrej Certik [EMAIL PROTECTED] wrote: One of the issues right now is that sage's piecewise is a completely separate class then the rest of the calculus library. I think it should descend from symbolic expression and be on the same level as, e.g. sin and addition (and probably even have a pynac counterpart). Letting the operands be the conditions (as symbolic equations, giving much more flexibility then we now have) and the corresponding expressions (as symbolic equations) would allow it to fit into the symbolic ring nicely. This would have the advantage that one could leverage all the generic SR functionality like subs, pow, composition, etc. without having to re-implement it for every method that's missing. Exactly. In fact, that is how Piecewise works in sympy. So I think pynac has to know how to properly dispatch stuff like .subs(), .series() and other things into the Piecewise class (after it is integrated into the calculus). And I know it is possible to do that with Cython. Btw, how about pattern matching? Will you extend the C++ code in ginac to handle Piecewise functions? Ok. Now I see what you mean, first let me explain why I failed to understand you before. Sage treats piecewise functions as functions, and in Sage, once you have a function, it remains a function even when you do arithmetic with it, unless you apply it to values and get an expression. E.g., sage: var('x,y,z,n') (x, y, z, n) sage: f(x) = 2*x^n sage: f x |-- 2*x^n sage: g = f + y; g x |-- y + 2*x^n sage: g.subs(x=z) 2*z^n + y sage: e = g(x,z); e y + 2*x^n sage: type(e) class 'sage.calculus.calculus.SymbolicArithmetic' So, when we were talking about piecewise functions, I thought that would also stay as a piecewise function object all along. No matter what arithmetic you do with it. When you start having piecewise (or conditional?) expressions, things do get complicated as you have been trying to point out all this time. :) Note that we already call python functions from c++ in pynac. Just look at the sage.symbolic.function.SFunction class. You can specify python functions for evaluation, derivation, conjugation, etc. just as ginac lets you do this with c++ functions. sage: from sage.symbolic.function import function as nfunction sage: var('x,y',ns=1) (x, y) sage: def npow(x, power_param=None): return x*power_param sage: foo = nfunction(foo, 1, power_func=npow) sage: foo(y)^(x+y) (x + y)*y sage: def nseries(*args, **kwds): return sum(args[0]^i for i in range(kwds['order'])) sage: foo = nfunction(foo, 1, series_func=nseries) sage: foo(y).series(y, 5) y^4 + y^3 + y^2 + y + 1 Once a couple more glitches are fixed, this should allow people to implement new special functions using only python. It should be straightforward to add a new class to ginac, which also dispatches substitution and pattern matching to custom functions as well. I'll have to think more about the best way to do this. As you might have noticed from the lack of progress on the pynac front, I don't have much time to spend on pynac these days. I will have at least a week to work on this in december, than more january. I hope to have most of the lower level stuff sorted out by then, so we can start moving things over to pynac. The other option is to reimplement the .subs(), .series() and pattern matching in Python. Burcin, please don't take my comments as sarcastic --- I am really interested how to handle this and how to cooperate as much as possible. Thanks for pointing these out. I don't use symbolics much, and was stuck with the idea of piecewise functions for some reason. I guess I hadn't looked at your initial example carefully. So piecewise functions are on my todo list now: http://wiki.sagemath.org/symbolics/pynac_todo Please suggest anything else that you think should be in there. I don't use symbolics enough to know about different use cases and common problems. Your experience here is definitely needed. I only wanted to do stuff in this issue: http://code.google.com/p/sympy/issues/detail?id=1214 e.g. substitue in the Piecewise function and then integrate it. Since the integration failed in sympy, I was curious how far Sage is on this front, and it is not far either. So the aim is that one should be able to do everything in the issue. Also, how will you approach to fix the bug in pynac series I reported here: http://groups.google.com/group/sage-devel/msg/a59f3c5d0586766b E.g. will you fix ginac, or rather reimplement it in Cython/Python? This was already on my todo list, see item 1/gamma(-1) = 0 in the above link. I will fix ginac, as this annoys me too. This is also on the ginac todo list (see the 4th item): http://www.ginac.de/ToDo.html You can actually still use Maxima for that, so that's not a problem in fact. And later
[sage-support] Re: substitution in and integration of piecewise functions
On Mon, 24 Nov 2008 12:19:55 +0100 Ondrej Certik [EMAIL PROTECTED] wrote: Hi, when I use regular expressions, I can use .subs(): sage: e = x+y sage: e.subs(x=y) 2*y but not with Piecewise: sage: var(h H x y) (h, H, x, y) sage: u = Piecewise([((0, h), x/h), ((h, H), 1)]) sage: u.subs(x=y) --- AttributeErrorTraceback (most recent call last) /home/ondra/ext/sage-3.1.2-debian32-intel-i686-Linux/ipython console in module() AttributeError: PiecewisePolynomial instance has no attribute 'subs' This means that substitution is simply not implemented for piecewise functions. I suppose no one needed this until now. snip Am I doing something wrong, or is it just not implemented yet? What is the plan of implementing this in pynac? I don't see how pynac is relevant here. Pynac will only provide a different internal format for representing symbolic expressions, the piecewise class will still be the same. You can open a trac ticket if you need this functionality. It is safe to change piecewise.py, and add a .subs() method. Since the user interface for the pynac expressions will be almost the same as the current ones, it should still work when the underlying representation changes. Cheers, Burcin --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
On Mon, Nov 24, 2008 at 6:19 AM, Ondrej Certik [EMAIL PROTECTED] wrote: Hi, when I use regular expressions, I can use .subs(): sage: e = x+y sage: e.subs(x=y) 2*y but not with Piecewise: sage: var(h H x y) (h, H, x, y) sage: u = Piecewise([((0, h), x/h), ((h, H), 1)]) I don't think variable endpoints are implemented in Sage's Piecewise. Maybe one could add a method=sympy option to use sympy's Piecewise in place of the Maxima interface? sage: u.subs(x=y) --- AttributeErrorTraceback (most recent call last) /home/ondra/ext/sage-3.1.2-debian32-intel-i686-Linux/ipython console in module() AttributeError: PiecewisePolynomial instance has no attribute 'subs' Ok. Now I'd like to integrate that: sage: u.integral() TypeError: Computation failed since Maxima requested additional constraints (use assume): Is h positive, negative, or zero? sage: assume(h0) sage: u.integral() TypeError: Computation failed since Maxima requested additional constraints (use assume): Is ?h+h positive, negative, or zero? What does ?h+h mean? sage: u.integral(x) --- AttributeErrorTraceback (most recent call last) /home/ondra/ext/sage-3.1.2-debian32-intel-i686-Linux/ipython console in module() /home/ondra/ext/sage-3.1.2-debian32-intel-i686-Linux/local/lib/python2.5/site-packages/sage/functions/piecewise.py in integral(self, x) 647 invs = self.intervals() 648 n = len(funcs) -- 649 return sum([funcs[i].integral(x,invs[i][0],invs[i][1]) for i in range(n)]) 650 651 def convolution(self,other): AttributeError: 'sage.rings.integer.Integer' object has no attribute 'integral' Am I doing something wrong, or is it just not implemented yet? What is the plan of implementing this in pynac? For comparison, in sympy you can do this: In [1]: var(h) Out[1]: h In [2]: u = Piecewise( (0, x=0), (1, x=h), (x/h, True)) In [3]: integrate(u, x) Out[3]: ⎧ 0 for x ≤ 0 ⎪ ⎪ x for h ≤ x ⎪ ⎨ 2 ⎪ x ⎪─── otherwise ⎪2⋅h ⎩ Substitution works like this: In [4]: e = (u-u.subs(x, y))**2/(x-y)**2 In [5]: e Out[5]: 2 ⎛- ⎧0 for y ≤ 0 + ⎧0 for x ≤ 0⎞ ⎜ ⎪ ⎪⎟ ⎜ ⎪1 for h ≤ y ⎪1 for h ≤ x⎟ ⎜ ⎨ ⎨⎟ ⎜ ⎪y ⎪x ⎟ ⎜ ⎪─ otherwise ⎪─ otherwise⎟ ⎝ ⎩h ⎩h ⎠ ── 2 (x - y) Now I'd like to integrate the resulting expression and that doesn't yet work in sympy: http://code.google.com/p/sympy/issues/detail?id=1214 so I was curious what is Sage's strategy to handle Piecewise functions in a regular symbolic expression. Ondrej --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
On Mon, Nov 24, 2008 at 7:25 AM, David Joyner [EMAIL PROTECTED] wrote: On Mon, Nov 24, 2008 at 6:19 AM, Ondrej Certik [EMAIL PROTECTED] wrote: Hi, when I use regular expressions, I can use .subs(): sage: e = x+y sage: e.subs(x=y) 2*y but not with Piecewise: sage: var(h H x y) (h, H, x, y) sage: u = Piecewise([((0, h), x/h), ((h, H), 1)]) I don't think variable endpoints are implemented in Sage's Piecewise. Maybe one could add a method=sympy option to use sympy's Piecewise in place of the Maxima interface? Replying to my own post: I am happy to copy my code over to sympy's piecewise class (or have someone else do it). In other words, even though it is GPL'd I'm happy to relicense it under BSD for sympy, if there is any interest. I might have time for that over the winter break. sage: u.subs(x=y) --- AttributeErrorTraceback (most recent call last) /home/ondra/ext/sage-3.1.2-debian32-intel-i686-Linux/ipython console in module() AttributeError: PiecewisePolynomial instance has no attribute 'subs' Ok. Now I'd like to integrate that: sage: u.integral() TypeError: Computation failed since Maxima requested additional constraints (use assume): Is h positive, negative, or zero? sage: assume(h0) sage: u.integral() TypeError: Computation failed since Maxima requested additional constraints (use assume): Is ?h+h positive, negative, or zero? What does ?h+h mean? sage: u.integral(x) --- AttributeErrorTraceback (most recent call last) /home/ondra/ext/sage-3.1.2-debian32-intel-i686-Linux/ipython console in module() /home/ondra/ext/sage-3.1.2-debian32-intel-i686-Linux/local/lib/python2.5/site-packages/sage/functions/piecewise.py in integral(self, x) 647 invs = self.intervals() 648 n = len(funcs) -- 649 return sum([funcs[i].integral(x,invs[i][0],invs[i][1]) for i in range(n)]) 650 651 def convolution(self,other): AttributeError: 'sage.rings.integer.Integer' object has no attribute 'integral' Am I doing something wrong, or is it just not implemented yet? What is the plan of implementing this in pynac? For comparison, in sympy you can do this: In [1]: var(h) Out[1]: h In [2]: u = Piecewise( (0, x=0), (1, x=h), (x/h, True)) In [3]: integrate(u, x) Out[3]: ⎧ 0 for x ≤ 0 ⎪ ⎪ x for h ≤ x ⎪ ⎨ 2 ⎪ x ⎪─── otherwise ⎪2⋅h ⎩ Substitution works like this: In [4]: e = (u-u.subs(x, y))**2/(x-y)**2 In [5]: e Out[5]: 2 ⎛- ⎧0 for y ≤ 0 + ⎧0 for x ≤ 0⎞ ⎜ ⎪ ⎪⎟ ⎜ ⎪1 for h ≤ y ⎪1 for h ≤ x⎟ ⎜ ⎨ ⎨⎟ ⎜ ⎪y ⎪x ⎟ ⎜ ⎪─ otherwise ⎪─ otherwise⎟ ⎝ ⎩h ⎩h ⎠ ── 2 (x - y) Now I'd like to integrate the resulting expression and that doesn't yet work in sympy: http://code.google.com/p/sympy/issues/detail?id=1214 so I was curious what is Sage's strategy to handle Piecewise functions in a regular symbolic expression. Ondrej --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
On Mon, Nov 24, 2008 at 1:25 PM, David Joyner [EMAIL PROTECTED] wrote: On Mon, Nov 24, 2008 at 6:19 AM, Ondrej Certik [EMAIL PROTECTED] wrote: Hi, when I use regular expressions, I can use .subs(): sage: e = x+y sage: e.subs(x=y) 2*y but not with Piecewise: sage: var(h H x y) (h, H, x, y) sage: u = Piecewise([((0, h), x/h), ((h, H), 1)]) I don't think variable endpoints are implemented in Sage's Piecewise. Maybe one could add a method=sympy option to use sympy's Piecewise in place of the Maxima interface? Well, even fixed endpoints doesn't seem to work properly: sage: u = Piecewise([((0, 1), x/h), ((1, 2), 1)]) sage: u**2 --- TypeError Traceback (most recent call last) /home/ondra/ext/sage-3.1.4-debian32-IntelXeon-x86-i686-Linux/ipython console in module() /home/ondra/ext/sage/local/lib/python2.5/site-packages/sage/rings/integer.so in sage.rings.integer.Integer.__pow__ (sage/rings/integer.c:9379)() TypeError: unsupported operand type(s) for ** or pow(): 'instance' and 'int' I don't see how pynac is relevant here. Pynac will only provide a different internal format for representing symbolic expressions, the piecewise class will still be the same. Well, if you want to do stuff like u**2, where u is piecewise, or use the .subs() that is implemented in the symbolic engine, you can either implement everything by hand (e.g. implement .subs(), implement __pow__, ...), but that's imho not the way, or, you can integrate it inside the symbolic engine, so that you can manipulate with it like with any other function -- that's the approach we took with sympy. You can open a trac ticket if you need this functionality. It is safe to change piecewise.py, and add a .subs() method. Since the user interface for the pynac expressions will be almost the same as the current ones, it should still work when the underlying representation changes. I think it should be discussed more first. My vision is that the symbolic engine should treat everything as first class citizens --- e.g. if you make series expansion, it does the right thing. Well, I just found series expansion of piecewise functions doesn't work in sympy. :) http://code.google.com/p/sympy/issues/detail?id=1216 But you see the pattern --- we just override the Piecewise._eval_nseries() and it will start working. btw. it doesn't work in Sage either: sage: taylor(u, x, 0.5) --- TypeError Traceback (most recent call last) /home/ondra/ext/sage-3.1.4-debian32-IntelXeon-x86-i686-Linux/ipython console in module() TypeError: taylor() takes exactly 4 arguments (3 given) sage: taylor(u, x, 0.5, 4) --- TypeError Traceback (most recent call last) /home/ondra/ext/sage-3.1.4-debian32-IntelXeon-x86-i686-Linux/ipython console in module() /home/ondra/ext/sage/local/lib/python2.5/site-packages/sage/calculus/functional.pyc in taylor(f, v, a, n) 332 333 if not isinstance(f, SymbolicExpression): -- 334 f = SR(f) 335 return f.taylor(v=v,a=a,n=n) 336 /home/ondra/ext/sage/local/lib/python2.5/site-packages/sage/calculus/calculus.pyc in __call__(self, x) 452 msg, s, pos = err.args 453 raise TypeError, %s: %s !!! %s % (msg, s[:pos], s[pos:]) -- 454 return self._coerce_impl(x) 455 456 def _coerce_impl(self, x): /home/ondra/ext/sage/local/lib/python2.5/site-packages/sage/calculus/calculus.pyc in _coerce_impl(self, x) 508 return self(x._sage_()) 509 else: -- 510 raise TypeError, cannot coerce type '%s' into a SymbolicExpression.%type(x) 511 512 def _repr_(self): TypeError: cannot coerce type 'type 'instance'' into a SymbolicExpression. So imho it is related to pynac, because if you use ginac's series expansion, it should be able to handle Piecewise. If you choose to implement series expansion yourself (that's what we did in sympy), you need to integrate Piecewise in somehow. Ondrej --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
On Mon, Nov 24, 2008 at 2:15 PM, David Joyner [EMAIL PROTECTED] wrote: On Mon, Nov 24, 2008 at 7:25 AM, David Joyner [EMAIL PROTECTED] wrote: On Mon, Nov 24, 2008 at 6:19 AM, Ondrej Certik [EMAIL PROTECTED] wrote: Hi, when I use regular expressions, I can use .subs(): sage: e = x+y sage: e.subs(x=y) 2*y but not with Piecewise: sage: var(h H x y) (h, H, x, y) sage: u = Piecewise([((0, h), x/h), ((h, H), 1)]) I don't think variable endpoints are implemented in Sage's Piecewise. Maybe one could add a method=sympy option to use sympy's Piecewise in place of the Maxima interface? Replying to my own post: I am happy to copy my code over to sympy's piecewise class (or have someone else do it). In other words, even though it is GPL'd I'm happy to relicense it under BSD for sympy, if there is any interest. I might have time for that over the winter break. Thanks! Btw, feel free to take any code from sympy as well and incorporate it in Sage. But it's a design issue --- I want to work on these things with Sage together, because clearly currently we have two, incompatible Piecewise functions implementations, and neither in Sage nor in sympy it works 100%. Ondrej --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: substitution in and integration of piecewise functions
On Mon, 24 Nov 2008 14:04:53 +0100 Ondrej Certik [EMAIL PROTECTED] wrote: On Mon, Nov 24, 2008 at 1:25 PM, David Joyner [EMAIL PROTECTED] wrote: On Mon, Nov 24, 2008 at 6:19 AM, Ondrej Certik [EMAIL PROTECTED] wrote: Hi, when I use regular expressions, I can use .subs(): sage: e = x+y sage: e.subs(x=y) 2*y but not with Piecewise: sage: var(h H x y) (h, H, x, y) sage: u = Piecewise([((0, h), x/h), ((h, H), 1)]) I don't think variable endpoints are implemented in Sage's Piecewise. Maybe one could add a method=sympy option to use sympy's Piecewise in place of the Maxima interface? Well, even fixed endpoints doesn't seem to work properly: sage: u = Piecewise([((0, 1), x/h), ((1, 2), 1)]) sage: u**2 --- TypeError Traceback (most recent call last) /home/ondra/ext/sage-3.1.4-debian32-IntelXeon-x86-i686-Linux/ipython console in module() /home/ondra/ext/sage/local/lib/python2.5/site-packages/sage/rings/integer.so in sage.rings.integer.Integer.__pow__ (sage/rings/integer.c:9379)() TypeError: unsupported operand type(s) for ** or pow(): 'instance' and 'int' Again, this is not implemented, patches are welcome. I don't see how pynac is relevant here. Pynac will only provide a different internal format for representing symbolic expressions, the piecewise class will still be the same. Well, if you want to do stuff like u**2, where u is piecewise, or use the .subs() that is implemented in the symbolic engine, you can either implement everything by hand (e.g. implement .subs(), implement __pow__, ...), but that's imho not the way, or, you can integrate it inside the symbolic engine, so that you can manipulate with it like with any other function -- that's the approach we took with sympy. Are you saying that your class which represents a piecewise function doesn't implement it's .subs() method, which in turn calls the .subs() methods of each of the pieces (and the endpoints in your case)? Similarly for the __pow__ method. Can you explain your design a little? You can open a trac ticket if you need this functionality. It is safe to change piecewise.py, and add a .subs() method. Since the user interface for the pynac expressions will be almost the same as the current ones, it should still work when the underlying representation changes. I think it should be discussed more first. My vision is that the symbolic engine should treat everything as first class citizens --- e.g. if you make series expansion, it does the right thing. Note that in the design of GiNaC, which AFAIK, you also follow in SymPy, these methods, e.g., arithmetic, substitution, etc. are implemented in each class of the hierarchy. I don't see why building on this hierarchy using python after a certain point is a problem. Well, I just found series expansion of piecewise functions doesn't work in sympy. :) http://code.google.com/p/sympy/issues/detail?id=1216 But you see the pattern --- we just override the Piecewise._eval_nseries() and it will start working. Same in Sage. I don't see your point. btw. it doesn't work in Sage either: sage: taylor(u, x, 0.5) --- TypeError Traceback (most recent call last) ... TypeError: cannot coerce type 'type 'instance'' into a SymbolicExpression. Can you open a ticket and list these things you found that don't work with piecewise functions. I.e., - substitutions - __pow__ - series expansion So imho it is related to pynac, because if you use ginac's series expansion, it should be able to handle Piecewise. If you choose to implement series expansion yourself (that's what we did in sympy), you need to integrate Piecewise in somehow. Pynac will only provide the basic operations one needs from symbolic expressions as a library. This is quite similar to your efforts of implementing a sympy core in Cython. http://groups.google.com/group/sympy/browse_thread/thread/aa3f4263bc3f7e23 The road plan for SymPy says this will be included in version 0.7: http://wiki.sympy.org/wiki/Plan_for_SymPy_1.0 So I really don't see what you mean above. From another message: On Mon, 24 Nov 2008 14:27:04 +0100 Ondrej Certik [EMAIL PROTECTED] wrote: Thanks! Btw, feel free to take any code from sympy as well and incorporate it in Sage. But it's a design issue --- I want to work on these things with Sage together, because clearly currently we have two, incompatible Piecewise functions implementations, and neither in Sage nor in sympy it works 100%. Piecewise functions are a great example of a way SymPy and Sage can work together. Since the interfaces to the underlying symbolic expressions are the same, the same python implementation can be used for both the projects. If SymPy had a stable and fast core at
[sage-support] Re: substitution in and integration of piecewise functions
On Mon, Nov 24, 2008 at 3:08 PM, Burcin Erocal [EMAIL PROTECTED] wrote: On Mon, 24 Nov 2008 14:04:53 +0100 Ondrej Certik [EMAIL PROTECTED] wrote: On Mon, Nov 24, 2008 at 1:25 PM, David Joyner [EMAIL PROTECTED] wrote: On Mon, Nov 24, 2008 at 6:19 AM, Ondrej Certik [EMAIL PROTECTED] wrote: Hi, when I use regular expressions, I can use .subs(): sage: e = x+y sage: e.subs(x=y) 2*y but not with Piecewise: sage: var(h H x y) (h, H, x, y) sage: u = Piecewise([((0, h), x/h), ((h, H), 1)]) I don't think variable endpoints are implemented in Sage's Piecewise. Maybe one could add a method=sympy option to use sympy's Piecewise in place of the Maxima interface? Well, even fixed endpoints doesn't seem to work properly: sage: u = Piecewise([((0, 1), x/h), ((1, 2), 1)]) sage: u**2 --- TypeError Traceback (most recent call last) /home/ondra/ext/sage-3.1.4-debian32-IntelXeon-x86-i686-Linux/ipython console in module() /home/ondra/ext/sage/local/lib/python2.5/site-packages/sage/rings/integer.so in sage.rings.integer.Integer.__pow__ (sage/rings/integer.c:9379)() TypeError: unsupported operand type(s) for ** or pow(): 'instance' and 'int' Again, this is not implemented, patches are welcome. I don't see how pynac is relevant here. Pynac will only provide a different internal format for representing symbolic expressions, the piecewise class will still be the same. Well, if you want to do stuff like u**2, where u is piecewise, or use the .subs() that is implemented in the symbolic engine, you can either implement everything by hand (e.g. implement .subs(), implement __pow__, ...), but that's imho not the way, or, you can integrate it inside the symbolic engine, so that you can manipulate with it like with any other function -- that's the approach we took with sympy. Are you saying that your class which represents a piecewise function doesn't implement it's .subs() method, which in turn calls the .subs() methods of each of the pieces (and the endpoints in your case)? No, it does implement exactly that. Actually, because of docstrings (and possibly changing the interface of things to the end user), we have one .subs() method implemented in Basic, with a nice docstring and the subclasses (including Piecewise) just implement _eval_subs(). The user is supposed to just call .subs(). But that's minor. But in pynac, so do you call ginac's subs method, or do you implement it on your own? I know you can pass Sage objects inside ginac, thanks to cython. I just want to clarify this is what you do. Similarly for the __pow__ method. We only implement __pow__ in Basic by default. The subclasses can override it for efficiency reasons, but don't have to. Can you explain your design a little? Yes -- please ask if more clarification is needed besides the above. You can open a trac ticket if you need this functionality. It is safe to change piecewise.py, and add a .subs() method. Since the user interface for the pynac expressions will be almost the same as the current ones, it should still work when the underlying representation changes. I think it should be discussed more first. My vision is that the symbolic engine should treat everything as first class citizens --- e.g. if you make series expansion, it does the right thing. Note that in the design of GiNaC, which AFAIK, you also follow in SymPy, these methods, e.g., arithmetic, substitution, etc. are implemented in each class of the hierarchy. Some of, yes. I don't see why building on this hierarchy using python after a certain point is a problem. It is not a problem, I am only curious if this is your plan. Well, I just found series expansion of piecewise functions doesn't work in sympy. :) http://code.google.com/p/sympy/issues/detail?id=1216 But you see the pattern --- we just override the Piecewise._eval_nseries() and it will start working. Same in Sage. I don't see your point. btw. it doesn't work in Sage either: sage: taylor(u, x, 0.5) --- TypeError Traceback (most recent call last) ... TypeError: cannot coerce type 'type 'instance'' into a SymbolicExpression. Can you open a ticket and list these things you found that don't work with piecewise functions. I.e., - substitutions - __pow__ - series expansion So imho it is related to pynac, because if you use ginac's series expansion, it should be able to handle Piecewise. If you choose to implement series expansion yourself (that's what we did in sympy), you need to integrate Piecewise in somehow. Pynac will only provide the basic operations one needs from symbolic expressions as a library. This is quite similar to your efforts of implementing a sympy core in Cython.
[sage-support] Re: substitution in a matrix (newby)
William Stein [EMAIL PROTECTED] writes: On 7/26/07, Roger Mason [EMAIL PROTECTED] wrote: You should do m.substitute(a=1) just like you're doing -- unfortunately, nobody implemented that yet, and it's doing some dumb generic behavior. [...] Fortunately, I just implemented this for SAGE-2.7.1, which I'll release very very soon. Thank you both very much. It seems I need a careful read of the user manual, I had not picked up on lambda expressions. I am compiling 2.7.1 so will soon be able to test blah.substitute(a=1). I don't know if lambda expressions are discussed in the user manual. The SAGE language is Python, and Python has a very nice lambda expression. Thus this nicely written book also applies to SAGE: http://docs.python.org/tut/ The section on lambda expressions is here: http://docs.python.org/tut/node6.html#SECTION00675 The compilation of 2.7.1 just finished and I had started looking for lambda in the docs when I saw your e-mail! Many thanks, Roger --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/ -~--~~~~--~~--~--~---
[sage-support] Re: substitution in a matrix (newby)
David William, David Joyner [EMAIL PROTECTED] writes: I would use the lambda notation: sage: m = lambda x: matrix([[x[0],x[1]],[x[2],x[3]]]) sage: m([1,2,3,4]) [1 2] [3 4] sage: m([1,2,3,a]) [1 2] [3 a] sage: m([a,b,3,4]) [a b] [3 4] There might be better ways though. William Stein [EMAIL PROTECTED] writes: You should do m.substitute(a=1) just like you're doing -- unfortunately, nobody implemented that yet, and it's doing some dumb generic behavior. [...] Fortunately, I just implemented this for SAGE-2.7.1, which I'll release very very soon. Thank you both very much. It seems I need a careful read of the user manual, I had not picked up on lambda expressions. I am compiling 2.7.1 so will soon be able to test blah.substitute(a=1). Roger --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/ -~--~~~~--~~--~--~---
[sage-support] Re: substitution in a matrix (newby)
On 7/26/07, Roger Mason [EMAIL PROTECTED] wrote: You should do m.substitute(a=1) just like you're doing -- unfortunately, nobody implemented that yet, and it's doing some dumb generic behavior. [...] Fortunately, I just implemented this for SAGE-2.7.1, which I'll release very very soon. Thank you both very much. It seems I need a careful read of the user manual, I had not picked up on lambda expressions. I am compiling 2.7.1 so will soon be able to test blah.substitute(a=1). I don't know if lambda expressions are discussed in the user manual. The SAGE language is Python, and Python has a very nice lambda expression. Thus this nicely written book also applies to SAGE: http://docs.python.org/tut/ The section on lambda expressions is here: http://docs.python.org/tut/node6.html#SECTION00675 William --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/ -~--~~~~--~~--~--~---
[sage-support] Re: substitution in a matrix (newby)
I would use the lambda notation: sage: m = lambda x: matrix([[x[0],x[1]],[x[2],x[3]]]) sage: m([1,2,3,4]) [1 2] [3 4] sage: m([1,2,3,a]) [1 2] [3 a] sage: m([a,b,3,4]) [a b] [3 4] There might be better ways though. On 7/24/07, Roger Mason [EMAIL PROTECTED] wrote: Hello, Suppose I have a symbolic matrix: sage: m = matrix([[a,b],[d,e]]) and I wish to substitute some or all elements of m with numbers. The obvious sage: m.substitute(a=1) returns [a b] [d e] so the substitution (which works fine on a list) does not work on a matrix. How do I do such substitutions? Thanks, Roger --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/ -~--~~~~--~~--~--~---