Negative numbers and non-integer exponents

4 views
Skip to first unread message

Shem

unread,
Feb 19, 2009, 5:12:42 PM2/19/09
to amrita-ebook
I am trying to plot half a parabola as a boundary however part of the
equation includes the term
(R - (Q^3 + R^2)^(1/2))^(1/3)

I tried

test = (-27)**(1/3)
.
.
.
printf("%d\n", $test)

and 0 is returned. If I remove the negative a 3 is returned.
What function can I use so that it would return the correct value? I
know C++ has the same problem with the pow function.

Thanks

michael

unread,
Feb 19, 2009, 5:43:40 PM2/19/09
to amrita...@googlegroups.com
you should do the calculations in the complex domain. if 27 is a complex
number declared by say numb=27 and numb is complex then it will give an
answer but the answer is complex

James Quirk

unread,
Feb 19, 2009, 6:29:41 PM2/19/09
to Shem, amrita-ebook

Michael is quite right that the cube root of a negative number
involves complex numbers. However, that is not the solution
to your problem. First off, the script fragment:

test = (-27)**(1/3)


printf("%d\n", $test)

is invalid, for AMRITA will try to run a procedure named "test."

The keyword "set" is used to assign to a string token.
But if you'd written:

set test = (-27)**(1/3)


printf("%d\n", $test)

you would actually have run:

printf("%d\n", (-27)**(1/3))

as a plain set assigns a string. To force an evaluation of the
RHS you would need to use:

set test #= (-27)**(1/3)


printf("%d\n", $test)

For details, see:

amrhelp set

Now to return to your question, the following draws
a half-parabola, in red, on the computational domain:

plugin amr_sol
EulerEquations
def Domain
patch <1,1,100,100>
end def

postscript on
plotfile ps/tmp.ps
autoscale

set xo = 20
set a = 5

p ::= sqrt(4*$a*(X[]-$xo))
r ::= Y[]<p[] ? 1 : -1
g ::= Y[]<p[] ? 0 : -1
b ::= Y[]<p[] ? 0 : -1


plot image rgb<r[],g[],b[]>
plot domain

It takes advantage of the fact that the plot image operator
treats a negative component as a transparent colour. The
downside of the above is that the parabola will be drawn
stepped. If that's a problem you can use the polygon
operator to draw a smooth parabola. For details, see:

amrhelp polygon

or you can use the stroke facility:

amrhelp stroke

In any event, you should recast your parabola in parametric
form thereby doing away with the need to take roots.

James

Reply all
Reply to author
Forward
0 new messages