Carl Love

Carl Love

28055 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

Maple has a built-in conditional ternary operator `if`. Note that the backquotes distinguish it from if as used in if-statements. It has smart evaluation rules: Only 1 of the 2nd and 3rd operands gets evaluated, becoming the operator's return value.

`if`(x=3, 1, 0)*x^2

`if`(g(x)=x^2, 1, 0)*f(x)

If you're defining objects, then it's possible to define the meaning of [...] when it's placed to the right of those objects. So, in that case, you could do it exactly the way you asked. DataFrames are examples of objects where the brackets are defined in a way similar to what you suggest. They are essentially matrices that can be indexed by boolean relations.

My code for this uses irem, which computes both an integer quotient and its remainder in one step. It also uses multiple assignment, which allows one to switch two variables without using an intermediate third variable. And, finally, this code works for any integers a and b, regardless of their signs or which is greater.

GCDwithBezout:= proc(a::integer, b::integer)
local q, r0:= a, r1:= b, s0:= 1, s1:= 0; 
     while r1 <> 0 do
         (r0, r1):= (r1, irem(r0, r1, 'q'));
         (s0, s1):= (s1, s0 - q*s1)
     od;
     sign(r0)*[r0, s0, `if`(b=0, 0, iquo(r0-s0*a, b))]
end proc:

 

Method using strictly the techniques of single-variable calculus:

You need to use function variables, which are expressions such as f(x) that show a dependency between a function and its independent variable(s). You want the derivative with respect to t, which makes t the independent variable. So, you need to replace z with z(t) in your original equation: 

Eq0:= z*t = cos(z + t):  #original equation
Eq1:= subs(z= z(t), Eq0); #function variable adjustment

Now, assuming that you understand the rules of explicit differentiation (most importantly for this problem the product rule), there are only two more steps needed to do implicit differentiation. First, take the derivative with respect to t of both sides of the equation:

Eq2:= diff(Eq1, t); #both sides done with one command!

And then solve for the derivative:

Ans:= solve(Eq2, diff(z(t), t));

If you were solving Eq2 by hand for the derivative, note that the equation is always linear in the derivative and thus easy to solve by Algebra-I methods.

An optional clean-up step would be to re-express the answer with the original z instead of z(t):

subs(z(t)= z, Ans);
 

Much easier method that uses a tiny bit of multi-variable calculus:

Amazingly, all of the above can be combined into two very short steps using a simple idea of multi-variable calculus: If we subtract one side of the original equation from the other, then the resulting expression can itself be considered a function of two independent variables, z and t. There's a remarkable and simple relationship between the derivatives of this new function, F, with respect to its independent variables and the derivative that we want:  dz/dt = - (dF/dt) / (dF/dz). To remember that formula, you can imagine that these are fractions where the dF cancels; then include a minus sign. Now, professors and textbooks may make a distinction between ordinary derivatives and partial derivatives--and they use a fancy curly letter d for the partials--but there's really no practical difference and Maple doesn't care. 

The first short step is to subtract one side from the other:

Eq:= z*t = cos(z+t): #original
F:= (lhs-rhs)(Eq); #left-hand side (lhs) - right-hand side (rhs)

And then apply the derivative relation that I explained:

Ans:= - diff(F,t) / diff(F,z);

That's all that there is to it! If you were doing it by hand, it doesn't even require the product rule or solving an equation for the derivative! Since this method is so much easier and so much less prone to error (when doing by hand), I recommend it to all my single-variable calculus students as a means of checking their work done by the first method, which their formal instructors may require them to use.

Implicit derivatives can usually be expressed in multiple equivalent forms which are totally correct but whose equivalence is not immediately apparent. To show the equivalence, you may need to reduce with respect to the original equation. 

The command is Statistics:-ColumnGraph. If you can't get the appropriate labels on the horizontal axis, let me know. It's fairly easy, but, unfortunately, no example of that appears on the help page.

The problem of the USB sticks is called an Integer Linear Program or ILP for short. It can be solved like this:

