Items tagged with for-loop

In C++, we use continue statement to continue with the loop. What is maple's equivalent statement of continue as used in C++?

Thanks:

So below is a calculation im trying to create, but it just tells me there is an unexpected IF error, and returns me to the third For line. I have tried it straight and as a procedure but i just cant seem to get it to work. Any ideas or tweaks to make this work would be much appreciated. All of the other variables I've named work in their respective lines of code, which I have left out as it is long as it is!


ComlexAlgorithm:= proc(L)
local N, j, kf, o, DeltaMag, DeltaBond;
global NConfig, Configz1, Col, Row, OldConfig, NewConfig, NewTable;

# Set Delta values to zero.
DeltaMag:=0:
DeltaBond:=0:

# Creates loops to loop through the correct changes in values i.e. Add -1, then 1 to first point, move across each point one by one, repeat from the beginning for each configuration then repeat whole process for the amount of rows.
printlevel:=4:
for N from -1 to 1 by 2 do
   for j from 1 to Col do
      for w from 0 to NConfig-1 do   
         for o from 1 to Row do

# Calculate DeltaMag.
if N=-1 then DeltaMag:=DeltaMag+1 else DeltaMag:=DeltaMag-1
end if:

# Calculate all relevant bonds i.e. 3 values for DeltaMag, except at "corner" lattice points where there is only 2 bonds.
if N= Configz1[w](o, j) then DeltaBond:= DeltaBond-1 else DeltaBond:= DeltaBond+1
end if:

if j-1>1 then
   if N <> Configz1[w](o, j+1) then DeltaBond:= DeltaBond+1
      if N = Configz1[w](o, j+1) then DeltaBond:= DeltaBond-1
else DeltaBond:= DeltaBond
end if:
end if:
end if:

if j+1<Col then
   if N <> Configz1[w](o, j+1) then DeltaBond:= DeltaBond+1
      if N = Configz1[w](o, j+1) then DeltaBond:= DeltaBond-1
else DeltaBond:= DeltaBond
end if:
end if:
end if:

# Calculate if, and by what degree, conifguration number changes using 2^(j-1) which is the arithmetric series for binary. Changes only occur when "incoming" spin is different.
if N= -1 then
   if Configz1[w](o, j) <> N then NewConfig[w]:= OldConfig[w-(2^(j-1))] 
else NewConfig[w]:= OldConfig[w]
end if:
end if:

if N= 1 then
   if Configz1[kf](o, j) <> N then NewConfig[w]:= OldConfig[w+(2^(j-1))] 
else NewConfig[kf]:= OldConfig[w]
end if:
end if:

NewTable[kf]:= [NewConfig[w], BondEnergy(w)+DeltaBond, MagEnergy(w)+DeltaMag];

# Set Delta values to zero to finsh.
DeltaMag:=0:
DeltaBond:=0:

end do:
end do:
end do:
end do:
end proc:

i want to write the result of a calculation to a file in a way that i can be able to call them later. for example :


restart;
n := 40; h := 40;
a := h*i/(2*n);

sys1 := [diff(x(t), t) = 2*t+1, diff(y(t), t) = 1+5*t, x(0) = 1, y(1) = 0];


for i from 0 by .5 to 1 do res := dsolve(sys1, numeric); res(a) end do;
 


i would like to write just the values of t and y(t) to a file/vector or whatever.

Thx in advance

Hi,

I would like to thank everyone who takes their time to respond to posts on this page. I have another question.

I have about 11 trillion lists that are of the form A shown below. The lists ofcourse have more elements than A (about 50 elements). What I want is to compute this value I call f, and the moment this value is found to be 1, we stop, and if all the values of f are 0, then we print(A). Here is my code, which has a problem and doesn't give me the result I want. How do I modify this so that if any value of f=1, then we stop, elif no 1 is found, we print just one value for A and not for every 0 produced?

with(ListTools);
A := [[1, 2], [1, 7], [5, 6], [1, 6], [1, 9], [6, 5], [9, 1], [2, 1], [7, 1], [6, 1], [5, 6]]; n := 9;
for i to n do for j to n do for k to nops(A) do if [i, j] = A[k] then a := Search([i, j], A); b := Search([j, i], A); if a < b then f := `mod`(b-a, 2); if f = 1 then break else print(A) end if end if end if end do end do end do;

