Joe Riel

9660 Reputation

23 Badges

20 years, 22 days

MaplePrimes Activity


These are replies submitted by Joe Riel

Note that you can pass an initializer procedure to Array, it is called with the indices and the value it returns is used for the initial value at that location.  So you could do

Array(0..500,0..500, (i,j) -> `if`(j=0 and i::odd, i!/...,  0));

In this case, since only 1/1000 of the Array is initialized to a nonzero value, it is faster to explicitly initialize the first column using an external do-loop.

Note that you can pass an initializer procedure to Array, it is called with the indices and the value it returns is used for the initial value at that location.  So you could do

Array(0..500,0..500, (i,j) -> `if`(j=0 and i::odd, i!/...,  0));

In this case, since only 1/1000 of the Array is initialized to a nonzero value, it is faster to explicitly initialize the first column using an external do-loop.

Automatic simplification doesn't convert the different to a quotient.  I suspect sum is recognizing the sqrt function and handling it differently.  You could hide it with a pair of forward quotes (but I like the generic approach of using an inert function):

sum(2/''sqrt''(k+2)-2/''sqrt''(k+3),k=1..n); 
                                                  2           2
                                           - ----------- + -------
                                             sqrt(n + 3)   sqrt(3)
limit(%, n=infinity);
                                                     1/2
                                                  2 3
                                                ------
                                                  3


Automatic simplification doesn't convert the different to a quotient.  I suspect sum is recognizing the sqrt function and handling it differently.  You could hide it with a pair of forward quotes (but I like the generic approach of using an inert function):

sum(2/''sqrt''(k+2)-2/''sqrt''(k+3),k=1..n); 
                                                  2           2
                                           - ----------- + -------
                                             sqrt(n + 3)   sqrt(3)
limit(%, n=infinity);
                                                     1/2
                                                  2 3
                                                ------
                                                  3


restart;
with(Statistics):

# parameters
s[1]:=100;
d:=4;
n:=100;

# stock price
randomize();

# Assign table used to temporarily store results
T := table();
for cnt to 10 do
    rand_ := Sample(RandomVariable(Normal(0,1)),n);
    for i from 2 to n do s[i]:=s[i-1]+rand_[i] end do;
    A:=[seq(s[i],i=1..n)];

# Trailing stop-loss
    B[1]:=(A[1]-d);
    for i from 2 to n do
        B[i]:=`if`(A[i]>A[i-1] and A[i]>(A[i]-d) and (A[i]-d)>B[i-1],(A[i]-d),B[i-1])
    od;
    C:=[seq(B[i],i=1..n)];
    r[1]:=0: for i from 2 to n do r[i]:=`if`(A[i]>C[i],0,1) od;
    w:=[seq(r[i],i=1..n)];
    wp[1]:=0: for i from 2 to n do wp[i]:=(w[i]+wp[i-1]) od;
    w_w_:=[seq(wp[i],i=1..n)];
    ww[1]:=0: for i from 2 to n do ww[i]:=(w_w_[i]+w_w_[i-1]) od;
    w_w:=[seq(ww[i],i=1..n)];

# Final Corrections
    C_[1]:=(A[1]-d): for i from 2 to n do C_[i]:=`if`(w_w[i]=0,C[i],C_[i-1]) od;
    C_C:=[seq(C_[i],i=1..n)];

########### Results #########

# Exit period due to stop loss
    u[1]:=0: for i from 2 to n do u[i]:=`if`(w_w[i]=1,i,0) od;
    Exit_period:=convert([seq(u[i],i=1..n)],'`+`' );

# Exit price stop loss
    Exit_price:=`if`(Exit_period=0,0,C_C[n]);

# Last close stock price
    Last_close:=`if`(A[n]<0,0,A[n]);

# profit loss with stop loss
    prof_s:=`if`(Exit_price=0,Last_close-A[1],Exit_price-A[1]);

    T[cnt] := prof_s;  # store result in table
# profit loss without stop loss
    prof_:=Last_close-A[1];
#############################
end do:
# convert table to a list.
A := convert(T,'list');

restart;
with(Statistics):

# parameters
s[1]:=100;
d:=4;
n:=100;

