Carl Love

Carl Love

28035 Reputation

25 Badges

12 years, 318 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

You should make it a procedure. In doing so, you can substantially shorten and simplify the code and substantially increase its functionality at the same time.

The increased functionality comes from loosening the definition of "equals" and adding flexibility to the definition of "less than". In the example below, I consider floats to be equal if they match to a certain number of decimal places; and I exchange the roles of `<` and `>`. 

Maple's "neutral operator" syntax makes it possible to add this functionality with adding only 2 characters to the body of the procedure.
 

restart:

BinarySearch:= proc(
    AA::And(rtable, satisfies(rtable_num_dims=1)), KEYVALUE, #item to look for
    {`&<`::appliable:= :-`<`, `&=`::appliable:= :-`=`} #optional arguments
)
local low:= lowerbound(AA), high:= upperbound(AA), mid;
    while low <= high do
        mid:= iquo(low+high, 2);
        if AA[mid] &= KEYVALUE then return mid fi;
        if AA[mid] &< KEYVALUE then low:= mid+1 else high:= mid-1 fi       
    od;
    false
end proc
:

R:= rand(-1.0..1.0):
A:= Statistics:-Sample(Uniform(-1,1), 10^6):

A:= sort(A, `>`): #i.e., in descending order

x:= R();

-.735125654

BinarySearch(A, x, `&<`= `>`, `&=`= ((x,y)-> abs(x-y) < .5e-5));

868278

A[%];

HFloat(-0.7351272592187323)

x:= R();

.681623130

BinarySearch(A, x, `&<`= `>`, `&=`= ((x,y)-> abs(x-y) < .5e-6));

159450

A[%];

HFloat(0.6816232246082847)

x:= R();

-.893854871

BinarySearch(A, x, `&<`= `>`, `&=`= ((x,y)-> abs(x-y) < .5e-6));

false

 

``


 

Download BinSearch.mw

 

Please use a bit more discrimination when deciding whether to make something a Post or a Question. The latter are far more common.

I tried your code up until your first error message. It works fine for me. You've not made any syntax or mathematical errors (at least up to that point). The error messages to me suggest (I'm just guessing) some sort of very weird, and serious, configuration problem. Note that the error messages spell out in some extremely verbose form exactly what you typed in: comma, lpar = left parenthesis, verbar = vertical bar, etc. I've never seen anything like it.

Are you able to perform any simple calculations with your Maple?

@permanoon123 While method= _NCrule does substantially increase the speed, its accuracy is unacceptable for this particular integral. 

@paulmcquad You had a Post (which I changed to a Question) requesting a symbol-size menu option (or perhaps you call it "bullet size" or "point size"). I guess that Post/Question got deleted. Anyway, the menu option already exists: Bring up the plot context menu, then select Symbol (7th item down in my version), then Symbol Size (bottom item), then enter a number.

Alternatively you can use the option symbolsize= in a plotting command, as shown in my Answer.

Note that my Answers (almost) always give the complete plaintext code needed to produce the results that I show, without any menu commands needed. That's because I like having reproducible results. 

@paulmcquad The equation of the perpendicular line that passes through (2, -1) is y = -7/3 + 2/3*x, not -5/3 + 2/3*x. Check your work on that, or do it with Maple (as I showed in my Answer).

In preparing my Answer, I misread the given point as (2,1) rather than (2,-1). However, the techniques of that Answer still apply; just change the point.

The best addition that you could make to your plot is adding option scaling= constrained. This makes the unit of measure on the two axes the same, thus perpendicular lines will actually appear perpendicular.

@goli Sometimes the roots of a polynomial can be expressed using sin and cos even though they can't be expressed in radicals. For example, it's easy to see that the 13 roots of x^13 - 1 are

[seq(cos(2*Pi/13*k)+I*sin(2*Pi/13*k), k= 0..12)]

although I believe that there's provably no way to put those in radical form (if I'm wrong about that, there are definitely larger denominators for which it is provably impossible).

@goli The outermost RootOf expression is degree 3, so there can't be a root with index=4. Each index applies only to the specific RootOf that it's an argument of, not to the overall system.

@goli When you use explicit, the solutions are expressed in radical form rather than RootOf form where that is possible, which usually means degree-4 or less. But because your original equations have a variable under a square root, not all of these explicit solutions actually satisfy the original system. Because of your parameter l, it cannot tell which of the solutions are the correct ones, (i.e., which satisfy the original equations). When the solutions are in RootOf ​​​​​​form, the use of index is an attempt to specify which solutions actually satisfy them. 

