Carl Love

Carl Love

28055 Reputation

25 Badges

12 years, 355 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

In terms of efficiency, there is practically no difference between Arrays and Matrices. Arrays can have arbitrary lower bounds and a arbitrary number of dimensions. Matrix lower bounds are always 1, and, of course, they have two dimensions.

There is a profound difference between lists and Vectors. Maple lists are always static, i.e., they cannot be changed. If you try to change a list or just one member of the list, then if Maple allows it at all, then it recreates the entire list from scratch. So if you are going to be changing the elements, use a Vector. If you are not going to change the elements, then it is very slightly more efficient to use lists. Coding lists is substantially more convenient than coding Vectors.

You asked:

How would I specify the allowed types of the elements of that list (for all elements, and for specific ones)?

To specify that all the elements of the list are a specific type, say posint, use ::list(posint). To specify a list whose first element must be a posint, whose second element can be anything, and which must have two elements, use ::[posint, anything]. Note that the word list does not appear in this spec. To specify a list whose second element must be a posint, but whose length is unlimited use 

::satisfies(L-> nops(L) > 1 and L[2]::posint)

Note that any type, no matter how complex, can be specified by satisfies; but it is more efficient to use the built-in types when possible.

And if the element of the list is a list again, how would I specify the type of that's elements? and so forth..

For example, ::list(list(posint)). If you want to specify that all the inner lists have the same length, then use ::listlist(posint). The former concept can be extended to any depth, but there is no listlistlist.

How would I specify the allowed size of the listArray, etc. ?

To specify a list with three elements, use ::[anything, anything, anything]. Note that you can't use to abbreviate this. So an alternative is

TypeTools:-AddType(list3, [anything $ 3]);
P:= proc(L::list3)
....

Arrays (and Vectors and Matrices) are easier to specify a size for: It's simply ::Array(1..3).

How do I specify the types of more than one output, if my proc returns more than one value?

Just specify multiple types separated by commas. For example,

P:= proc(L::list)::list,nonnegint; L,nops(L) end proc;

Note that the return value of procedures and the assigned values of local variables is only checked if you set

kernelopts(assertlevel= 2);

This is useful for debugging during code development. Afterwards, it becomes more efficient when the assertlevel reverts to its default value of 0. The type-checking of arguments does not depend on assertlevel; however, they are only type-checked if they are actually used in the procedure.

Is there also a way to specify that the ineger (or float) argument has to be greater than some given value, e.g. >2, or that it has to lie between two values, or out of a given set?

In general, use the satisfies construct described above. For some limited cases it is better to use a built-in type. For example, to specify an integer greater than 1, use ::And(posint, Not(identical(1))).

 

local D,I,O;
combinat:-randperm([A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S]);

If you want a string, then do

convert(combinat:-randperm(19) +~ 64, bytes);

(because "A" is the 65th character in ASCII).

After assigning the above expression to f, do

plot([(numer,denom)(f), z= -1..1]);

This will use the numerator for the horizontal coordinate and the denominator for the vertical. I picked a range for z that seemed to give a good plot, but you might want to change that.

The erf in the solution is correct. The full verification is in the attached worksheet. Also, I solved for y(3).

restart:
ODE:= x^2*(diff(y(x), x, x))+x^3*(diff(y(x), x))+(x^2-2)*y(x) = 0:
Sol:= dsolve(ODE):
Verify solution:
eval(lhs(ODE), Sol);

simplify(%);
                               0
The solution checks.

Solve BVP:
Sol:= dsolve({ODE, y(1)=1, y(2)=2});

eval(Sol, x=3);

 

evalf(%);
                    y(3) = 1.76345278364621

(Whew! I am sick and tired of cut-and-pasting worksheets line by line.)

 

Download erf.mw

MajMaj wrote: pathFILE := "\/MyDirectory\/blabla.txt";

Don't use the backslash to escape the forward slash! That will remove all slashes. That should be

pathFILE := "/MyDirectory/blabla.txt";

As nm says, the forward slash (/) will work with either OS. So, there's no need to use backslash at all.

The warning indicates that the expression (6) contains a variable other than n, m, or k. Without seeing expression (6), I can't tell what that variable is. If this is not enough info for you to be able to solve the problem, please post the actual expression and/or upload a worksheet containing it.

