Items tagged with code code Tagged Items Feed

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


It has been years since I last run one of my packages (not mine actually) and consequently maple.  The package was written in Maple V and run on Maple V5.  I have used updtscr to convert the old code and not many messages came out.   The updated code does not run unfortunately.  One of the problems is the following command:

linalg[dotprod](f,map((a,b) -> diff(b,a),vars,h));

Although linalg is deprecated, that does not seem to be the problem.   Suppose h, f (vector) and vars are:

The output is:

How to avoid the conjugate? Is there a simpler way to do that?

Many thanks for your patience and help.




Some years ago a postdoc working with me insisted to use Maple, since he was used to it. He left me some *.mws files with some calculations we had published together. I'd like to re-use the code now, preferably transposed to C or another programming language (there is not much use of Computer algebra in the code). Unfortunately it appears not so easy. I found the free Maple reader, and at least can now see the code. However, I did not find a way to copy-paste code from the Maple reader to a text file, or any other way to store the code in readable format (avoiding the Maple reader). I try to store the code by printing from the Maple reader with a kind of pdf writer, and a process started which kept my Laptop busy for more than an hour now without any output.

At the moment it looks like the two years of Postdoc work is lost, thanks to Maple and its strange restriction policy.

Can somebody let me know how to save the Maple input code of a *.mws file in a text file?



hello friend

please run this code and resend result to me.

thank you.


with(LinearAlgebra); with(plots);
c[1] := 0; c[2] := 1/2;
a[2, 1] := 1/8;
b[-1] := -1/3; b[1] := 2/3; b[2] := 5/6;
bb[2] := 5/12;
s := 2;
e[1] := Matrix(s+1, 1); e[2] := Matrix(s+1, 1); e[3] := Matrix(s+1, 1); e[4] := Matrix(s+1, 1);
c[0] := 0;
for ii to s+1 do e[1][ii, 1] := 1; e[2][ii, 1] := -1; e[3][ii, 1] := c[ii-1] end do;
e[1][1, 1] := 0; e[2][2, 1] := 0; e[4] := -e[3];
A := Matrix(s+1, s+1);
for i from 3 to s+1 do for j from 2 to i-1 do A[i, j] := a[i-1, j-1] end do; A[i, 1] := A[i, 2] end do;
Id := Matrix(s+1, s+1, shape = identity);
N := Matrix(s+1, s+1);
for i to s+1 do for j to s+1 do N[i, j] := -H*A[i, j]+Id[i, j] end do end do;
Bb := Matrix(s+1, 1);
for i from 3 to s+1 do Bb[i, 1] := bb[i-1] end do;
B := Matrix(s+1, 1);
for i from 2 to s+1 do B[i, 1] := b[i-1] end do; B[1, 1] := b[-1];
Z := Multiply(Transpose(Bb), 1/N);
for i to s+1 do Z[1, i] := H*Z[1, i] end do;
Q := Multiply(Transpose(B), 1/N);
for i to s+1 do Q[1, i] := H*Q[1, i] end do;
Dh := proc (H) options operator, arrow; Matrix([[1+Multiply(Z, e[1]), Multiply(Z, e[2]), 3/2+Multiply(Z, e[3]), -1/2+Multiply(Z, e[4])], [1, 0, 0, 0], [Multiply(Q, e[1]), Multiply(Q, e[2]), 1+Multiply(Q, e[3]), Multiply(Q, e[4])], [0, 0, 1, 0]]) end proc;
k := Matrix(4, 4);
for i to 4 do for j to 4 do if i = j then k[i, j] := ep-Dh(H)[i, j] else k[i, j] := -Dh(H)[i, j] end if end do end do;
sp := unapply(collect(Determinant(k), ep), H, ep);
sol := solve(sp(H, ep), ep, explicit);
sol := simplify(map(allvalues, {sol}));
l := numelems(sol);
inequal({evalc(abs(subs(H = x+I*y, sol[1]))) <= 1, evalc(abs(subs(H = x+I*y, sol[2]))) <= 1, evalc(abs(subs(H = x+I*y, sol[3]))) <= 1, evalc(abs(subs(H = x+I*y, sol[4]))) <= 1}, x = -3 .. 3, y = -3 .. 3, color = "Nautical 1");


I'm currently working on building a Grid Layout for a project, and I'm having trouble coding in the RunWindow and GetFile elements into buttons under the grid layout. I've gone through the overviews and examples for them, but had no luck. I'm using Maple 2016.1 for OS X.

Additionally, the structure of the code is slightly different as to how many of the example worksheets structure their Grid Layout code, since the code originated from a Maplet Builder file. I.e. in the example worksheets they would follow as:

