dharr

Dr. David Harrington

8205 Reputation

22 Badges

20 years, 337 days
University of Victoria
Professor or university staff
Victoria, British Columbia, Canada

Social Networks and Content at Maplesoft.com

Maple Application Center
I am a retired professor of chemistry at the University of Victoria, BC, Canada. My research areas are electrochemistry and surface science. I have been a user of Maple since about 1990.

MaplePrimes Activity


These are answers submitted by dharr

I changed your code to

f := "this:///Images/Maple.jpg";
img := Read(f);

and it works - you then get some warnings later that rotation angles should be in radians, which I'm sure you can fix.

Although

diff(ln(GAMMA(x)), x)=Psi(x)

Using the chain rule we find

diff(ln(GAMMA(1/x)), x)=-Psi(1/x)/x^2;

which explains the missing -x^2.

I don't think there is a builtin command, but there an implementation of a heap, which allows it to be done easily. If you wanted the whole list partially sorted, then just sort the selected ones and follow with the rest. (The smallest ones are in decreasing order; the largest are in increasing order.)

restart;

partselect:=proc(data::list,k::posint,compare:=`<`)
  local h,i;
  h:=heap:-new(compare,data[1..k][]);
  for i from k+1 to numelems(data) do
    heap:-insert(data[i],h);
    heap:-extract(h);
  end do;
  [while not heap:-empty(h) do
     heap:-extract(h)
   end do
  ];
end proc:
  

ds:=[seq(rand(1..100)(),1..20)];

[93, 45, 96, 6, 98, 59, 44, 100, 38, 69, 27, 96, 17, 90, 34, 18, 52, 56, 43, 83]

partselect(ds,3); # select smallest 3

[18, 17, 6]

partselect(ds,3,`>`); # select largest 3

[96, 98, 100]

ds:=StringTools:-Explode("partialsortingisfun");

["p", "a", "r", "t", "i", "a", "l", "s", "o", "r", "t", "i", "n", "g", "i", "s", "f", "u", "n"]

partselect(ds,3,lexorder);

["f", "a", "a"]

NULL

Download partselect.mw

One solution to this error is to supply an approximate solution, and since you said tanh(x) was a known solution, I tried that. But then I realized tanh(x) goes to -1, not 0, as x->-infinity. If I change the boundary condition to z(-15)=-1 it works. But if you really wanted z(-infinity)=0, then you can try a better approximate solution.

dsolve.mw

See

https://www.mapleprimes.com/questions/235168-How-Do-I-Generate-Magic-And-Semi-Magic

for some solutions.

You can add a constant to each cell to get other ones, but not sure what exactly you mean by random.

For your first case, the initial conditions are specified as 

dsolve({DE, R(0) = 1, D(R)(0) = 1}, numeric, range = 0 .. 20)

I made up a value for the derivative in the second condition; you will no doubt have a better value. Or perhaps you wanted a boundary condition as your second condition.

And a similar problem applies for your second problem.

PS: In your second problem in defining F you probably wanted an explicit multiplication after the first ), so (...)*(...)

Your second problem doesn't seem well-posed at theta=0.

Here some progress:

DIFFERENTIAL_EQUATION.mw

restart

with(numapprox); Lr := add((-1)^((1/2)*i-1)*Pi^i*r^(i+1)/2^(i-1), i = 2 .. 10, 2)

(1/2)*Pi^2*r^3-(1/8)*Pi^4*r^5+(1/32)*Pi^6*r^7-(1/128)*Pi^8*r^9+(1/512)*Pi^10*r^11

The presence of Pi here is a problem (problem also if we have x instead of Pi). Not sure if this is a bug, but certainly it shouldn't throw a cryptic error messsage.

pade(Lr, r, [2, 2])

Error, (in convert/ratpoly) invalid subscript selector

Make a version without the Pi. let rPi=r*Pi. LrPi is Pi times Lr

LrPi := expand(Pi*(eval(Lr, r = rPi/Pi)))

(1/2)*rPi^3-(1/8)*rPi^5+(1/32)*rPi^7-(1/128)*rPi^9+(1/512)*rPi^11

padeLrPi := pade(LrPi, rPi, [2, 2])

(1/2)*rPi^3

So go back to in terms of r

padeLr := expand((eval(padeLrPi, rPi = r*Pi))/Pi)

(1/2)*Pi^2*r^3

NULL

Download pade2.mw

The degree 3 result here is also a problem - I'll submit an SCR.

