Carl Love

Carl Love

28025 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

Please see my more-detailed Answer to your related Question. Addressing your Question here: p is a local variable of procedure quicksort. Thus there is a separate copy of p for each invocation of quicksort. The value of this local copy cannot change between the two recursive calls to quicksort. Maple is essentially the same as any other modern programming language in this regard.

You can use an inert %piecewise with empty strings for the conditions. Like this:



restart:

%piecewise(``, 3*x+2*y=7, ``, x+4*y= 3);

"`%piecewise`(,3 x+2 y=7,,x+4 y=3)"



Download Inert_piecewise.mw

 

Since your program works correctly, it is difficult for me to comment. But here is a way to increase your understanding of the program so that you can answer your own question. Rather than dealing with the overwhelming amount of redundant information provided by trace, use userinfo statements to display useful information. Also, indent your code. Here is your code indented and instrumented with userinfo statements. I included kernelopts(level) so that you can see the recursion depth: 23 is the first level, 49 the second, and 75 the third.

I also changed your array to a Vector since array is deprecated (no longer used in new code).

quicksort:= proc(A::Vector(numeric), m::nonnegint, n::nonnegint)
local
     partition:= proc(m::nonnegint, n::nonnegint)
     local i:= m, j:= n, x:= A[n];
          while i < j do
               if A[i] > x then
                    A[j]:= A[i];
                    j:= j-1;
                    A[i]:= A[j]
               else
                    i:= i+1
               end if
          end do;
          A[j]:= x;
          j
     end proc,
     p,k
;
     userinfo(
          6, quicksort, NoName,
          sprintf(
               "A = [%d], m=%d, n=%d, level=%d",
               A, m, n, kernelopts(level)
          )
     );

     if m < n then
          p:= partition(m,n);
          userinfo(6, quicksort, NoName, "p =", p);
          quicksort(A,m,p-1);
          quicksort(A,p+1,n)
     end if;
     A
end proc:

infolevel[quicksort]:= 6:
a:= < 2,4,1,5,3 >^%T:
quicksort(a,1,5);

A = [2 4 1 5 3], m=1, n=5, level=23
p = 3
A = [2 1 3 5 4], m=1, n=2, level=49
p = 1
A = [1 2 3 5 4], m=1, n=0, level=75
A = [1 2 3 5 4], m=2, n=2, level=75
A = [1 2 3 5 4], m=4, n=5, level=49
p = 4
A = [1 2 3 4 5], m=4, n=3, level=75
A = [1 2 3 4 5], m=5, n=5, level=75
           

That should be

f1:= eval(a1(t), sol1);

not f1(t).

Here I do the 3d plots using dsolve's parameters option. Then I paste separate plots of x(t) and y(t) together with display.

# System of two equations with a parameter:
Sol:= dsolve(
     {diff(x(t),t) = k, x(0) = 1, diff(y(t),t) = k, y(0) = -1},
     numeric, parameters= [k]
);

# x(t) and y(t) are necessarily computed at the same time. The
# procedure SOL is to avoid redoing those computations when they
# are plotted separately.
SOL:= proc(k,t)
option remember;
     Sol(parameters= [k]);
     Sol(t)
end proc;

X:= (k,T)-> eval(x(t), SOL(k,T)):
Y:= (k,T)-> eval(y(t), SOL(k,T)):

# Plot X and Y individually. Save plots to variables.
P1:= plot3d(X, -2..2, -2..2):
P2:= plot3d(Y, -2..2, -2..2):

# Piece together plots with `display`.
plots:-display([P1,P2], axes= boxed);

I have a feeling that this is not exactly what you want, but it may be the best that is possible: Use Print Preview from the File menu.

Here's an example where I compute the order of convergence of Newton's method applied to a specific function.

Newton:= proc(
     f::operator,  #function in operator form
     x0::complexcons, #initial guess of root r such that f(r)=0
     n::posint  #number of iterations
)
local
    x, N:= unapply(simplify(x - f(x)/D(f)(x)), x),
    R:= Vector(n, [N(x0)]),  
    k
;
    for k from 2 to n do
         R[k]:= N(R[k-1])
    end do;
    R #return vector of iterates
end proc:
 