dsolve({(D@@3)(y)(x)+D(y)(x)/x-y(x)/x^2, y(1)= 1, D(y)(1)=0, (D@@2)(y)(1)=1});

eval(%, x= 3);

evalf(%);

We can use an implicitplot to make a guess at an integer value for one of the variables and then solve for the other. If we get an integer, we're done.

restart:
S:= sum(
     (120*n^2+A*n+B)/16^n/(512*n^4+1024*n^3+712*n^2+194*n+15),
     n= 0..infinity
);

S:= simplify(S);
plots:-implicitplot(S=Pi, A= -200..200, B= -200..200);

From the plot, I guess B=47.

solve(eval(S, B= 47)=Pi, A);
                                          151

Download Pi_sum.mw

If you enclose a sequence in curly braces {}, then it is a set and Maple will put it in an order of its own choosing. If you want the order to matter, then you must enclose it in square brackets [] (or not enclose it at all---but that makes it tricky to work with), and it is called a list.

When you display a list of plots in the default inline GUI renderer, then the filled interiors of the polygons are printed in the reverse of the order that they are listed. This is shown by the following simple example:

p1:= polygonplot([[0,0], [2,0], [2,2], [0,2]], color= red, transparency= 0):
p2:= polygonplot([[1,1], [3,1], [3,3], [1,3]], color= blue, transparency= 0.5):
display([p1,p2]);

display([p2,p1]);

This rule does not necessarily apply to text, axes, gridlines, polygon borders, or any other 1- or 0-dimensional objects.

A:= Array(1..10, i-> .1*i, datatype= float):
dsolve({diff(y(x), x) = y(x), y(0)= 1}, numeric, output= A);

High-level symbolic routines such as diff and maximize cannot be translated. Yes, translation of "functions" is limited to the block that you found in the link.

You shouldn't rely on there being an x in the input expression; your procedure should work for other variables. Here are three possible approaches to this.

The first approach is to check whether there is only one variable in the expression and to use that variable.

refinedexample1:= proc(expr::algebraic)
local
     x:= indets(expr, And(name, Not(constant))),
     f:= unapply(expr, x[])
;
     if nops(x)>1 then
          error "expecting a single name in %1, but found %2", expr, x
     end if;
 
     f(Pi)
end proc;

The second approach is to require the user to pass in the variable. This is the approach used by most Maple procedures. You can make it default to x if nothing is passed in:

refinedexample2:= proc(expr::algebraic, x::name:= :-x)
local f:= unapply(expr, x);
     f(Pi)
end proc;

The third approach is a hybrid of the first two. If no variable is passed by the user, then it checks for a single variable and, if found, uses it; otherwise it uses the variable passed in. This is the approach used by most procedures in the Student package:

refinedexample3:= proc(expr::algebraic, X::name:= [][])
local x,f;
     if X = () then
          x:= indets(expr, And(name, Not(constant)));
          if nops(x) > 1 then
                error "expecting a single name in %1, but found %2", expr, x
          end if;
     else
          x:= {X}
     end if;
     f:= unapply(expr, x[]);
     f(Pi)
end proc;

Addressing your second question, about data type: The data type is literally algebraic, which I've used in the above procedure declarations.

It seems like there are some invisible characters which are commenting out the lines that define vlak2, vlak3, and vlak4 in procedure f. I have never seen a bug like this! And this is with 1D input no less! If I cut-and-paste your plaintext code, then it works (I had to delete the "10" output of the interface(rtablesize= infinity) command from your plaintext.)

Here's a workaround: Cut-and-paste procedure f to a Code Edit Region. It's on the Insert menu. To execute the code (i.e., to define the procedure), right click on the Region and select Execute Code.

Another workaround: Cut-and-paste the code to a plaintext file, like Microsoft Notepad. Then cut-and-paste it back to a new Execution Group in your worksheet.

Here is the worksheet with the first workaround applied. I also left the erroneous Execution Group in the file so that someone else can find the source of the bug.

vraag.mw

Let me know if you can use this.

There is no bug. You need to hande the symbolic case for u(t,x), i.e., when t is a symbol, not a number. If t is a symbol, then it is not 0, so the Dirac case in your `if` is never executed.

So, change your G definition from

G:= (x,xi,t)-> `if`(t=0, ...);

to

G:= (x,xi,t)-> `if`(t::realcons, `if`(t=0, ...), 'procname'(args));

First 333 334 335 336 337 338 339 Last Page 335 of 395