Ask Your Question

Edgar Brown's profile - activity

2023-05-31 20:10:56 +0200 received badge  Famous Question (source)
2023-05-22 12:31:37 +0200 received badge  Notable Question (source)
2023-03-19 00:36:47 +0200 received badge  Famous Question (source)
2022-01-27 04:28:34 +0200 received badge  Famous Question (source)
2021-11-06 07:10:41 +0200 received badge  Popular Question (source)
2021-05-06 19:03:44 +0200 received badge  Notable Question (source)
2021-04-28 10:50:44 +0200 received badge  Popular Question (source)
2021-04-28 10:50:44 +0200 received badge  Notable Question (source)
2020-09-28 21:31:22 +0200 commented answer How to implement 'derivative' in ExpressionTreeWalker

Yes!! That works. Thanks!!

2020-09-28 12:20:09 +0200 received badge  Nice Question (source)
2020-09-28 03:49:17 +0200 commented answer What is the difference between SR.var() and var()?

I am not sure I follow the design decision. This looks like two different name spaces. One is the Python one which seems to be where the interface lives, the other a sage one to which there is just indirect access. Is that correct? And why would omitting the assignment on the second one does not cause the same problem as in the first one?

2020-09-28 03:49:17 +0200 received badge  Commentator
2020-09-28 00:59:48 +0200 asked a question How to implement 'derivative' in ExpressionTreeWalker

I am trying to implement a method that requires traversing the expression tree. I have no problem with arithmetic functions, but I can't figure out how to deal with derivatives. Take this simple implementation for example:

sage: from sage.symbolic.expression_conversions import ExpressionTreeWalker
....: class ShowTree(ExpressionTreeWalker):
....:     def __init__(self, ring):
....:         self.ring = ring
....:     def arithmetic(self, ex, op):
....:         r = reduce(op, map(self, ex.operands()))
....:         print(['>>',ex, ex.operands(),op])
....:         return r
....:     def derivative(self, ex, op):
....:         r = operator(*map(self, ex.operands()))
....:         print(['d>>',ex, ex.operands(),op])
....:         return r

If you run it in the following:

var("a,b,x")
f = function('f')(x)
ShowTree(SR)(a*b+b*f(x).diff(x,2))

The following error occurs:

['>>', a*b, [a, b], <function mul_vararg at 0x34863e8c8>]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
....
<ipython-input-217-df4a3880d6a1> in derivative(self, ex, op)
      8         return r
      9     def derivative(self, ex, op):
---> 10         r = operator(*map(self, ex.operands()))
     11         print(['d>>',ex, ex.operands(),op])
     12         return r

TypeError: 'module' object is not callable

If that line is changed to:

    r = reduce(op,map(self, ex.operands()))

The result becomes:

(a, b, x)
['>>', a*b, [a, b], <function mul_vararg at 0x342dacbf8>]
['d>>', diff(f(x), x, x), [x], D[0, 0](f)]
['>>', b*diff(f(x), x, x), [b, diff(f(x), x, x)], <function mul_vararg at 0x342dacbf8>]
['>>', a*b + b*diff(f(x), x, x), [a*b, b*diff(f(x), x, x)], <function add_vararg at 0x342dacb70>]
a*b + b*x

That is the derivative operator is removed and substituted for the argument of the operator.

ExpressionTreeWalker is poorly documented at best, and I can't figure out what is expected as a return value from the derivative method (my first attempt comes from looking at the existing implementation). What am I missing?

2020-09-27 20:27:55 +0200 asked a question What is the difference between SR.var() and var()?

Is this a bug?

If you type:

sage: SR.var('a')
a
sage: a?
Object `a` not found.

However, this works:

sage: var('a')
a
sage: a?
Type:           Expression
String form:    a
....
2020-07-17 00:13:02 +0200 received badge  Nice Question (source)
2020-07-17 00:12:27 +0200 received badge  Popular Question (source)
2019-10-28 08:03:56 +0200 received badge  Teacher (source)
2019-10-28 08:03:56 +0200 received badge  Self-Learner (source)
2019-10-28 01:01:09 +0200 commented answer How to iterate over symbolic coefficients