In my opinion, this index stuff for parameterized solutions is not totally trustworthy, and when the parameter is given a numeric variable it is wise to check all the numeric solutions in the original equations.

@goli 

For a system of polynomial equations of degree higher than 1, eliminating variables tends to raise the degree of the remaining equations. This is why you got four roots. Here's a simple example of a pair of quadratic (i.e., degree 2) equations where you can easily eliminate a variable "by hand":

x^2 + x           + y       = 1, 
      x +   y^2 +       x*y = 2

You can solve the first equation for y:
     y = 1 - x^2 - x.

Substitute for y in the second equation:
     x + (1-x^2-x)^2 + x*(1-x^2-x) = 2.

Now you have a degree-4 equation. Since the original equations are polynomial (no radicals), all 4 roots of that yield valid solutions of the system.

That's the algebraic answer. The same thing can be easily understood geometrically. We know that ellipses can be described by degree-2 equations in two variables. The solutions of a pair of bivariate equations are the intersections of the corresponding curves. So, there are four solutions to the following pair:

plots:-implicitplot([x^2 + 2*y^2 = 1, 2*x^2 + y^2 = 1], x= -2..2, y= -2..2);

Here's more bells & whistles. The command plots:-textplot can be used to place formatted, rotated, colored text directly in the plot. This might be what you meant by "add bullet points to this plot". So here's the same plot with annotations of the 4 objects.

L:= y = -3/2*x + 2: Pt:= [2,1]:
m:= -1/implicitdiff(L, y, x):
Lperp:= y = m*(x-Pt[1]) + Pt[2]:
PtInt:= eval([x,y], solve({L, Lperp})):
plots:-display(
    plot(
        #Options specific to plotted objects:
        [rhs(L), rhs(Lperp), [PtInt], [Pt]], 
        x= -5..5, view= [-5..5, -5..5],
        style= [line$2, point$2], color= [red, "DarkGreen", black, red],
        symbolsize= 18, symbol= [solidcircle, soliddiamond],
        thickness= 0, 
        legend= [
            typeset(L, `\t`), 
            typeset(Lperp, ` `, `#mo(&perp;)`,`\t`),
            typeset(`\n`, PtInt, `\n\t\t`),
            Pt
        ],
        #options specific to axes:
        labels= [x,y],
        axesfont= [Helvetica, Bold, 10],
        #options for whole plot: 
        caption= "\nExample 7: The given information is in red.", 
        captionfont= [Times, 18],
        title= "Perpendicular Lines\n", titlefont= [Helvetica, Bold],
        legendstyle= [location= bottom, font= [Times, Bold, 14]],
        scaling= constrained, size= [900$2]
    ),
    plots:-textplot(
        [
            [
                Pt[1]+.6, Pt[2]+1, 
                typeset(`#mo(&perp;)`, ` `, Lperp), 
                rotation= arctan(m), 
                align= {above, right}, color= "DarkGreen"
            ],
            [Pt[], typeset(`  `, Pt), align= right, color= red],
            [
                PtInt[], typeset(`     `, PtInt), align= right, 
                font= [Times, Bold, 10]
            ],
            [
                -2, eval(rhs(L), x= -2) - 1, typeset(L), color= red,
                rotation= arctan(-1/m)
            ]
        ],
        font= [Times, Bold, 14]      
    )
);

@mmcdara I wouldn't consider these usages of to be neutral operators. These symbols have no meaning to the kernel as operators, whereas &*, etc., do. But an appropriate help page for this would be ?symbol, and a link to that should appear on ?&.

@permanoon123 You'll learn much better by typing it in yourself.

I have a feeling that your mathematical abilities are at a significantly higher level than the book that you're using. What grade are you in, and what math, statistics, or computer science courses are you currently taking in school?

@Kitonum I should've used realcons instead of numeric to catch symbolic constants like your Pi:

plots:-display(
    [seq](
        plot3d(0, op([1,2],j), op(2,j)),
        j= indets(J, Int(Int(algebraic, name= range(algebraic)), name= range(realcons)))
    ),
    orientation= [180, 0, 180]
);

Now this will handle your most-recent case as well as the earlier VectorCalculus cases.

I'll admit that there was no good reason for me to use numeric; it was just my mistake.

First 217 218 219 220 221 222 223 Last Page 219 of 708