Presently what I am doing is to Append all values of f into an Array and check that 1 is not an element of that Array. This is however very inconvenient since my lists are huge, and they are many, and we do not have to cpompute all the values of f once we come across a 1. Other than this, is there a way I can make my program run faster? Any suggestions are welcomed.

Thanks,

VIC.

I tried to use a for loop to assign the bounds for creating an RTable in a Maple C wrapper (actually it does not matter what I do here). I checked that the for loop syntax is correct C syntax, for example even as simple as for (int i = 0; i < 2; i++){...} but the loop becomes an infinite loop and takes forever to run. Has anyone encountered the same problem as I did

hello everybody, 

Is there some trick to bypass the limiited scope of the semicolon statement operator ?

More specifically, is it possible to force Maple to display (apart from the explicit use of the 'print' command) the results of the commands within two nested loops or within a loop and and a conditional statement ?


From the help page concerning statement operators it seems that it is truly impossible ...

"Note: This rule does not apply to statements enclosed in the body of another statement such as an if statement, loop, procedure, module, use statement, or try statement. In these cases, all enclosed statements must be separated by a semicolon or colon."


While not blocking it is a rather irritating limitation :-)

I'm new to Maple, and I come from using Matlab.  Apparently my Matlab way of thinking isn't working on Maple.  I'm trying  to ride a bit of code that simulates flipping a coin 1000 times.  The code counts one partiuclar result (1=heads) at the end prints that result.

 

Here's the code:

 

dice:=proc()

count:=0:  

for i from 1 to 10000 do  

roll := rand(1..2):  

roll:=()  

if (roll=1) then count:=count+1 end if  

end do  

print(count)  

 I get errors saying I didn't terminate my loops correctly.  Help, please.

the program is not recognized kthe program is not recognized the k

Assume we have a sreach with 10 for, for example

answer=0:

for i[1] from 1 by 1 to 5 do

    for i[2] from 1 by 1 to 5 do 

        ...

        if .... then answer=1: print(i[1],...,i[10]); "quitting the search"

        ...

     end do

end do

If I write break at qutting part, it will only exit from one for, one idea is putting if answer=1 break before end do of the rest for, that is why I used the local extra variable "answer" but is this the best idea? Any better idea which contains checking less if is apprecied.

Hi everyone,

Can anybody help me to write a programme in maple? I want to do a loop that iterate from an integer from 0 to N where I want to write a prodedure, say, MEC(N). Then I want to organise the output into a list of three lists  [[min points],[escape],[capture]], which, if correct, should include all integers from 0 to N exactly once.

> MEC := proc (N)
local IsEmpty, Escape, escape, capture, minpoints, Capture, z, MinPts, ic;
IsEmpty := array(0 .. 2*N);
Escape := array(0 .. 2*N);
Capture := array(0 .. 2*N);
MinPts := array(0 .. N);
IsEmpty := true;
Escape := true;
Capture := true;
MinPts := true;

for ic from 0 to N do
if IsEmpty then
z := ic;
MinPts[ic] := true;
z := ic;
while z <= 2*N and F(z) <= 2*N do
z := F(z);
IsEmpty[z] := true;
Escape[z] := true
end do;

z := ic;
while z <= 2*N and Finv(z) <= 2*N do
z := Finv(z);
IsEmpty[z] := true;
Capture[z] := true end do fi;
end do;
return [MinPts(z), Escape(z), Capture(z)]
end proc;

There are two operations involved in the program which is the function F and F inverse called Finv. 
i need to produce the output which contain the list of integers exactly once.

I could not figure out which logical expression that I do wrong.

Thank you for your help!

This is my code

 

am:=proc(dx,Nminus,Nplus)
global k_r,n,tau,a2,m;
k_r:=1/2;
a2:=a/2;
k=0;
u:=[]:
for n from Nminus to Nplus do
k:=k+1:
u(k):=max(exp((0.5*(k_r+1)^2)*0)*(exp(0.5*n*dx*(k_r+1))-exp(0.5*n*dx*(k_r-1))),0);
u:=[op(u),u(k)];
end do;
end proc;

 

I keep getting the error.

Error, (in am) invalid left hand side in assignment.

 

I would like to fill the array u with [u(1),u(2),...,u(Nplus-Nminus)]

 

Please help!

Hi

how to say 

i from 1 to 4 in maple

for example

if I have this eqauation

and I need to sub i in this equation to get 4 equation

Many thank

 

restart; with(Physics); with(Tetrads)

0, "%1 is not a command in the %2 package", Tetrads, Physics

(1)

