Maple 2020 Questions and Posts

These are Posts and Questions associated with the product, Maple 2020

When making a function and using the return type, I found Maple is not catching the case when the function returns wrong type.

Actually there are two issues in this example I like to ask about. First, here is the example

restart;
kernelopts(assertlevel):=2;
TypeTools:-AddType(type_A, record(x::integer)):

foo:=proc( A::type_A ) ::type_A;  #this is what return type should be
     print( type(A,'type_A')); #this prints TRUE
     A:-x :=1.5; #opps, wrong type, changes type of record now!
     print( type(A,'type_A')); #This now prints FALSE
     return A;
end proc:

A:=Record('x' = 1);
B:=foo(A):
eval(B)

The call to foo() goes through as expected since type is correct.

First issue: Inside the function doing A:-x :=1.5;  changed the type of record now, since is supposed to be integer and now become real. Is there a way to have Maple detect this at run time?

Second issue: The function was defined to return type_A  but due to this overwriting the field of the record by wrong value, it is no longer type_A  (the print now says false). But why Maple did not detect this? Since the function is defined to only return type_A ?

Is it possible to change the code above to catch these type mistakes I made?

given A := [[1, 0,9], [5, 0, 6], [4, 0, 0]]; and to remove 0 from each entry, I used map2(remove,has,A,0) which gives 

                   [[1, 9], [5, 6], [4]]

Is it possible to do the same thing using ~ notation for mapping?  (Element-wise Operator). Where now each element is each entry in A? I can't figure what the syntax would be if possible.  remove(has,??,0)~A

For exmaple in Mathematica one would use what is called slot number #. So the above mapping would look like in Maple as doing remove(has,#,0)~A  where now acts as place holder to be filled by each entry taken one at a time from list A as the mapping runs.  But do not know if this is possible in Maple.   

map2 does the job, but can be tricky to use when the function to map takes number of different arguments.

One work around is to make seperate function, and use that for mapping, as follows

f:= x->remove(has,x,0);
f~(A)

which gives  [[1, 9], [5, 6], [4]] correctly. 

So I find the above easier to understand and more clear than map2, and I am not unhappy with it, and so I see no reason to use map2 vs. ~ in this case.

But wanted to see if it was possible to use ~ without making separate function, but do it in-line if you well (using what is called pure function, or unamed function in functional programming).

 

I am trying to decide which is better to use, a table or Record, to pass lots of information back and forth between functions inside say a personal Maple package.

So instead of passing each argument on its own, as normally one does when calling a function, instead all the input is put into one table, or into one record, and this is passed instead. So the function always takes as input one argument.

When the function returns result, it also does the same. It updates any fields it needs in the table or in the record, o rmay be add new field, which the caller would have to know about, (since same person has programmed both) and returns that.

I find this easier to handle, than passing each argument on its own.  And on return, rather than return many results in list or sequence and making sure they are in correct order, the function simply update the fields in the table or the record. No problems with wrong order here as caller will read these fields by name later on.

So I made same toy example, one using table and one using record. 2 numbers are passed to a function. The function returns the result of multiplication by adding new field into the table/record.  Here is version using table.

restart;
foo:=proc(input::table)
   local x,y,result,updated;

   x      := input["x"];
   y      := input["y"];
   result := x*y;

   #finished computation. Return result. Copy all content
   #of passed table first, then add new field to write result to

   updated := eval(input);
   updated["result"] := result;
   return updated;      

end proc:

Called using

input:=table(["x"=4,"y"=6]);
r:=eval(foo(input)):
print(r["result"]);

prints 24

 

Here is version using Record

foo2:=proc(input::record)
   local x,y,result,updated;

   x      := input:-x;
   y      := input:-y;
   result := x*y;

   #finished computation. Return result
   #could not update input Record in place, even after deep copy
   #since "result" is not an existing field. So have to make a new Record
   #this is a problem, since this function need to know all fields now in Record

   updated := Record("x"=x,"y"=y,"result"=result);
   return updated;      

end proc:

called as

input:=Record( "x" = 4, "y"=6);
r:=eval(foo2(input)):
print(r:-result);

prints 24

 

Some observations: I found the table more flexible. Because the function called can add a new field to the table, even if this field do not exist. If called function wants to add a new result back to the caller, it can do this.  With Record, all fields have to be decided on when the record is created. So the function has to create new record from all the old fields and add the new field to do this.

This might not be a big problem actually, as one can decide at top of package what all the possible fields that needs to be in the Record, and creates such Record, giving default values for al field. So now Record will have all possible fields in it.  Using this the above can now be written as

restart;
foo3:=proc(input::record)
   local x,y,result;

   x      := input:-x;
   y      := input:-y;
   result := x*y;

   #finished computation. Return result  
   input:-result := result; 
   return input;      

end proc:

And called as

input:=Record( 'x' = 4, 'y'=6, 'result'=NULL);
r:=eval(foo3(input)):
print(r:-result);

24

