Items tagged with programming

My code

checkInSet := proc(L, x)
    local i;
    if nops(L) = 0 then
        return 0;
        for i from 1 to nops(L) do
            if x = L[i] then
                return i;
end proc:



M := {(a-2*b-2*c)^2*(2*a-b+2*c)^2, (a-2*b-c)^2*(a-b+2*c)^2, (a-2*b+c)^2*(a+b-2*c)^2, (a-2*b+2*c)^2*(2*a+b-2*c)^2, (a-b-2*c)^2*(2*a-b+c)^2, (a-b-c)^2*(a-b+c)^2, (a-b+c)^2*(a+b-c)^2, (a-b+2*c)^2*(2*a+b-c)^2, (a+b-2*c)^2*(2*a-b-c)^2};


But checkInSet(M, (a-2*b-2*c)^2*(2*a-b+2*c)^2) not work? Someone please help me.

Thanks you very much.


I was creating this code for the "Müller Method" for Numerical Analysis. Everything works fine till the if (for getting the x3) goes on. The problem is, the Maple doesn't detects the if.
I tried checking every variable and every variable is calculated except "x3"; even "disc".

I would like to know what happened. Anyways, here's the code:

x0 := 0; x1 := 8.4; x2 := 10; iter := 1000; tol := 10^(-8); f := proc (x) options operator, arrow; 3*x^3+7*x^2+x+2 end proc; f(x); plot(f(x), color = green); printf(" n          x0               x1               x2              x3                Error \n"); for i to iter do d0 := evalf((f(x1)-f(x0))/(x1-x0)); d1 := evalf((f(x2)-f(x1))/(x2-x1)); h1 := evalf(x2-x1); h0 := evalf(x1-x0); a := evalf((d1-d0)/(h1-h0)); b := evalf(a*h1+d1); c := evalf(f(x2)); disc := sqrt(-4*a*c+b^2); if abs(b+disc) > abs(b-disc) then x3 := x2+(-2*c)*(1/(b+sqrt(-4*a*c+b^2))); erry := abs((x3-x2)/x3) else x3 := x2+(-2*c)*(1/(b-sqrt(-4*a*c+b^2))); erry := abs((x3-x2)/x3) end if; if erry > tol then x0 := x1; x1 := x2; x2 := x3; printf("%2d     %2.8f      %2.8f       %2.8f         %2.5 f     %2.8 f \n", i, a, b, c, x3, erry) else printf("una raiz es: %2.8f ", x3); break end if end do;

I put the image (as it looks on my maple) and the "code" so you can copy-paste it in Maple.

Let us consider the linear integer programming problem:

A := Matrix([[1, 7, 1, 3], [1, 6, 4, 6], [17, 1, 5, 1], [1, 6, 10, 4]]):
 n := 4; z := add(add(A[i, j]*x[i, j], j = 1 .. n), i = 1 .. n):
restr := {seq(add(x[i, j], i = 1 .. n) = 1, j = 1 .. n), seq(add(x[i, j], j = 1 .. n) = 1, i = 1 .. n)}:
 sol := Optimization[LPSolve](z, restr, assume = binary);

Error, (in Optimization:-LPSolve) no feasible integer point found; 
use feasibilitytolerance option to adjust tolerance

sol1 := Optimization[LPSolve](z, restr, assume = binary, feasibilitytolerance = 100, integertolerance = 1);

Error, (in Optimization:-LPSolve) no feasible integer point found;
 use feasibilitytolerance option to adjust tolerance

That was OK in Maple 16, outputting


