How to convert Maple Code to Matlab code

My class partner did our homework in Maple code, but to submit it we need it to work in Matlab. She did this when she was in her home country for the holidays and doesn't have access to Maple anymore (and neither do I), but I have read that there is a built in function in Maple that can do it automatically. Can someone convert this for me or at least show me how? I have no experience at all with Maple.
restart: with(LinearAlgebra)
n:=5; A:=Matrix(n,n); B:=Vector(n,[1.0,1.5,1.0,1.0,1.0]); BB:=Vector(n,[1.0,1.5,1.0,1.0,1.0]);
for i to n do
for j to n do
if j=i then A[i,j]:=2; end if;
if i+1=j or i-1=j then A[i,j]:=-1 end if;
end do;
end do;
print(A);
RR:=LinearSolve(A,BB,method='Cholesky', inplace); A; B;
print(A); print(B);
CONJUGENT GRADIENT METHOD:
niter:=5:
X:=Vector(n); R:=Vector(n);R1:=Vector(n);R2:=Vector(n); W:=Vector(n); P:=Vector(n);
for i to n do X[i]:=0.0: R1[i]:=B[i];end do;
GR1:=NULL:GR2:=NULL;
for k to niter do
if k=1 then for i to n do P[i]:=R1[i]: end do;
else
restart: with(LinearAlgebra):
n:=5; A:=Matrix(n,n); B:=Vector(n,[1.0,1.5,1.0,1.0,1.0]); BB:=Vector(n,[1.0,1.5,1.0,1.0,1.0]);
for i to n do
for j to n do
if j=i then A[i,j]:=2; end if;
if i+1=j or i-1=j then A[i,j]:=-1 end if;
end do;
end do;
print(A);
RR:=LinearSolve(A,BB,method='Cholesky', inplace); A; B;
print(A); print(B);
CONJUGENT GRADIENT METHOD:
niter:=5:
X:=Vector(n); R:=Vector(n);R1:=Vector(n);R2:=Vector(n); W:=Vector(n); P:=Vector(n);
for i to n do X[i]:=0.0: R1[i]:=B[i];end do;
GR1:=NULL:GR2:=NULL;
for k to niter do
if k=1 then for i to n do P[i]:=R1[i]: end do;
else
beta:=add(R1[j]^2,j=1..n)/add(R2[j]^2,j=1..n);
for i to n do P[i]:=R1[i]+beta*P1[i]: end do;
end if;
for i to n do W[i]:=add(A[i,j]*P[j],j=1..n); end do;
vard:=add(P[j]*W[j],j=1..n); printf("k=%d vard=%e\n",k,vard);
alfa:=add(R1[j]^2,j=1..n)/vard;
for i to n do X[i]:=X[i]+alfa*P[i]: end do;
for i to n do R[i]:=R1[i]-alfa*add(A[i,j]*P[j],j=1..n): R2[i]:=R1[i]; R1[i]:=R[i]; P1[i]:=P[i]; end do;
eps:=sqrt(add(R[j]*R[j],j=1..n)); printf("k=%d eps=%e\n",k,eps); print(X);
for i to n do W[i]:=add(A[i,j]*X[j],j=1..n):end do: func:=add(X[j]*W[j],j=1..n)-add(B[j]*X[j],j=1..n);
GR1:=GR1,[k,func]; GR2:=GR2,[k,eps]; printf("k=%d func=%e eps=%e\n",k,func, eps); print(A); print(B);
end do;
RR; X;
plot([GR1],axes=boxed); plot([GR2],axes=boxed);
beta:=add(R1[j]^2,j=1..n)/add(R2[j]^2,j=1..n);
for i to n do P[i]:=R1[i]+beta*P1[i]: end do;
end if;
for i to n do W[i]:=add(A[i,j]*P[j],j=1..n); end do;
vard:=add(P[j]*W[j],j=1..n); printf("k=%d vard=%e\n",k,vard);
alfa:=add(R1[j]^2,j=1..n)/vard;
for i to n do X[i]:=X[i]+alfa*P[i]: end do;
for i to n do R[i]:=R1[i]-alfa*add(A[i,j]*P[j],j=1..n): R2[i]:=R1[i]; R1[i]:=R[i]; P1[i]:=P[i]; end do;
eps:=sqrt(add(R[j]*R[j],j=1..n)); printf("k=%d eps=%e\n",k,eps); print(X);
for i to n do W[i]:=add(A[i,j]*X[j],j=1..n):end do: func:=add(X[j]*W[j],j=1..n)-add(B[j]*X[j],j=1..n);
GR1:=GR1,[k,func]; GR2:=GR2,[k,eps]; printf("k=%d func=%e eps=%e\n",k,func, eps); print(A); print(B);
end do;
RR; X;
plot([GR1],axes=boxed); plot([GR2],axes=boxed);

 Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de En. de 2017

