Items tagged with procedure procedure Tagged Items Feed

Hi experts

 

In a procedure with no declared parameteres I would like to return (print) the passed arguments (expecting Matrix structures) in a modified form along with the name of the symbol holding the structure passed as arguments.

 

That is, the procedure just iterates through the _passed arguments in a for-loop to display the name of the the passed argument (the symbol), a colon and then the modified matrix structure.

My problem is that when looping from i to _npassed arguments, refering to _passed[i] gives me the evaluated form.Tat is what I need to modify the structure but not to list the symbol name.

 

Say I wanted to print a transposed version of my passed matrices. Then I would call

M:=<<1,2>|<3,4>>;

myProc(M);

And the result I want would be

M: <<1,3>|<2,4>>

But I don't know if _passed holds the symbol names or just the evaluated versions of the passed arguments??

And theoretically the passed argument (assuming a matrix) could be the matrix structure put directly in the procedure call, in which case there is no symbol to refer to.

 

I hope you get my question and can help me out.

 

Thanks

Simon

 

In this document, I think that is the relevent section. But I couldnt understand it.

 

I want something like this

>plot(sin(x),color=yellow);

>plot(sin(x),color=organe);

>plot(sin(x),color=red);

>plot(sin(x),color=green);

>plot(sin(x),color=ABC); # error

>plot(sin(x),color=CBA);# error

optional, but works fine within a given sets of choices.

 

How do I write a procedure like that?

 

Thanks!

to plot procedure...

November 16 2014 taro 120

Hello people in mapleprimes,

 

I want to draw a graph of a procedure.

 

filter:=x->if x<10 then 1-x/10 else 0 fi;

plot('filter(x)',x=0..20);

 

works well. But, I don't know why it is needed for filter(x) is wrapped with foward quotte ' .

