Carl Love

Carl Love

28025 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

I made this a separate Answer because it's a completely different solution to the problem, which doesn't use textplot.

Starting exactly where your original worksheet left off

PrePlot:= %:
dsol:= dsolve(
    {sys11[], z(0)=z0, y(0)=y0}, 
    numeric, parameters= [z0,y0]
):
Dots:= proc(ics)
local k;
    dsol(parameters= eval([z(0),y(0)], ics));
    plot(
        [seq](eval([[z(t), y(t)]], dsol(k)), k= [-1,0,1]),
        color= [green, yellow, red], style= point,
        symbol= soliddiamond, symbolsize= 24, _rest
    )
end proc
:
plots:-display(
    [PrePlot, Dots(ics1, legend= [t=-1, t=0, t=1]), Dots(ics2)]
);

 

Every if statement must end with one of fiend, or end if. Every proc statement must end with either end proc or end. So, you can do this:

Fib:= proc(n)
option remember;
    if n=1 then return 1
    elif n=2 then return 2
    else Fib(n-1)+Fib(n-2);
    fi
end proc;

 

I'll answer the question for another "forked" recursion: computing binomial coefficients from the Pascal's Triangle formula,
C(n,k) = C(n-1, k-1) + C(n-1, k). The first example uses option remember; the second doesn't.

restart:
Bin:= proc(n::nonnegint, k::nonnegint)
option remember;
    if k > n then 0
    elif n=0 or n=k or k=0 then 1
    else thisproc(n-1,k-1) + thisproc(n-1,k)
    fi
end proc
:
CodeTools:-Usage(Bin(120,60));
memory used=0.75MiB, alloc change=0 bytes, cpu time=16.00ms, real time=7.00ms, gc time=0ns
              96614908840363322603893139521372656

restart:
#The only difference is that I commented out the "option remember":
Bin:= proc(n::nonnegint, k::nonnegint)
(* option remember; *)
    if k > n then 0
    elif n=0 or n=k or k=0 then 1
    else thisproc(n-1,k-1) + thisproc(n-1,k)
    fi
end proc
:
CodeTools:-Usage(Bin(25,12)); #Much smaller numbers than above
memory used=1.11GiB, alloc change=0 bytes, cpu time=10.56s, real time=10.71s, gc time=625.00ms
                            5200300

Now, you just need to do the same thing with your procedure Fib.

Note that without option remember, computation of the first 30 Fibonacci numbers will take about 2^30 (~1 billion) procedure calls. So, I recommend that you lower that from 30 to 20 or so.

The command plots:-textplot can be used to plot text at any coordinates, at any angle, and in any color. If that text were an appropriately placed ">", it would look like an arrowhead on the curve.

f:= x-> x^2:
(Xmin,Xmax):= (0, 3):
PrePlot:= plot(f(x), x= Xmin..Xmax, color= red):
(Ymin,Ymax):= (min,max)(op([1,1], PrePlot));
                      Ymin, Ymax := 0., 9.

Angle:= arctan@(D(f)/((Ymax-Ymin)/(Xmax-Xmin))):
plots:-display(
    PrePlot,
    plots:-textplot(
        [seq([k, f(k), ">", rotation= Angle(k)], k= ceil(Xmin)..floor(Xmax))],
        color= red
    )
);

For the angle to work correctly, Ymax-Ymin must be the length of the y-axis and Xmax-Xmin must be the length of the x-axis. The relationship of these quantities to the actual curve is irrelevant.

Your seq could be changed to add or mul, your 1..100 could be changed to 1..77 or 34..58, and your ithprime(i) could be changed to ithprime(i)^2, etc.

It'll be much easier to use calculus than to use Maple's CDF command for this. Indeed, I can do the calculus in my head.

Hint: The integral of any PDF over its support equals 1. (The support of a PDF is the values of its independent variable (in this case) where the PDF is not 0.) So, it can be done with two lines of Maple code: the first uses solve and int to get the value of c. The second uses int and piecewise to get the final answer.

