Carl Love

Carl Love

28025 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

The line that begins with subimage:=  needs to end with a semicolon (or a colon).

I see that you've chosen to ignore our advice that you switch to 1D input.

Do this:

y:= 2*x^2+7:
plot(eval(y, x= sqrt(x1)), x1= -5..5);

I don't know enough about events to answer whether you're doing it wrong. But you can do what you want by trapping the error in a try ... catch block (see ?try). Something like this:

try
     TOVr(very_big_r)
catch "cannot evaluate the solution further right":
     return TOVr('last')
end try;
error "No dsolve error, use a bigger r";

If you provide your actual code in plaintext or 1D input form, I'll code it for you.

It doesn't come prepackaged, but here's a one-liner procedure to do it. I generalized it so that the base doesn't have to be 10, but defaults to 10.

logspace:= proc(a,b,n,{base:= 10})  evalf(base^~<seq(a..b, (b-a)/(n-1))>)  end proc;

I can see three approaches to deciding whether the stated integer factorization is "feasible":

  1. By happenstance, ifactor(P) may return the full prime factorization in a reasonable amount of time. This will happen when there's at most one large prime factor.
  2. We use the easy option to ifactor. Then ifactor will return the prime factors that it can get in a short amount of time and leave the rest unfactored.
  3. Use the timelimit command with ifactor. Then you can decide the maximum amount of time to spend on each factorization. The problem with this approach is that the result will be a full prime factorization or nothing. Unlike the easy option, it will not return the factors that have already been found when the time is up.

My guess is that a procedure combining approaches 1 and 2 is most promising. I have coded that below.

Are you sure about the requirement that kp2 be 56 bits? That seems much too strict, and I think that it would take hours to find a kp2 of exactly 56 bits. Are you sure that it's not 56 bits or more? Below, I coded it so that kp2 is between 56 and 500 bits. 

Please give the procedure WuSun below a few minutes to finish. I will also code something combining approaches 1, 2, and 3 and post it as a followup.


# Wu/Sun Schema A RSA key-generation algorithm

#Step 1:

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

638477039545866035527860419740482646570391916634928775736040584757086812229395112512513825510987293920066911133709134097913777979837572903596538728204373509530589747233207

(1)

R160:= rand(2^159..2^160-1):

WuSun:= proc(e::posint)
local kp1, x, P, p, dp, kp2, k, f;
     do
          #Step 2,5:
          kp1:= R160();
          while igcd(kp1,e) <> 1 do kp1:= R160() end do;

          #Step 3,6:
          x:= 1/e mod kp1;
          P:= (e*x-1)/kp1 + e;
          # We need P even.
          if irem(P,2) <> 0 then next end if;
          dp:= x+kp1;

          #Step 4,7:

          (* To understand the following block of code, you need to know
          the format that Maple uses as return values from ifactor(...)
          and from ifactor(..., easy). See ?ifactor. Note that we have
          to allow for the possibility that ifactor(..., easy) returns
          the full factorization. *)

          #In the case of partial factorization, extract the integer part.
          #If the factorization is complete, kp2 is just P.
          kp2:= lcoeff(expand(ifactor(P, easy)));
          if ilog2(kp2) > 56 then
               #Find a divisor of kp2 that exceeds 56 bits but is as
               #close to 56 as we can get.
               f:= {op(ifactor(kp2))}; #Use set form to sort the primes.
               #The next line takes all 2-factors except the first. We
               #need to reserve one 2 for p0.
               kp2:= expand(f[1])/2;
               for k from 2 while ilog2(kp2) < 56 do
                    kp2:= kp2*expand(f[k])
               end do;
               if ilog2(kp2) > 500 then next end if;
               p:= P/kp2+1;
               if isprime(p) then return (P,p,dp) end if
          end if
     end do
end proc:  

(P,p,dp):= WuSun(e);

738128369139531838496569556654872585925136555974030282183676186481539482269159258484159200907676482767878497234800377628089047432478417813368297172188311270732197373724736, 1918544712641923648670613788298823623760588329878670906610782998053997421196324793470688529185105921916883691012591844385509740821387976086985634331483163, 1568309843248958963093916698552291336065205971015

(2)

(Q,q,dq):= WuSun(e);

925236977107069560793616487316895377905249519434128416407262042755975946109512434956689807066041362698019505939233456189374304749765695310092084174550242779443341104382760, 612995618251549166027274494149527964947780585363585516998336792270311149073629737679340444932441188179739250519735880544604490268227318342176809333407319, 1616555250820037357480406825254465394169750917223

(3)

 


Download WuSun.mw

 

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.
First 271 272 273 274 275 276 277 Last Page 273 of 395