Carl Love

Carl Love

28095 Reputation

25 Badges

13 years, 100 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

Everytime that you invoke e() or kp1(), you get different random numbers. So, the e() and kp1() that you checked the gcd of are not the same e() and kp1() that you're trying to compute the modular inverse for. You need to do something like this:

E:= e();
KP1:= kp1();
while igcd(E,KP1) <> 1 do  KP1:= kp1()  end do;

Your way of choosing long random numbers is much more convoluted than it needs to be: rand will take a range as the argument. So, to get a 568-bit random number, do

e:= rand(2^567..2^568-1)();

(However, note that your algorithm requires e to be odd, so make that

e:= 1 + 2*rand(2^566..2^567-1)();

)

Finally, since you are new to Maple, please don't get used to using 2D input. That's the devil's path. Start using 1D input now.

Do yourself a huge favor and use exclusively 1D input for programming. There are numerous "invisible" bugs that can occur in 2D input that even us experts have trouble finding.

To give you a start, I've retyped most of your code as 1D input and attached a worksheet below.

For an image, height is the first dimension and width is the second dimension. You had those reversed, and I corrected it. Your code won't work as is if k > 1---you get an Array out of bounds error. I included a try ... catch block so that you can investigate this error. I think that you need to subtract more than 2 from each index when k > 1.


restart:

with(ImageTools):

with(FileTools):

################
# Input section
#

     filelocation:= "C:/Users/Carl/Desktop/I Become My Resting Place.jpg";
     k:= 1;
#############

"C:/Users/Carl/Desktop/I Become My Resting Place.jpg"

 

1

(1)

zimage:= Read(filelocation):

View(zimage);

zwidth:= Width(zimage):

kernel_length:= 2*k+1:

kernel_data:= Matrix(1..kernel_length, 1..kernel_length, 1).~(1/kernel_length^2):

new2zpic:= zimage: # Warning: this does not create a copy of zimage.

dummy:= 0:

for i from k+1 to Height(zimage,upper) - k do
     for j from k+1 to Width(zimage,upper) - k do
          for m from 1 to kernel_length do
               for n from 1 to kernel_length do
                    try
                         dummy := dummy + kernel_data[m,n]*new2zpic[i+m-2,j+n-2]
                    catch:
                         print(i+m-k, j+n-k);
                         error
                    end try
               end do
          end do;

          new2zpic[i,j]:= dummy;  dummy:= 0

     end do
end do;

 

View(new2zpic);

 


Download Ex_II_1.0_1d_input.mw

First, it's clear that the desired limit is the same as the limit as (x,y) -> (0,0) of

f:= sin(x*y)/(exp(x^2+y^2)-1);

We consider the limits along different radial paths approaching the origin. Use paths of the form y = m*x:

eval(f, y= m*x);

Now take the limit as x -> 0, which necessarily makes y -> 0.

limit(%, x= 0);

We see that the limit depends on m, that it is not path independent. Hence, the multidimensional limit does not exist.

You need to solve a system of five equations, not three: one for each partial derivative of the Lagrangian and one for each constraint.

Here's a trick for minimizing and maximizing distance functions: The extrema are obtained at the same points as the extrema of the square of the distance, which is easier to work with. In this problem, you'll still get the answers is you don't use this trick.

restart:
Con1:= x^2+y^2+2*z+x:
Con2:= z^2+y^2-1:
Obj:= x^2+y^2+z^2:
F:= Obj - L1*Con1 - L2*Con2:
G:= VectorCalculus:-Gradient(F, [x,y,z]):
Sols:= {solve({convert(G,list)[], Con1, Con2}, {x,y,z,L1,L2}, explicit)};

seq(evalf(eval(Obj, s)), s= Sols);

You can discard the complex solutions. So, the minimum distance is 1, obtained at four points, and the maximum distance is sqrt(5), obtained at one point.

 

RiemannSum is a command in a package, so, you need to refer to it with the package name prefix:

Student:-Calculus1:-RiemannSum(T(10), method= midpoint);

But what is your point in doing this? To numerically evaluate an integral? This is not the way to do it! Instead, use

evalf(T(10));

Your expression S1-S2 simplifies to x^2*Sum(..., k= n+1..infinity). We take this expression, replace infinity with 10000, replace Sum by add, and make it a procedure.

S1:= t-> abs(x^2*add(t^(k*alpha+1)/GAMMA(k*alpha+2), k= n+1..10000));

logplot(S1, 0..1);

Do you want to find one feasible point for every possible setting of your variables? Then you need to call LPSolve multiple times anyway. If you have 11 of these "boolean" (two-valued) variables, that's only 2^11 = 2048 calls to LPSolve. In my opinion, that's trivial compared to the huge performance and accuracy penalties you'd pay for making the constraints nonlinear by using a1, a2, etc., as decision variables.

Note that there's no need to use a nested for loop to set multiple variables to multiple values. There are numerous ways to iterate over a Cartesian product that have been discussed many times on MaplePrimes. For example, your double for loop above could be replaced with

nv:= 2: #Number of "boolean" variables.
Cons:= {x1 >= 0, x2 >= 0, x1 <= 1000, x2 <= 1000, a1*x1+x2 <= 700, x1+a2*x2 >= 300}:
BV:= combinat:-cartprod([[0,1]$nv]): #or [[-1,1]$nv]
while not BV[finished] do
     A:= BV[nextvalue]();
     Sol[A[]]:= Optimization:-LPSolve(0, eval(Cons, [a||(1..nv)]=~ A))
end do:

op(eval(Sol));

