Joe Riel

9660 Reputation

23 Badges

20 years, 5 days

MaplePrimes Activity


These are answers submitted by Joe Riel

It would be nice if ?FileTools had an export for creating a path to a filename, given its components.  ?AbsolutePath doesn't quite work here because you cannot specify more than two components (you could do AbsolutePath("Destop\\foo.txt", kernelopts('homedir'))).  That is, you'd want to do

FileTools:-CreatePath(kernelopts('homedir'), "Desktop", "foo.txt");

Using cat and manually inserting the directory separator isn't hard, and could be done with ?StringTools[Join] and kernelopts('dirsep'), but an export would be useful when automating this.

If the first argument is not a file ID, then it it must be a path to the file. That can either be relative to the current directory (use ?currentdir to determine what that is) or an absolute path.  I'm not a Windows user, so cannot tell you where the directory Desktop files is located.

What you might do, after figuring out where the Desktop directory lies, is to add the following assignment to your Maple initialization file (which you must create):

MyDesktop := "c:\\path_to_my_destop":

Then you should always be able to do

file := sprintf("%s\\somefile.txt", MyDesktop);
readdata(file, 2);

There appears to be a deficiency in the routine.  I'll submit an SCR.  You can work around it by converting to the TransferFunction form before executing BodePlot:

sys3:=DiffEquation(DGL, inputvariable=[y(t)], outputvariable=[x(t)]):
sys4 := TransferFunction(sys3);
BodePlot(sys4, thickness = 2);

A simple way to debug this is to enclose the script in a procedure and then run the procedure through Maple's syntax checker ?mint, an external program.  You can also use the less-powerful ?maplemint command from inside Maple.  For example:

check := proc()
    gam := 1.4;
    p[L] := 1;
    u[L] := 0;
    rho[L] := 1;
    p[R] := 1;
    u[R] := 1;
    rho[R] := 1;
    p1 = 1/2*(p[L]+p[R]);
    A[L] := 2/((gam+1)*rho[L]);
    A[R] := 2/((gam+1)*rho[R]);
    B[L] := (gam-1)*p[L]/(gam+1);
    B[R] := (gam-1)*p[R]/(gam+1);
    a[L] := (gam*p[L]/rho[L])^(1/2);
    a[R] := (gam*p[R]/rho[R])^(1/2);
    if p1 > p[L] then
        f[L] := (p1-p[L])*(A[L]/(p1+B[L]))^(1/2);
        df[L] := (A[L]/(p1+B[L]))^(1/2)*(1-(p1-p[L])/(2*(p1+B[L])));
    else
        f[L] := 2*a[L]*((p1/p[L])^((gam-1)/(2*gam))-1)/(gam-1);
        df[L] := (p1/p[L])^(-(gam+1)/(2*gam))/(rho[L]*a[L]);
    end if;
end proc:

maplemint(check);
    This equation was used as a statement:
      p1 = 1/2*p[L]+1/2*p[R]
    These names were used as global names, but were not declared:
      L, R, p1
    These local variables were assigned a value, but otherwise unused:
      df, f, u

The output correctly warns the user that an equation was used as a statement.

Using IsAnagram is a practical way to handle this.  However, a canned solution doesn't teach much.  Offhand, the method I'd use, if I had to implement this from scratch, would be to convert the two integers to lists of digits, sort each list, then compare the two lists.

Note that if one number has less digits than the other, you may have to add 0's to the shorter one.  That would allow 10 and 1 (01) to be anagrams.

You could also use the `/` function.  Combined with the ~ operator (Maple 14), you could do

L := [2,6,8]:
`/`~(L);
                               [1/2, 1/6, 1/8]


Actually, timing indicates that the string conversion is considerably faster, so you might want to stick with that. Something like

digitToInt := table([seq(sprintf("%d",d)=d, d = 0..9)]):

ToDigitsStr := proc(m :: posint );
local dig;
global digitToInt;
    [seq(digitToInt[dig], dig = StringTools:-Explode(sprintf("%a",m)))];
end proc:

You could use

convert(1234, base, 10);
                                        [4,3,2,1]

or do it yourself via

ToDigits := proc(m :: posint )
local q,k;
    q := m;
    [seq(irem(q,10,'q'), k=0..length(m)-1)];
end proc:

This means that the discrete equations in the model have a cyclic dependency (x->y->z->x) that prevents them from being properly ordered. One way to break such a loop is with a "pre" block (in the Signal/Discrete palette).  You might post the model or contact Maple support for further assistance.

It's wrong because the correct answer, for k an integer, is piecewise(k=0, 2*Pi, 0).

You could use ?gfun[rectoproc] to convert the recursion to a procedure, then apply that to a sequence.  For example,

(**) with(gfun):               
(**) L := [1,4,9,16,25]:                    
(**) rec := listtorec(L, f(n));             
   rec := [{(-n - 3) f(n + 3) + 4 f(n + 2) + (n + 3) f(n + 1), f(0) = 1, f(1) = 4, f(2) = 9}, ogf]

(**) F := rectoproc(rec[1],f(n),'remember');
         F := proc(n) option remember; -(-n*procname(-2 + n) - 4*procname(-1 + n))/n end proc

(**) seq(F(i), i=1..10);                    
                                4, 9, 16, 25, 36, 49, 64, 81, 100, 121

Faster is

subs([seq(t=NULL, t in t)],X);
c := [1,2]:
c := [c[], 3]:

Now add x in position i

c := [op(..i-1, c), x, op(i.., c)];

Alternatively, you could do

c := subsop(i = (x,c[i]), c);

What do you want to do with, say,

A := [2,2,2]:
B := [2]:

Do you want the empty list, or one with two 2's?  Or is this not a concern?

It would help if you indicated how you call the procedure, that is, what arguments you used.

First 60 61 62 63 64 65 66 Last Page 62 of 114