Carl Love

Carl Love

28095 Reputation

25 Badges

13 years, 100 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

A very often-discussed topic here on MaplePrimes is a better alternative for the awkward and slow combinat:-cartprod

L:= [ [ [1,2], [2,1] ], [ [3,4], [4,3] ], [ [5] ] ]:

CartProd:= proc(L::list(list))
local S, _i, V:= _i||(1..nops(L));
     [eval(subs(S= seq, foldl(S, [V], (V=~ L)[])))]
end proc:

(op~)~(CartProd(L));

Between the two inner loops you have two statements. The second of those statements needs a semicolon at its end.

Yes, there's a three-argument operator form of if`if`. It's very similiar to the (...?...:...) operator in C. For example:

seq(`if`(k::prime, 1, 0), k= 1..10);

     0, 1, 1, 0, 1, 0, 1, 0, 0, 0

What's a little more difficult in Maple is an embedded assign-and-increment statement such as your idx+= 1. This can be implemented like this:

`&+=`:= proc(x::evaln, inc::algebraic)
     assign(x, eval(x)+inc);
     eval(x)
end proc:

idx:= 1:
seq(`if`(i > 2, idx &+= i, idx+i), i= 1..10);

    2, 3, 4, 8, 13, 19, 26, 34, 43, 53

It's fairly easy. Here's a worksheet for it.


Finding the permutation that transforms one list into another

restart:

We start with a basic alphabet. This will work with any list of distinct elements.

L:= [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]:

We scramble it two ways. This will simulate your original two lists (which, of course, must be permutations of each other for this to work).

L||(1..2):= 'combinat:-randperm(L)' $ 2;

[c, h, i, l, g, q, u, k, v, x, m, y, b, s, n, a, r, j, t, p, f, z, o, w, e, d], [e, j, q, w, z, m, f, t, y, i, s, u, v, x, b, c, n, k, o, r, h, a, p, l, d, g]

Now we find the permutation that transforms L1 to L2.

Pdjc:= (`convert/disjcyc`@sort)~([L1,L2], output= permutation):

Pdjc1to2:= group:-mulperms(group:-invperm(Pdjc[2]), Pdjc[1]);

[[1, 25, 26, 5, 22, 16], [2, 18, 8, 19, 23, 20, 17, 15, 13, 9, 12, 7, 21], [3, 6, 11, 14, 10], [4, 24]]

That's the permutation that we want expressed in disjoint cycle (disjcyc) notation. To use it as an index, it needs to be in permlist notation.

P:= convert(Pdjc1to2, permlist, nops(L1));

[25, 18, 6, 24, 22, 11, 21, 19, 12, 3, 14, 7, 9, 10, 13, 1, 15, 8, 23, 17, 2, 16, 20, 4, 26, 5]

Finally, we show that P does indeed transform L1 into L2:

L1[P];

[e, j, q, w, z, m, f, t, y, i, s, u, v, x, b, c, n, k, o, r, h, a, p, l, d, g]

evalb(% = L2);

true

 

Now I put that together into a procedure that returns the permutation in disjcyc notation. If you need permlist notation, it's just a trivial change to the last line.

Perm1to2:= proc(L1::list, L2::list)
uses G= group;
local
     S1:= {L1[]},     
     Pdjc:= (`convert/disjcyc`@sort)~([L1,L2], output= permutation)
;
     if S1 <> {L2[]} or nops(L1) <> nops(S1) then
          error "Invalid input: Lists must have distinct elements and be "
               "permutations of each other"
     end if;
     G:-mulperms(G:-invperm(Pdjc[2]), Pdjc[1])
end proc:

 


Download permL1toL2.mw

Use the command Elements:

Elements(P2);

lprint(%);

{Perm([]), Perm([[1, 3, 5]]), Perm([[1, 5, 3]])}

Using the example that you provided, and automating the process of making the assumptions:

B:= [a-1, b+2, b-c, a*c-1]:
f:= a^2*c-a*c-a+1:
is(f <> 0) assuming (B <>~ 0)[];

     true

Unlike assume, assumptions made with assuming only last for duration of one command.

Since the x doesn't explicitly appear in g, you'd need to use

f:= unapply(piecewise(x<0, 0, x>0, g), x);

If you use the arrow form, then the x that appears in g isn't the same variable as the x to the left of the arrow: The former is a global variable, and the latter is a parameter.

This is almost certainly on a list of the top ten most-common Maple errors.

The button that you want is already there. It's the !!! in the middle of the top row of the tool bar.

The documentation about this use of % is at ?value.

The entire purpose of the command

%piecewise(``, x+y-2*z = 1, ``, 2*x+y-3*z = 5, ``, -x+y+z = 1);

is to achieve a certain prettyprinted effect: the equations in a column with a large curly brace on the left side. This was never intended to be converted from its inert form to active form. This command has no relation mathematically or computationally to the piecewise command; its only relation to piecewise is that its author wanted to display some equations in a manner similar to the way that Maple displays piecewise functions.

The name u remains---and must remain---entirely symbolic after the call to dsolve. Indeed, it is unusual (although not impossible) for any command to change the value of a name without an explicit assignment statement. If you do want a function that returns numeric evaluations of u, do this:

U:= X-> eval(u(x), num_n(X));

Now U(1) will return 1.

To load the contents of an audio file into a numeric Vector, use the command AudioTools:-Read (see ?AudioTools,Read).

If your variables were actually indexed as j[4] rather than as j_4, then the Answer of AmirHosein would suffice. To work with the actual j_4, etc., you need to use type suffixed. For example,

M:= mul(j_||k^rand(0..2)(), k= 0..15);

PrefLen:= length(j_); #Prefix length of `j_`

J:= indets(M, suffixed(j_, nonnegint));

add(parse(substring(j, PrefLen+1..-1))*degree(M,j), j= J);

     67

You can save them anywhere. I put them all in one directory (or folder) called "C:/Maple_packages". But you can put them in separate directories (or folders). To activate a package, all you have to do is append its directory name to the predefined global sequence libname:

libname:= libname, "C:/Maple_packages";

Now you can access the commands and the help pages of the packages just like you do with any regular Maple package.

Make sure that you append to libname rather than overwrite it. DON'T do

libname:= "C:/Maple_packages"; #DON'T DO THIS!!!

And you probably shouldn't do this either:

libname:= "C:/Maple_packages", libname; #Don't do this unless you really know what you're doing.

Once you're satisfied that the package is something that you'd like to use regularly, you can put the command which appends to libname into an initialization file, and then you can pretty much forget about it: Those add-on packages will be just like regular Maple packages.

(The Reply was updated for clarity due to Preben's Reply below.)

If RR1 and RR2 are two RealRanges, then RR1 being a subset of RR2 is equivalent to

is('x' in RR2) assuming 'x' in RR1;

Of course, the quotes can be omitted if you know that x is unassigned.

Operator in can be replaced with operator ::, which saves typing four spaces:

is(x::RR2) assuming x::RR1;

(Don't take this to mean that in can usually be replaced by ::. It usually can't be.)

Yes, it's possible to get the entire history of optimizations done with the Optimization package (procedures Maximize, Minimize, LPSolve, and NLPSolve) by using the procedural form of input (i.e., the objective and constraints are specified by procedures rather than expressions). See ?Optimization,General,OperatorForm. A crude way to do this would be to print out the values of the arguments inside the procedures. A more refined way is to increment an index and save those values to a table or Array. I'll post an example later.

First 246 247 248 249 250 251 252 Last Page 248 of 395