On Oct 2, 2008, at 10:43 PM, Ondrej Certik wrote:
> On Thu, Oct 2, 2008 at 6:43 PM, William Stein <[EMAIL PROTECTED]>
> wrote:
>>
>> On Thu, Oct 2, 2008 at 9:31 AM, jdmuys <[EMAIL PROTECTED]> wrote:
>>>
>>> Hi,
>>>
>>> I am a total newcomer, and here is very simple high-school level
>>> question for which I could not find an answer in several hours of
>>> searching:
>>>
>>> How can I use Sage to simplify ratios involving complex numbers?
>>>
>>> By simplify, I mean, to put into the canonical form a+b*i.
>>>
>>> For a very simple example: simplifying x=1/(1+i) would yield (1/2
>>> - i/
>>> 2)
>>>
>>> Note: this is simple to do by hand: multiply both numerator and
>>> denominator by the conjugate of the denominator. For my example,
>>> this
>>> leads to:
>>>
>>> x= (1-i)/[(1+i)(1-i)]
>>> x = (1-i)/[1^2-i^2]
>>> x = (1-i)/[1+1]
>>> x = (1-i)/2
>>> x = 1/2 -i/2
>>>
>>> I tried quite a number of things, none of which worked.
>>>
>>> Thanks, and sorry if my question is easy (well actually, I hope it's
>>> easy ;-)
>>>
>>
>> You could get the real and imaginary parts, as follows:
>>
>> sage: a = (1-I)/(1 + I)
>> sage: a.real() + I*a.imag()
>> -1*I
>>
>> If you're coefficients are all rational numbers, you could
>> alternatively define I to be the generator for the "ring" QQ[sqrt
>> (-1)],
>> as follows, and all such expressions will automatically
>> be simplified the moment you type them in:
>>
>> sage: I = QQ[sqrt(-1)].gen()
>> sage: 1/1 + I
>> I + 1
>> sage: 1/(1 + I)
>> -1/2*I + 1/2
>> sage: (1-I)/(1 + I)
>> -I
>>
>> Note that expressions like sqrt(2)*I will no longer work
>> with this new version of I. To get back the old I, you
>> can do
>> sage: reset('I')
>
> Or through some package, e.g. sometimes sympy's simplification
> works well:
>
> sage: a = (1-I)/(1 + I)
> sage: import sympy
> sysage: sympy.simplify(a)
> -I
> sage: SR(sympy.simplify(a))
> -1*I
>
> The SR() converts the expression back from a sympy expression to a
> Sage expression.
It's really sad that we don't have a more intuitive way to do this.
There's a maxima command (rectcoords or something like that) but it's
not obvious how to invoke it directly on the SR object.
I've actually been working on a patch for coercion that will allow
number fields to come with specified embeddings, in which case we
will let I be in QQ[sqrt(-1)] (or even perhaps ZZ[sqrt(-1)]), but
with a specified embedding into CC (and by extension SR) so that
stuff like I + sqrt(2) works as expected, but (1-I)/(1+I) simplifies
automatically (and fast).
- Robert
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---