Joe Riel

9660 Reputation

23 Badges

20 years, 20 days

MaplePrimes Activity


These are replies submitted by Joe Riel

More for amusement than practicality, here's one way (Maple13 req'd)

xp := [0.0, 3.3,  3.3,  0.0]:
yp := [0.0, 0.0, -3.3, -3.3]:
d := x -> x[..-2]-x[2..]:
sqrt~(`+`((`^`~)~(d~([xp,yp]),2)[]));
                                   [3.300000000, 3.300000000, 3.300000000]

Or maybe

sqr := rcurry(`^`~,2):
hypot := sqrt~@`+`@op@sqr~@d~@`[]`:
hypot(xp,yp);
                                   [3.300000000, 3.300000000, 3.300000000]

 

More for amusement than practicality, here's one way (Maple13 req'd)

xp := [0.0, 3.3,  3.3,  0.0]:
yp := [0.0, 0.0, -3.3, -3.3]:
d := x -> x[..-2]-x[2..]:
sqrt~(`+`((`^`~)~(d~([xp,yp]),2)[]));
                                   [3.300000000, 3.300000000, 3.300000000]

Or maybe

sqr := rcurry(`^`~,2):
hypot := sqrt~@`+`@op@sqr~@d~@`[]`:
hypot(xp,yp);
                                   [3.300000000, 3.300000000, 3.300000000]

 

You can save some memory and time by using 'storage=triangular[upper,strict]' for the Array. That also avoids the bug that Alex mentioned.

You can save some memory and time by using 'storage=triangular[upper,strict]' for the Array. That also avoids the bug that Alex mentioned.

The viewer could be useful.  I'm less optimistic about changing values. The problem is that re-executing the worksheet may then give different results because the changes to the values are not part of the flow.   Presumably one could provide an option in the dialog to embed the commands in the worksheet.

The viewer could be useful.  I'm less optimistic about changing values. The problem is that re-executing the worksheet may then give different results because the changes to the values are not part of the flow.   Presumably one could provide an option in the dialog to embed the commands in the worksheet.

Hmm.  Playing with this I noticed the following:

restart;
Asin := proc(x) arcsin(x)*180/Pi end proc:
`print/arcsin` := proc(x) 'arcsin'(x)*Pi/180 end proc:
Asin(x);
                                           180*arcsin(x)/Pi

 

What is interesting is the 180/Pi factor.  Change the 180 to 90:

Asin := proc(x) arcsin(x)*90/Pi end proc:
`print/arcsin` := proc(x) 'arcsin'(x)*Pi/180 end proc:
Asin(x);
                                 1/2 arcsin(x)

Messing with the print routine can have some strange effects.  I suppose this makes sense in that the actual output is not changed, just the display of that output.  To avoid an infinite recursion, the print routine probably reuses a previously computed result when the Pi/180 term is cancelled.

Hmm.  Playing with this I noticed the following:

restart;
Asin := proc(x) arcsin(x)*180/Pi end proc:
`print/arcsin` := proc(x) 'arcsin'(x)*Pi/180 end proc:
Asin(x);
                                           180*arcsin(x)/Pi

 

What is interesting is the 180/Pi factor.  Change the 180 to 90:

Asin := proc(x) arcsin(x)*90/Pi end proc:
`print/arcsin` := proc(x) 'arcsin'(x)*Pi/180 end proc:
Asin(x);
                                 1/2 arcsin(x)

Messing with the print routine can have some strange effects.  I suppose this makes sense in that the actual output is not changed, just the display of that output.  To avoid an infinite recursion, the print routine probably reuses a previously computed result when the Pi/180 term is cancelled.

You can improve Alec's suggestion with

UseDegrees := module()
option package; 
export sin,cos,tan; 
local ModuleLoad;
     ModuleLoad := proc()
     global `print/sin`, `print/cos`, `print/tan`;
         `print/sin` := proc(x) 'sin'(x*180/Pi) end proc:
         `print/cos` := proc(x) 'cos'(x*180/Pi) end proc:
         `print/tan` := proc(x) 'tan'(x*180/Pi) end proc:
         return NULL;
     end proc;
     sin := proc(x) :-sin(x*Pi/180) end;
     cos := proc(x) :-cos(x*Pi/180) end;
     tan := proc(x) :-tan(x*Pi/180) end;
     ModuleLoad();
end module:

The extension to cos and tan is trivial; the significant change is in the print procedures. Instead of :-sin(...) use 'sin'(...), that prevents an argument with Pi from being evaluated.

You can improve Alec's suggestion with

UseDegrees := module()
option package; 
export sin,cos,tan; 
local ModuleLoad;
     ModuleLoad := proc()
     global `print/sin`, `print/cos`, `print/tan`;
         `print/sin` := proc(x) 'sin'(x*180/Pi) end proc:
         `print/cos` := proc(x) 'cos'(x*180/Pi) end proc:
         `print/tan` := proc(x) 'tan'(x*180/Pi) end proc:
         return NULL;
     end proc;
     sin := proc(x) :-sin(x*Pi/180) end;
     cos := proc(x) :-cos(x*Pi/180) end;
     tan := proc(x) :-tan(x*Pi/180) end;
     ModuleLoad();
end module:

The extension to cos and tan is trivial; the significant change is in the print procedures. Instead of :-sin(...) use 'sin'(...), that prevents an argument with Pi from being evaluated.

Note that this depends on the set returned by randcomb being sorted.  That isn't necessarily the case, it depends on the size of the integers and the flavor/setting of the kernel.

Note that this depends on the set returned by randcomb being sorted.  That isn't necessarily the case, it depends on the size of the integers and the flavor/setting of the kernel.

You might try

indets({anames('user')});

 

You might try

indets({anames('user')});

 

readline returns 0 when there are no more lines to read.  That is the purpose of the while condition.  You are not testing the output of readline after the second call.  Note that building a list this way is not particularly efficient, it is O(n^2), where n is the number of lines read.  A better approach, useful if the file has many lines, is to insert each parsed line into a table, then convert the table to a list.

T := table();
for cnt do
   line := readline("s.txt");
   if line = 0 then break; end if;
   T[cnt] := parse(line);
end do:
L := convert(T, 'list');

A safer approach is to wrap the whole construction in a try statement in which you close the file in a finally clause, that way if an error occurs in the loop, say while parsing, the file is closed.

file := "s.txt";
try 
  T := table();
  for cnt do
    line := readline(file);
    if line = 0 then break; end if;
    T[cnt] := parse(line);
  end do;
finally 
  fclose(file);
end try;
L := convert(T, 'list');
First 115 116 117 118 119 120 121 Last Page 117 of 195