Joe Riel

9660 Reputation

23 Badges

20 years, 20 days

MaplePrimes Activity


These are replies submitted by Joe Riel

Your procedures have problems. It is not immediately clear what they are supposed to do, so I'm not going to suggest a correction, however, I will explain the errors I see.

First off, assigning a Maple procedure doesn't execute it.  You have to call it to do something.  Maybe you do that elsewhere, but in the worksheet there are no calls to the procedures Y, B1, and B2.  A call to a procedure uses parenthese, not square brackes.  That is, you would do B1( ... )  to call procedure B1 with the an appropriate argument sequence (here shown as ... ).

Consider the assignment to Y

Y := proc (m) 
local i, B, A, Y;
global t; 
    Y[i] := [];
    for i from -2 to t+2 do
        Y[i] := (-Y[i+2]+Y[i+1]*(4-B[2*i-2]*(4-B[2*i-3])))/(6+A[i]-B[2*i-4]-B[2*i-1]*(4-B[2*i-3]));
        Y[i] := [op(Y[i], i)] 
    end do 
end proc:

The first executable statement in Y is the assignment

  Y[i] := []

While that is syntactically correct, and will do something, it probably doesn't do what you expect.  It merely assigns the empty list to the entry in a table Y with index i (a local variable).  What you probably want to do here is to initialize an Array, maybe with zero values. You could do that with

  Y := Array(-2..t+2);

Note that you are using a local variable named Y that matches the global variable Y, which is the procedure name. That is perfectly legal, however, it might not be a wise choice.  Maybe you should name the procedure CreateY, then there would be less confusion.

The body of loop references A and B, which are declared as local variables.  That probably is not what you want.  Maybe they should be global? Better yet, they probably should be parameters to this procedure.

The procedure doesn't explicitly return anything, which means that the return value is the last thing computed,  Here that would be whatever was assigned to Y[t+2].  Probably you want to return the Array Y.  That can be done by adding

  return Y

after the end of the loop. If you want a list as output you can convert it to a list, say convert(Y, list).

If I knew what you really intended I could probably give better suggestions...

That is a common misconception.  In fact, you will find that a loop is essentially as efficient as your proposal (sans the dummy loop). For example,

$define TU [time(),kernelopts('bytesused')]
$define tic tu := TU
$define toc TU-tu
$define N 400

restart;
tic:
B := Array(1..N,1..N,(i,j)->i*3^(j-1));
toc;
                                [1.168, 8321826]

restart;
tic:
B := Array(1..N,1..N):
for i to N do
    for j to N do
        B[i,j] := i*3^(j-1);
    end do;
end do:
toc;
                                [1.264, 8564416]

Of course, I'd use the procedure in the Array constructor because it is shorter and clearer. I wonder what the instructor would say about the cute method of meeting the requirement 8-).
 

That is a common misconception.  In fact, you will find that a loop is essentially as efficient as your proposal (sans the dummy loop). For example,

$define TU [time(),kernelopts('bytesused')]
$define tic tu := TU
$define toc TU-tu
$define N 400

restart;
tic:
B := Array(1..N,1..N,(i,j)->i*3^(j-1));
toc;
                                [1.168, 8321826]

restart;
tic:
B := Array(1..N,1..N):
for i to N do
    for j to N do
        B[i,j] := i*3^(j-1);
    end do;
end do:
toc;
                                [1.264, 8564416]

Of course, I'd use the procedure in the Array constructor because it is shorter and clearer. I wonder what the instructor would say about the cute method of meeting the requirement 8-).
 

Yes.

I believe that the handling of forward slashes as directory separators depends soley on the OS, that is, Maple does nothing special.  They work because the Windows functions that Maple calls interprets them appropriately.  You might have a problem if the basename contained a forward slash (which is a bad idea, regardless).  I don't have a Windows machine to play with.

Yes.

I believe that the handling of forward slashes as directory separators depends soley on the OS, that is, Maple does nothing special.  They work because the Windows functions that Maple calls interprets them appropriately.  You might have a problem if the basename contained a forward slash (which is a bad idea, regardless).  I don't have a Windows machine to play with.

What error did you get?  If you copied the example (fprintf) verbatim you may get an error because the path given, "/tmp/bits.dat" does not exist on your machine.  Change it to something appropriate for your directory structure. Say

bin := [0,1,0]:
file := "c:\\my_data_files\\bits.dat": # use a path that exists. 
                                       # Backslashes are directory separators for Windows
fprintf(file, "%a\n", bin);
fclose(file);

What error did you get?  If you copied the example (fprintf) verbatim you may get an error because the path given, "/tmp/bits.dat" does not exist on your machine.  Change it to something appropriate for your directory structure. Say

bin := [0,1,0]:
file := "c:\\my_data_files\\bits.dat": # use a path that exists. 
                                       # Backslashes are directory separators for Windows
fprintf(file, "%a\n", bin);
fclose(file);

The Bits package was released in Maple12.  You could use Robert Israel's suggestion, or the following

int2bin := proc(m::nonnegint, bits::posint)
local q,i,L;
    q := m;
    L := [seq(irem(q,2,'q'), i=1..bits)];
    [seq(L[-i], i=1..bits)]; # ListTools:-Reverse will also work here
end proc:

Use map to apply this to elements in a list:

for k in [12,16] do
    v[k] := map(int2bin, a[k], 10);
end do;

The Bits package was released in Maple12.  You could use Robert Israel's suggestion, or the following

int2bin := proc(m::nonnegint, bits::posint)
local q,i,L;
    q := m;
    L := [seq(irem(q,2,'q'), i=1..bits)];
    [seq(L[-i], i=1..bits)]; # ListTools:-Reverse will also work here
end proc:

Use map to apply this to elements in a list:

for k in [12,16] do
    v[k] := map(int2bin, a[k], 10);
end do;

Is this OSX (i.e. *nix)? Then use forward slashes rather than backslashes (which generally work in Windows anyway).

You could use a for loop. Something like
 

for i to n do
  filename := sprintf("/home/me/data/data.%d", i); # create data.i
  data[i] := readdata(filename);
end do:

data[1] now contains the data from file data.1, etc.
 

You could also do this in one line with an Array:

data := Array(1..n, i -> readdata(cat("/home/me/data/data.", i)));

 

Is this OSX (i.e. *nix)? Then use forward slashes rather than backslashes (which generally work in Windows anyway).

You could use a for loop. Something like
 

for i to n do
  filename := sprintf("/home/me/data/data.%d", i); # create data.i
  data[i] := readdata(filename);
end do:

data[1] now contains the data from file data.1, etc.
 

You could also do this in one line with an Array:

data := Array(1..n, i -> readdata(cat("/home/me/data/data.", i)));

 

I don't fully understand your current problem. Could you give an example, or provide more details?  Possibly the problem is passing both procedure names to dsolve?  To do that, enclose them in a list:

  dsolve(..., 'known' = [minimum, two])

I don't fully understand your current problem. Could you give an example, or provide more details?  Possibly the problem is passing both procedure names to dsolve?  To do that, enclose them in a list:

  dsolve(..., 'known' = [minimum, two])

The emacs mode (which is completely free) provides access to Maple help pages. 

The emacs mode (which is completely free) provides access to Maple help pages. 

First 119 120 121 122 123 124 125 Last Page 121 of 195