Physics:-Setup(coordinatesystems = {X}, mathematicalnotation = true)

[coordinatesystems = {X}, mathematicalnotation = true]

(2)

Physics:-Define(Ybar[a], Y[a], GammaT[a, b, c], RicciT[b, c] = %d_[c](GammaT[`~a`, b, a])-%d_[a](GammaT[`~a`, b, c])+Physics:-`*`(GammaT[`~m`, b, a], GammaT[`~a`, m, c])-Physics:-`*`(GammaT[`~m`, b, c], GammaT[`~a`, m, a])+Physics:-`*`(GammaT[`~a`, b, m], GammaT[`~m`, c, a]-GammaT[`~m`, a, c]), RiemannT[k, l, m, n] = %d_[k](GammaT[m, n, l])-%d_[l](GammaT[m, n, k])+Physics:-`*`(GammaT[`~a`, m, l], GammaT[a, n, k])-Physics:-`*`(GammaT[`~a`, m, k], GammaT[a, n, l])+Physics:-`*`(GammaT[m, n, a], GammaT[`~a`, k, l]-GammaT[`~a`, l, k]))

RicciT[1, 3]

%d_[3](GammaT[`~1`, 1, 1]+GammaT[`~2`, 1, 2]+GammaT[`~3`, 1, 3]+GammaT[`~4`, 1, 4])+GammaT[`~4`, 1, 2]*GammaT[`~2`, 3, 4]+GammaT[`~1`, 1, 3]*GammaT[`~3`, 3, 1]+GammaT[`~2`, 1, 3]*GammaT[`~3`, 3, 2]+GammaT[`~4`, 1, 3]*GammaT[`~3`, 3, 4]-GammaT[`~2`, 1, 3]*GammaT[`~1`, 2, 1]-GammaT[`~2`, 1, 3]*GammaT[`~2`, 2, 2]-GammaT[`~2`, 1, 3]*GammaT[`~4`, 2, 4]-GammaT[`~3`, 1, 3]*GammaT[`~1`, 3, 1]-GammaT[`~3`, 1, 3]*GammaT[`~2`, 3, 2]-GammaT[`~3`, 1, 3]*GammaT[`~4`, 3, 4]-GammaT[`~4`, 1, 3]*GammaT[`~1`, 4, 1]-GammaT[`~4`, 1, 3]*GammaT[`~2`, 4, 2]-GammaT[`~4`, 1, 3]*GammaT[`~4`, 4, 4]-GammaT[`~1`, 1, 3]*GammaT[`~2`, 1, 2]-GammaT[`~1`, 1, 3]*GammaT[`~4`, 1, 4]+GammaT[`~1`, 1, 4]*GammaT[`~4`, 3, 1]+GammaT[`~2`, 1, 4]*GammaT[`~4`, 3, 2]+GammaT[`~4`, 1, 4]*GammaT[`~4`, 3, 4]+GammaT[`~1`, 1, 1]*GammaT[`~1`, 3, 1]+GammaT[`~2`, 1, 1]*GammaT[`~1`, 3, 2]+GammaT[`~4`, 1, 1]*GammaT[`~1`, 3, 4]+GammaT[`~1`, 1, 2]*GammaT[`~2`, 3, 1]+GammaT[`~2`, 1, 2]*GammaT[`~2`, 3, 2]+GammaT[`~1`, 3, 3]*GammaT[`~3`, 1, 1]+GammaT[`~2`, 3, 3]*GammaT[`~3`, 1, 2]+GammaT[`~3`, 1, 4]*GammaT[`~4`, 3, 3]-GammaT[`~3`, 4, 3]*GammaT[`~4`, 1, 3]-GammaT[`~1`, 1, 1]*GammaT[`~1`, 1, 3]-GammaT[`~1`, 1, 3]*GammaT[`~3`, 1, 3]-GammaT[`~2`, 1, 3]*GammaT[`~3`, 2, 3]-%d_[4](GammaT[`~4`, 1, 3])+%d_[1](GammaT[`~1`, 1, 3])+%d_[3](GammaT[`~3`, 1, 3])+%d_[2](GammaT[`~2`, 1, 3])

(3)

for a to 4 do for b to 4 do RicciT[a, b] end do end do

Error, (in index/PhysicsTensor) expected summation indices of type symbol, received: 1

 

 

Now, if I type the RicciT from (3) it displays the same result. However,......

 

RicciT[1, 3]

