Dear Rich
Thanks for your work. But have you read my last post (if not please do it, at least "my" definition of the squareroot and what my aim is)? I don't think your code it's what I'm looking for.
The inputs
n_roots(-9,2);
or
assume(x<0);
n_roots(x,2);
or
assume(x<0);
n_roots(x,2) ++ n_roots(x,2);
should all lead to an empty output (or an error). Because these cases are not defined according to "my" definition.
Please believe me, I don't want to get on someones nerves with this (seemingly) simple sqrt-problem.
Am 13.02.2011 um 20:53 schrieb Rich Hennessy:
> To satisfy everybody there is the following way. I defined two functions. "|" and nth_roots(), | is a infix operator which performs multiplication when both arguments are lists. Here is the output.
>
> (%i1) n_roots(-3,3);
> 5/6 1/3 5/6 1/3
> 3 %i - 3 3 %i + 3 1/3
> (%o1) [- --------------, --------------, - 3 ]
> 2 2
> (%i2) display2d:false;
> (%o2) false
> (%i3) n_roots(2,2);
> (%o3) [-sqrt(2),sqrt(2)]
> (%i4) n_roots(3,5) | n_roots(-2,3);
>
> (%o4) [-2^(1/3)*3^(1/5),3^(1/5)*(2^(1/3)*sqrt(3)*%i+2^(1/3))/2,-3^(1/5)*(2^(1/3)*sqrt(3)*%i-2^(1/3))/2,-2^(1/3)*3^(1/5)*%e^-(2*%i*%pi/5),3^(1/5)*(2^(1/3)*sqrt(3)*%i+2^(1/3))*%e^-(2*%i*%pi/5)/2,
> -3^(1/5)*(2^(1/3)*sqrt(3)*%i-2^(1/3))*%e^-(2*%i*%pi/5)/2,-2^(1/3)*3^(1/5)*%e^-(4*%i*%pi/5),3^(1/5)*(2^(1/3)*sqrt(3)*%i+2^(1/3))*%e^-(4*%i*%pi/5)/2,
> -3^(1/5)*(2^(1/3)*sqrt(3)*%i-2^(1/3))*%e^-(4*%i*%pi/5)/2,-2^(1/3)*3^(1/5)*%e^(4*%i*%pi/5),3^(1/5)*(2^(1/3)*sqrt(3)*%i+2^(1/3))*%e^(4*%i*%pi/5)/2,-3^(1/5)*(2^(1/3)*sqrt(3)*%i-2^(1/3))*%e^(4*%i*%pi/5)/2,
> -2^(1/3)*3^(1/5)*%e^(2*%i*%pi/5),3^(1/5)*(2^(1/3)*sqrt(3)*%i+2^(1/3))*%e^(2*%i*%pi/5)/2,-3^(1/5)*(2^(1/3)*sqrt(3)*%i-2^(1/3))*%e^(2*%i*%pi/5)/2]
> (%i5)
>
>
> Here is the code. The multiplication or addition of all of the roots of the two numbers can be a very complicated expression. In this case the list length = 5*3 = 15. That is why you need |. You can't overload * directly, I tried but got an error about * is a built in operator. You would need to handle + to when both sides are lists.
>
> Here is the code, it is very simple. It allows for multiple answers to a root calculation and comes very close to the definition that x^(1/n) = solve([y^(1/n)=x], y);
>
> n_roots(__c,__n):=
> block(
> [_x],
> map(lambda([_z], rhs(_z)), solve(_x^__n=__c,_x))
> );
>
> infix("|", 135, 135);
> infix("++", 101,101);
>
> "|"(_l1,_l2):=
> block
> (
> [_ans:[]],
> if listp(_l1) and listp(_l2) then
> (
> for _p in _l1 do (
> for _q in _l2 do (
> _ans : cons(_p*_q, _ans)
> )
> )
> )
> elseif listp(_l1) and not listp(_l2) or listp(_l2) and not listp(_l1) then
> (
> _ans : _l2 * _l1
> )
> else
> (
> _ans : _l2 * _l1
> ),
> _ans
> )$
>
> "++"(_l1,_l2):=
> block
> (
> [_ans:[]],
> if listp(_l1) and listp(_l2) then
> (
> for _p in _l1 do (
> for _q in _l2 do (
> _ans : cons(_p+_q, _ans)
> )
> )
> )
> elseif listp(_l1) and not listp(_l2) or listp(_l2) and not listp(_l1) then
> (
> _ans : _l2 + _l1
> )
> else
> (
> _ans : _l2 + _l1
> ),
> _ans
> )$
>
> This is possible to do in Maxima 5.23.2 so you don't need to worry about sqrt(x^2) = abs(x) if you use n_roots(x^2,2). I am not sure I like the name n_roots(), I was calling it nth_root() at some point but that is not so great either. The multitude of all possible answers is explicit in this case, you can get very long lists when multiplication or addition is involved.
>
> n_roots(x^2,2);
> [- x, x]
>
> Rich
>
> ----Original Message----- From: Richard Fateman
> Sent: Friday, February 11, 2011 10:10 AM
> To: Ren? K?lin
> Cc: maxima at math.utexas.edu
> Subject: Re: [Maxima] sqrt(x)*sqrt(x)
>
> On 2/9/2011 11:50 PM, "Ren? K?lin" wrote:
>> Hello
>>
>> I tried this (with Maxima 5.21.1):
>>
>> domain:real$
>> t:x^(1/2)*x^(1/2);
>> assume(x<0);
>> t;
>> forget(x<0)$
>> assume(x>=0);
>> t;
>>
>> with the output:
>>
>> (%o2) x
>> (%o3) [x<0]
>> (%o4) x
>> (%o6) [x>=0]
>> (%o7) x
>>
>> I only want to consider some real numbers. Because the squareroot of x is not defined for x<0, %o4 is wrong, isn't it?
>>
>> The Problem seems to be that t:x^(1/2)*x^(1/2) is simplified immediately to x in any case.
>>
>> (I sent this post 2 days ago with a wrong title. Sorry if you received it twice.)
> It seems hopeless to point out that x>0 does not mean that sqrt(x)>0,
> mathematically.
> There are 2 square roots. For example sqrt(16) is {-4, 4}, even though
> 16>0.
>
>
> The square root of x, for x<0 IS defined. It just happens to be imaginary.
> There is no simple way of making Maxima forget about complex numbers,
> even if you decide that certain of the inputs represent real numbers.
>
>
> What you seem to be insisting on is
> not that x>0, but that sqrt(x)>=0.
>
> Perhaps you should
> define a new function positive_sqrt(x):= .... if (x<0) then error else
> ....
>
> Yes, I know that in Maxima sqrt(z^2) comes out abs(z), but if you do
> assume(z>0), then abs(z) comes out as z.
>
> This first step, sqrt(z^2) to abs(z) is, in general, wrong, so there
> you are, with the subsequent step propagating the questionable result.
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima