Hi Urs,

On 12 Okt., 17:00, Urs Hackstein <urs.hackst...@googlemail.com> wrote:
> I have the following problem: Let f be a complex function in the
> complex variable s  that is given by a long (approximately 3 pages)
> term consisting of a lot of fractions nested into each other, basic
> arithmetic operations and square roots.

How did you define s? By var('s'), hence, as a symbolic variable? Or
is s actually a variable in a polynomial ring? Since your expression
contain square roots of polynomials (or only of coefficients?), power
series might be an option as well.

That may make a huge difference. Depending on the problem, each can be
better than the other.

And how did you define the coefficients? There are different
structures in Sage that behave like complex numbers.

I'm starting with arithmetic operations, postponing square roots. In
that case, the polynomial ring approach will readily solve your
problem. Symbolic expressions fail to the extent that I would call it
a bug - I'll report it to sage-devel.
I am also showing you a pitfall: It is quite easy to *accidentally*
define a symbolic expression when you actually want to create a
polynomial over a complex field or when taking the square root of a
polynomial.
Finally, I briefly indicate how power series may help you with square
roots.

How to create polynomials
--------------------------------------

To create s as an element of a polynomial ring with coefficients in K,
you can do
  P = K['s']
  s = P.gen()
in a script, or you can more pleasantly do
  sage: P.<s> = K[]
in an interactive session.

You say that the coefficients are complex - but are they really
"naked" complex numbers? Again, depending on the problem, you might
work with complex floats, with the complex interval field, or with the
field of algebraic numbers (when your coefficients are solutions to
algebraic equations).

Let us assume that you work over the complex interval field. If you
decide to consider "s" as element of a polynomial ring, then your
definitions may look as follows:
  sage: K = CIF
  sage: K
  Complex Interval Field with 53 bits of precision
  sage: P.<s> = K[]
  sage: P
  Univariate Polynomial Ring in s over Complex Interval Field with 53
bits of precision

And then, simplification should just work.

Let us create some more complicated arithmetic expression:
 sage: p1 = P.random_element()
 sage: p2 = P.random_element()
 sage: p3 = P.random_element()
 sage: p4 = P.random_element()
 sage: p5 = P.random_element()
 sage: p = ((p1/p2+p3)^2+(p3/(p4+p5)))/((p3/(p4+p5/(p1+p2)))+p2)
 sage: p
 ((-0.014910171719530? - 0.0819029681647261?*I)*s^14 +
(0.334831133184198? + 0.226920835016301?*I)*s^13 + (-1.31689500460014?
+ 0.33100925619084?*I)*s^12 + (1.12271025690267? - 2.9711886585730?
*I)*s^11 + (4.1636372058951? + 5.3375810146048?*I)*s^10 +
(-11.7982240203551? - 0.5358729413119?*I)*s^9 + (9.1839609650226? -
13.3936564432300?*I)*s^8 + (6.2198719561828? + 20.818526779122?*I)*s^7
+ (-22.0256922983008? - 5.4198967910775?*I)*s^6 + (14.576686446425? -
11.706831897246?*I)*s^5 + (5.6758660676201? + 14.853679842037?*I)*s^4
+ (-6.8405250783868? - 2.8000312050867?*I)*s^3 + (1.7390862645398? -
4.3186765269158?*I)*s^2 + (-0.85548154093647? + 1.01546197758495?*I)*s
- 0.895136615170288? + 0.554040475082552?*I)/((-0.169080624065409? +
0.012578831775274?*I)*s^12 + (-0.05414287388546? - 0.737750690892271?
*I)*s^11 + (1.52184676823240? + 1.16470671744095?*I)*s^10 +
(-5.0190991302301? + 2.36713627116690?*I)*s^9 + (1.9657655611365? -
9.4018627113920?*I)*s^8 + (12.6310011693864? + 13.2679738013862?
*I)*s^7 + (-23.8796945790930? + 2.7835626926490?*I)*s^6 +
(15.6411210970610? - 24.7657676923677?*I)*s^5 + (7.6541549026413? +
27.8461228023130?*I)*s^4 + (-17.7687638664456? - 12.1686300681916?
*I)*s^3 + (14.3341637239600? - 2.3920514116429?*I)*s^2 +
(-7.0332616091283? + 2.6003363350971?*I)*s + 1.28808529738263? -
0.24159917788167?*I)

So, the result is just a fraction of two polynomials.

How to create a symbolic expression
-----------------------------------------------------

