DuncanA

698 Reputation

8 Badges

15 years, 144 days

MaplePrimes Activity


These are answers submitted by DuncanA

Calling the Plot Builder inserts a call to plots[interactive]() into the Maple document.  Executing this call causes the 'Interactive Plot Builder' dialog to appear allowing expression(s) to be specified.  The plots[interactive]() call will be executed whenever the document is evaluated.  One way to prevent this is to delete the plots[interactive]() call after using the Plot Builder to output a 'plot' command instead of an actual plot.  In the following paragraph I explain in more detail how this can be done.

 

Calling the Plot Builder from the menu system inserts a call to plots[interactive]() into the Maple document.  If you are using Maple in Document Mode then the plots[interactive]() call will normally be invisible, but can be seen by expanding the Document Block (View->Expand Document Block).  Executing the plots[interactive]() call causes the 'Interactive Plot Builder' dialog to appear allowing expression(s) to be specified.  Clicking 'OK' on this dialog brings up another dialog where the plot type can be selected.  Clicking 'Plot' on this new dialog causes the plot to be displayed, but clicking 'Options' instead of 'Plot' causes a more advanced options dialog to be displayed where various advanced options can be selected.  Clicking 'Plot' on the options dialog will cause the plot to be displayed, but clicking 'Command' will return the 'plot' command used to produce the plot.  Copy the returned 'plot' command and paste it into a new paragraph or execution group your document.  Use Ctrl+Del to delete the plots[interactive]() call.  When the document is re-evaluated the plot will be displayed without the Plot Builder dialog.

 

---

Duncan

'iterationlimit' is an optional argument to the NLPSolve procedure which can be used to set the maximum number of iterations performed by the optimization algorithm.

?Optimization/NLPSolve 

?Optimization/General/Options 

---

Duncan

Maple help contains a worksheet for demonstrating the ImageTools package.

http://www.maplesoft.com/support/help/Maple/view.aspx?path=applications/ImageProcessing

The edge detection in this worksheet uses a gaussian kernel with variable sigma calculated using a procedure called 'makeMask'.  Download a copy of the worksheet, open it within Maple and from the menu bar Edit->Startup Code to see the 'makeMask' procedure.

---

Duncan

There doesn't appear to be a built in Maple function that calculates all possible partitions of a given set.  The attached worksheet contains an algorithm from Knuth that produces a "restricted growth string" representation of all possible partitions.  The output from this algorithm is then used to categorize a list of the integers 1 .. n producing all possible partitions.  The number of partitions is equal to the n'th Bell number.



 

restart;

delta := (a,b) -> `if`(a=b, 1, 0);  # Kronecker delta function

(1)

# Knuth, Donald E., The Art of Computer Programming (Fascicle 3B),
# Combinatorial Algorithms Generating All Combinations and Partitions,
# Section 7.2.1.5. Generating all set partitions

RestrictedGrowthStrings := proc(n)
    description "Algorithm H (Restricted growth strings in lexicographic order.)";
    local a, b, m, state, j, S;
    state := 1:
    S := {};
    while state > 0 do
        if state = 1 then
            a := Array(1 .. n, fill=0):  b := Array(1 .. n-1, fill=1):  m := 1:
            state := 2;
        elif state = 2 then
            if a[n] = m then
                state := 4
            else
                state := 3
            end if;
        elif state = 3 then
            a[n] := a[n] + 1;
            state := 2
        elif state = 4 then
            j := n - 1;
            while a[j] = b[j] do
                j := j - 1
            end do;
            state := 5
        elif state = 5 then
            if j = 1 then
                state := -1
            else
                a[j] := a[j] + 1;
                state := 6
            end if
        elif state = 6 then
            m := b[j] + delta(a[j], b[j]);
            j := j + 1;
            while j < n do
                a[j] := 0;
                b[j] := m;
                j := j + 1
            end do;
            a[n] := 0;
            state := 2
        else
            state := -1
        end if;
        S := S union {convert(a, list)};
    end do;
    S;
end proc:

n := 4:
S := RestrictedGrowthStrings(n);
nops(S);
combinat[bell](n); # The number of partitions of the integers 1 .. n is equal to the n'th Bell number.

 

 