0 votos

The conversion tool in Maple, CodeGeneration[Matlab], cannot convert calls like LinSolve. And the parts it can convert of your code are not difficult to convert by hand. The conversion tool does a poor job formatting.
(I have a revised version that does better formatting but I have not been able to track down some significant bugs in what I wrote.)

4 comentarios

Brendan O'Connor
Brendan O'Connor el 5 de En. de 2017
Thank you for the quick response and sorry for delayed one. The email that lets me know of replies is really slow. But I guess then that I will just have to find someone that knows Maple here and can help me convert it. I appreciate your input. Have a great day
Maple version:
method1 := proc (B::Vector)
local n, A, RR, i, j;
n := upperbound(B);
A := Matrix(n, n);
for i to n do
for j to n do
if j = i then
A[i, j] := 2
end if;
if i+1 = j or i-1 = j then
A[i, j] := -1
end if
end do
end do;
print(A);
RR := LinearAlgebra:-LinearSolve(A, BB, method = 'Cholesky', inplace); A; B;
print(A); print(B);
RR #return RR
end proc
Automatic Maple translation to MATLAB:
CodeGeneration[Matlab](method1);
Error, (in CodeGeneration:-IssueError) cannot analyze non-integer range boundary n
Revised Maple version to get around this limitation:
method1 := proc (B::Vector)
local A, RR, i, j;
A := Matrix(5, 5);
for i to 5 do
for j to 5 do
if j = i then
A[i, j] := 2
end if;
if i+1 = j or i-1 = j then
A[i, j] := -1
end if
end do
end do;
print(A);
RR := LinearAlgebra:-LinearSolve(A, BB, method = 'Cholesky', inplace); A; B;
print(A); print(B);
RR #return RR
end proc
Automatic Maple translation to MATLAB:
CodeGeneration[Matlab](method1);
Warning, the function names {`LinearAlgebra:-LinearSolve`} are not recognized in the target language
function method1return = method1(B)
A = [0 0 0 0 0; 0 0 0 0 0; 0 0 0 0 0; 0 0 0 0 0; 0 0 0 0 0;];
for i = 1:5
for j = 1:5
if (j == i)
A(i,j) = 2;
end
if (i + 1 == j || i - 1 == j)
A(i,j) = -1;
end
end
end
disp(sprintf(num2str(A)));
RR = LinearAlgebra:-LinearSolve(A, BB, method == Cholesky, inplace);
A;
B;
disp(sprintf(num2str(A)));
disp(sprintf(num2str(B)));
method1return = RR;
Your class partner knows Maple or else she would not have been able to write the code. You can see that the bulk of it translates very directly into MATLAB. And you can also see that the critical parts of it do not translate automatically. Your partner can supply information about what her intent was for each statement, and you (or other partners) can find the equivalent MATLAB. For example the "method = Cholesky" suggests that you might need to use a call to chol()
The first block of code marked "CONJUGENT GRADIENT METHOD:" contains within it "restart: with(LinearAlgebra):" and proceed to copy all of the original code including the "CONJUGENT GRADIENT METHOD:" line. It appears that someone had an accident with copy and paste. However, it is not possible to just delete all of the code above that point: the line before the "restart" contains an "if" that has a matching "end if" down the ways.
In Maple, "restart" means the equivalent of "clear all including whatever is being computed". After a restart you are no longer in the middle of what you were computing. The Maple code is broken, which is another reason you are better off re-writing it.
Walter Roberson
Walter Roberson el 10 de Abr. de 2017
I have no interest in typing in code from an image.
Stephen23
Stephen23 el 10 de Abr. de 2017
@Azamat Durzhanbayev: that is a nice screenshot, but what do you want us to do with it? It might look nice printed out and stuck on the wall.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 4 de En. de 2017

Comentada:

el 10 de Abr. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by