Joe Riel

9660 Reputation

23 Badges

20 years, 7 days

MaplePrimes Activity


These are answers submitted by Joe Riel

By assigning a neutral operator for the ?apply procedure, Maple 13's tilde can be used to separately process expression sequences, which are then recombined with a multiplication:

expr := -(1/12)*(-g*vb-g+z*g-g^(2/3)*z+g^(2/3)*vb)*(g^(1/3)+1)^5/g^(2/3):
`&apply` := apply:
`*`((expand, x->x) &apply~ selectremove(has, expr, {2/3,-2/3}));
                (1/3)       (1/3)    (1/3)               (1/3)     5
             (-g      vb - g      + g      z - z + vb) (g      + 1)
           - -------------------------------------------------------
                                       12

Using ?ArrayTools[IsEqual] avoids creating the intermediate array.

There is no need for the inner seq here, instead you can use the optional third argument to ?seq to have it step by 2:

L := [seq(1..12)]:
[seq([L[i..i+1], i=1..nops(L), 2)];
              [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]

While it presumably isn't what your instructor wants, the ?ListTools[LengthSplit] command can do most of the work:

[ListTools:-LengthSplit(L,2)];
               [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]

Do you want to simulate the game, that is, use a monte-carlo approach?  Or analyze it? 

What strategy maximizes the expected score of a turn?  This is a straight-forward, simple computation.

Here's are some possibly interesting variants:

  1. Two players each take one turn.  The player with the higher score after that turn is the winner.  Neither player knows the others score until the end.  Is there an optimal strategy? 
  2. Same as 1, but this time player 2 knows player 1's score.  What is player 1's optimal strategy? 

The do-loop as formulated is O(nops(a)^2) because it generates n versions of a list of n-elements.

 

One way is to terminate them with colons, see ?separators.

I didn't realize that there was a difference in evaluation using programmer indexing (see ?rtable_indexing); probably that is a bug. Change gradV(1,1) to gradV[1,1].  Note that the backquotes used around distributed do nothing; they should be forward quotes to delay evalution.

Use seq to create the list:

L := [seq(1..5)]:

You could use a for loop to print them

for item in L do
    print(item);
end do:

Simpler is to map print over the list (here I use lprint):

map(lprint, L):

In Maple13 I do

lprint~(L):

See the help page ?worksheet,documenting,styles.

Not a proof, but may lead you to one

convert(tan(phi)*tan(phi+Pi/2), 'sincos');
                                                                      -1

There is no good reason to recompute D(f) and (D@@2)(f) inside the loop.  Better to assign them to local variables.  You may then notice that there is a problem, no argument is passed to (D@@2)(f).  If you also assign some intermediate computations, you can then quickly see where things are going wrong by tracing the procedure call (i.e. call debug(NEWTON_RALPH) before evaluating.
 

What is is 'n' doing there?  It is in the snippet ( f(x)^2) n

 

z := 0.7*x + 5e-10*y; 
fnormal(z);                                
                                                                    0.7 x

Here's one approach,

proc()
local dsys, data, w;
    dsys := {diff(v(t),t) = -x(t), diff(x(t),t)=v(t), x(0)=1, v(0)=0};
    data := dsolve(dsys, 'numeric', 'output'=Array([seq(0..2, 0.1)]));
    w := 15;
    printf("%*s\n", w,  map(convert, data[1,1], string));
    printf("%*f\n", w, data[2,1]);
end proc():
              t            v(t)            x(t)
       0.000000        0.000000        1.000000
       0.100000       -0.099833        0.995004
       0.200000       -0.198669        0.980067
       0.300000       -0.295520        0.955336
       0.400000       -0.389418        0.921061
       0.500000       -0.479426        0.877583
       0.600000       -0.564643        0.825336
       0.700000       -0.644218        0.764842
       0.800000       -0.717356        0.696707
       0.900000       -0.783327        0.621610
       1.000000       -0.841471        0.540302
       1.100000       -0.891207        0.453596
       1.200000       -0.932039        0.362358
       1.300000       -0.963558        0.267499
       1.400000       -0.985450        0.169967
       1.500000       -0.997495        0.070737
       1.600000       -0.999574       -0.029200
       1.700000       -0.991665       -0.128844
       1.800000       -0.973848       -0.227202
       1.900000       -0.946300       -0.323290
       2.000000       -0.909298       -0.416147

As a bous, with Maple13 you can use convert~(data[1,1], string) to convert the elements in the Vector stored in data[1,1] to a string.  A slightly more interesting method is to assign an inline operator for convert and then use it with the ~ operator:

`&c` := convert:
data[1,1] &c~ string;

You might read www.mapleprimes.com/blog/joe-riel/maple-overload, which discusses some of the issues with overload and how to best handle them.  If you do use overloading, I suggest you use the following general format (here given as an example module):

mymodule := module()
export g;
local f, InvalidInput;
    InvalidInput := proc() cat("\n  ", StringTools:-FormatMessage(lastexception[2..-1])) end proc;
    g := overload ( [NULL
                     , proc(R::list , S::list, $)
                       option overload;
                           R, S;  # verify all parameters
                           try
                               print("this is the version with TWO lists");
                               (* actual code *)
                               f(1/2);  # an internal call with invalid input
                           catch "invalid input:" :
                               error InvalidInput();
                           end try
                       end proc

                     , proc(R::list, $)
                       option overload;
                           R; # verify all parameters
                           try
                               print("this is the version with ONE list");
                               (* actual code *)
                           catch "invalid input:" :
                               error InvalidInput();
                           end try
                       end proc
                    ]);
    f := proc(x::integer) x/3 end proc:
end module:

mymodule:-g([],[]);
                     "this is the version with TWO lists"

Error, (in g) 
  invalid input: f expects its 1st argument, x, to be of type
integer, but received 1/2

The reason for the try/catch statements, with the call to InvalidInput that slightly modifies the error message, is to trap and reraise error messages of the form "invalid input:" but in a form that does not trigger the overload switch mechanism.  To see this, comment out the call to InvalidInput in the first procedure; you will then get

mymodule:-g([],[]);
                     "this is the version with TWO lists"

Error, invalid input: no implementation of g matches the arguments in call,
g([],[])

 

 

 

First 79 80 81 82 83 84 85 Last Page 81 of 114