But it seems both methods suffer from the fact that type checking on the types of the arguments now inside is lost. (But I seem to remember one can do type checking on fields for a Record somehow). So if this is possible, this will be one advantage of Record over table.  But again, it is possible in both methods to check type of field by using whattype() or type() manually if needed.

But other than these two issues, and assuming one wants to pick between table and record, the question is, why choose Record over table?  If the main purpose is to just to pass information back and forth? Is there some hidden advatage to using Record I am overlooking that tables do not provide (again, for the purpose of passing arguments and results between functions and no more).

For me, I see table doing what I want from a record, but a little bit easier to work with as well (field names can be strings for example).  So with table one does  A["x"]=.....  While with Record one does A:-x=...

For me, I see a Maple table just like Python dictionary, so easy to underatand and work with.

Any thoughts from the experts on this subject? 

There are discrepancies between Maple's solution of Fourier transforms and the results printed in USA NIST Handbook of Mathematical Functions, page 30

fourier(exp(-a*abs(x))/sqrt(abs(x)),x,s) assuming a>0;
            /   /   (1/2)   (1/2)                (1/2)  
        1   |   |2 2      Pi      signum(s - _U1)       
       ---- |int|-------------------------------------,
       2 Pi |   |       /   2    \                      
            |   |       |_U1     |          (1/2)       
            |   |     a |---- + 1| (s - _U1)            
            |   |       |  2     |                      
            \   \       \ a      /                      

                                    \\
                                    ||
         _U1 = -infinity .. infinity||
                                    ||
                                    ||
                                    ||
                                    ||
                                    //


For this transform of
                 "exp(-a*abs(x))/sqrt(abs(x))"

 the result in the NIST table is
          "sqrt(a + sqrt(a^2 + s^2))/sqrt(a^2 + s^2)"

 .
fourier(sinh(a*t)/sinh(Pi*t),x,s) assuming a>-Pi, a<Pi;
                    2 sinh(a t) Pi Dirac(s)
                    -----------------------
                          sinh(Pi t)       

For this transform of sinh(a*x)/sinh(Pi*x)   the result in the NIST table is
                         "1/sqrt(2*Pi)"  "sin(a)/(cosh(s) + cos(a))"

 
fourier(cosh(a*t)/cosh(Pi*t),x,s) assuming a>-Pi, a<Pi;
                    2 cosh(a t) Pi Dirac(s)
                    -----------------------
                          cosh(Pi t)       

For this transform of cosh(a*x)/cosh(Pi*x) the result in the NIST table is  
                          "sqrt(2/Pi) cos(a/2)*cosh(s/2)/(cosh(s) + cos(a))"

These disparities are significant, apart from the fact that Maple failed to solve the first example above.

 

I've never noticed this before, becuase I do not like to with()  packages, but like to add the name of the package at each command and alwsys try to use explicit commands.

Compare this first example, which does not work, since the "." in the codeis the global "dot" command ?dot

restart;
VC:=VectorCalculus:
f:=y*sin(x)+y^2;
VC:-Nabla . VC:-Nabla(f,[x,y])

So the dot above is the one used for normal dot product, which is visible without loading any package as in: <1,2>.<3,4> gives 11

Now when I next loaded the VectorCalculus explicitly, but kept the same code the same, i.e. using the same ".", now it worked

restart;
with(VectorCalculus);
VC:=VectorCalculus:  #do not need this now, but to keep the code the same
f:=y*sin(x)+y^2;
VC:-Nabla . VC:-Nabla(f,[x,y])

It worked because Maple now used VectorCalculus:-`.`  ,Even though I did not tell it to do this, it searched the package since it is loaded) and now decided to use VectorCalculus:-`.` instead of the global `.` dot defined before loading this package.

But how did Maple know which dot to use? 

Did it say to itself, the dot in the middile must be VectorCalculus:-`.` and not the global `.` due to the type of the operands around the dot, it automatically found the correct match? 

So imagine if one has loaded more packages, and some might have its own `.` operator that takes the same operands, does Maple search them all to find the correct dot to use based on type of operands to bind it to? and how in this case it knows which to use, if there is more than one match among all loaded packages? I can see this causing confusion, becuase one is never sure in this case which operator was used. I do not know if Maple issues a warning in this case or not.

I do not use input 2D math in document mode. But was trying it now to see if I can enter this to check my hand solution

In worksheet, this is what I do

restart;
VC:=VectorCalculus:
f:=y*sin(x)+y^2;
VC:-DotProduct(VC:-Del,VC:-Gradient(f,[x,y]))

But I do not know how to enter   if possible, in document mode 2D math. THis is what I tried. Created document mode. Picked the DEL operator from the left side common symbols window. But I do not know how to do the rest.

Clearly I am doing something silly. This is why I prefer plain Maple code, becuase I can see what I am typing :)

But was wondering, how to do the above in docuemnt mode + 2D math? To make it look close to how it shows in the book. Does one need special palette for this?

 

 

Hello

In Section 14.5 - External Calling ..., page 489, the authors gave an example on how to use the command line in Unix to call maple with arguments. I followed the example but added a mpl-file to see if the arguments were sent to it.  

