Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

This is not a complete answer, as I have not figured out polynomial rings completely and it needs more work, but at least for the cases that I am handling by hand (instead of programmatically) this does what I need:

Definitions:

  var('a b x b_m b_s',domain='real')
  faux = function('faux', latex_name='f_{aux}')(b_m)
  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 and d/db
 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 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()))  # Generate a 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. But I think it does what is needed. Any suggestions on how to improve this?

Although @tmonteil answer walks the structure, I believe this is simpler to implement and understand. This is not a complete answer, as I have not figured out polynomial rings completely and it needs more work, but answer does what I need, at least for the cases that I am handling by hand (instead of programmatically) this does what I need:programmatically):

Definitions:

  var('a b x b_m b_s',domain='real')
  faux = function('faux', latex_name='f_{aux}')(b_m)
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 coefficientscoefficients).

The actual operations:

 exd = expand(f_m.subs(b = faux)).derivative(b_m)  # substitute the auxiliary function and d/db
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()))  # Generate a dictionary 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. But I think it does what is needed. Any suggestions on how to improve this?

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):

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. reason). But I think it does what is needed. Any suggestions on how to improve this?

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.

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). 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?

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.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?

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?

Leaving this answer as a warning. I decided to go in the direction of @tmontell's @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?