Not as general as @acer's solutions. It seems evalhf handles the Gamma function but not factorial so in this particular case converting to GAMMA works.

[Edit: This explanation is incorrect - see below]

failed_plot.mw

[Edit: the OP has now completely changed this post, so my responses here and below are no longer relevant.]

 

The equations in your post outline a laplace transform analytical solution and you seem to want to follow that, but in your worksheet you set up for a numerical solution of the original untransformed equations, abandoning the laplace transform method.

So forgetting laplace transforms and following your worksheet, you have two errors in formulating a consistent problem. You had strange parentheses after alpha0 in your Cond, which I guessed as a correction (in red). [Edit] What about initial conditions? 

new_paper_dust.mw

If you want the analytical solution then you can start by transforming the pdes to odes with intrans:-laplace, gving you Eq (16) etc, then solve the ode system - but then it is not a good idea to give numerical values to your parameters.

Maple tries to figure out the file format you want from the file extension, but doesn't recognize .dat. You can use the format option to specify which format you want, for example CSV:

restart

vv := Vector([1, 3, 5, 7])

Vector[column](%id = 36893490061895775284)

File output has the four numbers, one per line

Export("c:/Users/dharr/Desktop/testfile.dat", vv, format = "CSV")

8

NULL

Download testfile.mw

Edit: for saving Maple objects in a .m file for reading in later into Maple use the save command

@sursumCorda Vote up. I could not beat your code, which as you no doubt realize is very efficient because (1) CharacterFrequencies uses external code, (2) it works directly on the strings and (3) anyway most of the time is taken working out the primes.

I tried converting the integer to an Array of digits, and then invoking a compiled routine to search the Array. But the additional conversion time offsets potential efficiency gain, and the prime generation time still dominates. (Actually using numboccur 10 times, dopping out once you find 4, is more efficient and is close to your result)

Given that manipulating digits in integers is probably a common operation of interest I'm surprised that there isn't a better way to extract them. convert(p,base,10) is shockingly slow compared to the methods that go via sprintf("%d",p). Of course the sprint code could probably be easily modified to produce an Array or List.

I'm confused about the relative merits of nextprime over multiple calls to ithprime, and there my timings weren't consistent - perhaps there is an internal list that is added to as another prime is found, or there is a huge table anyway. 

restart;

Compilable procedure to find if any elements in Array e appear exactly four times.

has4:=proc(n::integer,e::Array(datatype=integer[4]),c::Array(datatype=integer[4]))
  local i,ei;
  for i from 1 to 10 do
    c[i]:=0;
  end do;
  for i to n do;
    ei:=e[i]-47;
    c[ei]:=c[ei]+1;
  end do;
  for i from 1 to 10 do
    if c[i]=4 then return true end if;
  end do;
  false;
end proc:

has4c:=Compiler:-Compile(has4);

proc () options call_external, define_external(maple_compiled_m7dc41b208b0ac686076b31608b0d668e, MAPLE, LIB = "C:\Users\dharr\AppData\Local\Temp\dharr-3388\maple_compiled_m7dc41b208b0ac686076b31608b0d668eyYxVbxdp.dll"); call_external(0, 140730599545072, true, false, false, args) end proc

Aseq:=proc(m::nonnegint,n::posint:=1);
  local p,e,ne,p4:=Array([]),c:=Array(1..10,datatype=integer[4]);
  p:=ithprime(n);
  to m do
    e:=Array(convert(sprintf("%d",p),'bytes'),datatype=integer[4]);
    ne:=upperbound(e);
    if has4c(ne,e,c) then p4 ,= p end if;
    p:=nextprime(p);
  end do;
  p4
end proc:

a:=CodeTools:-Usage(Aseq(10^6,10^6)):numelems(a);

memory used=3.40GiB, alloc change=12.00MiB, cpu time=39.20s, real time=34.18s, gc time=7.97s

42720

CodeTools:-Profiling:-Profile(Aseq);
numelems(Aseq(10^6,10^6));

42720

CodeTools:-Profiling:-PrintProfiles(Aseq);

Aseq
Aseq := proc(m::nonnegint, n::posint := 1)
local p, e, ne, p4, c;
     |Calls Seconds  Words|