# stock price
randomize();

# Assign table used to temporarily store results
T := table();
for cnt to 10 do
    rand_ := Sample(RandomVariable(Normal(0,1)),n);
    for i from 2 to n do s[i]:=s[i-1]+rand_[i] end do;
    A:=[seq(s[i],i=1..n)];

# Trailing stop-loss
    B[1]:=(A[1]-d);
    for i from 2 to n do
        B[i]:=`if`(A[i]>A[i-1] and A[i]>(A[i]-d) and (A[i]-d)>B[i-1],(A[i]-d),B[i-1])
    od;
    C:=[seq(B[i],i=1..n)];
    r[1]:=0: for i from 2 to n do r[i]:=`if`(A[i]>C[i],0,1) od;
    w:=[seq(r[i],i=1..n)];
    wp[1]:=0: for i from 2 to n do wp[i]:=(w[i]+wp[i-1]) od;
    w_w_:=[seq(wp[i],i=1..n)];
    ww[1]:=0: for i from 2 to n do ww[i]:=(w_w_[i]+w_w_[i-1]) od;
    w_w:=[seq(ww[i],i=1..n)];

# Final Corrections
    C_[1]:=(A[1]-d): for i from 2 to n do C_[i]:=`if`(w_w[i]=0,C[i],C_[i-1]) od;
    C_C:=[seq(C_[i],i=1..n)];

########### Results #########

# Exit period due to stop loss
    u[1]:=0: for i from 2 to n do u[i]:=`if`(w_w[i]=1,i,0) od;
    Exit_period:=convert([seq(u[i],i=1..n)],'`+`' );

# Exit price stop loss
    Exit_price:=`if`(Exit_period=0,0,C_C[n]);

# Last close stock price
    Last_close:=`if`(A[n]<0,0,A[n]);

# profit loss with stop loss
    prof_s:=`if`(Exit_price=0,Last_close-A[1],Exit_price-A[1]);

    T[cnt] := prof_s;  # store result in table
# profit loss without stop loss
    prof_:=Last_close-A[1];
#############################
end do:
# convert table to a list.
A := convert(T,'list');

Add a loop counter to the loop and replace the lprint with an assignment into the table:

T := table():
for cnt to nq do
... # lines not shown for clarity
   lprint(prof_s);
   T[cnt] := prof_s;
end do:
A := convert(T,'list');
Add a loop counter to the loop and replace the lprint with an assignment into the table:

T := table():
for cnt to nq do
... # lines not shown for clarity
   lprint(prof_s);
   T[cnt] := prof_s;
end do:
A := convert(T,'list');

For the most part, no.  The keyword 'dagtag' does not appear in many library procedures; I count 31.  I've checked all those and found a couple more that require forward quotes around the symbolic dagtag.  Also submitted those as bugs.

I was referring to the list of symbolic names of dagtags.  Except for the four that are protected, none are assigned anything (I believe), so there is no good reason to protect them.  Doing so merely takes these names out of the readily available namespace.  Adding forward quotes to the symbols in procedures that use these is sufficient. That is, one should do kernelopts(dagtag = 'INTPOS'). Safer is kernelopts('dagtag=INTPOS'), it doesn't really on dagtag itself being a protected symbol. 

That isn't too surprising.  There is no good reason to protect most of the names.  The proper solution is to use forward quotes around the unprotected names in the procedures.  ERROR and RETURN are (deprecated) Maple procedures.  DEBUG is used to start the debugger.  I believe TABLE is used for an inert form of tables [it is listed in the "undocumented protected names" help page].

Presumably an oversight.  I'll submit these as bugs...

convert/function does not evaluate the result, while `?()` does.

convert(sin,function,[Pi]);
                                    sin(Pi)

`?()`(sin,[Pi]);                           
                                       0

Also convert/function is not documented (the documentation for `?()` is not readily found, but exists, see ?use).  Note that the procedure convert/function is flawed in that it depends on an unprotected global (FUNCTION).
 

I see what you mean; I was fooled by the ability to edit the worksheet into thinking that it could communicate with the maple server.  Too bad Threads doesn't work here...

First 131 132 133 134 135 136 137 Last Page 133 of 195