Re: Re: Unexpected "Invalid comparison" error when plotting function defined with a Condition pattern
- To: mathgroup at smc.vnet.net
- Subject: [mg69989] Re: [mg69266] Re: Unexpected "Invalid comparison" error when plotting function defined with a Condition pattern
- From: "Chris Chiasson" <chris at chiasson.name>
- Date: Thu, 28 Sep 2006 06:18:42 -0400 (EDT)
- References: <ed93pr$t4f$1@smc.vnet.net> <200609040847.EAA23453@smc.vnet.net>
This happened because Compile + MachinePrecision numbers generated a complex argument to g, while g didn't have a definition to deal with complex input. By the way, there is some kind of problem in the front end involving multiple output cells per input cell (multiple input expressions without semicolons) and the use of InputForm as the default cell output type. g[ x_/; 0<x]=x; gc= Compile[ {y}, Abs[ g[ 10^y]]] CompiledFunction[{_Real}, {{3, 0, 0}, {3, 0, 1}}, {0, 1, 2, 2, 0}, {{1, 5}, {4, 10, 0}, {89, 264, 2, 0, 0, 3, 0, 0, 4, 0, 0}, {22, g, 4, 0, 0, 4, 0, 1}, {88, 36, 4, 0, 1, 3, 0, 1}, {2}}, Function[{y}, Abs[g[10^y]]], Evaluate] gc[1.] Less::nord: Invalid comparison with 10. + 0. I attempted. CompiledFunction::cfse: Compiled expression g[10. + 0. I] should be a machine-size complex number. CompiledFunction::cfex: External evaluation error at instruction 4; proceeding with uncompiled evaluation. 10. g2[x_Complex]=5; g2[ x_/; 0<x]=x; gc2= Compile[ {y}, Abs[ g2[ 10^y]]] CompiledFunction[{_Real}, {{3, 0, 0}, {3, 0, 1}}, {0, 1, 2, 2, 0}, {{1, 5}, {4, 10, 0}, {89, 264, 2, 0, 0, 3, 0, 0, 4, 0, 0}, {22, g2, 4, 0, 0, 4, 0, 1}, {88, 36, 4, 0, 1, 3, 0, 1}, {2}}, Function[{y}, Abs[g2[10^y]]], Evaluate] gc2[1.] 5. g3[ x_Real/; 0<x]=x; gc3= Compile[ {y}, Abs[ g3[ 10^y]]] CompiledFunction[{_Real}, {{3, 0, 0}, {3, 0, 1}}, {0, 1, 2, 2, 0}, {{1, 5}, {4, 10, 0}, {89, 264, 2, 0, 0, 3, 0, 0, 4, 0, 0}, {22, g3, 4, 0, 0, 4, 0, 1}, {88, 36, 4, 0, 1, 3, 0, 1}, {2}}, Function[{y}, Abs[g3[10^y]]], Evaluate] gc3[1.] CompiledFunction::cfse: Compiled expression g3[10. + 0. I] should be a machine-size complex number. CompiledFunction::cfex: External evaluation error at instruction 4; proceeding with uncompiled evaluation. 10. On 9/4/06, Andrew Moylan <andrew.j.moylan at gmail.com> wrote: > Hi David, thanks for your reply. For now I'm using a workaround like > one of yours below. > > David Park wrote: > > Andrew, > > > > I do not know precisely why you obtain those error messages. It seems to be > > a combination of selecting values in Plot, the use of Abs and the form of > > the definition of g. But I think there are ways around it. > > > > The following give error messages. > > > > g[x_ /; 0 < x] = x; > > Plot[Abs[g[10^y]], {y, 1, 2}]; > > > > Clear[g] > > g[x_ ] = If[x > 0, x, 0]; > > Plot[Abs[g[10^y]], {y, 1, 2}]; > > > > Clear[g] > > g[x_] := Piecewise[{{0, x <= 0}, {x, x > 0} }] > > Plot[Abs[g[10^y]], {y, 1, 2}]; > > > > The following do not give error messages. > > > > Clear[g] > > g[x_ /; 0 < x] = x; > > Plot[Sqrt[g[10^y]^2], {y, 1, 2}]; > > > > Clear[g] > > g[x_ /; 0 < x] = x; > > Plot[Abs[g[10^y]] // ComplexExpand // Evaluate, {y, 1, 2}]; > > > > Clear[g] > > g[x_ /; 0 < x] = x; > > data = Table[{y, Abs[g[10^y]]}, {y, 1, 2, 0.025}]; > > ListPlot[data, PlotJoined -> True]; > > > > Clear[g] > > g[x_?Positive] = x; > > Plot[Abs[g[10^y]], {y, 1, 2}]; > > > > Clear[g] > > g[x_] := Piecewise[{{0, x <= 0}, {x, x > 0} }] > > Plot[Abs[g[10^y]] // ComplexExpand // Evaluate, {y, 1, 2}]; > > > > So try using ComplexExpand on your more complicated expression. > > > > David Park > > djmp at earthlink.net > > http://home.earthlink.net/~djmp/ > > > > > > > > From: Andrew Moylan [mailto:andrew.j.moylan at gmail.com] To: mathgroup at smc.vnet.net > To: mathgroup at smc.vnet.net > > > > Hi, > > > > Please consider the following simple function, defined for positive x > > using a Condition pattern: > > > > g[x_ /; 0 < x] = x; > > > > When I try to plot this function in the following way: > > > > Plot[Abs[g[10^y]], {y, 1, 2}]; > > > > I unexpectedly receive some "Less::nord : Invalid comparison with 10. + > > 0. I attempted" errors. Can anyone explain why this is, and how I > > should best prevent it? > > > > Relevant note: > > Both of the following different plots of g succeed with no errors: > > Plot[Abs[g[y]], {y, 1, 2}]; > > Plot[g[10^y], {y, 1, 2}]; > > > > Irrelevant note: > > In my actual application, the function g is more complicated and is > > complex-valued (hence my use of Abs); but the error is reproducable > > with the very simple real-valued function g as defined above. > > > > Thanks for any insight you might be able to give me on this. > > > > Cheers, > > > > Andrew > > -- http://chris.chiasson.name/
- References:
- Re: Unexpected "Invalid comparison" error when plotting function defined with a Condition pattern
- From: "Andrew Moylan" <andrew.j.moylan@gmail.com>
- Re: Unexpected "Invalid comparison" error when plotting function defined with a Condition pattern