Items tagged with procedure

Feed

Perhaps I am being stupid, but I have a problem with functional operators, more specifically with looping through a collection of them. Consider the following code (just a study case):

nonIdMaps := []:
for f in [x -> x,x -> 2*x,x -> 3*x] do
   f,f(y);
   if f(y) <> y then nonIdMaps := [nonIdMaps[],f] end if
end do;
nonIdMaps,map(f -> f(y),nonIdMaps);

There are two things I do not understand about this output: 1.) Why the f's rather than the specific maps as given in the list? 2.) Why two times 3*y in the last line, rather than [2*y,3*y]? (this issue being of course quite likely a consequence of the first one). This behaviour can be compared with the following:

nonIdMaps := select(f -> f(y) <> y,[x -> x,x -> 2*x,x -> 3*x]):
nonIdMaps,map(f -> f(y),nonIdMaps);

the output of which I do understand, being as I expected it to be. I guess that there are some scoping rules I do not understand, or what?

When we first started trying to use Maple to create a maple leaf like the one in the Canada 150 logo, we couldn’t find any references online to the exact geometry, so we went back to basics. With our trusty ruler and protractor, we mapped out the geometry of the maple leaf logo by hand.

Our first observation was that the maple leaf could be viewed as being comprised of 9 kites. You can read more about the meaning of these shapes on the Canada 150 site (where they refer to the shapes as diamonds).

We also observed that the individual kites had slightly different scales from one another. The largest kites were numbers 3, 5 and 7; we represented their length as 1 unit of length. Also, each of the kites seemed centred at the origin, but was rotated about the y-axis at a certain angle.

As such, we found the kites to have the following scales and rotations from the vertical axis:

Kites:

1, 9: 0.81 at +/- Pi/2

2, 8: 0.77 at +/- 2*Pi/5

3, 5, 7: 1 at +/-Pi/4, 0

4, 6: 0.93 at +/- Pi/8

This can be visualized as follows:

To draw this in Maple we put together a simple procedure to draw each of the kites:

# Make a kite shape centred at the origin.
opts := thickness=4, color="#DC2828":
MakeKite := proc({scale := 1, rotation := 0})
    local t, p, pts, x;

    t := 0.267*scale;
    pts := [[0, 0], [t, t], [0, scale], [-t, t], [0, 0]]:
    p := plot(pts, opts);
    if rotation<>0.0 then
        p := plottools:-rotate(p, rotation);
    end if;
    return p;
end proc:

 

The main idea of this procedure is that we draw a kite using a standard list of points, which are scaled and rotated. Then to generate the sequence of plots:

shapes := MakeKite(rotation=-Pi/4),
          MakeKite(scale=0.77, rotation=-2*Pi/5),

          MakeKite(scale=0.81, rotation=-Pi/2),
          MakeKite(scale=0.93, rotation=-Pi/8),
          MakeKite(),
          MakeKite(scale=0.93, rotation=Pi/8),
          MakeKite(scale=0.81, rotation=Pi/2),
          MakeKite(scale=0.77, rotation=2*Pi/5),
          MakeKite(rotation=Pi/4),
          plot([[0,-0.5], [0,0]], opts): #Add in a section for the maple leaf stem
plots:-display(shapes, scaling=constrained, view=[-1..1, -0.75..1.25], axes=box, size=[800,800]);

This looked pretty similar to the original logo, however the kites 2, 4, 6, and 8 all needed to be moved behind the other kites. This proved somewhat tricky, so we just simply turned on the point probe in Maple and drew in the connected lines to form these points.

shapes := MakeKite(rotation=-Pi/4),
          plot([[-.55,.095],[-.733,.236],[-.49,.245]],opts),

          MakeKite(scale=0.81, rotation=-Pi/2),
          plot([[-.342,.536],[-.355,.859],[-.138,.622]],opts),
          MakeKite(),
          plot([[.342,.536],[.355,.859],[.138,.622]],opts),
          MakeKite(scale=0.81, rotation=Pi/2),
          plot([[.55,.095],[.733,.236],[.49,.245]],opts),
          MakeKite(rotation=Pi/4),
          plot([[0,-0.5], [0,0]], opts):
plots:-display(shapes, scaling=constrained, view=[-1..1, -0.75..1.25], axes=box, size=[800,800]);

Happy Canada Day!

Sorry for the simple subject.  Using unapply we can convert an expression to a function. 

a:=x^2+sin(x)

a:=unapply(a,x)

               a:= x-> x^2+sin(x)

How do you go the other way.  That is convert a function to an expression?

Hi,

I want to apply a rule to simplify an expression. The applyrule command works, when used directly in the worksheet. When I try to use the command within a procedure, Maple throws an error I cannot decipher:

