Items tagged with programming

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);


I want to make a package or module. Not sure. I cant get my head around what to do. I have loads of formulas I would like to be able to use without going back through work sheets. At present just want to keep it simple.

Have loaded a sample work sheet to use

Intend to place package in Maple2016 library folder.



Library Folder

C:\Program Files\Maple 2016\lib







Point2 := proc (x1, y1) options operator, arrow; [x1, y1] end proc

proc (x1, y1) options operator, arrow; [x1, y1] end proc


Point3 := proc (x1, y1, z1) options operator, arrow; [x1, y1, z1] end proc

proc (x1, y1, z1) options operator, arrow; [x1, y1, z1] end proc


a1 := Point3(x1, y1, z1)

[x1, y1, z1]


a2 := Point3(x2, y2, z2)

[x2, y2, z2]


a3 := Point3(x3, y3, z3)

[x3, y3, z3]


Line through 2 points

LinePts := proc (p1, p2) options operator, arrow; (p2[2]-p1[2])*x+(p1[1]-p2[1])*y-p2[2]*p1[1]+p1[2]*p2[1] end proc

proc (p1, p2) options operator, arrow; (p2[2]-p1[2])*x+(p1[1]-p2[1])*y-p2[2]*p1[1]+p1[2]*p2[1] end proc




Line := proc (a, b, c) options operator, arrow; a*x+b*y+c end proc

proc (a, b, c) options operator, arrow; a*x+b*y+c end proc


Lines s

LnPrll := proc (l1, l2) options operator, arrow; is(coeff(l1, x)*coeff(l2, y)-coeff(l2, x)*coeff(l1, y) = 0) end proc

proc (l1, l2) options operator, arrow; is(coeff(l1, x)*coeff(l2, y)-coeff(l2, x)*coeff(l1, y) = 0) end proc


Lines t

LnPrpnd := proc (l1, l2) options operator, arrow; is(coeff(l1, x)*coeff(l2, x)+coeff(l1, y)*coeff(l2, y) = 0) end proc

proc (l1, l2) options operator, arrow; is(coeff(l1, x)*coeff(l2, x)+coeff(l1, y)*coeff(l2, y) = 0) end proc




Quadrance between 2 points (2D)

Quadrance := proc (a1, a2) options operator, arrow; (a1[1]-a2[1])^2+(a1[2]-a2[2])^2 end proc

proc (a1, a2) options operator, arrow; (a1[1]-a2[1])^2+(a1[2]-a2[2])^2 end proc



Quadrance between 2 points (3D)

Qd3 := proc (a1, a2) options operator, arrow; (a1[1]-a2[1])^2+(a1[2]-a2[2])^2+(a1[3]-a2[3])^2 end proc

proc (a1, a2) options operator, arrow; (a1[1]-a2[1])^2+(a1[2]-a2[2])^2+(a1[3]-a2[3])^2 end proc


Triple Quad Formula (3 colinear points)

TQF := proc (Q1, Q2, Q3) options operator, arrow; (Q1+Q2+Q3)^2 = 2*Q1^2+2*Q2^2+2*Q3^2 end proc

proc (Q1, Q2, Q3) options operator, arrow; (Q1+Q2+Q3)^2 = 2*Q1^2+2*Q2^2+2*Q3^2 end proc



SpreadPoly := proc (n, s, rn) with(orthopoly); s-factor(1/2-(1/2)*T(n, 1-2*rn)) end proc

proc (n, s, rn) with(orthopoly); s-factor(1/2-(1/2)*T(n, 1-2*rn)) end proc


SpreadPoly(3, s, r)






Very new to using Maple or coding in general. Ran into a problem that I couldn't figure it out.

Any help would be greatly appreciated.

I'd like to find out what number Maple can still calculate the isprime for, and what number will make it go "No thanks, this is madness - ERROR".

I tried isprime (10100,000,000 + 2), and it can still calculate the result. No prime of course, that's the whole point of the +2.

I tried isprime(1010ˆ9 + 2) and it crashed.

But what is the tipping number?