(2)

# Use ListTools[Categorize] to partition the integers 1..n using restricted growth strings.
# For each element of set S, Categorize the elements of list L according to a routine that takes an element of S
L := [seq(i, i = 1 .. n)];
f := (a,b) -> s[a] = s[b]:
LL := NULL:
for s in S do
    t := ListTools[Categorize](f, L);
    LL := LL, [t]
end do:
LL;
nops([LL]);        # How many partitions are there?
combinat[bell](n); # How many partitions should there be?

 

 

 

(3)

 

 

 



Download set_partitions.mw

 

---

Duncan

From the menu system select Format->Styles, select the style you wish to modify (2D Math in this case), click the Modify button, and modify the style according to your preference.

---

Duncan

There appears to be an problem in the typesetting of the output.  Changing the method used to render the output produces differing results, one with parentheses, the other without.

restart:
with(Logic):
interface(prettyprint=1):
a &or (b &and c);
                              a &or (b &and c)
a &or b &and c;
                              (a &or b) &and c
interface(prettyprint=3):
a &or (b &and c);
                              a &or b &and c

a &or b &and c;
                              a &or b &and c

What I actually got when I pasted the typeset output (prettyprint=3) into this message was

a &or (b &and c);
                      Logic:-&or(a, Logic:-&and(b, c))
a &or b &and c;
                      Logic:-&and(Logic:-&or(a, b), c)

So the output is correct, but not typeset appropriately for a human reader.

---

Duncan

Suppress the output using a colon, assign the output to a variable and select a subset of the output using S[i..j]. See ?{}

 

with(combinat, setpartition) :
P := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} :
s:=setpartition(P, 1);
     {{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}}}
s:=setpartition(P, 2):
s[1 .. 3];
     {{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, 
      {{1, 2}, {3, 4}, {5, 6}, {7, 9}, {8, 10}}, 
      {{1, 2}, {3, 4}, {5, 6}, {7, 10}, {8, 9}}}
s:=setpartition(P, 5):
s[1 .. 3];
     {{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}, 
      {{1, 2, 3, 4, 6}, {5, 7, 8, 9, 10}}, 
      {{1, 2, 3, 4, 7}, {5, 6, 8, 9, 10}}}

 

---

Duncan

One option may be to expand the document block, i.e., put the cursor in the line then, from the "View" menu, select "Expand Document Block".  The contents of the expanded document block can then be edited.

Uploading the corrupted file will allow people to manipulate it and possibly provide a better answer.

---

Duncan

I would use a table.  If a symbol, e.g., Y is unassigned, a table will be created implicitly when the subscripted symbol is assigned to.

 

restart:

Y[t] := K[t]^a * L[t]^b;
K[t] := Y[t-1] - C[t-1];
C[t-1] := C[0] * (1 + gC[t-1])^t - 1;
gC[t-1] := k * Y[t-1];

(1)

# If symbol Y was unassigned, a table in created implicitly
type(Y, table);

(2)

 

 



Download 10_Implicit_table_cr.mw

Using a table in a loop allows previous values to be referenced.  You may or may not want to assign initial values for the boundary conditions.

 

restart:

C[0] := X1; Y[0] := X2; # Assign boundary conditions
for t from 1 to 3 do
  K[t] := Y[t-1] - C[t-1];
  Y[t] := K[t]^a * L[t]^b;
end do;

 

 

 

 

 

 

 

(1)

 

 



Download 20_A_table_for_diffe.mw

 


An Array can also be used, but note that unassigned values in an Array are initialised to zero and this may give incorrect results. 

 

restart:

C := Array(0 .. 2): Y := Array(0 .. 3): K := Array(1 .. 3):

C[0] := X1; Y[0] := X2; # Assign boundary conditions
for t from 1 to 3 do
  K[t] := Y[t-1] - C[t-1];
  Y[t] := K[t]^a * L[t]^b;
end do;

 

 

 

 

 

 

 

(1)

 

 



Download 30_An_Array_for_diff.mw

 

 

The 'fill' argument or an 'init' function can used to initialise otherwise uninitialised values.  The Array dimensions must be known in advance.

A Matrix or Vector is also possible, but these datatypes are indexed from 1 not 0 so the boundary point may need to be adjusted to reflect this.  The 'symbol' argument may be used for uninitialised values as an alternative to 'fill' or the 'init' function used with an Array.

'rsolve' may be used to find a solution to recurrence equations.

---

Duncan

@sasomao Judging by the statement from the core dump:

"machine is big endian but maple was not compiled so", 

I'm guessing that you have 
- a version of maple compiled for a machine with a little endian memory architecture, e.g., a PC, and you are trying to run this on a machine that has a big endian memory architecture, e.g., a Sun workstation. Or 
- perhaps you are using a library routine or some other precompiled code that was compiled for one architecture, but used on another, e.g., one of your own libraries that is not compiled for the host machine you are trying to use.

The code that you've written does not execute in parallel.

According to ?Threads[Task][Start], "The Start function creates a new Task and then waits for that Task to complete before returning the value returned by the Task."

Each call to Start, for example,

Threads:-Task:-Start(X1);

will run to completion before the next Task starts.

So, in your code

Threads:-Task:-Start(X1);
Threads:-Task:-Start(X2);
Threads:-Task:-Start(X1);
Threads:-Task:-Start(X2);

the tasks will run sequentially, not in parallel. The ?Threads[Task][Start] help page gives an example of how to use Start and Continue to execute tasks in parallel.

---

Duncan

To answer your questions,

1) str1 and str2 should not be 'global'. They should not even be declared inside of X1 and X2.