PROC |    1  44.625 454388814|
   1 |    1   0.000     18| p4 := Array([]);
   2 |    1   0.000     24| c := Array(1 .. 10,datatype = integer[4]);
   3 |    1   0.000     10| p := ithprime(n);
   4 |    1   3.247      7| to m do
   5 |1000000   7.347 55008673|     e := Array(convert(sprintf("%d",p),'bytes'),
                                  datatype = integer[4]);
   6 |1000000   1.490 2000872|     ne := upperbound(e);
   7 |1000000   2.005 4000000|     if has4c(ne,e,c) then
   8 |42720   0.030 127264|         p4 ,= p
                                end if;
   9 |1000000  30.506 393251946|     p := nextprime(p)
                            end do;
  10 |    1   0.000      0| p4
end proc
 

Simpler and unexpectedly faster than the above

restart;

Aseq2:=proc(m::nonnegint,n::posint:=1);
  local p,e,p4:=Array([]);
  p:=ithprime(n);
  to m do
    e:=Array(convert(sprintf("%d",p),'bytes'));
    if numboccur(e,48)=4 or numboccur(e,49)=4 or numboccur(e,50)=4 or numboccur(e,51)=4 or numboccur(e,52)=4 or
       numboccur(e,53)=4 or numboccur(e,54)=4 or numboccur(e,55)=4 or numboccur(e,56)=4 or numboccur(e,57)=4
    then
      p4 ,= p
    end if;
    p:=nextprime(p);
  end do;
  p4
end proc:

a:=CodeTools:-Usage(Aseq2(10^6,10^6)):numelems(a);

memory used=3.80GiB, alloc change=2.00MiB, cpu time=30.56s, real time=30.58s, gc time=2.48s

42720

CodeTools:-Profiling:-Profile(Aseq2);
numelems(Aseq2(10^6,10^6));

42720

CodeTools:-Profiling:-PrintProfiles(Aseq2);

Aseq2
Aseq2 := proc(m::nonnegint, n::posint := 1)
local p, e, p4;
     |Calls Seconds  Words|
PROC |    1  50.343 511132410|
   1 |    1   0.000     18| p4 := Array([]);
   2 |    1   0.000     10| p := ithprime(n);
   3 |    1   2.616      7| to m do
   4 |1000000   7.437 59084011|     e := Array(convert(sprintf("%d",p),'bytes'));
   5 |1000000   6.116 58645626|     if numboccur(e,48) = 4 or numboccur(e,49)
                                  = 4 or numboccur(e,50) = 4 or
                                  numboccur(e,51) = 4 or numboccur(e,
                                  52) = 4 or numboccur(e,53) = 4 or
                                  numboccur(e,54) = 4 or numboccur(e,
                                  55) = 4 or numboccur(e,56) = 4 or
                                  numboccur(e,57) = 4 then
   6 |42720   0.030 127264|         p4 ,= p
                                end if;
   7 |1000000  34.144 393275474|     p := nextprime(p)
                            end do;
   8 |    1   0.000      0| p4
end proc
 

@sursumCorda's routine

restart;

A161786__1 := proc(m::nonnegint, n::posint := 1, ` $`)::'Vector'(prime):
        options no_options:
        local p := ifelse(n = 1, 1, ithprime(n - 1)), vec := DEQueue();
        to m do
                if member(4, rhs~({StringTools['CharacterFrequencies'](nprintf("%d", (p := nextprime(p))), 'digit')})) then
                        vec ,= p
                fi
        od;
        Vector([vec[]], 'datatype' = prime)
end:

a:=CodeTools:-Usage(A161786__1(10^6,10^6)):numelems(a);

memory used=3.52GiB, alloc change=3.26MiB, cpu time=30.59s, real time=30.63s, gc time=2.52s

42720

CodeTools:-Profiling:-Profile(A161786__1);
numelems(A161786__1(10^6,10^6));

42720

CodeTools:-Profiling:-PrintProfiles(A161786__1);

A161786__1
A161786__1 := proc(m::nonnegint, n::posint := 1, $)
local p, vec;
     |Calls Seconds  Words|
PROC |    1  39.531 475133042|
   1 |    1   0.000     13| p := ifelse(n = 1,1,ithprime(n-1));
   2 |    1   0.000    122| vec := DEQueue();
   3 |    1   1.046      7| to m do
   4 |1000000  37.552 466278938|     if member(4,rhs~({StringTools['
                                  CharacterFrequencies'](nprintf("%d",
                                  (p := nextprime(p))),'digit')})) then
   5 |42720   0.277 1754551|         vec ,= p
                                end if
                            end do;
   6 |    1   0.656 7099411| Vector([vec[]],('datatype') = prime)
end proc
 

 

Download Aseq.mw

(worksheet not displaying right now)

contours.mw

Replace G0 with