%d_[3](GammaT[`~1`, 1, 1]+GammaT[`~2`, 1, 2]+GammaT[`~3`, 1, 3]+GammaT[`~4`, 1, 4])+GammaT[`~4`, 1, 2]*GammaT[`~2`, 3, 4]+GammaT[`~1`, 1, 3]*GammaT[`~3`, 3, 1]+GammaT[`~2`, 1, 3]*GammaT[`~3`, 3, 2]+GammaT[`~4`, 1, 3]*GammaT[`~3`, 3, 4]-GammaT[`~2`, 1, 3]*GammaT[`~1`, 2, 1]-GammaT[`~2`, 1, 3]*GammaT[`~2`, 2, 2]-GammaT[`~2`, 1, 3]*GammaT[`~4`, 2, 4]-GammaT[`~3`, 1, 3]*GammaT[`~1`, 3, 1]-GammaT[`~3`, 1, 3]*GammaT[`~2`, 3, 2]-GammaT[`~3`, 1, 3]*GammaT[`~4`, 3, 4]-GammaT[`~4`, 1, 3]*GammaT[`~1`, 4, 1]-GammaT[`~4`, 1, 3]*GammaT[`~2`, 4, 2]-GammaT[`~4`, 1, 3]*GammaT[`~4`, 4, 4]-GammaT[`~1`, 1, 3]*GammaT[`~2`, 1, 2]-GammaT[`~1`, 1, 3]*GammaT[`~4`, 1, 4]+GammaT[`~1`, 1, 4]*GammaT[`~4`, 3, 1]+GammaT[`~2`, 1, 4]*GammaT[`~4`, 3, 2]+GammaT[`~4`, 1, 4]*GammaT[`~4`, 3, 4]+GammaT[`~1`, 1, 1]*GammaT[`~1`, 3, 1]+GammaT[`~2`, 1, 1]*GammaT[`~1`, 3, 2]+GammaT[`~4`, 1, 1]*GammaT[`~1`, 3, 4]+GammaT[`~1`, 1, 2]*GammaT[`~2`, 3, 1]+GammaT[`~2`, 1, 2]*GammaT[`~2`, 3, 2]+GammaT[`~1`, 3, 3]*GammaT[`~3`, 1, 1]+GammaT[`~2`, 3, 3]*GammaT[`~3`, 1, 2]+GammaT[`~3`, 1, 4]*GammaT[`~4`, 3, 3]-GammaT[`~3`, 4, 3]*GammaT[`~4`, 1, 3]-GammaT[`~1`, 1, 1]*GammaT[`~1`, 1, 3]-GammaT[`~1`, 1, 3]*GammaT[`~3`, 1, 3]-GammaT[`~2`, 1, 3]*GammaT[`~3`, 2, 3]-%d_[4](GammaT[`~4`, 1, 3])+%d_[1](GammaT[`~1`, 1, 3])+%d_[3](GammaT[`~3`, 1, 3])+%d_[2](GammaT[`~2`, 1, 3])

(4)

 

If I type a different RicciT, then ...

 

RicciT[2, 3]

Error, (in index/PhysicsTensor) expected summation indices of type symbol, received: 1

 

 

The for loop is changing the tetrad definition of RicciT.

 

NULL

 

Download Question_about_tetrads_in_for_loop.mw

Is there a way to loop over an entire worksheet I already have? I am too lazy to retype everything into one for loop, and I couldn't just copy and paste everything into one command thing (the space you make when you shift-enter). Is there something I can put before my worksheet that runs it, changes one variable, runs it again, etc.?

Thanks! I hope it is clear what I am asking. 

So basic problem.

 

I want to create a list with undetermined size. I have some data, let's say these data objects are numbers for now (they can be expressions of variables, or even equations)

 

Here is a pseudocode,

 

for n=1:10

  list(n) = n+1;

end

 

So the above for loop will create a list of size 9, with objects being number 2 through 11.

 

I want the list to be in {} form

 

Now I awnt to take this further. Let's say my data objects aren't just numbers, but expressions. How would I create a list of expressions and turning them into an equation?

 

Example

 

e1 := a^2 + b

e2 := b + a*b

 

e3:= b*a + a*b^2

 

Pesudocode

 

for i = 1:3 do

list(i) = ei ~=0;

end

 

Output should look like

 

list = {a^2 + b = 0, b + a*b=0,b*a + a*b^2=0}

 

Notice the use of curly braces

 

1 2 Page 1 of 2