Carl Love

Carl Love

28065 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

As you've suggested, it's theoretically possible to find some inequality involving x and t that'll guarantee that the other part is at most 10. But, why bother, unless you have some other reason for finding that inequality? You can just use min to explicitly set the maximum at 10, like this:

Y:= (x,t)-> min(10, -(11*sin(t/2+1.145)-10)*((x-37)^2)+(-3.5*sin(t)+ 1));

I think that it's best to make it a function of both x and t. If you insist on it being a function of alone, then just remove the t from the left side of the arrow. 

Note that it's not necessary (or desirable) to use unapply to create this function because there's no meaningful evaluation possible before values are given for x or t. But neither is it incorrect to use unapply. It's just a matter of using a tool that's much larger than what's required to do the job.

It should be done with fsolve rather than solve because fsolve gives you more control over the range. Like this:

restart;
Digits := 32;
t0 := 1;
eq := 1-w*v^2-2*v*exp(-t/v);
equ := eval(eq, v = -t/ln(u));
#Lines above are exactly your code.

us:= subs(_E= eval(equ, t= t0), w= _W, _W-> fsolve(_E, u= -infinity..1));
vs := w-> -t0/ln(us(w));
plot(vs, 0 .. 10, labels= [w, ``], view= 0..1);

Do

plot([rhs(f), Y, Y= 0..10], labels= [typeset(epsilon/`2`/Pi), Y]);

Using plot rather than implicitplot is usually preferable when either the equation can be solved for one of its variables or its two variables can be expressed as functions of a third variable.

I can't work with your equations because of the reason that I just wrote in the Reply above. But apparently you already have some means of solving them (which command?). So, just add one more equation that expresses that you want the sum of the variables to be 32. That can be done simply as follows. Suppose that the list of equations in your worksheet is named Eqns. Then do

Con:= add(indets(Eqns, name)) = 32: #Constraint
Sys:= [Eqns[], Con]:

And proceed to solve Sys by whatever means you were using to solve Eqns.

Of course, this idea is worthless if your unspecified solver reduces all constants (such as the 32) modulo 2.

Some evaluations, such as simple arithmetic with explicitly numeric operands, are always done regardless of the encapsulation with -> or proc ... end proc or '...'. These are called automatic simplifications. That's what's happening with 2+3. If you have a specific need to prevent such an evaluation, please ask, because there are some specialized techniques available.

Except for the few usually innocuous automatic simplifications, the expression on the right side of the -> is not evaluated at the time that the arrow expression is created. In the vast, vast majority of cases of automatic simplification, its use does not change the final result. Obviously such is the case with 2+3.

Regarding your second question: If you want to evaluate first, then turn the result into an arrow expression, the standard command for that is unapply:

unapply(int(1,y), x);

Note that x-> e and proc(x) e end proc are semantically equivalent for any symbol x and expression e (which likely (but not necessarily) depends on x).

The usual way is to run in "batch mode"; that is, run command-line Maple with its input coming from and its output going to files specified on the command line that initiates the Maple session. See ?Maple for details of command-line options.

Here's a way to do it, but I suspect that there's an easier way. Suppose that MkL, and have already been defined. Then do 

Z:= Matrix(M$2): #All zero
T:= 
   Matrix((2^(k-1))$2, (i,j)-> `if`(i=j, L, F), shape= triangular[upper]) + 
   Matrix((2^(k-1))$2, (i,j)-> `if`(i=j, 0, Z), shape= triangular[lower])
:
P:= Matrix((2^(k-1)*M)$2, (i,j)-> T[iquo(i-1,M,'r')+1, iquo(j-1,M,'c')+1][r+1, c+1])/2^k;

The difference between my Answer and Tom's is that I've deconstructed the blocks so that the result is a "flat" Matrix.

Here's a complete implementation of RSA (key generation, encryption, and decryption). Maple makes this surprisingly easy. Notice that it's not much longer than your procedure, whose function is performed by my one-line procedure Stringify.

RSA:= module()
export
   bytelen:= 8,
   GenKey:= proc(B::And(posint, satisfies(B-> B>3)), seed::posint:= ())
   local r:= randomize(seed), p, q, C, e, d, R;
      p:= nextprime(rand(2^B..2^(B+1))());
      R:= rand(2^(B+5)..2^(B+6));
      do q:= nextprime(R()) until igcd(p-1, q-1) = 2;    
      C:= (p-1)*(q-1)/2;
      R:= rand(3..C-1);
      do e:= R() until igcd(e, C) = 1;
      if seed=() then randomize(r) fi;
      p*q, e, (1/e mod C)
   end proc,

   Encrypt:= (S::string, n::posint, e::posint)-> 
      Chunkify(
         sprintf("%m", Numerify~(Chunkify(S, iquo(ilog2(n),bytelen))) &^ ~ e mod n),
         interface('screenwidth')
      ),
   Decrypt:= (S::list(string), n::posint, d::posint)->
      cat(Stringify~(sscanf(cat(S[]), "%m")[] &^ ~ d mod n)[]),

   Chunkify:= proc(S::string, L::posint)
   local k, r, len:= length(S);
      [seq(S[1+L*(k-1)..min(L*k,len)], k= 1..iquo(len,L,r)+`if`(r>0,1,0))]
   end proc,

   Numerify:= (S::string)-> 
      convert(convert(S, bytes), base, 2^bytelen, 2^(length(S)*bytelen))[],
   Stringify:= (N::posint)-> cat(convert(convert(N, base, 2^bytelen), bytes)),
`;`;
end module:

Example usage:

(n,e,d):= RSA:-GenKey(32);
   n, e, d := 1570972592032860729287, 122831757217576938571, 
     678402947328820371811