I changed that quote to back quote ` but, in this case the result hadn't appeared.

And, without the foward quote ', error appears.

And, even if I wrapped x with ' as in plot('filter(x)','x' =0..20),

a graph appeared properly, though I cannot understand why wrapped x, 'x' is right.

 

Please teach me the logic of the above code.

Best wishes.

 

taro yamada

 

So the pseudocode I'm trying to translate into maple is for the greedy vertex coloring algorithm.

The pseudo code is:

Initialize:
1. for i = 1 to n do
2. f[i] := 0 (: no color assigned yet to vertex i :)
3. end(for)
Main loop:
4. for i = 1 to n do
5. let f[i] be the smallest positive integer
which is not in the set {f(j) : j ∈ adj[i]}
6. end(for)
7 return f

 

The closest I've gotten though is:

greedy := proc (G)

local i, j, n, k, L::list;

description "Take simple graph G and return a coloring of the vertices with at most Delta(G)+1 colors";

n := nops(Vertices(G));
L := [seq(x, i = 1 .. n)];
L := subsop(1 = c(1), L);
j := 1;
for i from 2 to n do
L[Vertices(G)[i]] := c(0)
end do;

for i from 2 to n do
   for k to i do
        if member(Vertices(G)[i], Neighbors(G, Vertices(G)[k])) then j := j+1
       end if; 
   end do;
L[Vertices(G)[i]] := c(j)
end do;
return L;
end proc;

Basically my procedure is returning more than delta+1 colors for most graphs, how can I edit it to fix this? I'm guessing the problem is somewhere in my nested loops. Also, I cannot use commands like greedycolor or isvertexcolorable. Thanks.

 

maple proc help...

November 04 2014 tuvok1153 5

Hi, I'm trying to write a maple procedure to differentiate a function from first principles and so far I have

First_Principle := proc (f); for f do A := simplify((f(x+h)-f(x))/h); Limit(A, h = 0) = limit(A, h = 0) end do end proc

and from there I have no idea where to go. Does anybody have any ideas? Thanks!

I have question about how to write the expression to get the order in the sequence?

For exmple, if I have this sequence,

E:=[0,2,7,15,26,40]

I want to write a procedure that tells me the location of any input,x in the sequence E.

For example, if I choose x:=3, then the number 3 is actually between 2 and 7, so it will give me the answer C(x)=3 (because 7 is the third elemet).

I try to write the procedure but there always wrong! I do not know what expression should I write!

C:=proc(x)
local
for i from 1 to nops(E) do
if x<=E[i] then
c:=E[i]
fi;od;
end;

Please help! 

So I'm trying to make a procedure to tell is a sequence is graphical or not.
seqgraph:= proc (L::list)

local k::integer, i::integer, n::integer, a::integer, S;

n := numelems(L);

S := convert(L, `+`);

a := 0;

if type(S, odd) then print("not graphical")

else for k to n do

if add(L[i], i = 1 .. k) <= k(k-1)+add(min(k, L[i]), i = k+1 .. n) then a := a+1

else a := a+2

end if;

end do;

end if;
if n = a then print(graphical)

else print("not graphical")

end if;

end proc;

 

 I'm trying to say that if that equality (which is part of the erdos gallai theorem) holds for that value of k then we'll add one to a value (a). Thus, if a=n at the end then the ineuqality was true for each k and thus it would it should print "graphical" but every list I test it one prints 'not graphical'. Where is my mistake? I get an error saying it can't execute add?

So if I have a procedure like 

with(GraphTheory)

tneighbors := proc (G::Graph)

local numvertices::integer, i::integer, currentvertex;

numvertices := nops(Vertices(G));

for i to numvertices do

currentvertex := Vertices(G)[i];

if nops(Neighbors(G, currentvertex)) = 2 then print(currentvertex)

end if;

end do;

end proc;

How do I make it so the output gets returned as a set?

For example, if I do  twoneighbors(G); and get
1

7

4

How do I make it so the output is listed as a set like {1,4,7}? Thanks.

I have a complicated equation which you can find in the file below. I want to multiply both sides of equaiton by cos(beta[1,j__1]*z) and integrate from 1 to L. I have many such similar equations so I decided to write a procedure to do these staffs for me.

Can you give me simple suggestions on how to write such a procedure. The procedure will take the "equation", "multiplier" and "limits of integration" as inputs and gives the "integrated equation" as the output. Integration is perfomed by the inert function "Int".

Many thanks.

Equation.mw

I'm used to Mathcad, and I am very new to Maple. Something I cannot figure out right now is how to define multiple elments of a matrix using a function.


Input Data

Define system dimensions as n:=2;

i:=1..n;

j:=1..n;

lambda:=Matrix(n);

Lambda:=Matrix(n);

upsilon:=vector(n); 

Minor side note: I originaly had upsilon defined using the syntax "Vector[row](ncomp)", but this was giving me an 'exponentiation' operation error, so I changed it to what it is now (basically a list/array, which I guess has different type definitions that no longer cause the error?).

lambda[1,2]:=471.0433;

lambda[2,1]:=883.7530;

upsilon[1]:=58.69;

upsilon[2]:=18.07;

The Problem I'm Having

Now I just want to define Lambda using a function to define all elements (like I would in Mathcad).

Lambda[i,j]:= (upsilon[j]/upsilon[i]) * exp (-lambda[i,j]/2853);

which gives me an extremely long error message:

Error, invalid input: exp expects its 1st argument, x, to be of type algebraic, but received Matrix(2, 2, {(1, 1) = -0.3504976272e-3, (1, 2) = -.16509955895845776, (2, 1) = -.30975332953088164, (2, 2) = -0.3504976272e-3}, datatype = float[8]

As far as I can tell (keep in mind that this is my very first Maple project) that it doesn't like lambda as a matrix? But shouldn't it just evaluate to the element? and why is (2,2) and (1,1) giving values, because it should intialize to zero, so exp(0) = 1 in these cases??

I tried to simplify further by just trying

Lambda[i,j]:=(upsilon[j]/upsilon[i]);

but then it just gives me 1, and when I look into Lambda all the elements are now 1 when only the diagnol elements should be 1 (the rest some fractional amount)??

I am at a complete loss. I thought about doing something like For i = 1 to n etc. but then it just looks like coding, which defeats the purpose of trying to make a calculation sheet documenting the procedure...

Any help and/or insights into what I am doing wrong here would be most welcomed :)

P.S. I've just noticed that there is an upload option :/ Wilson_Equation.mw.

 

 

``

restart:

 

C=2;

C = 2

(1)

w[2]*s[1]+w[1]*s[2];

s[1]*w[2]+s[2]*w[1]

(2)

C=3;

C = 3

(3)

(w[3]*s[2]+w[2]*s[3])*s[1];

(s[2]*w[3]+s[3]*w[2])*s[1]

(4)

C=4;

C = 4

(5)

((w[4]*s[3]+w[3]*s[4])*s[2]+w[2]*s[3]*s[4] )*s[1];

((s[3]*w[4]+s[4]*w[3])*s[2]+w[2]*s[3]*s[4])*s[1]

(6)

C=5;

C = 5

(7)

(((w[5]*s[4]+w[4]*s[5])*s[3]+w[3]*s[4]*s[5] )*s[2]+w[2]*s[3]*s[4]*s[5] )*s[1];

(((s[4]*w[5]+s[5]*w[4])*s[3]+w[3]*s[4]*s[5])*s[2]+w[2]*s[3]*s[4]*s[5])*s[1]

(8)

C=6;

C = 6

(9)

((((w[6]*s[5]+s[6]*w[5])*s[4]+w[4]*s[5]*s[6])*s[3]+w[3]*s[4]*s[5]*s[6])*s[2]+w[2]*s[3]*s[4]*s[5]*s[6])*s[1];

((((s[5]*w[6]+s[6]*w[5])*s[4]+s[5]*s[6]*w[4])*s[3]+s[4]*s[5]*s[6]*w[3])*s[2]+s[3]*s[4]*s[5]*s[6]*w[2])*s[1]

(10)

 

``


Download pattern.mw

 

This probably isnt too difficult to write a procedure to generate the expression, given C=2,3,4,5,...

But I may be thinking too much and stuck...

 

Also, I am thinking that if it is possible to use something like a "fibonacci sequence", in the famouse example here, which uses the in a procedure. Probably, we dont need to use this.

 

Thanks,

 

casper

 

Hello those who are in mapleprimes,

I want to devide both hand side of inequality by positive a.

And, the ways I want to know are two ways for doing this.

1)         c:=a<b: c/d assuming d>0;

returns 

I don't know why the result isn't a/d<b/d.

 

2)        the second way is one to focus on op(0,c) being <. And, from lhs(c), rhs(c), and op(0,c), 

I want to constract lhs(c)/d op(0,c) rhs(c)/d, that is lhs(c)/d<rhs(c)/d.

Please tell me about this.

 

Thank you in advance.

taro

 

Say the values of i^2 are being calculated from within a procedure,

 

mytest:=proc(n)

local ans;

    ans:=0;

    for i from 1 to n do

   ans:=ans+i^2;

   end do;

   return ans;

end proc;

 

Could I have a (time series) plot of i^2, while the values are being calculated?

The above is a toy example. What I want to archieve is to track a particular intermidiate values while the procedure is running.

 

Thanks,

 

casper

The permanent of a square matrix is defined similarly to the determinant, as a sum of products of sets of matrix entries that lie in distinct rows and columns. However, where the determinant weights each of these products with a ±1 sign based on the parity of the set, the permanent weights them all with a + 1 sign.While the determinant can be computed in polynomial time by Gaussian elimination, the permanent cannot. Valiant has showed that computing permanents is #P-hard, and even #P-complete for matrices in which all entries are 0 or 1 [L.G. Valiant, The Complexity of Computing the Permanent, Theoretical Computer Science 8 (1979) 189–201]. The development of both exact and approximate algorithms for computing the permanent of a matrix is an active area of research. (see Wikipedia: computing the permanent, more details on permanent)

The best known general exact algorithm is due to H. J. Ryser (1963) [Ryser, Herbert John (1963), Combinatorial Mathematics, The Carus mathematical monographs, The Mathematical Association of America. Ryser formula ]. Ryser's formula can be evaluated using O(2^{n}*n^2) arithmetic operations, whereas O(n!*n) arithmetic operations if we compute the permanent using the definition.

There have been codes of Ryser's algorithm for computing the permanent written in C language and Matlab. I cannot find a Ryser's procedure in Maple. So I try to write a Maple procedure to compute the permanent using Ryser's algorithm. The code is as follows:

permanentRyser := proc (M::Matrix) 
      local S, T, B, m, n, s, i, j, rowsum, sum, prod, perm;
      m, n := op(1, M);
      if m <> n then
               error "expecting a square Matrix, got dimensions %1, %2", m, n
      end if;
      rowsum := 0;
      sum := 0;
      prod := 1; 
      S := combinat:-subsets([seq(1 .. m)]);
      Snextvalue();
      while not Sfinished do
             T := Snextvalue();
             s := numelems(T);
             B := LinearAlgebra:-SubMatrix(M, [1 .. m], T);
             for i to m do
                  for j to s do
                          rowsum := rowsum+B[i][j];
                 end do;
                 prod := prod*rowsum; 
                 rowsum := 0;
             end do; 
            prod := (-1)^s*prod;
            sum := sum+prod;
            prod := 1 ;
      end do;
      perm := expand((-1)^m*sum) ;
end proc:
The last second statement "perm := expand((-1)^m*sum) ;" I mean to compute the permanent of  a matrix containing a variable, e.g. the characteristic matrix. If the matrix is numeric, then "expand" should be deleted.

Now I have two questions:

1. Suppose that A is random matrix of order 20, the time(permanentRyser(A)) is about 716 seconds and time(LinearAlgebra:-Permanent(A)) is about 66 seconds. We can see that LinearAlgebra:-Permanent(A) is much faster than permanentRyser(A).  I don't know whether the code I written is accurate and efficient. Thanks to anyone who gives a more efficient procedure of computing the permanent using Ryser's formular.

2. The source code of the function permanent is as follows.

proc (A::(`~Matrix`(square)), ` $`)
     option `Copyright (c) 1999 Waterloo Maple Inc. All rights reserved.`;
     local m, n, t1;
     m, n := op(1, A);
     if m <> n then
              error "expecting a square Matrix, got dimensions %1, %2", m, n
     end if;
     if n = 1 then t1 := A[1, 1] ;
     else
            tools/ClearRememberTable(Permanent/pminor`);
            t1 := [`$`(1 .. n)];
            t1 := Permanent/pminor(A, t1, t1, n)
      end if
end proc

I cannot understand the source code. Does the source code compute the permanent by Laplacian expansion? If the LinearAlgebra:-Permanent() compute the permanent by Laplacian expansion, then it should take more time than by using Ryser's algorithm.

Thanks all.

Hello those in Mapleprimes,

I want to obtain the result of the following codes,

shift:=(f::procedure)->(x->(f(x+1))):

shift(sin);

 

as x->sin(x+1)..................(1)

 

But, the result I could get was only

x -> f(x + 1)......................(2)

 

Please teach me how I can obtain the result as (1), not (2)?

 

Thanks in advance.

 

taro

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