Items tagged with procedure procedure Tagged Items Feed

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

Dear those who are in Mapleprimes,

 

As I can't understand how Maple calculate the quick sort, I write the present question, hoping that someone give me a hint.

In essence, nested procedure I cannot understand is this:

 

quicksort:=proc(A,m,n)

....................

if m<n then

quicksort(A,m,p-1);.................................(1)

quicksort(A,p+1,n)...................................(2) 

end if;

eval(A);

end proc:

 

Details were ommitted, but I have to say that p is the number between m and n, which is determined by ommitted parts of 

the procedure.

And, with this nested procedure, 

quicksort([2,4,1,5,3],1,5) is calculated.

 

What I want to know is how to process the parts of (1) and (2).

If after (1) is processed, (2) is processed, what is the value of p in (2).

Is it the p which is determined successively through processing nested value of (1)?

 

Thank you in advance.

 

taro

We have two texts in a certain language, represented as Maple  strings,  e. g.
"die einkommen der landwirte sind fuer die abgeordneten ein
buch mit sieben siegeln um dem abzuhelfen muessen dringend alle
subventionsgesetze verbessert werden" and
"die steuern auf vermoegen und einkommen sollten nach meinung
der abgeordneten nachdruecklich erhoben werden dazu muessen die
kontrollbefugnisse der finanzbehoerden dringend verbessert werden".
We have to find the maximal common subsequence of the words in the both texts,
i.e. "die  einkommen der abgeordneten muessen dringend verbessert werden" for
the above. If there are a few such ones, then only one is enough. How to program
that by SringTools and/or EssayTools in maximum efficient way? A compiled procedure is welcome.

As I stated in an earlier post, I'm new at this.

I'm trying to write a procedure to calculate what is called a "crescent latitude". The full formula can be seen here: https://db.tt/QAUzH5i0.

b is equal to 0.081819221; it takes a single parameter φ (latitude of a location in degrees)

 

I normally get errors like unable to parse, unable to match delimters, etc. and the result is the name of the procedure and whatevere value of the parameter I put in parenthesis, not the calculated value.

Anybody can help?

 

Thanlk you

Martina

Hi

I need you to help me in writting procedure with input "r" ( order of derivative) and some coefficients seq(alpha[i],i=1..N).  My code work very well, need only put all the element in procedure with output The Taylor series obtained in last line of my code and the order of error.  I want the procedure return the coefficients beta used in the series and the order of Error  and the coefficients beta[i]

May thinks.

Second_Question.mw

 

I will not modify my previous question: only I add this remark. I tried to write these lines of procedure.
Add_display.mw

I need only to add( if the procedure is true) these lines in my procedure.  These lines gives the degree of error computed in the code, but when I put these lines in the codes, there is an error. Thanks for your help.

Degree := degree(Error,stepsize);
    if (showorder) then:
       print(cat(`This stencil is of order `,Degree));
    fi:
    if (showerror) then:
       print(cat(`This leading order term in the error is `,Error));
    fi:
    convert(D[r$n](f)(vars) = stencil,diff);

 

 

 

Hi,

I want to display the matrix M, I used return M, in this procedure, but no matrix M displayed.

restart;
N:=2:
N:=2;
ff:=proc(N)
local M,i,p;
M:=array(1..2*N+1,1..2*N+1):
for i from 1 to 2*N+1 do
for  p from 1  to 2*N+1  do
   if p=1 then M[p,i]:=-2;
    elif  p=2*N+1  then M[p,i]:=-3;
else
M[p,i]:=0;
end if; end do; end do;
return M;
end proc;

 

Many thinks

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