If you insist on using symbolic variables (I *never* consciously used
symbolic variables for my own work, but there certainly are
applications), simplification will not automatically happen. Let us
compare with the "same" example in the symbolic ring:

  sage: SR
  Symbolic Ring
  sage: q1 = SR(p1)
  sage: q2 = SR(p2)
  sage: q3 = SR(p3)
  sage: q4 = SR(p4)
  sage: q5 = SR(p5)
  sage: q = ((q1/q2+q3)^2+(q3/(q4+q5)))/((q3/(q4+q5/(q1+q2)))+q2)
  sage: q
  ((((0.61891108886609514? - 0.092586296245679601?*I)*s -
0.98781137976673450? + 0.11138924672910667?*I)*s +
(((0.29144707263062198? - 0.42564378360017075?*I)*s -
0.61884952759953405? - 0.91492158538646940?*I)*s -
0.24947131951698487? - 0.096607726700608688?*I)/((-
(0.15155075241697081? + 0.78306247143200070?*I)*s +
0.57637682747181885? - 0.29257682595384749?*I)*s -
0.86140746403038726? + 0.51624793761351229?*I) - 0.50908151182035400?
- 0.090000947697624012?*I)^2 + (((0.61891108886609514? -
0.092586296245679601?*I)*s - 0.98781137976673450? +
0.11138924672910667?*I)*s - 0.50908151182035400? -
0.090000947697624012?*I)/((-(0.47146742486366700? +
0.57376175864452606?*I)*s + 0.26292911853237034? + 0.20257307216542353?
*I)*s + ((0.79619152926825932? + 0.39681742537994036?*I)*s -
0.29184858034623629? + 0.68656770475640916?*I)*s -
0.40666338022023152? - 1.3892263050186884?*I))/((-
(0.15155075241697081? + 0.78306247143200070?*I)*s +
0.57637682747181885? - 0.29257682595384749?*I)*s +
(((0.61891108886609514?...

I did not print the result completely, but you can see that it is not
simplified. You may try to simplify it manually, but there seems to be
a bug (with sage-4.6.2, at least):
 sage: q.simplify()
 Traceback (most recent call last):
 ...
 TypeError:

There's even no proper error message.

How to avoid accidentally creating a symbolic expression
----------------------------------------------------------------------------------

Note one pitfall, though: The symbolic ring "eats" everything, and the
symbol "I" denotes the complex unit AS AN ELEMENT OF THE SYMBOLIC
RING.

Hence, when you use the complex unit "I" in your polynomial
expressions, then you will inevitably end up with symbolic
expressions, facing the simplification problems exposed above:
  sage: p.parent()
  Univariate Polynomial Ring in s over Complex Interval Field with 53
bits of precision
  sage: I.parent()
  Symbolic Ring
  sage: (p+I-I).parent()
  Symbolic Ring

Solution: Define I=CIF.gen() (or I=CDF.gen() or I=CC.gen(), depending
on your coefficients), and then it should just work.

Square roots
------------------

Unfortunately, square roots of polynomials are symolic expressions:
  sage: p1.parent()
  Univariate Polynomial Ring in s over Complex Interval Field with 53
bits of precision
  sage: sqrt(p1).parent()
  Symbolic Ring

Hence, it may be better to work with power series. Their square roots
are again power series, so that one will not accidentally create
symbolic expressions:
  sage: PS = K[['s']]
  sage: r1 = PS(p1)
  sage: sqrt(r1)
  0.0950? - 0.5084?*I + (0.76? - 0.75?*I)*s + (-0.7? + 0.4?*I)*s^2 +
(1.5? + 0.1?*I)*s^3 + (-3.? - 2.?*I)*s^4 + (1.?e1 + 1.?e1*I)*s^5 + (0.?
e1 - 3.?e1*I)*s^6 + (-1.?e2 + 1.?e2*I)*s^7 + (1.?e3 - 1.?e2*I)*s^8 +
(-1.?e3 + 0.?e3*I)*s^9 + (2.?e3 + 1.?e4*I)*s^10 + (-1.?e4 - 1.?
e4*I)*s^11 + (0.?e5 + 1.?e5*I)*s^12 + (1.?e5 - 1.?e5*I)*s^13 + (-1.?e6
+ 1.?e6*I)*s^14 + (1.?e6 + 0.?e6*I)*s^15 + (-3.?e6 - 2.?e6*I)*s^16 +
(1.?e7 + 1.?e7*I)*s^17 + (0.?e7 - 4.?e7*I)*s^18 + (-7.?e7 + 1.1?
e8*I)*s^19 + O(s^20)
  sage: sqrt(r1).parent()
  Power Series Ring in s over Complex Interval Field with 53 bits of
precision

As you can see from the O(s^20), power series have a limited
precision. However, if you are lucky, it may help you to deal with the
square roots.

Good luck,
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

Reply via email to