This is usually a sign of a licensing problem. You'll probably need to directly contact Maplesoft's customer support to get it fixed.

One of the basic techniques of computer algebra is to solve the same problem several times over different prime fields and then "lift" those solutions to the integers. There are several algorithms for the lifting. Here, I use the oldest and most well known: Chinese remaindering (see help page ?chrem).

The code below finds an 20-digit (67-bit) value of d in 2.3 seconds. That's larger than wordsize. This only requires 13 primes, all less than 80. It attempts only 619 factorizations over finite fields and 13 irreducs over the integers.
 

restart
:

First, generate a suitably complicated example:

p1,p2:= 'randpoly([x,y], coeffs= rand(-2^33..2^33))'$2;

7292118554*x^2*y^3-6262890291*x^4+551415575*x*y^3-5966975316*x^2*y-6044637241*x*y^2+4637448791, 6492083754*x^3*y^2+1792855492*x^2*y^3+5169280949*x^4-4787552440*x*y^3-3142438976*y^3+7094595294*x*y

Check that both factors are irreducible (or else the problem is trivial):

irreduc(p1), irreduc(p2);

true, true

pp:= expand(p1*p2);

47341044396665371716*x^5*y^5+13073714797853998568*x^4*y^6-40659208311285432414*x^7*y^2+26466552265028799574*x^6*y^3+3579836096160068550*x^4*y^5-33922791533958883860*x^3*y^6-32374639466943366159*x^8-5903765788563875549*x^5*y^3-49940215697038518186*x^4*y^4-10837161074674577572*x^3*y^5-25554968543247613704*x^2*y^6-30844971824152054884*x^6*y-31246428133517221709*x^5*y^2+19680750552850382016*x^4*y^3+80301837210034055916*x^3*y^4+28939017772064418040*x^2*y^5-1732789794853451200*x*y^6-44432671985366890554*x^5*y+22662926145261620466*x^2*y^4+18994903661899505216*x*y^5-12226569040249721490*x^3*y^2-34569979390122633682*x^2*y^3+23972275687279382659*x^4-22202029274727100040*x*y^3-14572899830042478016*y^3+32900822368794589554*x*y

Check for integer factors:

content(pp);

1

Select a coefficient to become d:

m:= max(coeffs(pp));

80301837210034055916

p:= subs(m=d, pp);

47341044396665371716*x^5*y^5+13073714797853998568*x^4*y^6-40659208311285432414*x^7*y^2+26466552265028799574*x^6*y^3+3579836096160068550*x^4*y^5-33922791533958883860*x^3*y^6+d*x^3*y^4-32374639466943366159*x^8-5903765788563875549*x^5*y^3-49940215697038518186*x^4*y^4-10837161074674577572*x^3*y^5-25554968543247613704*x^2*y^6-30844971824152054884*x^6*y-31246428133517221709*x^5*y^2+19680750552850382016*x^4*y^3+28939017772064418040*x^2*y^5-1732789794853451200*x*y^6-44432671985366890554*x^5*y+22662926145261620466*x^2*y^4+18994903661899505216*x*y^5-12226569040249721490*x^3*y^2-34569979390122633682*x^2*y^3+23972275687279382659*x^4-22202029274727100040*x*y^3-14572899830042478016*y^3+32900822368794589554*x*y

Need this utility procedure for Cartesian products:

CartProdSeq := proc(L::seq(list))
local Seq,i,j;
option `Copyright (C) 2007, Joseph Riel. All rights reserved.`;
    eval([subs(Seq=seq, foldl(Seq
                              , [cat(i,1..nargs)]
                              , seq(cat(i,j)=L[j],j=nargs..1,-1)
                             ))]);
end proc
:

This procedure finds suitable values of d over small prime fields by brute force and uses Chinese remaindering to "lift" the ds to the integers.

