Items tagged with codegeneration codegeneration Tagged Items Feed

Hi everyone,

 

I converted a code from Maple 18 to Fortran 77 but the code is badly cut, for instance:

cg = -(F * h * p ** 2 - F * h * q ** 2 + 0.2D1 * F * k * p * q + c
     #os(F) * sqrt(-e ** 2 + 0.1D1) * p ** 2 - cos(F) * sqrt(-e ** 2 + 0.

Is there an option which can force the line cut to be done in an optimized way?

Thank you very much for your help!

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

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'm trying to use the CodeGeneration package to generate code for a series expansion. I'd like to wrap it in a function that specifies the arguments, so that the code generation package can generate a complete function definition along with definitions for all the temporary variables.

with(CodeGeneration):
f := proc(r): x->r end proc:
to_translate := f(convert(series(sin(x),x,20),polynom));

to_translate := proc (x) options operator, arrow; x-(1/6)*x^3+(1/120)*x^5-(1/5040)*x^7+(1/362880)*x^9-(1/39916800)*x^11+(1/6227020800)*x^13-(1/1307674368000)*x^15+(1/355687428096000)*x^17-(1/121645100408832000)*x^19 end proc


CodeGeneration['C']( to_translate );


Warning, procedure/module options ignored
double to_translate (double x)
{
  return(r);
}

Instead of using the value for 'r' passed in, CodeGenerate is producing a function which returns the bare symbol 'r' which is treated as a double. It shouldn't be an issue with lazy evalution because 'to_translate' is evaluated on the statement before the call to CodeGeneration, and to_translate has the full expression that I want to generate code for. How do I get CodeGeneration to produce the intented result?

hi,

 

having some issue using the codegeneration function in maple. I have a script written in Maple that I would like to do in Python 2.7. I knew to maple and not sure if my syntax is incorrect or if the program doesnt support the conversion, as an example,

 

q[Fick] := proc (t) options operator, arrow; -hm0(t)*(ps(T[C5](t))-p[inf]) end proceq2 := m[C5](t)*Cp[C5l](T[C5](t))*(diff(T[C5](t), t)) = q[heat](t)

 

thanks

that can run without maple using my own maple code?

And how about Maplesim? I heard that Maplesim can generate executable file

I'm trying to run c code generated from a simple MapleSim example (Spin Up Beam). To makes things even simpler, I set the input real expression to just time, and changed the input function in my c code to:

static void inpfn(double T, double *U)
{
U[0] = T;
}

I set all the state variables to 0 for the initial conditions. I ran the code and my output doesn't resemble the MapleSim output at all, and I'm getting errors. So my question is about the state variables, which I initially set to 0:

* State variable(s):
* x[ 0] = `Main.SpinUpBeam.FB3.uf1`(t)
* x[ 1] = diff(`Main.SpinUpBeam.FB3.uf1`(t),t)
* x[ 2] = `Main.SpinUpBeam.FB3.uf2`(t)
* x[ 3] = diff(`Main.SpinUpBeam.FB3.uf2`(t),t)
* x[ 4] = `Main.SpinUpBeam.FB3.vf1`(t)
* x[ 5] = diff(`Main.SpinUpBeam.FB3.vf1`(t),t)
* x[ 6] = `Main.SpinUpBeam.FB3.vf2`(t)
* x[ 7] = diff(`Main.SpinUpBeam.FB3.vf2`(t),t)

What do these represent for this flexible beam (FB3)? I.e. what is uf1, uf2, vf1, vf2? Should they be set to 0, or no?

Thanks.

Good Day Everybody, I m new to maple and would like to translate the following for loop from Matlab to Maple. The FromMatlab Translater didnt work for this expression related to the matrix Q, A, B D and A1. Any help would be greatly appreciated.

Related Matlab Code:

...

Q(3,3)=2*Q(3,3);

 for i=1:length(R)

    T(1,1)=cos(R(i))^2; 

    T(2,2)=cos(R(i))^2; 

    T(1,2)=sin(R(i))^2; 

    T(2,1)=sin(R(i))^2; 

    T(3,1)=-sin(R(i))*cos(R(i)); 

    T(3,2)=sin(R(i))*cos(R(i)); 

    T(1,3)=2*sin(R(i))*cos(R(i)); 

    T(2,3)=-2*sin(R(i))*cos(R(i)); 

    T(3,3)=cos(R(i))^2-sin(R(i))^2;

    T1(1,1)=cos(R(i)); 

    T1(1,2)=-sin(R(i)); 

    T1(2,1)=sin(R(i)); 

    T1(2,2)=cos(R(i));

    

    Qk(:,:,i)=inv(T)*Q*T; 

    Qk(:,3,i)=Qk(:,3,i)/2; 

    Qk1(:,:,i)=inv(T1)*Q1*T1;

    A=A+Qk(:,:,i)*(h(i+1)-h(i)); 

    B=B+.5*Qk(:,:,i)*(h(i+1)^2-h(i)^2); 

    D=D+1/3*Qk(:,:,i)*(h(i+1)^3-h(i)^3); 

    A1=A1+Qk1(:,:,i)*(h(i+1)-h(i));

end

Thanks a lot in advance.

Maple’s Code Generation makes it possible to translate your Maple code to various other programming languages including C, Python, and several others. In Maple 2015, we added a new Code Generation target to one of my other personal favourite languages, R. R is a programming language designed for statistical computing and graphics, so no code translation from Maple to R would be complete without attempting to translate as many commands as possible from Maple’s Statistics package. 

Translating code from one language to another is tricky business. Maple 2015 represented the first time that any Code Generation target language added the ability to translate commands from the Statistics package. With R, we found that many common statistics commands had almost a one-to-one mapping, such as Statistics:-Mean = mean, but several others were much more complicated, including several commands for dealing with probability functions that did not have direct mappings due to differences in how the systems handle symbolic probability functions.

A list of statistics commands that can be translated from Maple to R can be found here.

In addition to assisting me recall the correct syntax in R, having worked with CodeGeneration[R] for several months now, I find that one of my most common uses for Maple’s code generation to R is simply to pass data between the systems. A simple example:

 CodeGeneration:-R( LinearAlgebra:-RandomMatrix( 5, 2 ) );

translates to the following in R:

 cg <- matrix(c(-4,27,8,69,99,29,44,92,-31,67),nrow=5,ncol=2)

To see a couple more short examples, here’s a short video that I recorded on Code Generation to R: https://www.youtube.com/watch?v=0Mn1W2j56aA

 

A little known fact about Code Generation is that the translation files can be viewed in from the “samples” directory in your Maple install directory. Similar to many of Maple’s packages, you can view all of the source code that Code Generation uses for its translations. For example, you can view the translations for the commands that I mentioned above from the “FunctionTable.mm” file inside of your “%MapleInstallDir%/ samples/CodeGeneration/R” directory.

Should you have any feedback on this translation, or any other, please feel free to contact us. We’re also on the hunt for our next code generation targets, so let us know what other languages you would like to see added as Code Generation targets.

Hi All,

 

I am working on modeling dynamics for a robot. It requires a write some long expressions into C++.  When I do it, it has some strange problem in creation of C++ code from a vector.

Here is an example of the problem. I have a multivariable polynomial term, I using coeffs to get its coefficients and corresponding unevaluated variables, which works fine. But I can't convert the vector into C++

Ca := coeffs(term, [W, Rf, Rr, dxf, rcf, rcr], 'L'):

L;                           Rf, Rr, dxf

C(L, resultname="L11", output="dSpDdx1.cpp");

Error, (in Translate) options [Rr, dxf] not recognized.

I don't know why maple thought the unevaluated variable Rr and dxf are options instead of the vector I want to convert into c++. Does any one know what I did wrong?

 

Thanks in advance.

 

Everett

Hi everyone,

I would like to use structs

    struct sname {
        smem1;
        smem2;
    }

in my C code defined elsewhere. How do I get CodeGeneration to produce variable names like

    sname.smem1

?

The appoach to use the respective maple data type

  sname := Record('smem1', 'smem2'):
  CodeGeneration[C](sname);

results in

    Warning, procedure/module options ignored
    double smem1;
    double smem2;

and the appoach to define it as name

    CodeGeneration[C](`sname.smem1`);

results in

    Warning, the following variable name replacements were made: sname.smem1 -> cg0
    cg = cg0;

Your answer is appreciated

Fabian

Hallo,

I am facing a problem with the c-code generation of maplesim models:

When I am trying to compile a multibody model that contains more than about 80 bodies I get an error "Error, (in GetCompiledProc) internal error: unable to find matching".

Using the code generation template it says "Error in Component cmdGenerateCode eith caption "Generate C code": (in DSN/Pantelides) internal error: unable to find matching"

 

Has anyone a solution to this?

 

Edit:

Model: Ref_Model.msim

It is a simple systems of masses connected by spring/dampers.

I am using MapleSim 7.01

The error message occurs when I try to simulate the model in maplesim, too:

Simulating...
Using the Mark II engine
Simplifying system with 600 equations
Simplification removed 0 equations. Remaining equations: 600
Generating simulation code
DAE index reduction
DSN/RunSimulation: internal error: unable to find matching
(in DSN/RunSimulation) internal error occurred during simulation
Simulation failed.

 

 

I want to translate my maple code with matrice multiplication  to C code. But the command  "Multiply" cannot be translated. So I want to edit the function translation by myself. I use "AddFunction" command to create a new function  "Multiply" and want to specify its corresponding C code.

 


f := proc ()
local x,y;
x := Matrix([[1, 2], [3, 4]]);
y := Matrix([[1,3],[4,2]]);
Multiply(x, y);
NULL;
end proc;

LanguageDefinition:-Define("NewC", extend = "C", AddFunction("Multiply", [Matrix,Matrix]::Matrix,
proc (X, Y)
local a,b;
a := 2;
b := convert(a, string);
Printer:-Print("Mmultiply(", X);
Printer:-Print(",", Y, ")"); Printer:-Print("\n");
Printer:-Print("from", b, "to");
Printer:-Print(",", Y, "do") end proc, numeric = double));
Translate(f, language = "NewC")

 

But when I run it, the "Multiply" function cannot be translated.  The result is:

 

Warning, type signature [CodeGeneration:-Names:-ArrayType(integer,CodeGeneration:-Names:-ArrayRanges(1 .. 2,1 .. 2),CodeGeneration:-Names:-ArrayOptions()), CodeGeneration:-Names:-ArrayType(integer,CodeGeneration:-Names:-ArrayRanges(1 .. 2,1 .. 2),CodeGeneration:-Names:-ArrayOptions())] for function Multiply is not recognized
void f (void)
{
  int x[2][2];
  int y[2][2];
  x[0][0] = 1;
  x[0][1] = 2;
  x[1][0] = 3;
  x[1][1] = 4;
  y[0][0] = 1;
  y[0][1] = 3;
  y[1][0] = 4;
  y[1][1] = 2;
  Multiply(x, y);
  ;
}

I don't know why "Multiply(x,y)" cannot be translated.

If I put two "Multiply(x, y)" in the procedure, like this:


f := proc ()
local x,y;

Multiply(x, y);
x := Matrix([[1, 2], [3, 4]]);
y := Matrix([[1,3],[4,2]]);
Multiply(x, y);
NULL;
end proc;

LanguageDefinition:-Define("NewC", extend = "C", AddFunction("Multiply", [Matrix,Matrix]::Matrix,
proc (X, Y)
local a;
a := 2;
b := convert(a, string);
Printer:-Print("Mmultiply(", X);
Printer:-Print(",", Y, ")"); Printer:-Print("\n");
Printer:-Print("from", b, "to");
Printer:-Print(",", Y, "do") end proc, numeric = double));
Translate(f, language = "NewC")

THe result is:

Warning, cannot resolve types, reassigning x's type
Warning, cannot resolve types, reassigning y's type
Error, (in CodeGeneration:-IssueError) type 'Matrix' cannot be translated to target language

I don't know why type 'Matrix' cannot be translated.

 

 

There seems to be a bug in the CodeGeneration package for Python which leads to a deletion of braces in some cases.

# E.g.

CodeGeneration[Python](Pi*(a+2));

# leads to

cg5 = math.pi * a + 2

which is obviously wrong.

Hi all,

I'm using MAPLE 13 and I'd like to know if someone knows how to generate the variables associated to the CodeGeneration for C code. At this moment I need to create manually the variable generated from the C code.

Example:

JJ := Jacobian(convert(Pint, Vector), [P1xenu, P1yenu, P1zenu, roll, pitch, yaw, D1xbody, D1ybody, P2xenu, P2yenu, P2zenu, ROLL, PITCH, YAW, D2xbody, D2ybody]);

CodeGeneration['C'](JJ, optimize);

Output:

t1 = cov2 * cov2;
t2 = cov1 * cov1;
t4 = 0.1e1 / (t1 + t2);
t5.......

And in the C code I need to create:

double t1, t2, t3..... manually

I can I solve this issue?

Best regards

André Dias

Hi Mapleprimers,

I'm using CodeGeneration to convert a procedure I obtained with unapply() into a Matlab function.  I'm having problems getting the outputting function to run correctly in Matlab.  I'm going to dynamically generating equations, so directly editing the Matlab code won't work here.  I'm having problems getting any output in Matlab.  Here is the code I'm working on:  Series_addGear_codegen.mw

Ideally I would like output in a matrix.  I've tried putting the unapplied procedure in another procedure, but the CodeGeneration doesn't work.

This is the maple output from the function:

unapp(1,2,3,4,5);
{BAT_A = -2.267032891, BAT_V = 271, EM2_A = .4615464218, EM2_P = 125.0790803, EM2_T = -1, EM2_V = 271, EM2_W = 2, GBa_T = 12, GBa_W = 5/3, GBb_T = -4, GBb_W = 5, GEN_A = 1.805486469, GEN_P = 489.2868330, GEN_T = -12, GEN_V = 271, GEN_W = 5/3, ICE_mdot_g = 20}

Since I'll know the order of variables, I want the Matlab function to output:

[-2.267032891,  271,  .4615464218,  125.0790803,  -1, ...]

This is the output after putting the Matlab function in Matlab:

>> unapp(1, 1, 1, 1, 1)
Undefined function or variable 'BAT_A'.

Error in unapp (line 39)
unappreturn = unique([BAT_A == -t35 / 0.271e3 - t73 / 0.271e3 BAT_V == 271 EM2_A == t35 /
0.271e3 EM2_P == t35 EM2_T == -FD_T EM2_V == 271 EM2_W == FD_W GBa_T == t44 GBa_W == t41 GBb_T
== -ICE_T GB

This is the Matlab code that is generated by Maple 18:

MCode:=CodeGeneration[Matlab](unapp, declare = [FD_T::float, FD_W::float, GB_R::float, ICE_T::float, ICE_W::float],defaulttype=float,optimize,defaulttype = numeric);
Warning, could not preprocess. Found `abs` or similar in the 'While'/'For' conditions.
Warning, procedure/module options ignored
function unappreturn = unapp(FD_T, FD_W, GB_R, ICE_T, ICE_W)
t2 = 0.1e1 * FD_T * FD_W;
t5 = abs(FD_W);
t7 = abs(FD_T);
t9 = t5 ^ 2;
t13 = t7 ^ 2;
t15 = t9 * t5;
t21 = t13 * t7;
t23 = t9 ^ 2;
t31 = t13 ^ 2;
t33 = 0.1483000000e3 - 0.4267000000e1 * t5 - 0.1277000000e2 * t7 + 0.3640000000e-1 * t9 - 0.1160000000e1 * t5 * t7 + 0.2580000000e0 * t13 - 0.1181000000e-3 * t15 + 0.5994000000e-3 * t9 * t7 - 0.1171000000e-3 * t5 * t13 - 0.1739000000e-2 * t21 + 0.1245000000e-6 * t23 - 0.1200000000e-5 * t15 * t7 + 0.1584000000e-5 * t9 * t13 - 0.4383000000e-6 * t5 * t21 + 0.2947000000e-5 * t31;
if (-t2 == 0.0e0)
t35 = 0.0e0;
elseif (-t2 < 0.0e0)
t35 = t33;
else
t35 = -t33;
end
t36 = ICE_T * ICE_W;
t37 = 0.1e1 * t36;
t41 = ICE_W / GB_R;
t42 = abs(t41);
t44 = ICE_T * GB_R;
t45 = abs(t44);
t47 = t42 ^ 2;
t51 = t45 ^ 2;
t53 = t47 * t42;
t59 = t51 * t45;
t61 = t47 ^ 2;
t69 = t51 ^ 2;
t71 = 0.5280000000e-11 - 0.3849000000e-13 * t42 + 0.7190000000e2 * t45 + 0.1168000000e-15 * t47 - 0.1296000000e1 * t42 * t45 - 0.2489000000e1 * t51 - 0.1451000000e-18 * t53 - 0.1326000000e-3 * t47 * t45 + 0.8141000000e-2 * t42 * t51 + 0.4539000000e-2 * t59 + 0.6325000000e-22 * t61 + 0.2091000000e-6 * t53 * t45 - 0.3455000000e-5 * t47 * t51 - 0.2499000000e-4 * t42 * t59 + 0.5321000000e-4 * t69;
if (-t37 == 0.0e0)
t73 = 0.0e0;
elseif (-t37 < 0.0e0)
t73 = t71;
else
t73 = -t71;
end
unappreturn = unique([BAT_A == -t35 / 0.271e3 - t73 / 0.271e3 BAT_V == 271 EM2_A == t35 / 0.271e3 EM2_P == t35 EM2_T == -FD_T EM2_V == 271 EM2_W == FD_W GBa_T == t44 GBa_W == t41 GBb_T == -ICE_T GBb_W == ICE_W GEN_A == t73 / 0.271e3 GEN_P == t73 GEN_T == -t44 GEN_V == 271 GEN_W == t41 ICE_mdot_g == t36]);

 

 

1 2 3 Page 1 of 3