I decided I should write a loop, and let maple write away the last number till it crashes. Is this possible? I think I'll need something like

> for n from 5 to ??? do isprime (10ˆn + 2) and ??? end do

This isn't suitable at all, but I cant figure out how to program "to error" or "write away this number"...


I hope the question is clear and look forward to ideas on how to make this work.


Ok i have used a diverse range of programming IDE now, but i am of course alot more comfortable the maple interface simply because of the amount of time i have spent on it, also it was my first "real" introduction to programming, but i was keen to trial and review of all the major CAS out on the market, but for a common purposed package within them they provide, for example the number theory package.


So the question is quite broad, im looking for suggestions for the most unbias way to go about this.

it would need to be a coauthored review with a representative writer for the seasoned user for each one right?

Dear Community,

I would like to calculate a rather sophisticated function (see it in the attachmenet) Once I try to evaluate it at certain values I get the error message:

"Error (in pD) illegal use of an object as a name" 

What object Maple means? I guess its a minor mistake, yet I'm lost here, and the online help says "There is no help page available for this error"

Tx for the kind help in advance,


Walking into the big blue Maplesoft office on August 3rd was a bit nerve wracking. I had no idea who anyone was, what to expect, or even what I would be doing. As I sat in the front hall waiting for someone to receive me, I remember thinking, “What have I gotten myself into?”. Despite my worries on that first day, interning at Maplesoft has been a great experience! I never knew that I would be able to learn so much about programming and working in a company in such a short amount of time. Although Maple was a programming language that was foreign to me a couple weeks ago, I feel like I’m relatively well versed in it now. Trying to learn a new language in this short timespan hasn’t been easy, but I think that I picked it up quickly, even if I’ve had my fair share of frustrations.

Chaos Game example on Rosetta Code

At Maplesoft, I’ve been contributing to the Rosetta Code project by writing short programs using Maple. The Rosetta Code project is dedicated to creating programming examples for many different tasks in different programming languages. My summer project has been to create solutions using Maple for as many tasks as possible and to post these to Rosetta Code; the goal being to have the list of tasks without Maple implementation shrink with each passing day. It’s nice to feel like I’m leaving a mark in this world, even if it is in such a small corner of the internet.

Flipping Bits example on Rosetta Code/MapleCloud

This internship, of course, came with its share of challenges. During my work on the Rosetta Code project, I posted solutions for a total of 38 tasks. Some of them were easy, but some of them took days to complete. On some days, I felt like I was on top of the world. Everything I made turned out great and I knew exactly how to tackle each problem. Other days were slower. I’ve spent ages just staring at a computer monitor trying to figure out just how on earth I was going to make this machine do what I wanted it to do! The 24 Game task was particularly hard, but also very educational. Through this task, I learned about modules, a concept previously unknown to me. I’m fairly sure that the 24 Game also took me the longest, whereas the Increment a numerical string task took me no time at all. Despite it being easy, the Increment a numerical string task wasn’t particularly fun; a bit of a challenge is required for something to be entertaining, after all. My personal favourite was the Fibonacci n-step number sequences task. It was the first really challenging task I encountered, and for after which the feeling of finally completing a task that I spent so long on, of finally overcoming that mountain, was extremely satisfying. Not all challenges end in satisfaction, however. I often found myself accidentally doing something that made the window freeze. I would close the program, then cry a bit on the inside when I realized I just lost the past half an hour’s worth of unsaved work. Nevertheless, I’m glad I got to face all these obstacles because they have made me more resilient and a better programmer.

The following is the code for the Fibonacci n-step number sequences task

numSequence := proc(initValues :: Array)
	local n, i, values;
n := numelems(initValues);
values := copy(initValues);
for i from (n+1) to 15 do
values(i) := add(values[i-n..i-1]);
end do;
return values;
end proc:
initValues := Array([1]):
for i from 2 to 10 do
initValues(i) := add(initValues):
printf ("nacci(%d): %a\n", i, convert(numSequence(initValues), list));
end do:
printf ("lucas: %a\n", convert(numSequence(Array([2, 1])), list));

