Items tagged with codegeneration codegeneration Tagged Items Feed

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 ?

 

Thanks 

Chintan Pathak 

Research Scholar, 

University of Washington

 

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:

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

1 2 3 Page 1 of 3