Joe Riel

9660 Reputation

23 Badges

20 years, 3 days

MaplePrimes Activity


These are answers submitted by Joe Riel

I don't have a complete, coherent answer.  My surprise is that the call before the restart works. You probably won't be able to use ModuleApply in the fashion you desire. As you undoubtedly know, you can do the following.  Add the static export

Apply :: static := proc( self :: ModularArithmetic, n :: posint )
    modp(n, self:-p);
end proc:

Then use it as

 

m7 := NewModulo(7):
Apply(m7, 42);
                            0

That will work before and after the restart.

Assuming a is nonnegative, then you want to use the floor.

Carl's suggestion of using an Array of records is a good one, given what we know.  Depending on what you want to do, there could be more efficient (however defined) alternatives. If all the cells have the same set of characteristics, you could assign the finite characteristics to slots in another dimension of the Array. An advantage of that is that you can then cheaply access arbitrary slices of the data.

A record is another possibility.  If you are creating a package with multiple procedures, using records as output is convenient in that the format is partially self-documenting, provided you pick appropriate field names for the record. To return a record you can do

return Record('f' = fval, 'g'=gval);

where fval and gval have been assigned values.

Insert the statement 

k;

after the loop.  If this is part of a larger procedure, you would, instead, use 

print(k);

Better might be to add a message.  One way to do that is to use printf:

printf("value of k is %a\n", k);

 

Do you want to print them as ascii text, with no 2D math?  In that case you could do something like

val := [0,0,1]:

printf("%d%d%d\n", op(val));
001

If you would rather print the result using 2D output, you could do

nprintf("#mn(\"%d%d%d\");", op(val));
                                         001

You would combine that with the rest of the equation: P(...) = ... ; where the first ellipsis represents the nprintf expression, the second whatever you intend on the rhs.

 

 

I used setattribute to save the index of a value with the value, took the minimum of the chosen values, then used attributes to return its attribute, which is the index.

argmin := proc(d :: list, U :: set(posint))
local u;
attributes(min(seq(setattribute(evalf(d[u]),u), u=U)));
end proc:

argmin([10,7,4,2,5,7],{2,3,6});
                                                  3

You can use the Iterator package in the application center:

with(Iterator):
C := CartesianProduct( L1, L2, L3, ... ):  # L1, L2, etc, are the lists to iterate through
add(f(c), c in C);

If you actually need to iterate through all the values, you can do

for c in C do f(c) end do:

Note that c is assigned a 1D dimensional Array, so f would have to be written accordingly.

Note that the symbol e is not assigned in Maple.  As Carl does, use the function exp for the exponential function.

