2
$\begingroup$

I would like to extract the integer part of a real variable. To be more specific, given a vector alpha I would like to extract the integer part of (||alpha||+Pi)/(2*Pi):

Get["AceGen`"];

SMSInitialize["GetFloor", "Language" -> "C"]; 
SMSModule["GetFloor", Real[alpha$$[3], floor$$]]; 
α ⊨ Table[SMSReal[alpha$$[i]], {i, 1, 3}]; 

GetFloor[α_] := Module[
  {normα},
  normα ⊨ SMSSqrt[α . α];
  SMSInteger[IntegerPart[N[normα + Pi/(2*Pi)]]]
];

SMSExport[GetFloor[α], floor$$]; 
SMSWrite[];

FilePrint["GetFloor.c"]; 

I checked in the C code produced and the instruction IntegerPart seems skipped by Acegen.

Is what I am trying to do possible at all in AceGen ? If so, what would be the syntax ?

$\endgroup$
  • $\begingroup$ Perhaps the documentation for AceGen could help? $\endgroup$ – Somos Jan 5 '19 at 21:52
  • $\begingroup$ The IntegerPart is a Mathematica function and will be evaluated at the time when you call your Module. It cannot be transfered to C code. If you define a variable as an integer i.e. n[DoubleRightTee]SMSInteger[...], the integer part of a real value will be taken and should be visible in your generated C code as iXXX=(int)V[...]. Also n has to be defined in both branches of SMSIF by using the appropriate equal AceGen signs (se help for SMSIf) or if you use inline IF: n[DoubleRightTee]SMSIF[...]; $\endgroup$ – BHudobivnik Jan 6 '19 at 21:16
  • $\begingroup$ @MarcoMag Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Pinti Jan 11 '19 at 9:53
  • $\begingroup$ @MarcoMag I have edited the code in your question for readability. I hope it still conveys your intention? I think your AceGen syntax is not correct, but maybe there are other ways to achieve your goals. I suggest that you expand your question with a more complete minimal working example and some additional explanation. $\endgroup$ – Pinti Jan 11 '19 at 9:59
  • $\begingroup$ @Pinti thanks for the suggestion. I modified the question. Regarding the formatting of the code, I followed the instructions given here but without success $\endgroup$ – MarcoMag Jan 11 '19 at 15:17
2
$\begingroup$

The function SMSFloor[] seems to do the job. Here is the code:

Get["AceGen`"];

SMSInitialize["GetFloor", "Language" -> "C", "Mode" -> "Debug"];
SMSModule["GetFloor", Real[alpha$$[3], floor$$]];

α ⊨ Table[SMSReal[alpha$$[i]], {i, 1, 3}];

GetFloor[α_] := Module[
  {normα}, 
  normα ⊨ SMSSqrt[α.α];
  SMSFloor[normα/(2*Pi) + 1/2]
]

SMSExport[GetFloor[α], floor$$];
SMSWrite[];

FilePrint["GetFloor.c"];
| improve this answer | |
$\endgroup$
  • $\begingroup$ Are you sure that this code really works? Because FilePrint shows suspiciously short C subroutine. I think AceGen doesn't play nicely with local symbols in Module. (?) $\endgroup$ – Pinti Jan 11 '19 at 16:44
  • $\begingroup$ @Pinti: My bad, I did not test the C code produced. The answer that was suggested lead to incorrect result despite the fact that AceGen did not throw any error. The value return was always (*floor)=floor(0.13894156133957242e1);. However, putting normα outside N[] seems to solve the problem. $\endgroup$ – MarcoMag Jan 27 '19 at 17:27
  • $\begingroup$ Good job! In fact you can omit N function at all, because AceGen knows it has to convert it to real number. $\endgroup$ – Pinti Jan 28 '19 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.