Optimization:-LPSolve(
   32*x + 64*y + 128*z, 
   {15*x + 20*y + 30*z <= 200, x >= 0, y >= 0, z >= 0},
   assume= integer, maximize
);
     [832, [x = 0, y = 1, z = 6]]

The 832 is the maximum number of GB.

There are a vast number of ways to do that. Here's one way:

FullSpeed:= 3600:  #whatever number you want
FullTorque:= 700:  #whatever number you want

Current:= (tq, sp)->
   #Put your equation for current here
   tq^2 * sp #I just made that up
:
P:= plot3d(Current, 0..FullTorque, 0..FullSpeed, grid= [11,11]);
interface(rtablesize= 11): #Allowed Matrix size to print.
Current_Matrix:= op([1,3], P);
#The row index varies the torque; the column, the speed.

 

Except for one small detail, this is a straightforward elementary textbook linear-programming problem. That detail is that the problem states the resource consumption in units per day, whereas we need days per unit. This is the reason for the 1 /~ in the code below.
 

restart:

Decision variables: F = fully assembled units, K = kits.

V:= <F,K>:

Profits by product per unit, in currency ($):

P:= <50,40>:

Resource consumption in days per unit, not units per day. Element (i,j) is resources used by department i to make 1 unit of product j.

C:= 1 /~ <200, 200; 100, 300>:

Resource availability by department in days:

R:= <1,1>:

Optimization:-LPSolve(P.V, {seq(C.V <=~ R), seq(V >=~ 0)}, maximize);

[8500., [F = HFloat(50.00000000000008), K = HFloat(149.99999999999991)]]

So, the maximum profit is $8500 when F = 50 and K = 150.

plots:-display(
   plots:-inequal({seq(C.V <=~ R), seq(V >=~ 0)}, seq(V =~ 0..200), color= pink),
   plots:-implicitplot(
      [P.V = 8500, seq(C.V =~ R)], seq(V =~ 0..200),
      color= [green, red, purple], thickness= [2,1,1],
      legend= [Profit, Fabrication, Assembly]
   )
);

 


 

Download LP.mw

It's a mystery to me why FrequencyTable doesn't offer that option. The similar command Statistics:-TallyInto does offer that option. If you need the output of TallyInto formatted similarly to FrequencyTable, let me know.

The matrix or any other structure or structures that can be assigned to a name can be saved by

save M "MyData.map";

where is the matrix and the quoted string is any filename of your choice. Then, in another worksheet, do

read "MyData.map":  #Use : to suppress lengthy output!

and then the name will have the same value as it had in the first worksheet. The above uses a plaintext file. If you read it with a regular text editor, it'll look like an elaborate Maple command starting M:=. If you change the file extension from .map to .m, a faster but non-plaintext file format will be used.

If contains references to other variables which are not being saved in the same batch, then there can be subtle differences between the .m and plaintext formats. These differences are too deep to explain here.

@Carl Love Here's the code for your most recently described sequence. It uses three auxilliary tables as well as a remember table. One of those tables, used, is declared sparse, which means that the table's entry at unused indices is 0.

restart:
a:= module()
export
   #Record index (position) where values occur, other than most recent.
   pos:= table([0= 0]),
   nextpos:= table([0= 1]), #most-recent position for each value. 
   used:= table(sparse, [0= 2]), #times value has been used
   ModuleApply:= proc(n::nonnegint)
   option remember;
   local p:= thisproc(n-1), r:= `if`(used[p] > 1, pos[p], thisproc(p-1));
      (pos[r], used[r], nextpos[r]):= (nextpos[r], used[r]+1, n); #shuffle down
      r
   end proc
;
   (ModuleApply(0), ModuleApply(1)):= (0,0)
end module
:
seq(a(k), k= 0..30);
0, 0, 0, 1, 0, 2, 0, 4, 1, 3, 0, 6, 2, 5, 0, 10, 3, 9, 1, 8, 4, 
7, 0, 14, 5, 13, 2, 12, 6, 11, 0

