Joe Riel

9660 Reputation

23 Badges

20 years, 5 days

MaplePrimes Activity


These are replies submitted by Joe Riel

@acer I haven't thought of anything significantly better. Negligibly better was looping through all indices using programming indices [A(j) with j from 1 to numelems(A)] and breaking on a hit.

Addendum: the difference isn't entirely negligible. Most of the time in the loop is generating Matrix B, not doing the comparison.  With that in mind, the for-loop is a better approach.

There is a minor conceptual problem with the test. With the given seed, the A matrix is

                                                       [53     74    79    68]
                                                       [                     ]
                                                       [36    100    11    55]
                                                       [                     ]
                                                       [52     36    81    22]
                                                       [                     ]
                                                       [64     56    33    39]

Because B's value are between -80 and 20, and we are looking for A[..] < B[..], this can only fail at index [2,3]. While that probably doesn't come into play, if the search order differed for different tests, the results would be skewed.

There is a minor conceptual problem with the test. With the given seed, the A matrix is

                                                       [53     74    79    68]
                                                       [                     ]
                                                       [36    100    11    55]
                                                       [                     ]
                                                       [52     36    81    22]
                                                       [                     ]
                                                       [64     56    33    39]

Because B's value are between -80 and 20, and we are looking for A[..] < B[..], this can only fail at index [2,3]. While that probably doesn't come into play, if the search order differed for different tests, the results would be skewed.

Here's the method I came up (I'm aware of the 'once' technique, but that is presumably a bit sophisticated for a homework assignment).

Unique := proc(L)
local i,p;
    [seq(`if`(member(L[i],L,'p') and p < i
              , NULL
              , L[i]
             )
         , i = 1..nops(L))];
end proc:


Unique([1,2,1,3]);
                              [1,2,3]

Here's the method I came up (I'm aware of the 'once' technique, but that is presumably a bit sophisticated for a homework assignment).

Unique := proc(L)
local i,p;
    [seq(`if`(member(L[i],L,'p') and p < i
              , NULL
              , L[i]
             )
         , i = 1..nops(L))];
end proc:


Unique([1,2,1,3]);
                              [1,2,3]

@GrecoRoman Here's one method of doing this, recursively. 

Reverse := proc(s :: string)
local half,len;
    len := length(s);
    if len <= 1 then
        return s;
    end if;
    half := iquo(len,2);
    cat(procname(s[half+1..-1]), procname(s[1..half]));
end proc:

The idea is that if we have a function, Reverse, that reverses a string, then to reverse, say "abcdef", we can do cat(Reverse("def"),Reverse("abc")).

@GrecoRoman Here's one method of doing this, recursively. 

Reverse := proc(s :: string)
local half,len;
    len := length(s);
    if len <= 1 then
        return s;
    end if;
    half := iquo(len,2);
    cat(procname(s[half+1..-1]), procname(s[1..half]));
end proc:

The idea is that if we have a function, Reverse, that reverses a string, then to reverse, say "abcdef", we can do cat(Reverse("def"),Reverse("abc")).

@Cody Because this is clearly a homework problem, I'm reluctant to answer it directly.  Rather, I'll answer a slightly different problem. Here is a recursive procedure that doubles each character in a string. A typical recursive procedure divides the problem into similar, but smaller problems, solves those, then combines the results to get the complete solution.  Here the input string is split into two substrings, and the characters in each substring are doubled and the results combined.  The splitting stops when a string of length 0 or 1 is passed to the procedure.  The result for that is easily computed.

Double := proc(s :: string)
local half,len;
    len := length(s);
    if len <= 1 then
        return cat(s,s);
    end if;
    half := iquo(len,2);
    cat(procname(s[1..half]), procname(s[half+1..len]));
end proc:

Double("abc");
                                   "aabbcc"

Note the use of ?procname in the procedure.  That is one way that a Maple procedure can call itself.

@Cody Because this is clearly a homework problem, I'm reluctant to answer it directly.  Rather, I'll answer a slightly different problem. Here is a recursive procedure that doubles each character in a string. A typical recursive procedure divides the problem into similar, but smaller problems, solves those, then combines the results to get the complete solution.  Here the input string is split into two substrings, and the characters in each substring are doubled and the results combined.  The splitting stops when a string of length 0 or 1 is passed to the procedure.  The result for that is easily computed.

Double := proc(s :: string)
local half,len;
    len := length(s);
    if len <= 1 then
        return cat(s,s);
    end if;
    half := iquo(len,2);
    cat(procname(s[1..half]), procname(s[half+1..len]));
end proc:

Double("abc");
                                   "aabbcc"

Note the use of ?procname in the procedure.  That is one way that a Maple procedure can call itself.

Determining what is wrong, without the model, is nearly impossible.  Could you upload the model to this site?  Use the green arrow.

@Alejandro Jakubi It was that use of the has that I was referring to. The example I had in mind was

selectfun(x(x) + x(t), x, x); 
                                          {x(t), x(x)}

The example you gave is better.

@Alejandro Jakubi It was that use of the has that I was referring to. The example I had in mind was

selectfun(x(x) + x(t), x, x); 
                                          {x(t), x(x)}

The example you gave is better.

I wasn't aware of ?selectfun. It is a simple specialization of ?indets.  Use showstat to inspect it:

showstat(selectfun);
selectfun := proc(e, f::{name, {list, set}(name)}, x::{name, {list, set}(name)}, $)
local answer;
   1   answer := indets(e,('specfunc')('anything',f));
   2   if 2 < nargs then
   3     answer := select(has,answer,x)
       end if;
   4   answer
end proc
 

If you look closely you might notice a subtle bug.

I wasn't aware of ?selectfun. It is a simple specialization of ?indets.  Use showstat to inspect it:

showstat(selectfun);
selectfun := proc(e, f::{name, {list, set}(name)}, x::{name, {list, set}(name)}, $)
local answer;
   1   answer := indets(e,('specfunc')('anything',f));
   2   if 2 < nargs then
   3     answer := select(has,answer,x)
       end if;
   4   answer
end proc
 

If you look closely you might notice a subtle bug.

It's not clear what you have done.  Using the green up-arrow to post a worksheet with the code will improve the likelihood of a relevant response.

First 57 58 59 60 61 62 63 Last Page 59 of 195