The bug in one of the principal Maple commands lasts since Maple 2015, where the above code causes "Kernel connection has been lost". The SCRs about it were submitted three times (see

I found 4 ways so far to call a Maple sin function (to return numerical value).

Can you find more ways?







I am maple newbie. Sometimes when I look at Maple code to try to understand the algorithm (which is hard, since I do not know Maple well), I see the code puts ` ` around some keywords. And sometimes it does not. For example, sometimes I see something like (these are random samples) from Maple code shown using showstat()

return `if`(assigned(r),r,{})
r := `union`(r,{solve(op(1,expr) = 1,vs)})
v2 := `intersect`(vs,indets(op(1,expr),'name'));

But sometimes, they do not put ` ` around functions name or keywords, like this:

if nops(v2) = 1 then
t1 := remove(a -> has(a,RootOf),t1);
for t in expr do

and so on.

Can some expert please give what is the rule thumb to use? Should user adopt this method also? When to put ` ` and when not to put ` `?   I understand that ` ` prevents one-time evaluation (or rather, holds off immediate evaluation) and ``  `` prevents two times evaluation and so on. But when to use ` ` is what is confusing me.


how to convert a nested for loop to iterative version with stack

I was trying to write a procedure that would compute a simple linear equation using the Extended Euclidean Algorithm. I was thinking of a procedure like the following:

solveEeaMatrix := proc (a::list, b::list) 
 local c::list;  
 c := a -iquo(a[1],b[1])*b;  
 while (c[1] <> gcd(a[1],b[1]) do 

I am basically stuck at this part as

1) I don't know how to setup a multi-dimensional array that could dynamically grow(as a possible solution).

2) I can't come up with a recursive function that could possibly take care of this.

In short, if I am given for example an equation like: 84*x+203*y = 14

I will transform it into 2 linear equation as follow:
row0 := [203, 0, 1] row1 := [84, 1, 0] Subsequently, I will perform the following:

c := a -iquo(a[1],b[1])*b;  

Where aand b are both lists and arguments of the procedure and cbeing another list and a local variable.

But I don't know how to do the following programmatically:

row3 := row1-iquo(row1[1], row2[1])*row2;
row4 := row2-iquo(row2[1], row3[1])*row3;
row5 := row3-iquo(row3[1], row4[1])*row4;
and so on ...                  

Any hint would be appreciated.

Please Sir/Ma, I'm trying to generate a recurrent relations of this series and I try to use "if" "else" condition but I didn't get it right. Any one with useful suggestions. Appreciate 


Y[0] := A; Y[1] := B;
if k = a then delta(k-a) := 1 else 0 end if;
for k from 0 to 10 do Y[k+2] := solve(add(delta(i-1)*(k-i+1)*(k-i+2)*Y[k-i+2], i = 0 .. k)+add((delta(i)-delta(i-1))*(k-i+1)*Y[k-i+1], i = 0 .. k)+lambda*Y[k] = 0, Y[k+2]) end do;
y := sum(Y[j]*x^j, j = 0 .. 10);


So as to build a function which gives several outputs, i have made a code with this manner :

Input:=[i1, ..., in]


      export o1, o2, o3
      end module

End proc;

By doing, output1:=Output(Input):-o1; , i obtained my result. However, as my structure begins with the procedure (proc End proc), I can't export my result in package.

For this reason, I'm thinking about changing the structure of my code by starting with the creation of the module and put all the functions inside.

Input:=[i1, ..., in]


export o1,o2,o3;
option package;
end proc;

end proc;

end proc;
End module;

Can you give me your feedback on the two structures ? Do you think that the second choice Module->Functions is more appropriated ?

Thanks a lot of your help and feedback.

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.

# 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.
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.

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

On some platforms, my editor of choice has become the aptly named Sublime Text. Unfortunately, it does not seem to have built in syntax highlighting for the Maple programming language and so I set out to write some.  In the end, I wrote enough highlighting to keep me sane when looking at Maple source, but it could use a lot more work.  So in case anyone is interested I've put what I have in a Github repository: SublimeTextMaple

If you use Sublime Text, please download it and add your own enhacements and share in turn.

I Could Not Write An If Then Or Ifelse Statement. Please Help Me.

