将表达式导出到 C,第 1 部分 [wolfram-mathematica]

发布于 2024-10-27 12:50:00 字数 859 浏览 0 评论 0原文

我在 Mathematica 中生成了几个表达式,我想将它们导出到外部 C 程序的源代码中。 “CForm”几乎达到了我想要的效果,除了求幂被表示为对 Power() 的调用。我的表达式仅涉及较小的幂,因此我更希望 C 中的表达式使用内联乘法而不是调用 Power()

例如 CForm[2 hgt^2 k1inv^3 mx0 wid^2 + hgt^2 k1inv^3 wid^3] 产生

2*Power(hgt,2)*Power(k1inv, 3)*mx0*功率(宽度,2) + Power(hgt,2)*Power(k1inv,3)*Power(wid,3)

..而我想生成的是:

2*hgt*hgt*k1inv*k1inv*k1inv* mx0*宽度*宽度 + hgt*hgt*k1inv*k1inv*k1inv*wid*wid*wid

我最初尝试挑选表达式的内部 Power[..] 部分,并将其重新映射为乘法x_Symbol^y_Integer /; y> 1:> Fold[Times, 1, Table[#1, {#2}]] 被 mathematica 阻止,立即将我精心生成的子表达式 a*a*a 转换回 Power[a,3] ;-) 我知道它只是想提供帮助,但我不知道如何让它停止,在这种情况下......

正如我写的这个问题,它有我想到我可以捕获的输出CForm 转换为字符串,然后对其执行字符串模式匹配和操作,但这是一个好方法吗?我想我更喜欢将其作为 Mathematica 表达式来处理,因为我进行重新映射然后输出..?

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..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2024-11-03 12:50:00

对于当前的情况,您可以使用类似以下内容:

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]];

例如,

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

将结果包装在 Hold 中,以防止其计算返回到 Power-s。您可以将其转换为
随时使用 ToString[HoldForm@@result] 之类的字符串。或者你可以进一步操纵。

编辑:

作为替代方案,您可以这样做:

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}]]]];

这也将保留术语的原始顺序,并删除不必要的括号,因此这似乎与您的规格完全一致。

一般来说,您可能想看看版本 8 的新“符号 C 生成”功能。将代码映射到符号 C 表达式可能是一种更可靠的方法。这样,您就不必一直担心评估,并且最终可以使用新功能生成整个 C 程序。

编辑2:

为了说明如何使用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

IMO这种方法有几个优点。也许两个主要的问题是可组合性(可以将这样的表达式嵌套在符号形式中,从较小的代码块构建更大的代码块),以及不需要太多考虑评估的事实(我不需要任何技巧) 按住此处)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文