Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4096180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:00:46+00:00 2026-05-20T20:00:46+00:00

I have several expressions I have generated in Mathematica that I would like to

  • 0

I have several expressions I have generated in Mathematica that I would like to export into the source code of an external C program. “CForm” almost does what I want except that exponentiation is expressed as calls to Power(). My expressions involves only small powers so I would prefer that the expression in C use inlined multiplication rather than a call to Power().

For example CForm[2 hgt^2 k1inv^3 mx0 wid^2 + hgt^2 k1inv^3 wid^3] yields

2*Power(hgt,2)*Power(k1inv,3)*mx0*Power(wid,2) +
Power(hgt,2)*Power(k1inv,3)*Power(wid,3)

..whereas what I would like to generate is:

2*hgt*hgt*k1inv*k1inv*k1inv*mx0*wid*wid +
hgt*hgt*k1inv*k1inv*k1inv*wid*wid*wid

My initial attempts to pick out the internal Power[..] parts of the expression and remap it to a multiplication using x_Symbol^y_Integer /; y > 1 :> Fold[Times, 1, Table[#1, {#2}]] have been stymied by mathematica immediately converting my carefully generated sub expressions a*a*a right back into Power[a,3] 😉 I know it is only trying to help but I can’t figure out how to ask it to stop, in this case…

As I have written this question it has occurred to me that I could capture the output of CForm into a string and then perform string pattern matching and manipulation on that, but is that a good way to go? I think I prefer to work on it as a Mathematica expression as I do my remapping and then output..?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:00:47+00:00Added an answer on May 20, 2026 at 8:00 pm

    For the case at hand, you may use something like the following:

    Clear[getCFormNoPowers];
    getCFormNoPowers[expr_] :=
      Module[{times},      
       Apply[Function[code, Hold[CForm[code]], HoldAll],
         Hold[#] &[expr /. x_Symbol^y_Integer /; y > 1 :> 
             times @@ Table[x, {y}]] /. times -> Times]];
    

    For example,

    In[52]:= getCFormNoPowers[2 hgt^2 k1inv^3 mx0 wid^2+hgt^2 k1inv^3 wid^3]
    
    Out[52]= Hold[2*mx0*(hgt*hgt)*(wid*wid)*(k1inv*k1inv*k1inv) + 
    hgt*hgt*(k1inv*k1inv*k1inv)* (wid*wid*wid)]
    

    The result is wrapped in Hold, to prevent its evaluation back to Power-s. You can convert it to
    a string at any time, using something like ToString[HoldForm@@result]. Or you can manipulate is further.

    Edit:

    as an alternative, you can do this:

    Clear[getCFormNoPowers];
    getCFormNoPowers[expr_] :=
     Block[{Times},
       SetAttributes[Times, {Flat, OneIdentity}];
       Apply[Function[code, Hold[CForm[code]], HoldAll],
       Hold[#] &[expr /. x_Symbol^y_Integer /; y > 1 :> Times @@ Table[x, {y}]]]];
    

    which will also keep the original order of your terms and will get rid of unnecessary parentheses, so this one seem to correspond precisely to your specs.

    Generally, you may want to have a look at the new “symbolic C generation” capabilities of the version 8. Mapping your code to symbolic C expressions may be a more robust approach. In that way, you don’t have to worry about evaluation all the time, and you may use the new functionality to generate entire C programs at the end.

    Edit 2:

    To illustrate how the problem can be solved with the SymbolicC:

    Needs["SymbolicC`"];
    
    Clear[getCFormNoPowersSymC];
    getCFormNoPowersSymC[expr_] :=
      Block[{Times},
       SetAttributes[Times, {Flat, Orderless}];
       ToCCodeString[
         expr /. x_Symbol^y_Integer /; y > 1 :> Times @@ Table[x, {y}] //.     
           HoldPattern[(op : (Times | Plus))[args__]] :>  COperator[op, {args}]]];
    
    In[53]:= getCFormNoPowersSymC[2 hgt^2 k1inv^3 mx0 wid^2+hgt^2 k1inv^3 wid^3]
    
    Out[53]= 2 * hgt * hgt * k1inv * k1inv * k1inv * mx0 * wid * wid + 
        hgt * hgt * k1inv * k1inv * k1inv * wid * wid * wid
    

    This method IMO has several advantages. Perhaps the two main ones being composability (one can nest such expressions in their symbolic form, building larger blocks of code from smaller ones), and the fact that one does not have to think much about evaluation (I didn’t need any tricks with Hold here).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several old 3.5in floppy disks that I would like to backup. My
It's been several years since I have used regular expressions, and I was hoping
I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on
We have several jobs that run concurrently that have to use the same config
I have several applications that are part of a suite of tools that various
We have several .NET applications that monitor a directory for new files, using FileSystemWatcher.
I have several queries that use a WITH clause, or Common Table Expression, with
I have several ASP:TextBox controls on a form (about 20). When the form loads,
I have several tables whose only unique data is a uniqueidentifier (a Guid) column.
We have several wizard style form applications on our website where we capture information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.