G0:=proc(y) if not type(y,numeric) then return 'procname'(args) end if;
    if y<0 then exp(y)/(1+exp(y)) else 1/(exp(-y)+1) end if;
   end proc; 

Use prev[1..-1] := curr[1..-1]; to copy the contents regardless of size (or the for loop), rather than prev:=curr; You cannot assign to a formal parameter.

This has nothing to do with the size - your first attempt had prev=curr, not prev:=curr so did not work either.

Here's one way to do it. Of course you could do it all in one loop.

restart;

with(ImageTools):

Import some prn files plt1.prn, plt2.prn,plt3.prn

for i to 3 do
  img[i]:=Read(cat(interface(worksheetdir),"/plt",i,".png"));
end do:

Images are 600x800 pixels

lowerbound(img[1]);
upperbound(img[1]);

1, 1, 1

600, 800, 3

Pad 50 pixels white all around

for i to 3 do
  padded[i]:=PadImage(img[i],border=50,fill=1,reindex=true);
end do:

lowerbound(padded[1]);
upperbound(padded[1]);

1, 1, 1

700, 900, 3

Write out padded files as .tiff

for i to 3 do
  Write(cat(interface(worksheetdir),"/plt",i,".tiff"),padded[i]);
end do;

11270

11284

11284

NULL

Download framepng.mw

Test_prn_files.zip

if the singularity is replaced by the limit of the function at the origin, then you can make some progress, though I'm being careless about checking consistency of different limit directions. What do we know about the signs of the parameters?

restart

R0 := -(1-tanh((1/2)*l*sqrt(k*y^2+x^2)*d)^2)*x/(k*y^2+x^2)+2*tanh((1/2)*l*sqrt(k*y^2+x^2)*d)*x/(l*(k*y^2+x^2)^(3/2)*d)

-(1-tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)^2)*x/(k*y^2+x^2)+2*tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)*x/(l*(k*y^2+x^2)^(3/2)*d)

eval(R0, {x = 0, y = 0})

Error, numeric exception: division by zero

However, the function seems smooth, aside from the isolated singularity at the origin

params := {d = 1, k = 1, l = 1}; plot3d(eval(R0, params), x = -.1 .. .1, y = -.1 .. .1)

{d = 1, k = 1, l = 1}

So we should be able to evaluate the limit.

limit(R0, {x = 0, y = 0})

limit(-(1-tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)^2)*x/(k*y^2+x^2)+2*tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)*x/(l*(k*y^2+x^2)^(3/2)*d), {x = 0, y = 0})

Aagh. But

limit(R0, x = 0); `assuming`([limit(R0, y = 0)], [x::real]); limit(%, x = 0)

0

(-4*exp(abs(x)*l*d)*l*d*abs(x)+2*exp(2*abs(x)*l*d)-2)/(x*l*d*abs(x)*(exp(2*abs(x)*l*d)+2*exp(abs(x)*l*d)+1))

0

We could proceed slowly working out all the limits of the required derivatives, e.g.

diff(R0, x); limit(%, x = 0); `assuming`([limit(%, y = 0)], [y::real])

tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)*l*d*x^2*(1-tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)^2)/(k*y^2+x^2)^(3/2)-(1-tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)^2)/(k*y^2+x^2)+3*(1-tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)^2)*x^2/(k*y^2+x^2)^2+2*tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)/(l*(k*y^2+x^2)^(3/2)*d)-6*tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)*x^2/(l*(k*y^2+x^2)^(5/2)*d)

(-4*exp((k*y^2)^(1/2)*l*d)*(k*y^2)^(7/2)*l*d+2*k^3*y^6*exp(2*(k*y^2)^(1/2)*l*d)-2*k^3*y^6)/(k*y^2*(k*y^2)^(7/2)*l*d*(exp((k*y^2)^(1/2)*l*d))^2+2*k*y^2*(k*y^2)^(7/2)*l*d*exp((k*y^2)^(1/2)*l*d)+k*y^2*(k*y^2)^(7/2)*l*d)

(1/6)*d^2*l^2

diff(R0, y); limit(%, x = 0)

tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)*l*d*k*y*(1-tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)^2)*x/(k*y^2+x^2)^(3/2)+3*(1-tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)^2)*x*k*y/(k*y^2+x^2)^2-6*tanh((1/2)*l*(k*y^2+x^2)^(1/2)*d)*x*k*y/(l*(k*y^2+x^2)^(5/2)*d)

0

NULL

Download mtaylor.mw

First 25 26 27 28 29 30 31 Last Page 27 of 81