X1 := proc () local str, sid, b;
str := ""; sid := Open("google.com", 80); Write(sid, cat("GET /finance/historical?q=INDEXSP:.INX&histperiod=daily HTTP/1.0 \n\n")); 
b := Read(sid); while b <> false do str := cat(str, b); b := Read(sid) end do; 
Close(sid); str;
end proc:

X2 := proc () local str, sid, b;
str := ""; sid := Open("google.com", 80); Write(sid, cat("GET /finance/historical?q=INDEXEURO:.FCHI&histperiod=daily HTTP/1.0 \n\n")); 
b := Read(sid); while b <> false do str := cat(str, b); b := Read(sid) end do; Close(sid); str; 
end proc:

A more sensible thing to do is define a single function and pass the ticker symbol in as an argument

X := proc (symb) local str, sid, b;
str := ""; sid := Open("google.com", 80); Write(sid, cat("GET /finance/historical?q=", symb, "&histperiod=daily HTTP/1.0 \n\n")); 
b := Read(sid); while b <> false do str := cat(str, b); b := Read(sid) end do; 
Close(sid); str;
end proc:
Create(X("INDEXSP:.INX"), str1); 
Create(X("INDEXEURO:.FCHI"), str2);
str1;
str2;

 

2) str1 and str2 in your code were empty because after initializing them with the empty string ("") no other value was assigned to them.

3) If I were writing code to do what you are trying to do I would use Tasks. As stated in the ?Threads help page, "The Task interface is the preferred interface for developing multithreaded code."  Using Threads[Create] is more low-level.  According to my understanding of Maple's implementation, the Thread you create will not finish executing before control is returned to the caller so your code would have to cope with that situation.  Using Tasks avoids this problem.

4) I may answer that later.

---

Duncan

In both X1 and X2, the string read from the socket is assigned to variable 'b' and the contents of 'b' is then concatenated with variable 'str'. Finally, 'str' is returned from the procedure and this is the data you want.


The calls to Create should be


Create(X1(), str1);

Create(X2(), str2);

or something similar.  In this way, str1 and str2 will be assigned the values returned from X1 and X2.

In the original Socket programming example, b is a local variable, but in your example b is a global variable.  Making b a shared global variable is likely to lead to errors when separate threads are writing to the shared variable.  I would make b local to avoid problems with two threads writing to a shared variable.

---

Duncan

I couldn't replicate the error you mention, but I think you want the following

rtoc := (x,y,z) -> <sqrt(x^(2)+y^(2)) | arctan(y, x) | z>;

 

---

Duncan

Click on your username in the top right of the page and you will be taken to your own user page where links to all of your posts can be found.

---

Duncan

4 5 6 7 Page 6 of 7