English 中文(简体)
数学权宜数和特定系数
原标题:Mathematica exponentiation and finding a specified coefficient

我有以下法典,我确实想这样做,但很缓慢。 我不会这样说,除非在我处理守则时,即我把它分成部分,单独处理,它几乎是瞬时的。

我的守则是:

Coefficient[Product[Sum[x^(j*Prime[i]), {j, 0, Floor[q/Prime[i]]}], 
                        {i, 1, PrimePi[q]}], x, q]

照片为清晰度添加了:

“entergraph

我认为,它正在努力优化这笔钱,但并不肯定。 是否有办法制止这种情况?

此外,由于我的所有系数都是积极的,而且我只想到X^qth的系数,是否可采用数学方法消除所有超出这一数值的代数,而不是所有与这些数值的重复性?

最佳回答

我可能误解你想要的东西,但由于系数将取决于q,我假定你希望按具体<代码>q进行评价。 由于我怀疑(与你一样)选择假肢和钱财的时间,我改写。 你们有:

With[{q = 80}, Coefficient[!(
*UnderoverscriptBox[([Product]), (i = 1), (PrimePi[q])]((
*UnderoverscriptBox[([Sum]), (j = 0), ([LeftFloor]
*FractionBox[(q), (Prime[i])][RightFloor])]
*SuperscriptBox[(x), (j*Prime[i])]))), x, q]] // Timing
(*
-> {8.36181, 10003}
*)

which I rewrote with purely structural operations as

With[{q = 80},
 Coefficient[Times @@ 
 Table[Plus @@ Table[x^(j*Prime[i]), {j, 0, Floor[q/Prime[i]]}],
        {i, 1, PrimePi[q]}], x, q]] // Timing
(*
-> {8.36357, 10003}
*)

(这只是拟订了一份术语清单,然后又增加了内容,因此没有进行象征性的分析。)

单单单体的建立是瞬时的,但是它有数千条术语,因此可能发生的情况是<代码>。 高效的Expand解决这一难题。 因此:

 With[{q = 80}, Coefficient[Expand[!(
 *UnderoverscriptBox[([Product]), (i = 1), (PrimePi[q])]((
 *UnderoverscriptBox[([Sum]), (j = 0), ([LeftFloor]
 *FractionBox[(q), (Prime[i])][RightFloor])]
 *SuperscriptBox[(x), (j*Prime[i])])))], x, q]] // Timing
 (*
 -> {0.240862, 10003}
 *)

这也是我的方法。

因此,在表达之前和在您采用系数之前,仅用“<代码>Expand。

问题回答

我认为,最初的法典之所以缓慢,是因为Co Efficiency甚至用非常大的措辞开展工作——如果稍微扩大的话,这些表述就不适于记忆。

The original polynomial:

poly[q_, x_] := Product[Sum[ x^(j*Prime[i]), 
                            {j, 0, Floor[q/Prime[i]]}], {i, 1, PrimePi[q]}]

参看“<代码>q太大,扩大聚合物会增加记忆,变得相当缓慢:

In[2]:= Through[{LeafCount, ByteCount}[poly[300, x]]] // Timing
        Through[{LeafCount, ByteCount}[Expand@poly[300, x]]] // Timing
Out[2]= { 0.01, { 1859,   55864}}
Out[3]= {25.27, {77368, 3175840}}

现在请以三种不同的方式和时间来界定系数

coeff[q_]    := Module[{x}, Coefficient[poly[q, x], x, q]]
exCoeff[q_]  := Module[{x}, Coefficient[Expand@poly[q, x], x, q]]
serCoeff[q_] := Module[{x}, SeriesCoefficient[poly[q, x], {x, 0, q}]]

In[7]:= Table[   coeff[q],{q,1,30}]//Timing
        Table[ exCoeff[q],{q,1,30}]//Timing
        Table[serCoeff[q],{q,1,30}]//Timing
Out[7]= {0.37,{0,1,1,1,2,2,3,3,4,5,6,7,9,10,12,14,17,19,23,26,30,35,40,46,52,60,67,77,87,98}}
Out[8]= {0.12,{0,1,1,1,2,2,3,3,4,5,6,7,9,10,12,14,17,19,23,26,30,35,40,46,52,60,67,77,87,98}}
Out[9]= {0.06,{0,1,1,1,2,2,3,3,4,5,6,7,9,10,12,14,17,19,23,26,30,35,40,46,52,60,67,77,87,98}}

In[10]:=    coeff[100]//Timing
          exCoeff[100]//Timing
         serCoeff[100]//Timing
Out[10]= {56.28,40899}
Out[11]= { 0.84,40899}
Out[12]= { 0.06,40899}

So SeriesCoefficient is definitely the way to go. Unless of course you re a bit better at combinatorics than me and you know the following prime partition formulae (oeis)

In[13]:= CoefficientList[Series[1/Product[1-x^Prime[i],{i,1,30}],{x,0,30}],x]
Out[13]= {1,0,1,1,1,2,2,3,3,4,5,6,7,9,10,12,14,17,19,23,26,30,35,40,46,52,60,67,77,87,98}

In[14]:= f[n_]:=Length@IntegerPartitions[n,All,Prime@Range@PrimePi@n]; Array[f,30]
Out[14]= {0,1,1,1,2,2,3,3,4,5,6,7,9,10,12,14,17,19,23,26,30,35,40,46,52,60,67,77,87,98}




相关问题
Optimizing a LAN server for a game

I m the network programmer on a school game project. We want to have up to 16 players at once on a LAN. I am using the Server-Client model and am creating a new thread per client that joins. ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

Most optimized way to store crawler states?

I m currently writing a web crawler (using the python framework scrapy). Recently I had to implement a pause/resume system. The solution I implemented is of the simplest kind and, basically, stores ...

Do bitwise operations distribute over addition?

I m looking at an algorithm I m trying to optimize, and it s basically a lot of bit twiddling, followed by some additions in a tight feedback. If I could use carry-save addition for the adders, it ...

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签