Carl Love

Carl Love

28055 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

When the number of equations is different from the number of variables being solved for, it's usually a bad idea to use solve. Use eliminate instead. The syntax is the same; just change solve to eliminate.

eliminate(
     [a = -(-y+1)/(x-y+2),
      b = -(-x^2+2*x*y-y^2-3*x+3*y-2)/(x-2*y+3),
      c = -(x*y-y^2-2*x+4*y-4)/(x*y-y^2-x+2*y-1),
      d = -(-x*y+y^2+x-2*y+1)/(x-2*y+3)
      ],
[x,y]
);

The second set is expressions that equal 0. So you can think of it as providing solutions for b and c.

You removed your code from your Question. That isn't a good sign.

Anyway, here is a very fast way to generate a large number of your random walks using directions sampled from your probability distribution.

 

restart:


Dirs:= Statistics:-Distribution(PDF= (phi-> cos(phi/2)/4)):
n:= 2^9: #walk length
N:= 2^4: #number of walks
DirBuffer:= Matrix((N,n), datatype= float[8]):
Walks:= Array((1..N,1..n,1..2), datatype= float[8]):


FillBuffer:= proc(B::rtable)
     Statistics:-Sample(Dirs, B, method= [envelope, range= -Pi..Pi]);
     [][]
end proc:


DoWalks_hf:= proc(W::Array, B::Matrix, n::posint, N::posint)
local k,K,phi;
     for K to N do
          W[K,1,1]:= 0:  W[K,1,2]:= 0:
          for k from 2 to n do
               phi:= B[K,k];
               W[K,k,1]:= W[K,k-1,1] + cos(phi);
               W[K,k,2]:= W[K,k-1,2] + sin(phi)
          end do
     end do
end proc:

DoWalks:= proc(W::Array, B::Matrix)
local n, N;
     FillBuffer(B);
     (N,n):= op(1,B);
     evalhf(DoWalks_hf(W, B, n, N));
     [][]
end proc:
     

CodeTools:-Usage(DoWalks(Walks, DirBuffer));

memory used=1.98MiB, alloc change=8.02MiB, cpu time=109.00ms, real time=111.00ms

 

(r,c):= trunc(sqrt(N))$2:
plots:-display(
     Matrix((r,c), (i,j)-> plot(<Walks[(i-1)*c+j,..]>)),
     thickness= 0, gridlines= false
);

 

Download RandDirWalk.mw

Array plots won't display in MaplePrimes, but you'll see in the worksheet that every walk is plotted on its own set of axes.

Regarding your followup question where you want to form the conjugate i*j*i^(-1) for every pair of elements i, j in A: It can be done like this:

B:= {seq(seq(PermConjugate(i,j), i= A), j= A)};

This computation takes about a minute because there are 705600 pairs. The end result is simply the set again (no surprise).

 

You can essentially enter them as in the book, and then make some substitutions: y to y(x), dy to diff(y(x), x) and dx to 1. Also, I've never seen arctg or tg. I just changed these immediately to arctan and tan, respectively.


13.1

eq:= sqrt(1-y^2)*dx+y*sqrt(1-x^2)*dy = 0;

(1-y^2)^(1/2)*dx+y*(1-x^2)^(1/2)*dy = 0

(1)

dSubs:= [dy= diff(y(x),x), dx= 1, y= y(x)];

[dy = diff(y(x), x), dx = 1, y = y(x)]

(2)

eq1:= eval(eq, dSubs);

(1-y(x)^2)^(1/2)+y(x)*(1-x^2)^(1/2)*(diff(y(x), x)) = 0

(3)

dsolve(eq1);

arcsin(x)+(y(x)-1)*(y(x)+1)/(1-y(x)^2)^(1/2)+_C1 = 0

(4)

13.2

eq:= x*y*dx+(x+1)*dy = 0;

x*y*dx+(x+1)*dy = 0

(5)

eq1:= eval(eq, dSubs);

x*y(x)+(x+1)*(diff(y(x), x)) = 0

(6)

dsolve(eq1);

y(x) = _C1*(exp(-x)*x+exp(-x))

(7)

13.3

eq:= x*(y^2+1)*dx+cos(x)^2*dy=0;

x*(y^2+1)*dx+cos(x)^2*dy = 0

(8)

eq1:= eval(eq, dSubs);

x*(y(x)^2+1)+cos(x)^2*(diff(y(x), x)) = 0

(9)

dsolve(eq1);

y(x) = -tan(x*tan(x)+ln(cos(x))+_C1)