Error, (in PatternMatching:-AlgStruct:-InsertPattern) first operand of `::' must be a name

 

Here is a full demonstration worksheet:

restart:

anexp:=abs(x)^2;

abs(x)^2

(1)

simplify(anexp);

abs(x)^2

(2)

rmabssq := proc(inexp)
description "removes the abs^2 construct in an expression":
local ruleabssqared1,ruleabssqared2,outexp:
    ruleabssqared1:= abs(''a''::algebraic)^2= ''a''^2:
    ruleabssqared2:= abs('expand'(-''a'')::algebraic)^2= ''a'':
    outexp:= applyrule([ruleabssqared1,ruleabssqared2],inexp):
    return outexp:
end proc;
 

proc (inexp) local ruleabssqared1, ruleabssqared2, outexp; description "removes the abs^2 construct in an expression"; ruleabssqared1 := abs(''a''::algebraic)^2 = ''a''^2; ruleabssqared2 := abs((('expand')(-''a''))::algebraic)^2 = ''a''; outexp := applyrule([ruleabssqared1, ruleabssqared2], inexp); return outexp end proc

(3)

## does not work :(
rmabssq(anexp);

Error, (in PatternMatching:-AlgStruct:-InsertPattern) first operand of `::' must be a name

 

## works!
ruleabssqared1:= abs(''a''::algebraic)^2= ''a''^2:
ruleabssqared2:= abs('expand'(-''a'')::algebraic)^2= ''a'':
newexp:= applyrule([ruleabssqared1,ruleabssqared2],anexp);

x^2

(4)

 


 

Download applyrule_4.mw

Thanks for your help

I am getting the following error while calculating function containing diffrential. Thankz in advance
 

KK := proc (x) options operator, arrow; diff(diff(x^2, x)) end proc;

proc (x) options operator, arrow; diff(diff(x^2, x)) end proc

(1)

KK(2)

Error, (in KK) invalid input: diff received 2, which is not valid for its 2nd argument

 

``


 

Download FUNCTION_(1).mw

I recently encontered a very strange result.

Lets define the procedure:

Fg := proc(x0,y0)
if (x0>=0)and(x0<=3) and (y0<=x0+2) and (y0>=x0-1) and (y0>=0) and (y0 <=3) then
return y0*(3-y0)*x0*(3-x0)*(x0+2-y0)*(y0-x0+1);
else
return 0;
end if:
end proc:

The plot looks like needed:

plot3d('Fg'(x,y), x=0..3, y=0..3);

But integration returns weird result:

evalf(Int('Fg'(x,y), [x=0..1, y=0..2.1]));

7.888753239

evalf(Int('Fg'(x,y), [x=0..1, y=0..2.2]));

Error, (in evalf/int) when calling 'Fg'. Received: 'cannot determine if this expression is true or false: 0 <= x and x <= 3 and y <= x+2 and x-1 <= y and 0 <= y and y <= 3'

Hello,

I have two simple module's:

1) ------------------------------------------------------------------------

Point := proc(xx::float,yy::float)
    return module()
        local x := xx,y := yy;
        export ShowPoint,GetX,GetY;
        
        ShowPoint := proc()
            printf("Point X,Y -> [%f,%f]",x,y);
        end proc;

        GetX := proc()
            return x;
        end proc;        
        GetY := proc()
            return y;
        end proc;    
    end module:
end proc:

2) ------------------------------------------------------------------------

PointMath := module()
    option package;  
    export `+`;
    
    `+` := proc(a::Point(float,float),b::Point(float,float))
        option overload;
        Point(a:-GetX()+b:-GetX(),a:-GetY()+b:-GetY());
    end proc;
end module:

------------------------------------------------------------------------

Next I use first module:

p1:=Point(1.2,1.4);

p2:=Point(1.0,2.0);

Finally I want to add above two points:

with(PointMath)

p2:=p1+p2

The results is:

p2:=p1+p2

Why is not called operator '+' and two points are not added?

Best,

Rariusz

 

 

 

I wrote some procedures in an older version of Maple to sort through a matrix and find repeated elements, and then set the matrix elements equal to one another. As an example, consider the matrix:

Matrix(4, 4, {(1, 1) = 31774769/38880000, (1, 2) = -90858559/233280000, (1, 3) = -76547233/69984000, (1, 4) = -119275567/139968000, (2, 1) = -90858559/233280000, (2, 2) = 1506841/6480000, (2, 3) = 442249/720000, (2, 4) = 85514221/116640000, (3, 1) = -76547233/69984000, (3, 2) = 442249/720000, (3, 3) = 592249771/349920000, (3, 4) = 121562339/87480000, (4, 1) = -119275567/139968000, (4, 2) = 85514221/116640000, (4, 3) = 121562339/87480000, (4, 4) = 170546323/87480000}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])
Due to its symmetric nature the upper triangular matrix is just the same as the lower triangular. i.e. the elements are equal: (2,1)=(1,2), (3,1)=(1,3) etc...