Some interesting patterns that I've noticed so far:

  1. a(2^n) = 2^(n-2) - 1  (generalized in #12)
  2. a(2^n+1) = 3*(2^(n-2)-1) = 3*a(2^n)
  3. a(2^n-1) = 3*2^(n-2)-2 = a(2^n+1)+1
  4. a(2^n-2) = 0
  5. a(2^n+2) = 3*2^(n-4) - 2
  6. a(2^n-3) = 3*2^(n-3) - 1
  7. a(2^n+3) = 3*2^(n-2) - 4
  8. a(2^n-4) = 2^(n-2) - 2 = a(2^n) - 1
  9. a(2^n+4) = 2^(n-2) = a(2^n) + 1
  10. a(2^n-5) = 3*2^(n-3)
  11. a(2^n+5) = 3*2^(n-2) - 5,  n > 3
  12. a(m*4^n) = m*4^(n-1) - 1, 
  13. a(3*2^n+1) = 2^(n+1) - 3,  n > 1
  14. a(3*(2^n-1)) = 2^n - 1
  15. a(3*2^n - 1) = 2^(n+1) - 2
  16. a(3*2^n - 2) = 0
  17. a(4*n+2) = a(n-1)  (generalized in #18)
  18. a(n) = a(4^m*(n+2) - 2)

Except where noted (#11 and #13), these apparently hold true for all values of the variables such that the exponents and sequence indices are nonnegative integers. 

While using even a large number of tables is faster than backtracking the sequence, I wonder if anyone can code the above with fewer auxilliary tables.

You wrote:

  • However, I've been told that maple doesn't run in a distributed-memory parallel sense;

The Grid package implements distributed-memory parallelism.

  • as parallelisation in maple is very new.

The Grid package has been around for many years and so has the Threads package (shared-memory parallelism). A related package, process, is so old that it's been deprecated. So it looks like what you've "been told" is very out of date.

 

I derived a one-line closed-form nonrecursive formula for your sequence:

A_nr:= (n::posint)-> (N-> `if`(n>3*N-2,7,5)*N-n-3)(2^(ilog2(n+1)-1)):
A_nr(0):= 0
:
seq(A_nr(k), k= 0..20);
   0, 1, 2, 4, 3, 6, 5, 10, 9, 8, 7, 14, 13, 12, 11, 22, 21, 20, 19, 18, 17

My previous Answer is still valuable because there are many other sequences that can't be put in a nonrecursive form and require memory of the previously computed values.

I didn't use Maple or any high-powered mathematics to derive the above formula, just basic arithmetic and about 45 minutes of mental perseverance.

It can be done efficiently with two tables, one a remember table (in the background), and one to record the values already used:

a:= module()
local
   a:= table([0= NULL, 1= NULL]), #values already used
   ModuleApply:= proc(n::nonnegint)
   option remember;
   local p:= thisproc(n-1), r:= p-1;
      if assigned(a[r]) then r:= 2*p fi;
      a[r]:= NULL; #Record that r has been used.
      r
   end proc
;
   ModuleApply(0):= 0; #Initialize remember table.
   ModuleApply(1):= 1  #    "         "       "
end module
:
seq(a(k), k=0..20); 
   0, 1, 2, 4, 3, 6, 5, 10, 9, 8, 7, 14, 13, 12, 11, 22, 21, 20, 19, 18, 17

 

 

To use NLPSolve, you need to make the objective function into a procedure which numerically evaluates the integral. The minimize command won't help.

f2:= (dat::listlist)-> add(dat[.., 1]) - 2*nops(dat);

or

f2:= proc(dat::listlist) local d; add(d[1] - 2, d= dat) end proc;

or

f2:= (dat::listlist)-> add(dat[..,1] -~ 2);

Note that the syntax dat[.., 1] considers dat as a two-column matrix, the first column being the one that you're interested in.

First 129 130 131 132 133 134 135 Last Page 131 of 395