有没有办法使用 Compile[] 编译这些函数?似乎 Mathematica 总是在这方面失败。
我在 NSolve 和 NMinimize 中有纯数值函数,但它仍然不起作用。
一个小例子是:
tempfunc = Compile[{}, NSolve[x^2 - 5 x + 6 == 0, x]]
tempfunc[]
这会报错...
知道如何编译这些函数并使它们更快吗?
有没有办法使用 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}