Compilation Mathematica:编译NSolve、nmimimize和其他数值函数

Compilation Mathematica:编译NSolve、nmimimize和其他数值函数,compilation,wolfram-mathematica,numerical-methods,minimize,Compilation,Wolfram Mathematica,Numerical Methods,Minimize,有没有一种方法可以使用compile[]编译这些函数?看来Mathematica在这方面总是失败的 我在NSolve和NMinimize中有纯数值函数,但它仍然不起作用 一个很小的例子是: tempfunc = Compile[{}, NSolve[x^2 - 5 x + 6 == 0, x]] tempfunc[] 这将给出一个错误 知道如何编译这些函数并使它们更快吗?这似乎是不可能的。评估: Compile`CompilerFunctions[] // Sort 这将给出“可编译”函数的

有没有一种方法可以使用compile[]编译这些函数?看来Mathematica在这方面总是失败的

我在NSolve和NMinimize中有纯数值函数,但它仍然不起作用

一个很小的例子是:

tempfunc = Compile[{}, NSolve[x^2 - 5 x + 6 == 0, x]]
tempfunc[]
这将给出一个错误


知道如何编译这些函数并使它们更快吗?

这似乎是不可能的。评估:

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}
   ]
  ]
结果是对未编译的Mathematica代码进行评估:

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}

这是一个非常古老的问题,但重要的是要看到“可编译”的意思是“可以在编译代码中使用”。这些函数本身已经以优化/编译的形式实现,因此您不需要进行任何“编译”操作。在这里,您可能会获得一些东西来编译传递给
nmimimimize
@agentp的表达式:感谢您在评论中对“compileable”一词的出色解释,是的,您希望最小化的函数的编译可以/应该提高计算速度。我认为目前没有其他方法可以提高性能。。。