Maple was a great software to program with and a fairly straightforward language to learn. Having previously programmed in Java, I found Maple similar enough that transitioning wasn’t too difficult. In fact, every once in a while when I didn`t know what to do for a task, I would take a look at the Java example in Rosetta Code and it would point me in a direction or give me some hints. While the two languages are similar, there are still many differences. For example, I liked the fact that in Maple, lists started at an index of 1 rather than 0 and arrays could an arbitrary starting index. Although it was different from what I was used to, I found that it made many things much less confusing. Another thing I liked was that the for loop syntax was very simple. I never once had to run through in my head how many times something would loop for. There were such a wide variety of commands in Maple. There was a command for practically anything, and if you knew that it existed and how to use it, then so much power could be at your fingertips. This is where the help system came in extremely handy. With a single search you might find that the solution to the exact problem you were trying to solve already existed as a Maple command. I always had a help window open when I was using Maple.

Multiplication Tables example on Rosetta Code

Spending my summer coding at Maplesoft has been fun, sometimes challenging, but an overall rewarding experience. Through contributing to the Rosetta Code project, I’ve learned so much about computer programming, and it certainly made the 45 minute drive out to Waterloo worth it!

Yili Xu,
Maplesoft SHAD Intern

pls help review this code, its doesnt return a solution



Digits := 16;
M := .5; lambda := .5; Pr := .72; beta := 1; L[w] := 0; m := 1; R := 1; Ec := 1;
N := 7;
for j from 0 to N do J[j] := sum(f[k](t)*(diff(f[j-k](t), `$`(t, 2))), k = 0 .. j) end do;
for i from 0 to N do K[i] := sum((diff(f[k](t), t))*(diff(f[i-k](t), t)), k = 0 .. i) end do;
for j from 0 to N do G[j] := sum(f[k](t)*(diff(theta[j-k](t), t)), k = 0 .. j) end do;
for j from 0 to N do H[j] := sum((diff(f[k](t), t))*theta[j-k](t), k = 0 .. j) end do;
for i from 0 to N do P[i] := sum((diff(f[k](t), t, t))*(diff(f[i-k](t), t)), k = 0 .. i) end do;
epsilon := 1; delta := 0;
f[0] := proc (t) options operator, arrow; L[w]+epsilon+delta*A*t+(1/2)*A*t^2 end proc;
1 2
t -> L[w] + epsilon + delta A t + - A t
theta[0] := proc (t) options operator, arrow; 1+B*t end proc;
t -> 1 + B t
for i to N do f[i] := simplify(-((m+1)*(1/2))*(int(int(int(J[i-1], t = 0 .. eta), t = 0 .. eta), t = 0 .. eta))+m*(int(int(int(1-K[i-1], t = 0 .. eta), t = 0 .. eta), t = 0 .. eta))-M*(int(int(int(diff(f[i-1](t), t)-1, t = 0 .. eta), t = 0 .. eta), t = 0 .. eta))-lambda*(int(int(int(theta[i-1](t), t = 0 .. eta), t = 0 .. eta), t = 0 .. eta))); f[i] := unapply(f[i], eta); theta[i] := simplify(-3*Pr*R*(((m+1)*(1/2))*(int(int(G[i-1], t = 0 .. eta), t = 0 .. eta))-(2*m-1)*(int(int(H[i-1], t = 0 .. eta), t = 0 .. eta))+Ec*(int(int(P[i-1], t = 0 .. eta), t = 0 .. eta)))/(4+3*R)); theta[i] := unapply(theta[i], eta) end do;
for k from 2 to 5 do W[k] := pade(diff(F(eta), eta), eta, [k, k]); Q[k] := pade(Theta(eta), eta, [k, k]); SOLL1[k] := expand(coeff(numer(W[k]), eta^k)) = 1; SOLL2[k] := expand(coeff(numer(Q[k]), eta^k)) = 0; SOL[k] := solve({SOLL1[k], SOLL2[k]}, {A, B}); print([k] = SOL[k]) end do;
Warning, computation interrupted







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