S:= "It's so easy and so much fun to write an RSA module in Maple!":
E:= RSA:-Encrypt(S, n, e);
E := 
  ["7*"7>xa***p9OT`O""7&prhA'H;w!37""7p#**zDPbe$yf:"7AJG!Q%e'*)*[a5"6Ty!yo%*\+8T]"7", 
  "rB?_()RpV(3d""7$="y+67I`,^7"6[6%4Tr(ogYe("]

RSA:-Decrypt(E, n, d);
"It's so easy and so much fun to write an RSA module in Maple!"

Maple has special commands (such as ilog10, ilog2, iquo, irem, ilcm, igcd, &^, and mod) for high-speed large-integer arithmetic. You should avoid floating-point and/or symbolic commands such as log10floorevalf, and frac when doing large-integer arithmetic.

I confirm Tom's work; there are no typos. I obtained identical results independently.

restart:
(k,j):= (3,0):
add(
   add(`if`(l=m, 0, mul(`if`(q=m or q=l, 1, r-q+1), q= 0..k)), l= 0..k)
   /mul(`if`(l=m, 1, m-l), l= 0..k),
   m= j+1..k
);
eval(%, r= 1); 

My code is almost identical to Tom's, which is just a coincidence.

You wrote the following the very hand-wavy excuse:

  • (i know the command is weird, i.e. subs(beta=beta, ..), etc.. this is part of some larger code base and i just put this in when trying to undertand what's going on).

Your command is weird, period. "When trying to understand what's going on" is the last place that you'd want to do something weird. The weirdness is not so much the beta= beta part; it's when that's combined with an assuming clause that uses beta. Personally, I'd steer clear of any use of subs inside assuming.

These things might not have anything to do with your issue. I can't tell until you post the executable code.

The backslash is the escape character in Maple. So, if you want to pass a string literal that contains backslashes, then you must code each backslash as \\. So, if you want to pass a literal that has four backslashes, as you show (two at the beginning and two at the end), then it needs to be

"\\\\((.+)\\\\)"

You almost have it. I like your symbolic approach. Some tips and corrections:

  • If gamma is supposed to be a constant that you will specify, then you should use a different name for it, because gamma is a pre-defined constant in Maple, similar to Pi. While it's not an error to use your own value for gamma (or Pi for that matter), doing so can lead to incorrect numeric results being returned in some situations which otherwise would've (and should've!) returned error messages.
  • f should return a column vector, not a matrix.
  • In the definition of f, you've inappropriately used square brackets for parentheses.
  • You need to give a little bit of structural definition before you can do diff~(X, ...), etc. X:= t-> Vector(n, i-> x[i](t)) is sufficient (or use any other unused symbol for x).
  • Shouldn't tau become in the sys:= ... definition? Mathematically, it makes sense either way, but there's a big difference in meaning. Using tau would make it a constant-coefficient system (and perhaps that's what you intend).

There are commands numer and denom that extract the numerator and denominator of expressions. So,

coeff(denom(expr2), sin(a)^2)

I was surprised at how easy this was: To find (and print neatly) all references to _SolutionsMayBeLost in all library procedures whose names begin with isolve (This could be repeated for any names):

macro(pre= "isolve", SMBL= "_SolutionsMayBeLost", ST= StringTools):

#Select all library entries (19) whose names begin "isolve":
IP:= map(
   p-> convert(p[1][1..-3], name), #-3 to remove .m file extension
   select(p-> p[1][1..length(pre)]=pre, LibraryTools:-ShowContents())
);   
#Select those (4) that contain any reference to _SolutionsMayBeLost:
SIP:= select(p-> has(ToInert(eval(p)), SMBL), IP);

#Select the lines in those procedures that contain _SolutionsMayBeLost:
R:= map(
   p-> [
      p, 
      select(
         L-> ST:-Search(SMBL,L) > 0, ST:-Split(debugopts(procpretty= p), `\n`))[]
   ], 
   SIP
):
#Formatted print:
printf(cat("\n", seq(cat("%s\n"$r, "\n\n"), r= nops~(R))), op~(R)[]);  

isolve
global _SolutionsMayBeLost;
  26   _SolutionsMayBeLost := '_SolutionsMayBeLost';
  28   if _SolutionsMayBeLost = true then


isolve/inequalities
global _SolutionsMayBeLost;
  10       _SolutionsMayBeLost := true;


isolve/inequalities/convert_to_Z
global _SolutionsMayBeLost;
  21           _SolutionsMayBeLost := true;
  28           _SolutionsMayBeLost := true;
  34               _SolutionsMayBeLost := true;
  39                   _SolutionsMayBeLost := true;
  41           _SolutionsMayBeLost := true;
  43       _SolutionsMayBeLost := true;


isolve/inequalities/internal
global _SolutionsMayBeLost;
   6           _SolutionsMayBeLost := true;

So, if you execute the above in Maple 2018, you'll see that there are 19 library entries whose names begin with isolve. All are procedures. Of those, there are only 4 that contain any reference at all to _SolutionsMayBeLost. Only 3 of those set the value, and all 3 of those are specific to inequalities.

Here is another workaround. It is similar to yours, but it doesn't require any foreknowledge about which variable(s) are good candidates for elimination.

`isolve/sep_degrees`:= proc(Sys::set({algebraic, `=`}))
local L, N, S, LS, LN;
   (L,N):= selectremove(type, subsindets(Sys, `=`, lhs-rhs), 'linear');  
   S:= {isolve(eval(N, (L:= isolve(L))), _rest)};
   (LN,LS):= selectremove(type, L, name=name);
   eval(map(`union`, S, LS), (rhs=lhs)~(LN))[]
end proc:

sys:={x+y=10, x^2-y^2+z^2=1}:
`isolve/sep_degrees`(sys, n);

 

First 156 157 158 159 160 161 162 Last Page 158 of 395