There are a few errors in your procedure. The first is my fault, and also affects quadsum. I erroneously thought that isqrt(n) returned essentially floor(sqrt(n)) and that iroot(n,r) returned essentially floor(n^(1/r)). They both return something closer to round(...) rather than floor(...). So, replace iroot with this:

Iroot:= (n::nonnegint, r::posint)-> trunc(evalf(n^(1/r))):

That should be good enough for the smallish integers that we're working with here.

The next error is that iquo(n,3) needs to be iquo(n,2). If there's a solution of the form x=y, then 2*x^3 = n, not 3*x^3. Likewise, the check 3*x3 <> n needs to be 2*x3 <> n.

Finally, x=x+1 needs to be changed to x:= x+1.

It is not an error, but there's no need for listcub to be global. I made it local.

So, the fully corrected procedure (with Iroot above) is

cubesum:= proc(n::nonnegint)
local
     k:= 0, listcub:= table(),
     x:= Iroot(iquo(n,2),3), y:= x, x3:= x^3, y3:= x3
;
     if 2*x3 <> n then  x:= x+1;  x3:= x^3;  y:= x;  y3:= x3  end if;
     while x3 <= n do
          y:= Iroot(n-x3, 3);  y3:= y^3;
          if x3+y3 = n then  k:= k+1;  listcub[k]:= [y,x] end if;
          x:= x+1;  x3:= x^3
     end do;
     convert(listcub,list)
end proc:

cubesum(1729);

 

Just use and between the two conditions and drop the second if.

if irem(cubeprod,3)=0  and modp(cubeprod,2)<>0 then
     cubesum(cubeprod)
end if;

You claim that your second form, the nested if statements, is also erroneous; however, I can find no error in it. What error does it give you? It will do the same thing as the the form with and. The and form is easier to read. 

I notice that you use both irem and modp. This is confusing because they do the same thing. For pure integer arithmethic, use irem; it is a little bit faster because modp also processes matrices and polynomials.

You don't need "to plot inside the for loop." You need to plot after the for loop is finished, like this:

plot(zip(`[]`, vds, convert(Qg, list)));

I don't understand your second question "How can I set the x-axis of the plot in decimal notation?" The above command will give you an x-axis from 0 to 2. Isn't that what you want?

And please, please try to present your code in a neater way. Some tips:

  1. You can upload an actual worksheet.
  2. The space bar is your friend.
  3. Indent continuation lines.
  4. Make sure that input and output are clearly distinguished. For example, I always use bold for input.
  5. Don't include > at the beginning of your input lines. People need to manually delete those before they can execute your code.
  6. Most output is not worth including in a post.
  7. Select format "preformatted" from the MaplePrimes toolbar before pasting monospaced output. If you don't, the exponents all get pushed to the left, as you can see above.

Regardless of differences between versions, I can make the whole thing run in 12 seconds by a trivial application of evalhf. After your definition of procedure J, add the line

Jhf:= (x,y)-> evalhf(J(x,y)):

Then plot3d(Jhf, ...) instead of plot3d(J, ...).

I thought that plot3d already used evalhf by default, but I guess that I was wrong about that.

One benefit of the OP having posted a screen shot rather than plain text is that it clearly shows that both P and P appear in the input. My guess is that they are being treated as different variables and that this is the source of the anomaly. Furthermore, I guess that the upright P is actually the Greek letter capital Rho copied from a palette.

Simply include the argument scene= [x(t),y(t)] in your DEplot command and you'll get this:


restart:

RS:= Student:-Calculus1:-RiemannSum(
    f(x), x= a..b, method= left, output= sum, partition= n
);

(b-a)*(Sum(f(a+i*(b-a)/n), i = 0 .. n-1))/n

Approx:= evalf(eval(RS, [f= sqrt, a= 0, b= 4, n= 100]));

5.291703576

Exact:= int(sqrt(x), x= 0..4);

16/3

evalf(Exact);

5.333333333


Download RiemannSum.mw

While Kitonum's brute-force search does find the solutions to the equation, it doesn't use the algorithm given in the Question, which is a much better algorithm. I implemented the algorithm in the Question, and then I made a few improvements to it.

My procedure, like the algorithm in the Question, does not list the symmetric and negative solutions. If those are desired, they are trivial to generate by post-processing the output of the procedure.

quadsum:= proc(n::nonnegint)
local
     k:= 0, mylist:= table(),
     x:= isqrt(iquo(n,2)), y:= x, x2:= x^2, y2:= y^2
;
     if 2*x2 <> n then   x:= x+1;  x2:= x2+2*x-1;  y:= x;  y2:= x2  end if;
     while x2 <= n do
          y:= isqrt(n-x2);  y2:= y^2;
          if x2+y2 = n then  k:= k+1;  mylist[k]:= [x,y]  end if;
          x:= x+1;  x2:= x2+2*x-1
     end do;
     convert(mylist, list)
end proc:

Applying this procedure to random input on the order of 100 billion (10^11), it returns the solutions nearly instantaneously (156 ms). I let Kitonum's procedure run for over 23 minutes with this input with no results.

n:= eval('rand'(2^17..2^18)()^2 + rand(2^17..2^18)()^2);

st:= time():  quadsum(n);  T:= time()-st;

[[221539, 205782], [236579, 188298], [238766, 185517],
  [257901, 157838], [260634, 153283], [269181, 137722],
  [270774, 134563], [281011, 111618], [285826, 98637],
  [287843, 92586], [297357, 54814], [297549, 53762],
  [297978, 51331], [300477, 33754], [302242, 8691], [302323, 5154]
  ]
                             0.156

The algorithm used by isolve is even faster, by about a factor of two.

First 272 273 274 275 276 277 278 Last Page 274 of 395