Joe Riel

9530 Reputation

23 Badges

20 years, 28 days

MaplePrimes Activity


These are answers submitted by Joe Riel

It means that there are two instances of the C component of Turbo in the model. That shouldn't happen, though its hard to say what went wrong. Could you upload the model?

One way to accomplish is to use Maple's ?setattribute function to tag each function value with its corresponding domain point.

X := [3,4,-1,2,4]:
f := x -> x^2:

Evaluate f at each x in X, and attribute the value with x. ?SFloat is needed because attributes cannot be applied to integers, but can be to floats


fX := [seq(setattribute(SFloat(f(x)),x), x in X)];
                         fX := [9., 16., 1., 4., 16.]

Compute the min and max values of fX, then get the attributes, which are the corresponding x values.


map(attributes, [min,max](fX));
                                    [-1, 4]

I don't know the purpose of SortX, but if you just want the 10 largest values you may be better off sorting the entire Matrix and picking off the largest 10 elements.  That can be done in one line, using ?ArrayTools[Alias] and the builtin ?sort procedure:

 sort(ArrayTools:-Alias(Ans,[nstock^2]),`>`)[1..10];

Note that ?dsolve permits defining parameters and then reassigning them.  For example


(**) sys := diff(x(t),t) = a*x(t) + b:
(**) 
(**) integ := dsolve({ sys, x(0) = x0}
(**)                 , numeric
(**)                 , 'parameters' = [a,b,x0]
(**)                ):
(**) 
(**) integ('parameters' = [1,2,3]):
(**) integ(10);
                                              [t = 10., x(t) = 110130.139885515]

(**) # Now reassign the parameters
(**) integ('parameters' = [1,1,0]);
                                                  [a = 1., b = 1., x0 = 0.]

(**) integ(10);
                                              [t = 10., x(t) = 22025.4312857109]

You could check the time-stamp.  That is, save the old one, then look to see if it changes. Use ?FileTools[ModificationTime] to get the time-stamp.

You probably should use ?irem and check for remainder 0. Once you find a factor of n, you should exit the loop. Also, rather than printing a value, return true or false.

NaivePrimeTest := proc(n)
local k;
    for k from 2 to isqrt(n) do
        if irem(n,k) = 0 then
           return false;
       end if;
   end do;
   return true;
end proc:

There probably is a nice way that I cannot immediately find.  Here is a less nice way,

J := frontend(Student:-VectorCalculus:-Jacobian
              , [map(rhs,[rossler_sys]), [x,y,z](t)]
              , [{`*`,`+`,list},{}]
             );
                              [ 0      -1       -1   ]
                              [                      ]
                         J := [ 1      a        0    ]
                              [                      ]
                              [z(t)    0     x(t) - c]

You can use ?FileTools[ReadDirectory].  For example, the following code creates a list of all files in the directory /tmp with extension .mpl and then reads them, as Maple scripts:

files := FileTools:-ReadDirectory("/tmp", 'returnonly' = "*.mpl"):
for file in files do read file; end do;

The notes in the pdf are confusing.  I don't see how t is related to m (the assignment m := t+1 makes little sense), and what defines the recursion when t (or is it m?) is less than 6?  Is A defined?  As a complete guess, I'd try something like

B := proc(M)
local m;
    if M < 6 then
        return 'B'(M)
    else
        m := iquo(M,2);
        if M :: even then
            return 1/(6 + A(m) - B(2*m-4) - B(2*m-1)*(4-B(2*m-3)));
        else
            return B(2*m)*(4-B(2*m-1));
        end if;
    end if;
end proc:

Gack, I just realized my implementation of bogosort was far too efficient.  The actual algorithm is supposed to shuffle the list until it is sorted, thus, O(n!). Fortunately, implementing the actual algorithm is much easier:

bogosort := proc(L :: list(numeric))
local l;
    l := L;
    while not ListTools:-Sorted(l) do
        l := combinat:-randperm(l);
    end do;
    return l;
end proc:
> time(bogosort(combinat:-randperm(5)));
                                     0.036

> time(bogosort(combinat:-randperm(6)));
                                     0.100

> time(bogosort(combinat:-randperm(7)));
                                     2.876