(10)

13.4

eq:= x*y*arctan(x)*dx+(y^2+1)*dy=0;

x*y*arctan(x)*dx+(y^2+1)*dy = 0

(11)

eq1:= eval(eq, dSubs);

x*y(x)*arctan(x)+(y(x)^2+1)*(diff(y(x), x)) = 0

(12)

dsolve(eq1);

y(x) = 1/(1/LambertW(_C1*exp(-arctan(x)*x^2+x-arctan(x))))^(1/2)

(13)

dsolve(eq1, implicit);

(1/2)*arctan(x)*x^2-(1/2)*x+(1/2)*arctan(x)+(1/2)*y(x)^2+ln(y(x))+_C1 = 0

(14)

13.5

eq:= exp(x-y)*dy-dx = 0;

exp(x-y)*dy-dx = 0

(15)

eq1:= eval(eq, dSubs);

exp(x-y(x))*(diff(y(x), x))-1 = 0

(16)

dsolve(eq1);

y(x) = -ln(1-_C1*exp(x))+x

(17)

13.6

eq:= dy/dx = tan(y/x)+y/x;

dy/dx = tan(y/x)+y/x

(18)

eq1:= eval(eq, dSubs);

diff(y(x), x) = tan(y(x)/x)+y(x)/x

(19)

dsolve(eq1);

y(x) = arcsin(x*_C1)*x

(20)

 


Download 6_ODEs.mw

The correct syntax for the select command is

A:= select(g-> PermOrder(g)=4, H);

Think of it as having this English translation: "Select g such that PermOrder(g)=4 from H."

It's never allowed for an indexed name such as H[i] to be a left-side operand of ->i.e., a parameter. That was the proximate cause of your error message.

I posted code for extracting the extrema from plots, so you're not "on your own" as Mac Dude says. The code can be found here:

http://www.mapleprimes.com/questions/206660-Maximum-Point-Of-Plot#answer221288

The commands that are (guaranteed to be) thread safe are listed in the help page ?index,threadsafe. Unfortunately, it's a very small list.

Detecting whether a large number is prime is much, much easier computationally than factoring a product of two medium-sized primes. This fact is the basis of most modern cryptography systems. (There is no known theoretical reason for this fact---it may just be the current state of the known algorithms.)

restart:
p:= nextprime(2^99):
q:= nextprime(p):
n:= q*p:
CodeTools:-Usage(ifactor(n));

memory used=313.25MiB, alloc change=164.03MiB, cpu time=9.28s, real time=9.36s
(633825300114114700748351602943) (633825300114114700748351603131)

p:= nextprime(2^999):
%-2^999;

1239

restart:
p:= 2^999+1239:

CodeTools:-Usage(isprime(p));

memory used=3.33MiB, alloc change=8.00MiB, cpu time=32.00ms, real time=39.00ms
true

Note that the second number has five times as many digits as the first!

So, if n is a product of several small, easy-to-find factors and one large prime, then ifactor knows to stop after finding the small factors because it can quickly detect that the remaining factor is prime.