I believe your answer provides the path to the only reliable choice. I think I have to walk the tree of what I assume to be mostly a polynomial looking at sums and products of elements and expanding the substitution in all of those cases, but stopping at anything else such as division and exponentiation and removing the substitution on those subtrees. Although conceptually it works, I can't see the path for doing that in code. Your example enumerates the tree backwards from this, which means that I would have to account for all (unknown) cases, instead of known ones such as sums and products.

I still get the feeling that a properly defined symbolic ring would be able to do this, but I don't see how.

2019-10-25 14:33:56 +0200 answered a question How to iterate over symbolic coefficients

Leaving this answer as a warning. I decided to go in the direction of @tmontell 's answer, although I can see multiple ways to get the functions to do what I want in specific instances, there are too many corner cases that will cause problems in the future.


Although @tmonteil answer walks the structure, I believe this is simpler to implement and understand. This answer does what I need, at least for the cases that I am handling by hand (instead of programmatically):

WARNING: There is an issue where if I simplify before applying the final substitution these do not work, I can't see a reason for that. This question might illuminate the reason.

Definitions:

  var('a b x b_m b_s',domain='real')
  faux = function('faux', latex_name='f_{aux}')(b_m) # Auxiliary function
  R = PolynomialRing(SR,names='a,b_s')  # Polynomial ring over the symbolic ring
  f_m = a*x^b     # target function

Note that the definitions of R is critical, a & b_s are not the same as the variables being used (see answer: map functions into polynomial coefficients).

The actual operations:

 exd = expand(f_m.subs(b = faux)).derivative(b_m)  # substitute the auxiliary function
 exd = exd.derivative(b_m)      # calculate the derivative
 exd = exd.subs({diff(faux,b_m) : 1 - 3*(b_s-b_m)^2})  # replace by known derivative 
 exd = exd.subs({faux : b_m}).expand()    # replace by function evaluation

This set of operations do what I need, now all that is left is to transform exd into a proper polynomial so I can manipulate its coefficients.

 exp = exd.polynomial(ring=R)         # It's now represented as a polynomial
 dict(zip(exp.monomials(), exp.coefficients()))  # Dictionary with the desired terms

Which results in:

 {a: -3*b_m^2*x^b_m*log(x) + x^b_m*log(x),
  a*b_s: 6*b_m*x^b_m*log(x),
  a*b_s^2: -3*x^b_m*log(x)}

Some cleanup might be needed, and there are some issues (e.g., PolynomialRing does not support "show()" for some reason), and in some cases the substitution breaks for no apparent reason (a bug?). But I think it does what is needed. Any suggestions on how to improve this?

2019-10-25 14:00:13 +0200 commented question How can I map functions into polynomial coefficients

@vdelecroix A typo

2019-10-25 13:58:40 +0200 received badge  Scholar (source)
2019-10-25 13:52:10 +0200 commented answer How can I map functions into polynomial coefficients

Thanks! That works.

I guess the generator syntax is critical as R.<a,c> = Poly... definitively does not work.

But why is the "show" method not implemented on a PolynomialRing????

2019-10-25 03:07:32 +0200 asked a question How can I map functions into polynomial coefficients

Let's say I have the following expression (from a wide range of possibilities) :

pol =  3*a*x^(-b)*log(x)*b^2 - 6*a*b*c*sin(x*b) + 3*a*c^2 + 5

And I want to extract the coefficients of the polynomial over the polynomial ring over a & c, so that these result in:

a^0*c^0  :  5
a^1*c^0  :  3*x^(-b)*log(x)*b^2
a^0*c^1  :  0
a^1*c^1  :  -6*b*sin(x*b)
etc.

How can I define the polynomial ring?

How can I map an existing expression that defines "pol" (which is the result of other manipulations) into such ring?