Example (Linux):

/opt/maple2020/bin/maple -c n:=4 -c 'path:="/home/eduardo/examples";' example.mpl > output.txt &

Maple sends out the following error message: "Error, incorrect syntax in parse: '/' unexpected (near 7th character of parsed string)".

The msg is clear where the error is but I could not figure out what to do. The single quotes were supposed to take care of that, weren't they?  

Many thanks.

Ed

Edit: I have added the double-quotes.   The problem still persists.  

I wanted to overwrite an existing variable of type table by the one returned from a call to a proc().

But it does not work, unless eval() is called on the returned result from the proc()

But if the variable that recieves the return value from the proc() was not already a table type, then it works without using eval. Why is that? 

Here is an example

restart;
foo:=proc(A::table)
  A["c"]:=3; #add new field
  return A;
end proc;

A:=table():
A["a"]:=1:
A["b"]:=2:
B:=foo(A):
whattype(B);
print(B)

           table(["a" = 1, "c" = 3, "b" = 2])

In the above, it worked. is table now, and assigned the updated table from the proc.  But 

A:=table():
A["a"]:=1:
A["b"]:=2:
A:=foo(A): #without eval, it does not work.
whattype(A);
print(A)

             symbol
               A

I expected A to be overwritten, just like B was. To fix this, I have to change A:=foo(A): by A:=eval(foo(A)):

But why eval was not needed in the first example, and was needed for the second example?

btw, the same thing happens with Record(), not just table()

just like to understand the reasoning behind this. I expected both cases to work the same way. But it works different if the variable happend to be unassigned (like B above).

Hello all,

I was hoping to get some general tips for tackling numeric integrals. As someone with little experience in the subject, I find myself overwhelemed by the many different integration methods. 

Experts, what are the first steps you take when trying to find a numeric solution to an integral? How might you zero-in on a particular integration method? What about tweaking error parametrs, etc.? Is there a general framework for approaching these problems, or is it all guesswork?

Thanks!

How can I create an even function, g, in Maple? I want Maple to give g(x) - g(-x) as 0.

Hello, I was given the problem "Set N:=100. (i) Form four lists L[1], L[3], L[5], L[7] where L[r] contains all primes 2< p < N such that p mod 8 = r ." and came up with the following code:

N := 100;
List1 := [];
List3 := [];
List5 := [];
List7 := [];
for p from 2 to N do
    if isprime(p) and p mod 8 = 1 then List1 := [op(List1), p]; end if;
    if isprime(p) and p mod 8 = 3 then
        List3 := [op(List3), p];
    end if;
    if isprime(p) and p mod 8 = 5 then
        List5 := [op(List5), p];
    end if;
    if isprime(p) and p mod 8 = 7 then
        List7 := [op(List7), p];
    end if;
end do;
List1;
List3;
List5;
List7;
 

this gave me the answer I wanted, however using the above code I have to answer this second question: 

In the notation of Problem 1, make a procedure with input = arbitrary  positive integer N and output = the list [nops(L[1]), nops(L[3]), nops(L[5]), nops(L[7])]. Do some experiments to see for which (if any) r, L[r] is largest. 

I am unsure of how to create a procedure out of the code I already have. I created this: 
 

restart;
F := proc(n)

local N, p, List1, List3, List5, List7;

List1 := [];

List3 := [];

List5 := [];

List7 := [];

for p from N do

if isprime(p) and p mod 8 = 1 then List1 := [op(List1), p]; end if;

if isprime(p) and p mod 8 = 3 then List3 := [op(List3), p]; end if;

if isprime(p) and p mod 8 = 5 then List5 := [op(List5), p]; end if;

if isprime(p) and p mod 8 = 7 then List7 := [op(List7), p]; end if;

return [nops(List1), nops(List3), nops(List5), nops(List7)];

end do;

end proc;

 

however, when I input F(100) or any other value of N, I am receiving the error message "Error, (in F) initial value in for loop must be numeric or character"

 

Any ideas on how to improve my program to get the output I desire?

thank you 

 

 

 

Unfortunately, I can't solve the problem in MAPLE that appears in the last line of the picture (0=1 instead of a0=1) unless I write a0 seperately and add the partial sum starting from k=1 to it.

I assume the problem is with 00 in the power series but since MAPLE defines 00:=1 I'm not sure about it. Anyone knows a better solution or can tell me if I made a mistake somewhere?


 

This worksheet plots the non-overlapped images of two spheres, but plotted procedure p fails to successfully pass the choice of color to the implicitplot3d command.

How can this be accompished?

Sphere_exclusions.mw

Hi there.

I noticed that in Maple 2020 default font size of document (Times New Roman 12) looking bigger than in previous versions. How can I make the view the same as in previous versions?

Thank you

Hi there.

How supress this warning in Maple 2020 when I use "e" just like a variable not an exponent:

Warning, if e is meant to be the exponential e, use command/symbol completion or palettes to enter this special symbol, or use the exp function

?

Thank you

First 48 49 50 51 52 53 54 Page 50 of 55