(A side comment on your timing technique: It's more accurate to include the timing commands and the code being timed in a single execution group.)

Regarding Christopher's Answer: There's no need for you to be forced to accept seq's default limitation to integer values of m. Any number of a values of in the range 1..2 (or in any range) can be accomodated like this:

linspace:= (ab::range, n::posint)-> [seq(op(1,ab)-`-`(op(ab))/(n-1)*_k, _k= 0..n-1)]:plots:-display(seq(plot(sin(m*x), x= -Pi..Pi), m= linspace(1..2, 9)));

The above plot uses 9 values of m.

Regarding Kitonum's Answer: I wouldn't be so quick to reject the idea of a 3-d plot. If contructed properly and viewed from a certain angle, it'll look like the 2-d plot that you want. I think that the continuous color gradation used in 3-d plots improves this particular plot:

plot3d(
     sin(m*x), m= 1..2, x= -Pi..Pi,
     style= wireframe, thickness= 2,
     orientation= [0,90], axes= normal, labels= [``, x, y]
);

The aboves uses plot3d's default of 49 values of m. That can be controlled with the grid option.

Here are six more easy (one-line) methods to plot the ellipse, all starting with the defining expression

f:= (x,y)-> 3*x^2-3*x*y+6*y^2-6*x+7*y-9;

All of these methods produce fine plots without the need for increasing the default number of points plotted.

1. The command algcurves[plot_real_curve]  produces very accurate plots of bivariate polynomial equations (of genus 0).

algcurves[plot_real_curve](f(x,y), x, y);

Note that this command does all the work. In particular, it computes the appropriate ranges for x and y.

2. The ellipse is the intersection of the surfaces z = f(x,y) and z = 0, so the command intersectplot can be used.

plots:-intersectplot(f(x,y), 0, x= -2..4, y= -2..2, orientation= [270,0], axes= normal);

This command shows just the xy-plane because of the orientation option.

3. The ellipse is a level curve of f(x,y), so a contourplot showing just one contour can be used.

plots:-contourplot(f(x,y), x= -2..4, y= -2..2, contours= [0]);

4. A slight variation of the above is contourplot3d, which is faster due to (I believe) some externally compiled processing.

plots:-contourplot3d(f(x,y), x= -2..4, y= -2..2, contours= [0], orientation= [270,0], axes= normal);


The next two methods first perform some algebraic manipulation of the expression before plotting. Neither of these require knowledge or specification of ranges for x and/or y.

5. Make a conversion to polar coordinates.

plot(solve(subs([x,y]=~ r*~[cos,sin](t), f(x,y)), r)[1], t= -Pi..Pi, coords= polar);

The [1] is to select one of the two solutions for r. It doesn't matter which is used.

6. Use a parametrization in Cartesian coordinates.

plot([eval(algcurves[parametrization](f(x,y), x, y, t), t= tan(u))[], u= -Pi/2..Pi/2]);

The tangent substitution is needed because parametrization returns a parametrization with domain t= -infinity..infinity.

Here are some slight improvements. Since this is a fundamental algorithm (see the Wikipedia article "Exponentiation by squaring"), small improvements can add up to significant savings. In this version, by using the three-argument form of irem I avoid the recomputation of the quotient n/2, and by using thisproc for recursion I avoid the name evaluation of using Puis.

Puis:= proc(X, n::integer)
option remember;
local q,r;
     if n = 0 then 1
     elif n = 1 then X
     elif n < 0 then thisproc(1/X, -n)
     else
          r:= irem(n,2,q);
          `if`(r=1,X,1)*thisproc(X*X, q);
     end if
end proc;

Here's an efficient and non-recursive way to do it that still uses the definition, in a way:

fib:= (n::nonnegint)-> (<1,1;1,0>^n)[1,2]:
fib(101);

     573147844013817084101

Any constant-coefficient linear recurrence can be computed this way---by taking a matrix power.

FWIW, here's my rational roots procedure:

RationalRoots:= proc(f::And(polynom(integer), satisfies(f-> nops(indets(f,name))=1)))
uses NT= numtheory;
local x:= indets(f,name)[], p, q;
     select(
          r-> eval(f, x= r) = 0,      
          {0, seq(seq([p/q, -p/q][], p= NT:-divisors(tcoeff(f))), q= NT:-divisors(lcoeff(f)))}
     )
end proc:

f:= x-> piecewise(x<0, x^2-2*x+1, x>0, 1-x, undefined);

Why do you say "Piecewise function in Maple are continuous function"?

FWIW, here's my somewhat minimalist Jacobi method procedure. I simply used the Wikipedia article "Jacobi method" and modified the algorithm to use Maple's vector-mode operations. I use two convergence criteria: the magnitudes of both the error and the residual need to be less than epsilon.

Jacobi:= proc(A::Matrix, b::Vector, x0::Vector, epsilon::positive, m::posint)  
uses LA= LinearAlgebra;  
local
     k,
     x:= Vector(x0),
     p:= Vector(op(1,x)),
     D:= Matrix(A, shape= diagonal),
     R:= A-D,
     B:= Vector(b)
;  
     D:= D^(-1);  B:= D.B;  R:= D.R;
     for k to m while LA:-Norm(A.x-b) > epsilon or LA:-Norm(x-p) > epsilon do
          p:= x;
          x:= B-R.p      
     end do;
     if k > m then WARNING("Max iterations reached.") end if;    
     x  
end proc:

Example of use:

n:= 3:
A:= LinearAlgebra:-RandomMatrix(n):
#Force A to be strictly diagonally dominant:
A:= A^+.A:
A:= Matrix(op(1,A), (i,j)-> `if`(i=j, n, 1)*A[i,j]):
b:= LinearAlgebra:-RandomVector(n):
x:= Jacobi(evalf(A), evalf(b), Vector(n, fill= 1.), 1e-6, 99);

A.x-b;

First 240 241 242 243 244 245 246 Last Page 242 of 395