maplet := Maplet('onstartup' = 'Action1', 'reference' = 'Maplet1',
         BoxLayout('background' = "#D6D3CE", 'border' = 'false', 'halign' = 'center', 'inset' = '5', 'reference' = 'BoxLayout1', 'valign' = 'center', 'vertical' = 'false', 'visible' = 'true',
                       BoxColumn( BoxCell('hscroll' = 'never', 'value' = 'Button1', 'vscroll' = 'never'),
         GridLayout('background' = "#D6D3CE", 'border' = 'false','halign'='center','inset'='5', 'reference' = 'GridLayout1', 'valign' = 'center', 'visible' = 'true',
                   GridRow('valign' = 'top', GridCell('height' = '1', 'hscroll' = 'never', 'value' = 'BoxLayout1', 'vscroll' = 'never', 'width' = '1' ))),
         Window('layout'= 'GridLayout1', 'reference' = 'W1', 'resizable' = 'true', 'title' = "Maplet"),
          Action('reference' = 'Action1', RunWindow('window'= 'W1'))

However the structure for the code I am working with has action at the very start of the code, follwed by the the code for the buttons then layouts/window.  E.g. (the code has been shortened)

with (Maplets[Elements]):
maplet :=
Evaluate('function'='plot3d(x^2*cos(y),x = -1 .. 1,y = -2*Pi .. 2*Pi)','target'='Plotter1','waitforresult'='true')),
Button('background'="#D6D3CE",'caption'="Insert Molecular Geometry",'enabled'='true','foreground'="#000000",'onclick'='clickButton1','reference'='Button1','visible'='true'),





If anyone would be able to provide an example of code or some guidance I could follow that would be greatly appreciated! 

Dear Forum, 


I am a new Maple user, and its symbolic prowess is really amazing. So we are trying to interface it with a C library. I want to generate some C code through Maple, and am trying the CodeGeneration package. 

But the default conversion of C(a, b) is b = C language equivalent of expression a.

Now this should be fine for most purposes, but the C library that we are working with, "ACADOToolkit" in this case, requires the equations to be formatted in a certain way. So, I need the following equation in C:


f << dot(v) == (u-0.2*v*v)/m


Now the LHS part of == is to be hard-coded, but we want to generate the equation on the right using maple. Even if I define an equation as 

eq1:= diff(v(t),t)=(u(t)-0.2*v(t)*v(t))/m(t) and then use C(rhs(eq1)), I get the result in the form of cg = u - 0.2 ...., whereas I want this to be assigned to something else, in this case - "f << dot(v)= ".


How can I achieve this ?



Chintan Pathak 

Research Scholar, 

University of Washington


I have problem to get real answer in a simple equation. maple just give me complex answer.

how i can get parametric real answer? Ihave trid this two way but not applicaple.

with(RealDomain); assume(T::real)

My code is:
Qz := 7.39833755306637215940309264474*10^7*sqrt(1/T)*(T-297.2)/T-16242.7935852035929839431551189*sqrt(1/T)/T;

q := (.6096*(299.2-T))/(sqrt(1.60000000000000000000000000000*10^(-9)-r^2)-0.346410161513775458705489268300e-4);

with(RealDomain); assume(T::real);

e := simplify(solve({0 = q-Qz}, {T}))

and the result like:

e := {T = 1/RootOf(-609600000000000000000000000000000000000000000000000000000+(879515018020273730453559011332895956000000000000000000000000000*sqrt(-625000000*r^2+1)-761682348615485390130551939524898425387968750740910059296172487)*Z^5+(-2959335021226548863761237057896000000000000000000000000000000*sqrt(-625000000*r^2+1)+2562859306691152293409465394507279449380503585614734443742000)*_Z^3+182392320000000000000000000000000000000000000000000000000000*_Z^2)^2}

dose anyone hase any opinion?

I accidentally came across a nice Mma animation. Unfortunately, I am able to present only few frames of it in MaplePrimes. See two inconsecutive frames below


I find this animation very deep. I don't remember something similar. It looks like an iterative
map shown in its dynamics. Not being an expert in Mathematica, I don't understand the machinery of the generating code.
n = 1000;
r := RandomInteger[{1, n}];
f := (#/(.01 + Sqrt[#.#])) & /@ (x[[#]] - x) &;
s := With[{r1 = r}, p[[r1]] = r; q[[r1]] = r];
x = RandomReal[{-1, 1}, {n, 2}];
{p, q} = RandomInteger[{1, n}, {2, n}];
Graphics[{PointSize[0.007], Dynamic[If[r < 100, s];
Point[x = 0.995 x + 0.02 f[p] - 0.01 f[q]]]}, PlotRange -> 2]
Here is its fragment translated into Maple:
>FromMma(" (#/(.01 + Sqrt[#.#])) & /@ (x[[#]] - x) &;");
map(unapply(_Z1/(0.1e-1+sqrt(_Z1 . _Z1)), _Z1), unapply(x(_Z1)-x, _Z1))
To my regret,
>FromMma(" n = 1000;
r := RandomInteger[{1, n}];
f := (#/(.01 + Sqrt[#.#])) & /@ (x[[#]] - x) &;
s := With[{r1 = r}, p[[r1]] = r; q[[r1]] = r];
x = RandomReal[{-1, 1}, {n, 2}];
{p, q} = RandomInteger[{1, n}, {2, n}];
Graphics[{PointSize[0.007], Dynamic[If[r < 100, s];
Point[x = 0.995 x + 0.02 f[p] - 0.01 f[q]]]}, PlotRange -> 2]");
Error, (in MmaTranslator:-FromMma) incorrect syntax (at position 11) in last character of "...0)


I have .mla. package and I would like to create the source code associated to this package.

In the past, you have also help me to obtain the source code of procedure of the module.

Here the post where Carl Love has helped me :

How can I do to create the complete source code of package directly ?

Is there a direct way enabling to not have to read one procedure by one procedure but to obtain the code source for all the package ?

Thank you for your help



I should be able to use F3 to break into a multline nested loop and insert a new line of code, and then F4 to close it up again before execution.  The F4 works for closing things up.  But the F3 does NOT work.  Is this a known problem?  Is there another way to do it short of a lot of cutting and pasting?

Euler := proc (f::procedure, a, b, alpha, n::integer)
local w, h, i, xN, yN;
w := array(1 .. 2, 1 .. n+1);
h := (b-a)/n;
i := 1;
xN := a;
yN := alpha;
while i <= n+1 do
w[1, i] = xN;
w[2, i] = yN;
print*w[1, i]
yN = yN+h*f(yN, xN);
xN = xN+h;
i = i+1
end do;
end proc

using the code generator assistant I entered the following function

p := proc (z::(float[8]))

local a::integer, accm::(float[8]), k::integer, k1::(float[8]), c;
c := Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], order = C_order, datatype = float[8]);
k1 := 1;
c[1] := evalf(sqrt(2*Pi));
a := 12;
for k to a-1 do c[k+1] := evalf(exp(a-k)*(a-k)^(k-1/2)/k1); k1 := -k1*k end do;
accm := c[1];
for k to a-1 do accm := accm+evalf(c[k+1]/(z+k)) end do;
accm := accm*evalf(exp(-z-a)*(z+a)^(z+1/2));
return accm/z
end proc

the code-generated julia code follows

function input(z)
c = [0,0,0,0,0,0,0,0,0,0,0,0]
k1 = 1
c[0] = (sqrt(2 * pi))
a = 12
for k = 1:a - 1
c[k] = (exp(a - k) * (a - k) ^ (k - 1//2) / k1)
k1 = -k1 * k
accm = c[0]
for k = 1:a - 1
accm = accm + (c[k] / (z + k))
accm = accm * (exp(-z - a) * (z + a) ^ (z + 1//2))
return(accm / z)

two things are wrong

1: no end after loop end

2: array index starts at 0, it should be 1 and of course the array references should reflect that


btw, it would be nice to be able to enter code tags like [code] code here [/code]

I have a .mpl file containing some procedures and other code.
When I open the file, everything is placed in a single execution group
(I use 1D math, worksheet mode).

Is it possible to have distinct execution groups?
For example, after each two blank lines (or other separator) in the .mpl file.

The same question when using .txt files as Maple input (in this case the code being not executed).

Thank you.

I use Mathematica. This code finds integer points on the sphere

(x-2)^2 + (y-4)^2 + (c-6)^2 =15

and select two of them so that distance of two this points equal to 4.

ClearAll[a, b, r, c];
a = 2;
b = 4;
c = 6;
r = 15; ss =
Subsets[{x, y, z} /.
Solve[{(x \[Minus] a)^2 + (y \[Minus] b)^2 + (z \[Minus] c)^2 ==
r^2, x != a, y != b, z != c, x y z != 0}, {x, y, z},
Integers], {2}];
t = Select[ss, And @@ Unequal @@@ Subsets[Flatten[#], {2}] &];
Select[ss, Apply[EuclideanDistance, #] === 4 &]


and this code select four points on the shere so that none of three points make a right triangle

ClearAll[a, b, r, c];
a = 2;
b = 4;
c = 6;
r = 15;
ss = Subsets[{x, y, z} /.
Solve[{(x - a)^2 + (y - b)^2 + (z - c)^2 == r^2, x != a, y != b,
z != c, x y z != 0, x > y}, {x, y, z}, Integers], {4}];
nonright =
Pick[ss, (FreeQ[#, \[Pi]/2] &) /@ ({VectorAngle[#2 - #1, #3 - #1],
VectorAngle[#1 - #2, #3 - #2],
VectorAngle[#1 - #3, #2 - #3]} & @@@ ss)];
Select[nonright, (12 == Length[Union @@ #] &)]

 I am looking for a  procedure in Maple.  I have some problems with this sphere. For example:

Choose four points so that 12 coordinates difference and it makes a square.

Can your code improve with sphere?

Dear all;


Hello everybody, I need your help to dispaly some values obtained using my function f. When I run the code there is no results obtained. Many thanks.


# The vectors e(i) satify the folowing conditions
e(0)*e(1)=e(n-1) assuming  1<n;
e(1)*e(1)=e(n-1) assuming  1<n: :
e(2)*e(1)=e(n) assuming  1<n:
for i from 1  to n-1 do
end do:

# We define the function f
for i from 2  to 3 do
end do:

for i from 4 to n do
end do:

# We define the two vectors

#Question : I would like to compute the following  but there is no display of the solution. 
f(x*y)- f(x)*y-x*f(y);

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