What is the purpose of that exercise?  The point of bogosort is to be inefficient.  As such, the following violates that principle by using an Array as the internal data structure; a far more inefficient method would subsitute into a list (thus limiting is "use" to 100 element lists, which probably isn't unreasonable).

bogosort := proc(L :: list(numeric))
local A,i,j,n,pick,sorted;
    A := Array(L);
    n := nops(L);
    if n<2 then return L end if;
    pick := rand(1..n);
    sorted := false;
    do
        # check whether Array is sorted
        sorted := true;
        for i to n-1 do
            if A[i] > A[i+1] then
                sorted := false;
                break
            end if;
        end do;
        if sorted then
            return [seq(A[i], i=1..n)];
        end if;

        # randomly pick two elements and sort them
        i := pick();
        j := pick();
        if i < j then
            if A[i] > A[j] then
                (A[i],A[j]) := (A[j],A[i]);
            end if;
        elif j < i then
            if A[j] > A[i] then
                (A[i],A[j]) := (A[j],A[i]);
            end if;
        end if;
    end do;
end proc:


bogosort(combinat:-randperm(100));

x^n with n a negative integer. 

Here's an improvement on the method I previously described.  To avoid touching most of the image a mask is created. 

with(ImageTools):

img1 := Read("watermark.jpg");
img2 := RGBtoGray(img1);
img3 := Threshold(img2,0.9);#View(img3);

# Create a mask.  First use a "bandpass" filter to remove all
# intensities not between 0.7 and 0.85.

bandpass := proc(x) if x < 0.7 or x > 0.85 then 0 else 1 end if; end proc:
mask := map[evalhf](eval(bandpass), img2);

# Now use convolutions followed by threshold operations to remove
# the fine lines.
kernel := Matrix(7,1,fill=1): mask := Threshold(Convolution(mask,kernel,weight=1), 0.5);
kernel := Matrix(1,7,fill=1): mask := Threshold(Convolution(mask,kernel,weight=1), 0.5);
kernel := Matrix(20, fill=1): mask := Threshold(Convolution(mask,kernel,weight=1),0.01);

# Apply the filter operation previously described.
filter := proc(x) if x < 0.7 then x else 1 end if; end proc:
img5 := map[evalhf](eval(filter), img2);

# Now use the mask, and its complement, to combine the filtered image
# with the original image.  Only the region in the mask is filtered.

img6 := Mask(img5, mask);
img7 := Mask(img2, Complement(mask));
img8 := evalhf(img6+img7);

# View and save the result
View(img8);

Write("nowatermark.jpg", img8);

Is the textfile a Maple script?  The ?read command works for that.  Maybe the problem you are having is entering the correct path.  Be aware that backslashes in Maple strings are used to escape particular characters (so "\t" is the tab character).  To enter an actual backslash character you have to escape it, that is, double it: "\\".

Here is a simple method that removes the watermark without too much lossage. This could be improved with masks, but requires more work.

First, let's try the really easy way, which uses the ?ImageTools[Threshold] procedure

with(ImageTools):
img1 := Read("watermark.jpg"):
img2 := RGBtoGray(img1): # convert to grayscale
img3 := Threshold(img2): # use Threshold function to remove watermark
View(img3);

That removes the watermark, but is a bit grainy.  Rather than using a pure threshold function, which moves the bits to either 0 or 1, lets first figure out where the intensity of the watermark lies by plotting its histogram.

watermark := GetSubImage(img1,275,125,30,30):  # zoom in on portion with watermark
Preview(watermark);
PlotHistogram(watermark);

The plot shows a spike between about 0.7 and 0.8.  The following filter leaves intensites that are below 0.7 alone, but moves those above that threshold to 1 (white).

filter := proc(x) if x < 0.7 then x else 1 end if; end proc:
img5 := map[evalhf](eval(filter), img2); # apply filter to grayscale image
View(img5);  # better than img3, lets save it
Write("nowatermark.jpg", img5);

Followup: Christopher222's use of the ?ImageTools[Clip] procedure is a simpler way to apply the filter I manually created above.

First 74 75 76 77 78 79 80 Last Page 76 of 114