2

有沒有辦法使用Compile []編譯這些函數?看來Mathematica總是失敗。Mathematica:編譯NSolve,NMinimize和其他數值函數

我在NSolve和NMinimize中有純粹的數值函數,但它仍然不起作用。

一個小小的例子是:

tempfunc = Compile[{}, NSolve[x^2 - 5 x + 6 == 0, x]] 
tempfunc[] 

這會給出一個錯誤......

不知道如何編寫這些功能,讓他們更快?

回答

0

看來這是不可能的。評估:

Compile`CompilerFunctions[] // Sort 

這將給出「可編譯」功能列表。 Mathematica的10.3它會給:

{Abs, AddTo, And, Append, AppendTo, Apply, ArcCos, ArcCosh, ArcCot, ArcCoth, 
ArcCsc, ArcCsch, ArcSec, ArcSech, ArcSin, ArcSinh, ArcTan, ArcTanh, Arg, 
Array, ArrayDepth, Internal`Bag, Internal`BagPart, BitAnd, BitNot, BitOr, 
BitXor, Block, BlockRandom, Boole, Break, Cases, Catch, Ceiling, Chop, 
Internal`CompileError, System`Private`CompileSymbol, Complement, 
ComposeList, CompoundExpression, Conjugate, ConjugateTranspose, Continue, 
Cos, Cosh, Cot, Coth, Count, Csc, Csch, Decrement, Delete, DeleteCases, 
Dimensions, Divide, DivideBy, Do, Dot, Drop, Equal, Erf, Erfc, EvenQ, Exp, 
Fibonacci, First, FixedPoint, FixedPointList, Flatten, 
NDSolve`FEM`FlattenAll, Floor, Fold, FoldList, For, FractionalPart, FreeQ, 
Gamma, Compile`GetElement, Goto, Greater, GreaterEqual, Gudermannian, 
Haversine, If, Im, Implies, Increment, Indexed, Inequality, Compile`InnerDo, 
Insert, IntegerDigits, IntegerPart, Intersection, InverseGudermannian, 
InverseHaversine, Compile`IteratorCount, Join, Label, Last, Length, Less, 
LessEqual, List, Log, Log10, Log2, LogGamma, LogisticSigmoid, LucasL, Map, 
MapAll, MapAt, MapIndexed, MapThread, NDSolve`FEM`MapThreadDot, MatrixQ, 
Max, MemberQ, Min, Minus, Mod, Compile`Mod1, Module, Most, N, Negative, 
Nest, NestList, NonNegative, Not, OddQ, Or, OrderedQ, Out, Outer, Part, 
Partition, Piecewise, Plus, Position, Positive, Power, PreDecrement, 
PreIncrement, Prepend, PrependTo, Product, Quotient, Random, RandomChoice, 
RandomComplex, RandomInteger, RandomReal, RandomSample, RandomVariate, 
Range, Re, Internal`ReciprocalSqrt, ReplacePart, Rest, Return, Reverse, 
RotateLeft, RotateRight, Round, RuleCondition, SameQ, Scan, Sec, Sech, 
SeedRandom, Select, Set, SetDelayed, Compile`SetIterate, Sign, Sin, Sinc, 
Sinh, Sort, Sqrt, Internal`Square, Internal`StuffBag, Subtract, 
SubtractFrom, Sum, Switch, Table, Take, Tan, Tanh, TensorRank, Throw, Times, 
TimesBy, Tr, Transpose, Unequal, Union, Unitize, UnitStep, UnsameQ, VectorQ, 
Which, While, With, Xor} 

我試圖

cnm = Compile[{{a, _Real}}, 
    Block[{v, r}, 
    {v, r} = NMinimize[2 x^2 + x - a, x]; 
    {v, x /. r} 
    ] 
    ] 

和結果的非編數學代碼評價:

In[35]:= cnm[2] 

During evaluation of In[35]:= CompiledFunction::cfse: Compiled expression {-2.125,{x->-0.25}} should be a machine-size real number. >> 

During evaluation of In[35]:= CompiledFunction::cfex: Could not complete external evaluation at instruction 1; proceeding with uncompiled evaluation. >> 

Out[35]= {-2.125, -0.25} 
+0

這是一個非常古老的問題,但它是非常重要的看到「可編譯」意味着「可以在編譯代碼中使用」。這些函數本身已經以優化/編譯的形式實現,所以你沒有做任何事情來「編譯」它們。在這裏,你可能會獲得一些東西來編譯你傳遞的表達式* to *'NMinimize' – agentp 2017-06-28 15:38:07

+0

@agentp:感謝您在評論中對「compilable」這個單詞進行了很好的說明,並且對於希望最小化的函數的編譯可以/應該改進計算速度。在這個時候,我看不到其他方式來提高性能...... – drgrujic 2017-06-29 11:35:39