Find_d:= proc(p::polynom(integer), d::name)
local
    pf:= 2, Es:= Array(1..0),Em:= Array(1..0), Ms:= Array(1..0),
    Mm:= Array(1..0), C:= {coeffs(p)}, Ep, i, ML, e, dt, EL
;
    `mod`:= mods;
    do
        pf:= nextprime(pf);
        if 0 in C mod pf then next fi;
        Ep:= Array(1..0);
        for i from (1-pf)/2 to (pf-1)/2 do
            if (Factor(eval(p, d= i)) mod pf)::{`*`, `^`} then
                Ep,= i
            fi
        od;
        if numelems(Ep)=0 then
            return "Impossible to factor"
        elif numelems(Ep)=1 then
            Es,= seq(Ep); Ms,= pf
        else
            Em,= [seq(Ep)]; Mm,= pf
        fi;
        ML:= [seq(Ms), seq(Mm)]; EL:= seq(Es);
        for e in CartProdSeq(seq(Em)) do
            dt:= chrem([EL,e[]], ML);
            if not irreduc(eval(p, d= dt)) then
                userinfo(1, Find_d, ML, [EL,seq(Em)]);
                return dt
            fi
        od
    od
end proc
:   

infolevel[Find_d]:= 1:

CodeTools:-Usage(Find_d(p, d));

Find_d: [19 23 29 31 37 41 43 53 59 61 71 73 79] [-2 10 -14 -10 -9 -15 21 21 -19 -9 -10 0 19]

memory used=210.06MiB, alloc change=0 bytes, cpu time=2.30s, real time=2.11s, gc time=421.88ms

80301837210034055916

m;

80301837210034055916

ilog10(m);

19

ilog2(m);

66

 


 

Download ChineseLifting.mw

If the result containing xs were named res, you could do

eval(res, x= x(t))

There are some shorter and cruder possibilities for your particular example, but they won't work for more complicated examples.

When you get a non-smooth implicitplot, the first thing to try is the gridrefine option. It's a positive integer defaulting to 1. Here, I've set gridrefine= 3 for your plot:

The minimum is 0 at a=0; the value of is irrelevant. It's not a local minimum because a=0 is one of the boundaries. This can be confirmed by simple plotting:

plot3d(f, 2..5, -1..0)

An iron-clad proof that this is the minimum will be harder to obtain.

So, the Optimization package was more accurate in this case.

Here is VV's algorithm as a predicate (i.e., returns true or false) procedure. This doesn't rely on the vertices being numeric and tries to do a minimal amount of copying:

IsOuterplanar:= proc(G::Graph)
uses GT= GraphTheory;
    GT:-IsPlanar(GT:-GraphJoin(G, GT:-PathGraph(1)))
end proc
:

 

The cases that don't work via a direct built-in distributive property can be done with map:

e:= a <= b;
map(exp, e);
map(`/`, e, -2);

Caution: This works regardless of whether the results are mathematically valid.

Many of these distributive properties are documented at ?evalapply.
 

Using solve(..., parametric), a definitive programmatic solution can be obtained in Maple 2019:

f:= 2*x^5-x^3*y+2*x^2*y^2-x*y^3+2*y^5;
solve({f, x < 0, y < 0}, parametric);

      [ ]

Note that those brackets won't be empty if either or both inequalities are reversed.

The parametric option only works for polynomial systems and systems that can be easily reduced to polynomials.

I find ColumnGraph difficult to use when you have non-categorical horizontal-axis data (such as real-number measurements), and it becomes clear that this wasn't its intended usage. Instead, it can be done with a regular plot command like this:

plot(
   ((x,y)-> [[x,0],[x,y]])~(yr, sle), 
   thickness= 70, xtickmarks= yr, view= [0..0.35, DEFAULT]
);
 

The colors can be easily changed if you want.

First 109 110 111 112 113 114 115 Last Page 111 of 395