I have attached a minimal working example Maple script, which processes an input matrix, finds repeats and sets the elements equal to one another (e.g. [1,2]=[2,1], literally printing this) and saves the output to a file using a specific format for processing by another program. The matrix in the file is numeric but the actual matrices contain a lot of algebraic terms and can get quite large. This is where my procedures struggle as they take a very, very long time to process larger algebraic matrices to group the repeats. Is there a more Maple friendly way of going about this problem rather than these "archaic" procedures?

Here is the script:

duplicates_matrix_.mw

Thank you in advance

-Yeti

I have a program:

Running abb(1,3) gives error: Error, (in abb) illegal use of a formal parameter

Might be a silly question: Is it not allowed? Do I have to define a local variable for b in order to change it?

Hi I have this code

 psi:=proc(n,x);
 (1/sqrt(sqrt(pi)*2^n*factorial(n))*exp(-x^2/2)*HermiteH(n,x))
 end proc;
 psi := proc(n, x) exp(-1/2*x^2)*HermiteH(n, x)/sqrt(sqrt(pi)*2^n*n!) end proc

 psi2=proc(a,x);
 psi(a,x):=(1/sqrt(sqrt(pi)*2^a*factorial(a))*exp(-x^2/2)*HermiteH(a,x))
 end proc;
psi2 = (proc(a, x)
    psi(a, x) := exp(-1/2*x^2)*HermiteH(a, x)/sqrt(sqrt(pi)*2^a*a!)

end proc)
 for n from 0 to 2 do;
 for a from 0 to 2 do;
 result=proc(n,a);
 result(n,a)=psi*psi2
 end proc;
print(evalf(int(result(n,a),x=0..infinity)));
od;
od;


it returns

 Float(infinity) signum(result(0, 0))

                     Float(infinity) signum(result(0, 1))

                     Float(infinity) signum(result(0, 2))

                     Float(infinity) signum(result(1, 0))

                     Float(infinity) signum(result(1, 1))

                     Float(infinity) signum(result(1, 2))

                     Float(infinity) signum(result(2, 0))

                     Float(infinity) signum(result(2, 1))

                     Float(infinity) signum(result(2, 2))

I know the results for (0,0), (1,1) and (2,2) should be 1 and the rest should be 0.

Can anybody help fix this please

I have a procedure, and I want to use it so many times whthin my equations, because all notation in my equations are in indicial format, it will be so nice if I can call the procedure in indicial form rather than paranthesis form.

Is there anyone knows how is it possible in Maple?

Rr is my procedure that accepts 2 inputs, So there is no problem to call it using Rr(x,y), I am trying to call it with Rr[x,y] (indicial form

)

Is it possible to do so in Maple? It will be so great to do so.

I want to produce a user defined function in Maple and use it in my program like Matlab. My function in Matlab is as below:

function R=ff(i,m)
K=1;
Q= zeros( i );
for j=1:2:i
 Q(j)=2*i-K;
 K=4+K;
end
R=Q(m);

How to produce this function in Maple and use it in my program?

How can I produce the same output with something like alphadeg := alpharad * 180/Pi?

T := proc (p, q, R) alpharad = evalf[5](arccos((1/2)*(p^2+R^2-q^2)/(p*R))), alphadeg = 180*evalf[5](arccos((1/2)*(p^2+R^2-q^2)/(p*R)))/Pi end proc;
T(3, 4, 3.75);
           alpharad = 1.2515, alphadeg = 71.70566805

When I try alphadeg = alpharad * 180/Pi I get
Error, illegal use of an object as a name
 

Thanks! Les    AB59_20170205_Post.mw

Hello I was trying to manipulate maple to write a procedure checking a matrix , say A with n rows and n columns. That matrix A given any row/column the sum of the entries for every row and column are equal. For example matrix [(-1,2)(2,-1)], every row and column in this matrix sums to 1. The entries in the matrix can be any real number.

Maple provides efficient vectorization and automatic parallelization for many common operators. For example

x -> 2*~x*~cos~(x*~x)

But in my application it is common to want to create rather long vectorized operators starting from some complicated symbolic computations. Doing conversions by hand from symbolic expressions to element-wise operations is laborious and error prone.

As a very simple example consider that it is possible to obtain (almost) the same result as above by writing the following as a vectorized operation

D(x->sin(x^2))~

But there are at least two problems with this. First of all it is not nearly as efficient as the first operator and second, perhaps not unrelated, is that the datatype returned when applying this operator to a Vector/rtable of hardware floats (e.g. datatype=float[8]) becomes something  more general.

My question is how can I convert a complicated symbolic expression into an efficient numeric element-wise vector operation?

I have tried several different approaches but so far without success. In the case above for example it seemed natural to expect that the following derivative

D(x->sin~(x^~2))

would produce a vectorized result, but this is not the case. In another attempt I was unable to see how to perform substitions into an expression, e.g. like this

unapply(subs(`*`=`*`~, cos=cos~, diff(sin(x),x)), x)

I would be glad to receive suggestions and/or references to relevant documentation. 

 

1 2 3 4 5 6 7 Last Page 1 of 31