f := unapply(x^2-2, x); a := 1; b := 2; n := 10; Digits := 10;
x -> x  - 2
c := evalf(eval((a*f(b)-b*f(a))/(f(b)-f(a))));
if  f(c)*f(a)<0 then ;
          "      k:=evalf(eval(|(f(c))/(b-c)|)) and "

                          /(1 + k) a f(b) - b f(a)\
             x[i] := evalf|-----------------------|
                          \  (1 + k) f(b) - f(a)  /
            "     elif f(x[i])*f(a)<0 then b:=x[i]"
                 "     else b:=c and a:=x[i] "
                  "     if f(c)*f(a)>0 then "
                 "      k:=|(f(c))/(b-c)|and "

                          /a f(b) - b f(a) (1 + k)\
             x[i] := evalf|-----------------------|
                          \  f(b) - f(a) (1 + k)  /
            "     elif f(x[i])*f(a)>0 then a:=x[i]"
              "     else a:=c and b:=x[i] end if"

Error, unterminated 'if' statement

       if fApplyFunction(c)sdotfApplyFunction(a)lt0 then , 

       Typesetting:-merror("unterminated 'if' statement")))

I am trying to implement Subresultant p.r.s. algorithm for calculating greatest common divisor. The algorithm decribed in the book:

My code return the correct GCD, however the sub-resultant terms are different from the result of the built-in function. The last term a[i-1] is huge and involves fractions. I think my implementation is same as the algorithm described in the textbook.

I have attached the file. Could anybody spot anything wrong in the code? Why do fractions still appear? In my code, "lsr" is last subresultant term returned from the built-in function, the second one is my result.


[Chain, ChangeOfOrder, Construct, Cut, DahanSchostTransform, Dimension, Empty, EqualSaturatedIdeals, EquiprojectableDecomposition, Extend, ExtendedNormalizedGcd, IsAlgebraic, IsEmptyChain, IsInRadical, IsInSaturate, IsIncluded, IsPrimitive, IsStronglyNormalized, IsZeroDimensional, IteratedResultant, LastSubresultant, Lift, ListConstruct, NormalizeRegularChain, NumberOfSolutions, Polynomial, Regularize, RemoveRedundantComponents, SeparateSolutions, Squarefree, SquarefreeFactorization, SubresultantChain, SubresultantOfIndex, Under, Upper]


A42vlastsub := proc (f, g) local i, a, dt, bt, om; i := 1; if degree(f) < degree(g) then a[0] := primpart(g, x); a[1] := primpart(f, x) else a[0] := primpart(f, x); a[1] := primpart(g, x) end if; dt[0] := degree(a[0])-degree(a[1]); bt[2] := (-1)^(dt[0]+1); om[2] := -1; while a[i] <> 0 do a[i+1] := normal(prem(a[i-1], a[i], x)/bt[i+1]); dt[i] := degree(a[i])-degree(a[i+1]); i := i+1; om[i+1] := (-lcoeff(a[i-1]))^dt[i-2]*om[i]^(1-dt[i-2]); bt[i+1] := -lcoeff(a[i-1])*om[i+1]^dt[i-1] end do; return a[i-1] end proc;



f := (y^2-1)*((y+1)*x^4+(y^2-1)*x^3+(y^3-1)*x^2+(y^4-1)*x+y^5-1);



R := RegularChains:-PolynomialRing([y, x]);



lsr := LastSubresultant(src, R);




mylastsr := A42vlastsub(primpart(f, x), primpart(g, x));






How can I ask Maple to wake me up after running the code?

It takes a long time to finish the job. So it would be helpful to play a beep or something to inform me the answer is ready.

Thank you in advance for your help.

hi i have this code that doesnt work and I cant find the problem. I cant make the matrix include more than 2 rows. i want this to give me the whole nxn matrix, and then the solution to the wronskian. 

can you pls help?:) thanks! 

funcs := [x, 3*x, x^2, 5*x];
n := nops(funcs);
printlevel := n;
count := 1;
listM[count] := funcs;
for i from 2 to n do listN := diff(funcs, `$`(x, i-1));
count := count+1;
listM[count] := listN end do;
M := convert(listM, matrix);

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