#Example function:
f:= x-> x^3-1:
true_root:= 1:
Digits:= 100:
n:= 8:
R:= Newton(f, .5, n):
#Compute the logs of the absolute errors.
lnE:= map(x-> ln(abs(x-true_root)), R):
Digits:= 15:
seq(lnE[k+1]/lnE[k], k= 1..n-1);

The sequence above appears to be converging to 2. That's the order of convergence.

According to ?abs :

The derivative of abs is denoted by abs(1, x). This is signum(x) for all non-0 real numbers, and is undefined otherwise.

Therefore,

D(conjugate)(x) assuming x::real;

                   1

Maple does it this way so that the derivatives of expressions involving conjugate will simplify, as above, when appropriate assumptions are made.

 

 

 

 

I did it for the first five of your points. The idea is the same for more points. This goes through the color spectrum from red to blue, so it passes through orange, yellow, and green also.

Pts:= [[.1733, .0048], [.1726, .0048], [.1714, .0051],
[.1689, .0069], [.1644, .0109]]:
N:= nops(Pts):
plots:-pointplot(Pts, color= [seq(COLOR(HUE, (k-1)*.65/N), k= 1..N)]);

This one makes a direct transition from red ro blue, so it goes through shades of purple:

plots:-pointplot(Pts, color= [seq(COLOR(RGB, (N-k)/N, 0, k/N), k= 1..N)]);

The problem with colorstyle= HUE cycling back to red (which Acer discussed in his lengthy Reply) can be easily overcome by rescaling the colors after the PLOT structure has been generated.

Mandelbrot:= proc(x,y)
local n,c,z;
     c:= x+I*y;  z:= c;
     for n to 48 while abs(z) < 2 do  z:= z^2+c  end do;
     n
end proc:

P:= plots:-densityplot(
     Mandelbrot, -2.1..0.6, -1.2..1.2, grid= [500,500],
     colorstyle= HUE,
     axes= boxed, scaling= constrained,
     style= patchnogrid,
     labels= [Re,Im], labelfont= [HELVETICA,ROMAN,24],
     size= [500,500]  #Maple 18 only
):

#Rescale the colors:
applyop(`*`, [1,4,2], P, .85);

Remove the size option if you aren't using Maple 18. Use size=[1000,1000] for a better view in a worksheet.

A more complicated rescaling can be used. For example, one could stretch out the visually optimal green range.

The only reference that I used to write my code is the last example at ?densityplot , which does a Julia set. I am sorry that it is similar to someone else's code. It is rather obvious if you understand the Mandelbrot set and how densityplot works.

This surface is smooth and approximates the points.

A:= `<|>`(v||(1..6), w||(1..3), x1, x2, y1):
plots:-display(
     Statistics:-ScatterPlot3D(A^%T, lowess, bandwidth=2, showpoints= false, transparency= .3),
     plots:-pointplot3d(A^%T, symbol= solidsphere, color= red, symbolsize= 16)
);

Condensing Acer's ideas into a simple procedure, I get this:

FactorGroup:= proc(G::GroupTheory:-Group, N::satisfies(N-> GroupTheory:-IsNormal(N,G)))
# Returns the group G/N.
uses GT= GroupTheory;
     GT:-CustomGroup(
          GT:-LeftCosets(N,G),
          `.`= ((g1,g2)-> GT:-LeftCoset(Representative(g1) . Representative(g2), N)),
          `/`= (g-> GT:-LeftCoset(Representative(g)^(-1), N))
          `=`= ((g1,g2)-> member(Representative(g1) . Representative(g2)^(-1), N))
     )
end proc:

Example:

G:= Alt(4):
H:= SylowSubgroup(2,G):
GH:= FactorGroup(G,H);

GroupOrder(GH);

3

Edit: Procedure updated to reflect Acer's correction to a bug discussed below.

The reason that you can't change the corner entries is that doing so makes the Matrix no longer circulant---it violates the indexing function of the Matrix.

Your Matrix is equivalent to a symmetric band matrix:

N:= 8:
A:= LinearAlgebra:-BandMatrix([[-1$N-1], [2$N]], shape= symmetric);

 

Use a local declaration in your procedure, like this:

My_Proc:= proc(...parameter declarations...)
local L, P, V;

   ...procedure statements...

end proc:

It can be done like this:

Plex:= < u | v | w | x | y | z >;

for i from 2 to 5 do Plex:= Plex[[i, 1..i-1, i+1..-1]] end do;

 

First 303 304 305 306 307 308 309 Last Page 305 of 395