You can create, say, 25/100, as a Maple rational, without using symbols or the empty function, however, because the resulting expression is nonstandard (created via hacking Maple's internal format), there is no guarantee that Maple will correctly process it. Here is how it can be done.

f := (i::integer,j::integer) -> sscanf(sprintf("#%m%m",i,j),"%m")[]:

f(25,100);
                      25/100

%*2;
                       25/50

%/5;
                        5/50

f(0,0);
                         0/0

%+1;
                          0

There is a way to partially do what you want, without using subs.  The parameter modifier depends can be used here.

F := proc(m :: `module`)
   ( x :: depends('Vector'(m:-dim))) -> something;
end proc:

M := module() export dim := 2; end module:
fm := F(M);
                  fm := (x::depends('Vector'(M:-dim))) -> something

fm(<a,b>);
                  something

fm(<a,b,c>);
                  error, invalid input, fm expects ... 

 

Note that the declaration depends on the value of M:-dim at the time of execution, so

M:-dim := 4:
fm(<a,b>);
                error, invalid input, fm expects ...
fm(<a,b,c,d>);
                something

If you really want the M:-dim evaluated at the time the procedure is created, you could use unapply, however, that might not be practical with your real procedure.  Here it can be done as

unapply(something, x :: 'Vector'(M:-dim));
                                 (x :: Vector(4)) -> something

 

The usual way to do this is slightly nasty looking.

 

F := proc(m :: `module`)
  subs('_d' = m:-dim, (x::Vector(_d)) -> something);
end proc:

 If you are using a recent version of Maple, you might consider using objects rather than modules for M. 

Here's an implementation using a custom iterator.  A Maple iterator is implemented with an object that exports the ModuleIterator procedure.  That procedure returns two procedures: a predicate, hasnext, that returns true/false depending whether the iteration should continue, and a procedure, getnext, that returns the next value.  In this implementation hasnext does all the work; getnext just returns the Array that is reused for each iteration.

RoundRobin := module()
option object;
local number := 5;
export
    ModuleCopy :: static := proc( self :: RoundRobin
                                  , proto :: RoundRobin
                                  , { number :: posint := proto:-number }
                                  , $
                                )
        if number < 3 then error "must have at least 3 competitors"; end if;
        self:-number := number;
        self;
    end proc;
    
export
    ModuleIterator :: static := proc( self :: RoundRobin )
    local A,n,hasnext,getnext,first,is_odd;
        if self:-number :: odd then
            n := self:-number+1;
            is_odd := true;
        else
            n := self:-number;
            is_odd := false;
        end if;
        
        A := Array(1..n, i->i, 'datatype'=integer[4]);
        if is_odd then A[n] := 0; end if;
        first := true;
        getnext := proc()
            A;
        end proc;
        hasnext := proc()
            if first then
                first := false;
                return true;
            else
                A[..] := A[[1,4,2,seq('(i+3,i)',i=3..n-3,2),n-1]];
                return evalb(A[2] <> 2);
            end if;
        end proc;
        (hasnext,getnext);
    end proc;
    
end module:

# Example of usage

PrintPairings := proc(n :: posint)
local m,RR,rr,rnd;    
    RR := Object(RoundRobin, number=n):
    rnd := 1;
    for rr in RR do
        printf("Round %d:\n", rnd);
        for m to n by 2 do
            if rr[m]=0 then
                printf("  %d sits out\n", rr[m+1]);
                next;
            elif rr[m+1]=0 then
                printf("  %d sits out\n", rr[m]);
                next;
            else
                printf("  %d vs %d\n", rr[m],rr[m+1]);
            end if;
        end do;
        rnd := rnd+1;
    end do:
    NULL;
end proc:

PrintPairings(7):
Round 1:
  1 vs 2
  3 vs 4
  5 vs 6
  7 sits out
Round 2:
  1 vs 4
  2 vs 6
  3 sits out
  5 vs 7
Round 3:
  1 vs 6
  4 sits out
  2 vs 7
  3 vs 5
Round 4:
  1 sits out
  6 vs 7
  4 vs 5
  2 vs 3
Round 5:
  1 vs 7
  5 sits out
  6 vs 3
  4 vs 2
Round 6:
  1 vs 5
  7 vs 3
  2 sits out
  6 vs 4
Round 7:
  1 vs 3
  5 vs 2
  7 vs 4
  6 sits out


Use the symbol `&delta;x`.  To ease the typing you might assign it to dx, or alias it to dx, say via alias(`&delta;x`=dx). So you could do

alias(`&delta;x`=dx, `&Delta;x`=Dx):

and then type dx and Dx to display the two forms of interest.

Purists might object to the italicized Delta.  You could use the more complicated form `#mrow(mo("&Delta;"),mi("x"))`.  Using mo, the MathML math-operator form, is a hack; it makes more sense to use mi("&Delta;",fontstyle="normal"); alas, Maple's 2D formatter sets that as italic.

You can use the following technique to convert a fancy Math expression to a Maple symbol. In a 2D Math region, use the palettes to create the symbol you want.  Select it, right-click, and from the context-senstive menu, select 2D Math --> convert to --> Atomic Identifier.  Again select the symbol, but this time pick 2D Math --> Convert to --> 1D Math Input.  The result should be a symbol, in 1D Maple input, that displays as the typeset symbol of interest.

As Markiyan notes, an identity map is an endomorphism.  While not answering your question, note that endomorphism is an adjective, that is, it describes a property of some maps.  An identity map specifies a particular map (given the domain).

First 29 30 31 32 33 34 35 Last Page 31 of 114