2019-10-20 05:26:09 +0200 commented answer How to iterate over symbolic coefficients

I might have found a much simpler and general way to solve my specific issue that just involves a couple of function manipulations, I'll try to implement it and post it as soon as I get a chance by the end of this coming week.

2019-10-19 04:02:04 +0200 commented answer How to iterate over symbolic coefficients

Yikes, thanks!! It will take me a while to digest that, I was hoping for some simpler pattern matching.

Could this be easily extended to other non-linear functions such as log(x), sin(x), etc.? Or particular cases would have to be treated one by one (e.g., by iterating through a list of all nonlinear functions). I don't know enough to see if there are edge cases, but I will definitively look into TreeWalker.

2019-10-19 03:56:05 +0200 received badge  Supporter (source)
2019-10-19 00:17:33 +0200 received badge  Student (source)
2019-10-18 22:22:27 +0200 commented answer How to iterate over symbolic coefficients

@tmontel I added some clarification to the question. I hope that covers it.

2019-10-18 22:21:52 +0200 received badge  Editor (source)
2019-10-18 15:41:26 +0200 commented question How to iterate over symbolic coefficients

@tmontell They are supposed to remain unknown until I can coerce the final output of a long set of operations into a numeric framework.

2019-10-18 15:09:41 +0200 commented question How to iterate over symbolic coefficients

I don't have experience with either, but it's obvious to me that a polynomial ring is what I want and a symbolic ring might provide me with the proper iteration tools. When I tried the polynomial ring of polynomial coefficients I kept getting errors due to the same variable being used on the exponents and the monomials. I have no idea about the symbolic ring or how to use it, but given that this is just a minor step in a long process doing things by hand is not an option.

2019-10-18 15:08:10 +0200 commented answer How to iterate over symbolic coefficients

I know that with playing around with the expressions I can get Sage to do it, but this is just a minor step in a complicated process, so I need a reliable and automatable method that goes beyond simple pattern matching. Iterating though the non-monomial elements seems to be the ticket, but I don't know how to do that.

2019-10-18 10:56:27 +0200 asked a question How to iterate over symbolic coefficients

I am a relative newbie to Sage but not to algebraic manipulation in general.

I have a family of expressions with non-linear coefficients which I am not sure how best to represent. In the end it has to be an (infinite) ring of polynomials representing a Volterra space, but I have the same terms inside non-linear functions (which I need to evaluate to a known value) and as the polynomial variables.

Here is an example, where simple symbolic manipulation doesn't work:

f_m = a*x^b
ex = expand(f_m.subs({b : b_m + (b_s-b_m)^3})).derivative(b_m).full_simplify()
ex.subs({b_m + (b_s-b_m)^3 : b_m }).show()

Producing:

  -(3*a*b_m^2 - 6*a*b_m*b_s + 3*a*b_s^2 - a)*x^(-b_m^3 + 3*b_m^2*b_s - 3*b_m*b_s^2 + b_s^3 + b_m)*log(x)

When what I want is:

  -(3*a*b_m^2 - 6*a*b_m*b_s + 3*a*b_s^2 - a)*x^(-b_m)*log(x)

How can I iterate over non-linear elements (exponent and the arguments to any non-monomial terms) carrying out direct substitutions, while leaving monomials alone. I want the result to remain as a polynomial ring on b_s


Clarification: The intent is to use the code for a generic non-linear construct. It's intended to handle any general non-linear expression in L2, but these are manipulated to belong to a class of Volterra polynomials with non-linear coefficients. like the log(x) and x^bm on the above example. Those are the coefficients of the monomials.

Idealy whatever process I follow should be able to identify the non-monomial coefficients in the expression and remove the monomial terms from them by doing the substitution b_s = b_m. Basically:

    x.subs(b_s = b_m) for x not a monomial term in expression.

Which would make the resulting expression strictly a polynomial ring on b_s with non-linear coefficients on x and b_m.