On 09/03/12 03:26, mazkime wrote:
> Hello,
> I would like to factorize an expression with sage that contains constant
> variables (i.e. parameters), but I cannot figure out how to do that.
> 
> Here is an example : x, y are variables and A is a parameter
> 
> *********************
> var('A x y')
> 
> f = A*x + x + A^2*exp(y) + y
> print (f.factor())
> *********************
> 
> Sage returns 'A^2*e^y + A*x + x + y'.
> Instead, I would like that sage returns '(A+1)*x + (A^2+1)*exp(y)'.

Did you mean,

  f = A*x + x + A^2*exp(y) + exp(y)

in the input? Your desired output is not equal to f. If you did, this
can sort of be done:

  sage: A,x,y = var('A,x,y')
  sage: f = A*x + x + A^2*exp(y) + exp(y)
  sage: result = 0
  sage: basis = [x, exp(y)]
  sage: for b in basis:
  ....:     coeff = f.collect(b).coefficient(b)
  ....:     f -= coeff*b
  ....:     result += coeff*b
  ....:
  sage: result
  (A + 1)*x + (A^2 + 1)*e^y

But it relies on collect() doing the right thing, which won't always
happen unfortunately. You may need to call expand() right before
collect() on some expressions. Others just won't work at all.

I suggest sprinkling simplify() in there, and checking that bool(f==0)
at the end so you can at least tell when it has failed (or succeeded
unprovably).

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


Reply via email to